From b341c722fdc5f73474347af15ca855dd8727aa35 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 1 Apr 2017 21:28:57 +0300 Subject: [PATCH] copied stash over --- lib/vool.rb | 17 +++- lib/vool/assignment_statement.rb | 13 +++ lib/vool/basic_values.rb | 33 ++++++++ lib/vool/call_site_statement.rb | 5 ++ lib/vool/compiler.rb | 141 +++++++++++++++++++++++++++++++ lib/vool/field_access.rb | 5 ++ lib/vool/function_statement.rb | 5 ++ lib/vool/if_statement.rb | 5 ++ lib/vool/operator_expression.rb | 5 ++ lib/vool/return_statement.rb | 5 ++ lib/vool/statements.rb | 5 ++ lib/vool/while_statement.rb | 5 ++ 12 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 lib/vool/assignment_statement.rb create mode 100644 lib/vool/basic_values.rb create mode 100644 lib/vool/call_site_statement.rb create mode 100644 lib/vool/field_access.rb create mode 100644 lib/vool/function_statement.rb create mode 100644 lib/vool/if_statement.rb create mode 100644 lib/vool/operator_expression.rb create mode 100644 lib/vool/return_statement.rb create mode 100644 lib/vool/statements.rb create mode 100644 lib/vool/while_statement.rb diff --git a/lib/vool.rb b/lib/vool.rb index 9a0f002f..550b57e1 100644 --- a/lib/vool.rb +++ b/lib/vool.rb @@ -1,3 +1,18 @@ -require_relative "vool/compiler" +module Vool + class Statement + end +end + require_relative "vool/class_statement" require_relative "vool/method_statement" + +require_relative "vool/while_statement" +require_relative "vool/if_statement" +require_relative "vool/return_statement" +require_relative "vool/statements" +require_relative "vool/operator_expression" +require_relative "vool/call_site_statement" +require_relative "vool/basic_values" +require_relative "vool/assignment_statement" + +require_relative "vool/compiler" diff --git a/lib/vool/assignment_statement.rb b/lib/vool/assignment_statement.rb new file mode 100644 index 00000000..383b8f0e --- /dev/null +++ b/lib/vool/assignment_statement.rb @@ -0,0 +1,13 @@ +module Vool + class Assignment < Statement + attr_accessor :name , :value + def initialize(n = nil , v = nil ) + @name , @value = n , v + end + end + + class FieldDef < Statement + attr_accessor :name , :type , :value + end + +end diff --git a/lib/vool/basic_values.rb b/lib/vool/basic_values.rb new file mode 100644 index 00000000..01afc9a8 --- /dev/null +++ b/lib/vool/basic_values.rb @@ -0,0 +1,33 @@ +module Vool + class IntegerStatement < Statement + attr_accessor :value + def initialize(value) + @value = value + end + end + class FloatStatement < Statement + attr_accessor :value + def initialize(value) + @value = value + end + end + class TrueStatement < Statement + end + class FalseStatement < Statement + end + class NilStatement < Statement + end + class StringStatement < Statement + attr_accessor :value + def initialize(value) + @value = value + end + end + class NameStatement < Statement + attr_accessor :value + alias :name :value + def initialize(value) + @value = value + end + end +end diff --git a/lib/vool/call_site_statement.rb b/lib/vool/call_site_statement.rb new file mode 100644 index 00000000..a419bb8a --- /dev/null +++ b/lib/vool/call_site_statement.rb @@ -0,0 +1,5 @@ +module Vool + class CallSite < Statement + attr_accessor :name , :receiver , :arguments + end +end diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 6325ad6b..3ada35d6 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -21,6 +21,147 @@ module Vool arg.first end + def on_function statement + return_type , name , parameters, statements , receiver = *statement + w = FunctionStatement.new() + w.return_type = return_type + w.name = name.children.first + w.parameters = parameters.to_a.collect do |p| + raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter + p.children + end + w.statements = process(statements) + w.receiver = receiver + w + end + + def on_field_def statement + type , name , value = *statement + w = FieldDef.new() + w.type = type + w.name = process(name) + w.value = process(value) if value + w + end + + def on_class_field statement + type , name = *statement + w = ClassField.new() + w.type = type + w.name = name + w + end + + def on_while_statement statement + branch_type , condition , statements = *statement + w = WhileStatement.new() + w.branch_type = branch_type + w.condition = process(condition) + w.statements = process(statements) + w + end + + def on_if_statement statement + branch_type , condition , if_true , if_false = *statement + w = IfStatement.new() + w.branch_type = branch_type + w.condition = process(condition) + w.if_true = process(if_true) + w.if_false = process(if_false) + w + end + + def process_first code + raise "Too many children #{code.inspect}" if code.children.length != 1 + process code.children.first + end + alias :on_conditional :process_first + alias :on_condition :process_first + alias :on_field :process_first + + def on_statements statement + w = Statements.new() + return w unless statement.children + return w unless statement.children.first + w.statements = process_all(statement.children) + w + end + alias :on_true_statements :on_statements + alias :on_false_statements :on_statements + + def on_return statement + w = ReturnStatement.new() + w.return_value = process(statement.children.first) + w + end + + def on_operator_value statement + operator , left_e , right_e = *statement + w = OperatorExpression.new() + w.operator = operator + w.left_expression = process(left_e) + w.right_expression = process(right_e) + w + end + + def on_field_access statement + receiver_ast , field_ast = *statement + w = FieldAccess.new() + w.receiver = process(receiver_ast) + w.field = process(field_ast) + w + end + + def on_receiver expression + process expression.children.first + end + + def on_call statement + name_s , arguments , receiver = *statement + w = CallSite.new() + w.name = name_s.children.first + w.arguments = process_all(arguments) + w.receiver = process(receiver) + w + end + + def on_int expression + IntegerExpression.new(expression.children.first) + end + + def on_true expression + TrueExpression.new + end + + def on_false expression + FalseExpression.new + end + + def on_nil expression + NilExpression.new + end + + def on_name statement + NameExpression.new(statement.children.first) + end + def on_string expression + StringExpression.new(expression.children.first) + end + + def on_class_name expression + ClassExpression.new(expression.children.first) + end + + def on_assignment statement + name , value = *statement + w = Assignment.new() + w.name = process name + w.value = process(value) + w + end + + private + def get_name( statement ) return nil unless statement raise "Not const #{statement}" unless statement.type == :const diff --git a/lib/vool/field_access.rb b/lib/vool/field_access.rb new file mode 100644 index 00000000..ff7bed9f --- /dev/null +++ b/lib/vool/field_access.rb @@ -0,0 +1,5 @@ +module Vool + class FieldAccess < Expression + attr_accessor :receiver , :field + end +end diff --git a/lib/vool/function_statement.rb b/lib/vool/function_statement.rb new file mode 100644 index 00000000..747a9eb2 --- /dev/null +++ b/lib/vool/function_statement.rb @@ -0,0 +1,5 @@ +module Vool + class FunctionStatement < Statement + attr_accessor :return_type , :name , :parameters, :statements , :receiver + end +end diff --git a/lib/vool/if_statement.rb b/lib/vool/if_statement.rb new file mode 100644 index 00000000..bec83227 --- /dev/null +++ b/lib/vool/if_statement.rb @@ -0,0 +1,5 @@ +module Vool + class IfStatement < Statement + attr_accessor :branch_type , :condition , :if_true , :if_false + end +end diff --git a/lib/vool/operator_expression.rb b/lib/vool/operator_expression.rb new file mode 100644 index 00000000..794a9e83 --- /dev/null +++ b/lib/vool/operator_expression.rb @@ -0,0 +1,5 @@ +module Vool + class OperatorExpression < Statement + attr_accessor :operator , :left_expression , :right_expression + end +end diff --git a/lib/vool/return_statement.rb b/lib/vool/return_statement.rb new file mode 100644 index 00000000..9fc44f12 --- /dev/null +++ b/lib/vool/return_statement.rb @@ -0,0 +1,5 @@ +module Vool + class ReturnStatement < Statement + attr_accessor :return_value + end +end diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb new file mode 100644 index 00000000..ffa51a9d --- /dev/null +++ b/lib/vool/statements.rb @@ -0,0 +1,5 @@ +module Vool + class Statements < Statement + attr_accessor :statements + end +end diff --git a/lib/vool/while_statement.rb b/lib/vool/while_statement.rb new file mode 100644 index 00000000..21a18686 --- /dev/null +++ b/lib/vool/while_statement.rb @@ -0,0 +1,5 @@ +module Vool + class WhileStatement < Statement + attr_accessor :branch_type , :condition , :statements + end +end