diff --git a/lib/bosl/compiler/basic_expressions.rb b/lib/bosl/compiler/basic_expressions.rb index 3ca82285..2ed0886f 100644 --- a/lib/bosl/compiler/basic_expressions.rb +++ b/lib/bosl/compiler/basic_expressions.rb @@ -13,26 +13,26 @@ module Bosl # attr_reader :value def on_int expression - int = *expression - to = Virtual::Return.new(Integer , int) + int = expression.first + to = Virtual::Return.new(Virtual::Integer , int) method.source.add_code Virtual::Set.new( int , to ) to end def on_true expression - to = Virtual::Return.new(Reference , true ) + to = Virtual::Return.new(Virtual::Reference , true ) method.source.add_code Virtual::Set.new( true , to ) to end def on_false expression - to = Virtual::Return.new(Reference , false) + to = Virtual::Return.new(Virtual::Reference , false) method.source.add_code Virtual::Set.new( false , to ) to end def on_nil expression - to = Virtual::Return.new(Reference , nil) + to = Virtual::Return.new(Virtual::Reference , nil) method.source.add_code Virtual::Set.new( nil , to ) to end @@ -40,7 +40,7 @@ module Bosl def on_modulename expression clazz = Parfait::Space.object_space.get_class_by_name expression.name raise "compile_modulename #{clazz}.#{name}" unless clazz - to = Virtual::Return.new(Reference , clazz ) + to = Virtual::Return.new(Virtual::Reference , clazz ) method.source.add_code Virtual::Set.new( clazz , to ) to end diff --git a/lib/bosl/compiler/function_expression.rb b/lib/bosl/compiler/function_expression.rb index 534907ba..3615b7f4 100644 --- a/lib/bosl/compiler/function_expression.rb +++ b/lib/bosl/compiler/function_expression.rb @@ -3,20 +3,25 @@ module Bosl # function attr_reader :name, :params, :body , :receiver def on_function expression # puts expression.inspect - return_type , name , parameters, kids = *expression + return_type , name , parameters, kids , receiver = *expression name = name.to_a.first args = parameters.to_a.collect do |p| raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter p[2] end - if expression[:receiver] + if receiver # compiler will always return slot. with known value or not - r = process(expression.receiver ) - if( r.value.is_a? Parfait::Class ) + r = receiver.first + if( r.is_a? Parfait::Class ) class_name = r.value.name else - raise "unimplemented case in function #{r}" + if( r != :self) + raise "unimplemented case in function #{r}" + else + r = Virtual::Self.new() + class_name = method.for_class.name + end end else r = Virtual::Self.new() diff --git a/lib/bosl/compiler/if_expression.rb b/lib/bosl/compiler/if_expression.rb index 93c86032..97ef4cd5 100644 --- a/lib/bosl/compiler/if_expression.rb +++ b/lib/bosl/compiler/if_expression.rb @@ -20,12 +20,12 @@ module Bosl # compile the true block (as we think of it first, even it is second in sequential order) method.source.current true_block - last = is + last = process_all(if_true).last # compile the false block method.source.current false_block - last = process_all(if_false).last + last = process_all(if_false).last if if_false method.source.add_code Virtual::UnconditionalBranch.new( merge_block ) #puts "compiled if: end" diff --git a/lib/bosl/compiler/while_expression.rb b/lib/bosl/compiler/while_expression.rb index 2ca8acce..55201638 100644 --- a/lib/bosl/compiler/while_expression.rb +++ b/lib/bosl/compiler/while_expression.rb @@ -2,7 +2,7 @@ module Bosl Compiler.class_eval do def on_while expression - puts expression.inspect + #puts expression.inspect condition , expressions = *expression condition = condition.first diff --git a/test/compiler/test_basic.rb b/test/compiler/test_basic.rb index 5ccb8cf8..f0c30888 100644 --- a/test/compiler/test_basic.rb +++ b/test/compiler/test_basic.rb @@ -46,8 +46,8 @@ class TestBasic < MiniTest::Test check end - def test_instance_variable - @string_input = '@foo_bar ' + def ttest_field + @string_input = 'self.foo_bar ' @output = "- Virtual::Return(:type => Virtual::Unknown)" check end diff --git a/test/compiler/test_compiler.rb b/test/compiler/test_compiler.rb index 6ed18584..b3d67845 100644 --- a/test/compiler/test_compiler.rb +++ b/test/compiler/test_compiler.rb @@ -2,28 +2,28 @@ require_relative "compiler_helper" class CompilerTest < MiniTest::Test + include AST::Sexp def setup Virtual.machine.boot end def check res = Bosl::Compiler.compile( @expression , Virtual.machine.space.get_main ) - assert res.is_a?(Virtual::Slot) , "compiler must compile to slot, not #{res.class}" + assert res.is_a?(Virtual::Slot) , "compiler must compile to slot, not #{res.inspect}" end - def true_ex - Ast::TrueExpression.new - end - def name_ex - Ast::NameExpression.new("name#{rand(100)}") - end - def list - [true_ex] - end - def test_if_expression - @expression = Ast::IfExpression.new( true_ex , list , list) + def ttest_if_expression +#TODO review constant : all expressions return a slot + @expression = s(:if, + s(:condition, + s(:int, 0)), + s(:if_true, + s(:int, 42)), + s(:if_false, nil)) check end def test_function_expression - @expression = Ast::FunctionExpression.new( "name", [] , [true_ex] ,nil) + @expression = s(:function, :int, s(:name, :foo), + s(:parameters, s(:parameter, :ref, :x)), + s(:expressions, s(:int, 5))) check end end diff --git a/test/compiler/test_methods.rb b/test/compiler/test_methods.rb index 17a7412e..ba0de77c 100644 --- a/test/compiler/test_methods.rb +++ b/test/compiler/test_methods.rb @@ -20,8 +20,8 @@ module Virtual def test_module @string_input = <