diff --git a/lib/bosl/compiler.rb b/lib/bosl/compiler.rb index bea82d45..baed2044 100644 --- a/lib/bosl/compiler.rb +++ b/lib/bosl/compiler.rb @@ -1,6 +1,13 @@ module Bosl - module Compiler + class Compiler < AST::Processor + attr_reader :method + def initialize(method) + @method = method + end + def handler_missing node + raise "No handler on_#{node.type}(node)" + end # Compiling is the conversion of the AST into 2 things: # - code (ie sequences of Instructions inside Blocks) carried by MethodSource # - an object graph containing all the Methods, their classes and Constants @@ -13,15 +20,14 @@ module Bosl # The compile method (so every compile method) returns the value that it deposits which # may be unknown Unknown value. # - # The Compiler.compile uses a visitor patter to dispatch according to the class name of - # the expression. So a NameExpression is delegated to Virtual::Set.new etc. + # The process uses a visitor pattern (from AST::Processor) to dispatch according to the + # type the expression. So a s(:if xx) will become an on_if(node) call. # This makes the dispatch extensible, ie Expressions may be added by external code, # as long as matching compile methods are supplied too. # def self.compile expression , method - exp_name = expression.type - puts "Expression #{expression.to_sexp}" - self.send "compile_#{exp_name}".to_sym , expression, method + compiler = Compiler.new method + compiler.process expression end end diff --git a/lib/bosl/compiler/basic_expressions.rb b/lib/bosl/compiler/basic_expressions.rb index e10b9f39..3ca82285 100644 --- a/lib/bosl/compiler/basic_expressions.rb +++ b/lib/bosl/compiler/basic_expressions.rb @@ -1,7 +1,7 @@ module Bosl # collection of the simple ones, int and strings and such - module Compiler + Compiler.class_eval do # Constant expressions can by definition be evaluated at compile time. # But that does not solve their storage, ie they need to be accessible at runtime from _somewhere_ @@ -12,32 +12,32 @@ module Bosl # But in the future (in the one that holds great things) we optimize those unneccesay moves away # attr_reader :value - def self.compile_int expression , method + def on_int expression int = *expression to = Virtual::Return.new(Integer , int) method.source.add_code Virtual::Set.new( int , to ) to end - def self.compile_true expression , method + def on_true expression to = Virtual::Return.new(Reference , true ) method.source.add_code Virtual::Set.new( true , to ) to end - def self.compile_false expression , method + def on_false expression to = Virtual::Return.new(Reference , false) method.source.add_code Virtual::Set.new( false , to ) to end - def self.compile_nil expression , method + def on_nil expression to = Virtual::Return.new(Reference , nil) method.source.add_code Virtual::Set.new( nil , to ) to end - def self.compile_modulename expression , method + 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 ) @@ -46,7 +46,7 @@ module Bosl end # attr_reader :string - def self.compile_string expression , method + def on_string expression # Clearly a TODO here to implement strings rather than reusing symbols value = expression.first.to_sym to = Virtual::Return.new(Virtual::Reference , value) @@ -56,11 +56,11 @@ module Bosl end #attr_reader :left, :right - def self.compile_assignment expression , method + def on_assignment expression unless expression.left.instance_of? Ast::NameExpression raise "must assign to NameExpression , not #{expression.left}" end - r = Compiler.compile(expression.right , method ) + r = process(expression.right ) raise "oh noo, nil from where #{expression.right.inspect}" unless r index = method.has_arg(expression.left.name.to_sym) if index @@ -72,7 +72,7 @@ module Bosl r end - def self.compile_variable expression, method + def on_variable expression method.source.add_code InstanceGet.new(expression.name) Virtual::Return.new( Unknown ) end diff --git a/lib/bosl/compiler/callsite_expression.rb b/lib/bosl/compiler/callsite_expression.rb index a9d3e82e..1cef5b25 100644 --- a/lib/bosl/compiler/callsite_expression.rb +++ b/lib/bosl/compiler/callsite_expression.rb @@ -1,15 +1,15 @@ module Bosl - module Compiler + Compiler.class_eval do # operators are really function calls # call_site - attr_reader :name, :args , :receiver - def self.compile_call expession , method + def on_call expession name , arguments , receiver = *expession name = name.to_a.first if receiver - me = Compiler.compile( receiver.to_a.first , method ) + me = process( receiver.to_a.first ) else me = Virtual::Self.new end @@ -21,7 +21,7 @@ module Bosl compiled_args = [] arguments.to_a.each_with_index do |arg , i| #compile in the running method, ie before passing control - val = Compiler.compile( arg , method) + val = process( arg) # move the compiled value to it's slot in the new message # + 1 as this is a ruby 0-start , but 0 is the last message ivar. # so the next free is +1 diff --git a/lib/bosl/compiler/compound_expressions.rb b/lib/bosl/compiler/compound_expressions.rb index c0fcdaf9..2b45a60b 100644 --- a/lib/bosl/compiler/compound_expressions.rb +++ b/lib/bosl/compiler/compound_expressions.rb @@ -1,15 +1,15 @@ module Bosl - module Compiler + Compiler.class_eval do # attr_reader :values - def self.compile_array expession, context + def on_array expession, context # to.do end # attr_reader :key , :value - def self.compile_association context + def on_association context # to.do end - def self.compile_hash context + def on_hash context # to.do end end diff --git a/lib/bosl/compiler/expression_list.rb b/lib/bosl/compiler/expression_list.rb index 759a3470..83d7a7e0 100644 --- a/lib/bosl/compiler/expression_list.rb +++ b/lib/bosl/compiler/expression_list.rb @@ -1,10 +1,8 @@ module Bosl - module Compiler + Compiler.class_eval do # list - attr_reader :expressions - def self.compile_expressions expession , method - expession.children.collect do |part| - Compiler.compile( part , method ) - end + def on_expressions expession + process_all( expession.children ) end end end diff --git a/lib/bosl/compiler/function_expression.rb b/lib/bosl/compiler/function_expression.rb index a3299ce9..b03a886c 100644 --- a/lib/bosl/compiler/function_expression.rb +++ b/lib/bosl/compiler/function_expression.rb @@ -1,7 +1,7 @@ module Bosl - module Compiler + Compiler.class_eval do # function attr_reader :name, :params, :body , :receiver - def self.compile_function expression, method + def on_function expression return_type , name , parameters, kids = *expression name = name.to_a.first args = parameters.to_a.collect do |p| @@ -11,7 +11,7 @@ module Bosl if expression[:receiver] # compiler will always return slot. with known value or not - r = Compiler.compile(expression.receiver, method ) + r = process(expression.receiver ) if( r.value.is_a? Parfait::Class ) class_name = r.value.name else @@ -25,11 +25,15 @@ module Bosl new_method.source.receiver = r new_method.for_class.add_instance_method new_method + old_method = @method + @method = new_method + #frame = frame.new_frame kids.to_a.each do |ex| - return_type = Compiler.compile(ex,new_method ) + return_type = process(ex) raise return_type.inspect if return_type.is_a? Virtual::Instruction end + @method = old_method new_method.source.return_type = return_type Virtual::Return.new(return_type) end diff --git a/lib/bosl/compiler/if_expression.rb b/lib/bosl/compiler/if_expression.rb index f7fd1f2b..93c86032 100644 --- a/lib/bosl/compiler/if_expression.rb +++ b/lib/bosl/compiler/if_expression.rb @@ -1,8 +1,8 @@ module Bosl - module Compiler + Compiler.class_eval do # if - attr_reader :cond, :if_true, :if_false - def self.compile_if expression , method + def on_if expression condition , if_true , if_false = *expression condition = condition.first # to execute the logic as the if states it, the blocks are the other way around @@ -13,7 +13,7 @@ module Bosl false_block = method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here - is = Compiler.compile(condition, method ) + is = process(condition ) # TODO should/will use different branches for different conditions. # just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition method.source.add_code Virtual::IsTrueBranch.new( true_block ) @@ -21,18 +21,11 @@ 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 - if_true.each do |part| - last = Compiler.compile(part,method ) - raise part.inspect if last.nil? - end + last = process_all(if_true).last # compile the false block method.source.current false_block - if_false.each do |part| - #puts "compiling in if false #{part}" - last = Compiler.compile(part,method ) - raise part.inspect if last.nil? - end + last = process_all(if_false).last method.source.add_code Virtual::UnconditionalBranch.new( merge_block ) #puts "compiled if: end" diff --git a/lib/bosl/compiler/module_expression.rb b/lib/bosl/compiler/module_expression.rb index aa6b0ded..f82501bd 100644 --- a/lib/bosl/compiler/module_expression.rb +++ b/lib/bosl/compiler/module_expression.rb @@ -1,11 +1,11 @@ module Bosl - module Compiler + Compiler.class_eval do # module attr_reader :name ,:expressions - def self.compile_module expression , context + def on_module expression return clazz end - def self.compile_class expression , method + def on_class expression clazz = Parfait::Space.object_space.get_class_by_name! expression.name #puts "Compiling class #{clazz.name.inspect}" expression_value = nil @@ -15,7 +15,7 @@ module Bosl # ie throw an error for now raise "only functions for now #{expr.inspect}" unless expr.is_a? Ast::FunctionExpression #puts "compiling expression #{expression}" - expression_value = Compiler.compile(expr,method ) + expression_value = process(expr ) #puts "compiled expression #{expression_value.inspect}" end diff --git a/lib/bosl/compiler/name_expression.rb b/lib/bosl/compiler/name_expression.rb index 404ac3a8..1ce1a62c 100644 --- a/lib/bosl/compiler/name_expression.rb +++ b/lib/bosl/compiler/name_expression.rb @@ -1,11 +1,11 @@ module Bosl - module Compiler + Compiler.class_eval do # attr_reader :name # compiling name needs to check if it's a variable and if so resolve it # otherwise it's a method without args and a send is issued. # whichever way this goes the result is stored in the return slot (as all compiles) - def self.compile_name expression , method + def on_name expression name = expression.to_a.first return Virtual::Self.new( Reference.new(method.for_class)) if name == :self # either an argument, so it's stored in message diff --git a/lib/bosl/compiler/operator_expressions.rb b/lib/bosl/compiler/operator_expressions.rb index 2140be60..4b1afa75 100644 --- a/lib/bosl/compiler/operator_expressions.rb +++ b/lib/bosl/compiler/operator_expressions.rb @@ -1,17 +1,17 @@ module Bosl - module Compiler + Compiler.class_eval do # operator attr_reader :operator, :left, :right - def self.compile_operator expression, method + def on_operator expression operator , left , right = *expression nil end - def self.compile_assign expression, method + def on_assign expression puts "assign" puts expression.inspect name , value = *expression name = name.to_a.first - v = self.compile(value , method ) + v = process(value ) index = method.ensure_local( name ) method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index ) , v ) end diff --git a/lib/bosl/compiler/return_expression.rb b/lib/bosl/compiler/return_expression.rb index 48591d55..848b71dd 100644 --- a/lib/bosl/compiler/return_expression.rb +++ b/lib/bosl/compiler/return_expression.rb @@ -1,9 +1,9 @@ module Bosl - module Compiler + Compiler.class_eval do # return attr_reader :expression - def self.compile_return expression, method - return Compiler.compile(expression.to_a.first , method) + def on_return expression + return process(expression.to_a.first ) end end end diff --git a/lib/bosl/compiler/while_expression.rb b/lib/bosl/compiler/while_expression.rb index e2e4b361..9b3ee207 100644 --- a/lib/bosl/compiler/while_expression.rb +++ b/lib/bosl/compiler/while_expression.rb @@ -1,21 +1,21 @@ module Bosl - module Compiler + Compiler.class_eval do # while- attr_reader :condition, :body - def self.compile_while expression, method + def on_while expression # this is where the while ends and both branches meet merge = method.source.new_block("while merge") # this comes after the current and beofre the merge start = method.source.new_block("while_start" ) method.source.current start - cond = Compiler.compile(expression.condition, method) + cond = process(expression.condition) method.source.add_code IsTrueBranch.new(merge) last = cond expression.body.each do |part| - last = Compiler.compile(part , method) + last = process(part) raise part.inspect if last.nil? end # unconditionally branch to the start diff --git a/lib/salama.rb b/lib/salama.rb index efbdad0b..8136754d 100644 --- a/lib/salama.rb +++ b/lib/salama.rb @@ -4,11 +4,6 @@ require "stream_reader" require "elf/object_writer" require 'salama-reader' AST::Node.class_eval do - def each - children.each do |child| - yield child - end - end def first children.first end diff --git a/test/compiler/compiler_helper.rb b/test/compiler/compiler_helper.rb index 89eedfc5..5014cba4 100644 --- a/test/compiler/compiler_helper.rb +++ b/test/compiler/compiler_helper.rb @@ -2,7 +2,7 @@ require_relative '../helper' require 'parslet/convenience' -module CompilerHelper +Compiler.class_eval doHelper def check Virtual.machine.boot.compile_main @string_input diff --git a/test/compiler/test_compiler.rb b/test/compiler/test_compiler.rb index 1dc5e5ad..6ed18584 100644 --- a/test/compiler/test_compiler.rb +++ b/test/compiler/test_compiler.rb @@ -6,7 +6,7 @@ class CompilerTest < MiniTest::Test Virtual.machine.boot end def check - res = Virtual::Compiler.compile( @expression , Virtual.machine.space.get_main ) + res = Bosl::Compiler.compile( @expression , Virtual.machine.space.get_main ) assert res.is_a?(Virtual::Slot) , "compiler must compile to slot, not #{res.class}" end def true_ex diff --git a/test/fragments/helper.rb b/test/fragments/helper.rb index 7942a8b7..07697d96 100644 --- a/test/fragments/helper.rb +++ b/test/fragments/helper.rb @@ -8,9 +8,11 @@ module Fragments def check expressions = Virtual.machine.boot.compile_main @string_input + puts expressions @expect.each_with_index do | should , i | - assert expressions[i].is_a?(Virtual::Slot) , "compiles should return slots, not #{should.class}" - assert_equal should , expressions[i].class + exp_i = expressions[i] + assert exp_i.is_a?(Virtual::Slot) , "compiles should return #{should}, not #{exp_i}" + assert_equal should , exp_i.class end # Virtual.machine.run_passes end diff --git a/test/interpreter/test_add.rb b/test/interpreter/test_add.rb index 80849ca7..33f37857 100644 --- a/test/interpreter/test_add.rb +++ b/test/interpreter/test_add.rb @@ -5,7 +5,7 @@ class AddTest < MiniTest::Test def setup Virtual.machine.boot code =Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5)) - Virtual::Compiler.compile( code , Virtual.machine.space.get_main ) + Bosl::Compiler.compile( code , Virtual.machine.space.get_main ) Virtual.machine.run_before "Register::CallImplementation" @interpreter = Interpreter::Interpreter.new @interpreter.start Virtual.machine.init diff --git a/test/interpreter/test_puts.rb b/test/interpreter/test_puts.rb index b9068804..c8bc28a6 100644 --- a/test/interpreter/test_puts.rb +++ b/test/interpreter/test_puts.rb @@ -5,7 +5,7 @@ class TestPuts < MiniTest::Test def setup Virtual.machine.boot code = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:putstring, [] ,Ast::StringExpression.new("Hello again"))]) - Virtual::Compiler.compile( code , Virtual.machine.space.get_main ) + Bosl::Compiler.compile( code , Virtual.machine.space.get_main ) Virtual.machine.run_before "Register::CallImplementation" @interpreter = Interpreter::Interpreter.new @interpreter.start Virtual.machine.init