diff --git a/lib/vool/class_statement.rb b/lib/vool/class_statement.rb index 8e39d94f..b31c76d2 100644 --- a/lib/vool/class_statement.rb +++ b/lib/vool/class_statement.rb @@ -3,7 +3,7 @@ module Vool attr_reader :name, :super_class_name , :body def initialize( name , supe , body) - @name , @super_class_name , @body = name , supe , (body || []) + @name , @super_class_name , @body = name , supe , body end end diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 3ada35d6..6930760a 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -1,5 +1,5 @@ module Vool - class Compiler < ::Rubyx::Passes::TotalProcessor + class Compiler < AST::Processor def self.compile(input) ast = Parser::Ruby22.parse( input ) @@ -8,7 +8,7 @@ module Vool def on_class( statement ) name , sup , body = *statement - ClassStatement.new( get_name(name) , get_name(sup) , body ) + ClassStatement.new( get_name(name) , get_name(sup) , process_all(body) ) end def on_def( statement ) @@ -21,6 +21,41 @@ module Vool arg.first end + def on_int expression + IntegerStatement.new(expression.children.first) + end + + def on_float expression + FloatStatement.new(expression.children.first) + end + + def on_true expression + TrueStatement.new + end + + def on_false expression + FalseStatement.new + end + + def on_nil expression + NilStatement.new + end + + def on_str expression + StringStatement.new(expression.children.first) + end + alias :on_string :on_str + + def on_dstr + raise "Not implemented dynamix strings (with interpolation)" + end + + def on_return statement + w = ReturnStatement.new() + w.return_value = process(statement.children.first) + w + end + def on_function statement return_type , name , parameters, statements , receiver = *statement w = FunctionStatement.new() @@ -35,23 +70,6 @@ module Vool 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() @@ -71,47 +89,15 @@ module Vool 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 = OperatorStatement.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 @@ -125,31 +111,12 @@ module Vool 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) + NameStatement.new(statement.children.first) end def on_class_name expression - ClassExpression.new(expression.children.first) + ClassStatement.new(expression.children.first) end def on_assignment statement diff --git a/lib/vool/field_access.rb b/lib/vool/field_access.rb deleted file mode 100644 index ff7bed9f..00000000 --- a/lib/vool/field_access.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Vool - class FieldAccess < Expression - attr_accessor :receiver , :field - end -end diff --git a/test/vool/test_basic_values.rb b/test/vool/test_basic_values.rb new file mode 100644 index 00000000..dcdfdc2a --- /dev/null +++ b/test/vool/test_basic_values.rb @@ -0,0 +1,27 @@ +require_relative "../helper" + +module Vool + class TestBasicValues < MiniTest::Test + + def test_nil + lst = Compiler.compile( "nil") + assert_equal NilStatement , lst.class + end + def test_false + lst = Compiler.compile( "false") + assert_equal FalseStatement , lst.class + end + def test_true + lst = Compiler.compile( "true") + assert_equal TrueStatement , lst.class + end + def test_integer + lst = Compiler.compile( "123") + assert_equal IntegerStatement , lst.class + end + def test_string + lst = Compiler.compile( "'string'") + assert_equal StringStatement , lst.class , lst.inspect + end + end +end diff --git a/test/vool/test_compiler.rb b/test/vool/test_compiler.rb index 0974d5e2..1be9c2ab 100644 --- a/test/vool/test_compiler.rb +++ b/test/vool/test_compiler.rb @@ -1,3 +1,4 @@ require_relative "helper" require_relative "test_class_statement" require_relative "test_method_statement" +require_relative "test_basic_values"