switching to ast::processor

using instance methods instead of context
This commit is contained in:
Torsten Ruger 2015-09-19 18:56:18 +03:00
parent 3a885a8e46
commit 775bca50ac
18 changed files with 71 additions and 73 deletions

View File

@ -1,6 +1,13 @@
module Bosl 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: # Compiling is the conversion of the AST into 2 things:
# - code (ie sequences of Instructions inside Blocks) carried by MethodSource # - code (ie sequences of Instructions inside Blocks) carried by MethodSource
# - an object graph containing all the Methods, their classes and Constants # - 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 # The compile method (so every compile method) returns the value that it deposits which
# may be unknown Unknown value. # may be unknown Unknown value.
# #
# The Compiler.compile uses a visitor patter to dispatch according to the class name of # The process uses a visitor pattern (from AST::Processor) to dispatch according to the
# the expression. So a NameExpression is delegated to Virtual::Set.new etc. # 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, # This makes the dispatch extensible, ie Expressions may be added by external code,
# as long as matching compile methods are supplied too. # as long as matching compile methods are supplied too.
# #
def self.compile expression , method def self.compile expression , method
exp_name = expression.type compiler = Compiler.new method
puts "Expression #{expression.to_sexp}" compiler.process expression
self.send "compile_#{exp_name}".to_sym , expression, method
end end
end end

View File

@ -1,7 +1,7 @@
module Bosl module Bosl
# collection of the simple ones, int and strings and such # 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. # 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_ # 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 # But in the future (in the one that holds great things) we optimize those unneccesay moves away
# attr_reader :value # attr_reader :value
def self.compile_int expression , method def on_int expression
int = *expression int = *expression
to = Virtual::Return.new(Integer , int) to = Virtual::Return.new(Integer , int)
method.source.add_code Virtual::Set.new( int , to ) method.source.add_code Virtual::Set.new( int , to )
to to
end end
def self.compile_true expression , method def on_true expression
to = Virtual::Return.new(Reference , true ) to = Virtual::Return.new(Reference , true )
method.source.add_code Virtual::Set.new( true , to ) method.source.add_code Virtual::Set.new( true , to )
to to
end end
def self.compile_false expression , method def on_false expression
to = Virtual::Return.new(Reference , false) to = Virtual::Return.new(Reference , false)
method.source.add_code Virtual::Set.new( false , to ) method.source.add_code Virtual::Set.new( false , to )
to to
end end
def self.compile_nil expression , method def on_nil expression
to = Virtual::Return.new(Reference , nil) to = Virtual::Return.new(Reference , nil)
method.source.add_code Virtual::Set.new( nil , to ) method.source.add_code Virtual::Set.new( nil , to )
to to
end end
def self.compile_modulename expression , method def on_modulename expression
clazz = Parfait::Space.object_space.get_class_by_name expression.name clazz = Parfait::Space.object_space.get_class_by_name expression.name
raise "compile_modulename #{clazz}.#{name}" unless clazz raise "compile_modulename #{clazz}.#{name}" unless clazz
to = Virtual::Return.new(Reference , clazz ) to = Virtual::Return.new(Reference , clazz )
@ -46,7 +46,7 @@ module Bosl
end end
# attr_reader :string # attr_reader :string
def self.compile_string expression , method def on_string expression
# Clearly a TODO here to implement strings rather than reusing symbols # Clearly a TODO here to implement strings rather than reusing symbols
value = expression.first.to_sym value = expression.first.to_sym
to = Virtual::Return.new(Virtual::Reference , value) to = Virtual::Return.new(Virtual::Reference , value)
@ -56,11 +56,11 @@ module Bosl
end end
#attr_reader :left, :right #attr_reader :left, :right
def self.compile_assignment expression , method def on_assignment expression
unless expression.left.instance_of? Ast::NameExpression unless expression.left.instance_of? Ast::NameExpression
raise "must assign to NameExpression , not #{expression.left}" raise "must assign to NameExpression , not #{expression.left}"
end end
r = Compiler.compile(expression.right , method ) r = process(expression.right )
raise "oh noo, nil from where #{expression.right.inspect}" unless r raise "oh noo, nil from where #{expression.right.inspect}" unless r
index = method.has_arg(expression.left.name.to_sym) index = method.has_arg(expression.left.name.to_sym)
if index if index
@ -72,7 +72,7 @@ module Bosl
r r
end end
def self.compile_variable expression, method def on_variable expression
method.source.add_code InstanceGet.new(expression.name) method.source.add_code InstanceGet.new(expression.name)
Virtual::Return.new( Unknown ) Virtual::Return.new( Unknown )
end end

View File

@ -1,15 +1,15 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# operators are really function calls # operators are really function calls
# call_site - attr_reader :name, :args , :receiver # call_site - attr_reader :name, :args , :receiver
def self.compile_call expession , method def on_call expession
name , arguments , receiver = *expession name , arguments , receiver = *expession
name = name.to_a.first name = name.to_a.first
if receiver if receiver
me = Compiler.compile( receiver.to_a.first , method ) me = process( receiver.to_a.first )
else else
me = Virtual::Self.new me = Virtual::Self.new
end end
@ -21,7 +21,7 @@ module Bosl
compiled_args = [] compiled_args = []
arguments.to_a.each_with_index do |arg , i| arguments.to_a.each_with_index do |arg , i|
#compile in the running method, ie before passing control #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 # 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. # + 1 as this is a ruby 0-start , but 0 is the last message ivar.
# so the next free is +1 # so the next free is +1

View File

@ -1,15 +1,15 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# attr_reader :values # attr_reader :values
def self.compile_array expession, context def on_array expession, context
# to.do # to.do
end end
# attr_reader :key , :value # attr_reader :key , :value
def self.compile_association context def on_association context
# to.do # to.do
end end
def self.compile_hash context def on_hash context
# to.do # to.do
end end
end end

View File

@ -1,10 +1,8 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# list - attr_reader :expressions # list - attr_reader :expressions
def self.compile_expressions expession , method def on_expressions expession
expession.children.collect do |part| process_all( expession.children )
Compiler.compile( part , method )
end
end end
end end
end end

View File

@ -1,7 +1,7 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# function attr_reader :name, :params, :body , :receiver # function attr_reader :name, :params, :body , :receiver
def self.compile_function expression, method def on_function expression
return_type , name , parameters, kids = *expression return_type , name , parameters, kids = *expression
name = name.to_a.first name = name.to_a.first
args = parameters.to_a.collect do |p| args = parameters.to_a.collect do |p|
@ -11,7 +11,7 @@ module Bosl
if expression[:receiver] if expression[:receiver]
# compiler will always return slot. with known value or not # 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 ) if( r.value.is_a? Parfait::Class )
class_name = r.value.name class_name = r.value.name
else else
@ -25,11 +25,15 @@ module Bosl
new_method.source.receiver = r new_method.source.receiver = r
new_method.for_class.add_instance_method new_method new_method.for_class.add_instance_method new_method
old_method = @method
@method = new_method
#frame = frame.new_frame #frame = frame.new_frame
kids.to_a.each do |ex| 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 raise return_type.inspect if return_type.is_a? Virtual::Instruction
end end
@method = old_method
new_method.source.return_type = return_type new_method.source.return_type = return_type
Virtual::Return.new(return_type) Virtual::Return.new(return_type)
end end

View File

@ -1,8 +1,8 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# if - attr_reader :cond, :if_true, :if_false # 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 , if_true , if_false = *expression
condition = condition.first condition = condition.first
# to execute the logic as the if states it, the blocks are the other way around # 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 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. # 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 # 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 ) 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) # compile the true block (as we think of it first, even it is second in sequential order)
method.source.current true_block method.source.current true_block
last = is last = is
if_true.each do |part| last = process_all(if_true).last
last = Compiler.compile(part,method )
raise part.inspect if last.nil?
end
# compile the false block # compile the false block
method.source.current false_block method.source.current false_block
if_false.each do |part| last = process_all(if_false).last
#puts "compiling in if false #{part}"
last = Compiler.compile(part,method )
raise part.inspect if last.nil?
end
method.source.add_code Virtual::UnconditionalBranch.new( merge_block ) method.source.add_code Virtual::UnconditionalBranch.new( merge_block )
#puts "compiled if: end" #puts "compiled if: end"

View File

@ -1,11 +1,11 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# module attr_reader :name ,:expressions # module attr_reader :name ,:expressions
def self.compile_module expression , context def on_module expression
return clazz return clazz
end end
def self.compile_class expression , method def on_class expression
clazz = Parfait::Space.object_space.get_class_by_name! expression.name clazz = Parfait::Space.object_space.get_class_by_name! expression.name
#puts "Compiling class #{clazz.name.inspect}" #puts "Compiling class #{clazz.name.inspect}"
expression_value = nil expression_value = nil
@ -15,7 +15,7 @@ module Bosl
# ie throw an error for now # ie throw an error for now
raise "only functions for now #{expr.inspect}" unless expr.is_a? Ast::FunctionExpression raise "only functions for now #{expr.inspect}" unless expr.is_a? Ast::FunctionExpression
#puts "compiling expression #{expression}" #puts "compiling expression #{expression}"
expression_value = Compiler.compile(expr,method ) expression_value = process(expr )
#puts "compiled expression #{expression_value.inspect}" #puts "compiled expression #{expression_value.inspect}"
end end

View File

@ -1,11 +1,11 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# attr_reader :name # attr_reader :name
# compiling name needs to check if it's a variable and if so resolve it # 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. # 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) # 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 name = expression.to_a.first
return Virtual::Self.new( Reference.new(method.for_class)) if name == :self return Virtual::Self.new( Reference.new(method.for_class)) if name == :self
# either an argument, so it's stored in message # either an argument, so it's stored in message

View File

@ -1,17 +1,17 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# operator attr_reader :operator, :left, :right # operator attr_reader :operator, :left, :right
def self.compile_operator expression, method def on_operator expression
operator , left , right = *expression operator , left , right = *expression
nil nil
end end
def self.compile_assign expression, method def on_assign expression
puts "assign" puts "assign"
puts expression.inspect puts expression.inspect
name , value = *expression name , value = *expression
name = name.to_a.first name = name.to_a.first
v = self.compile(value , method ) v = process(value )
index = method.ensure_local( name ) index = method.ensure_local( name )
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index ) , v ) method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index ) , v )
end end

View File

@ -1,9 +1,9 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# return attr_reader :expression # return attr_reader :expression
def self.compile_return expression, method def on_return expression
return Compiler.compile(expression.to_a.first , method) return process(expression.to_a.first )
end end
end end
end end

View File

@ -1,21 +1,21 @@
module Bosl module Bosl
module Compiler Compiler.class_eval do
# while- attr_reader :condition, :body # 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 # this is where the while ends and both branches meet
merge = method.source.new_block("while merge") merge = method.source.new_block("while merge")
# this comes after the current and beofre the merge # this comes after the current and beofre the merge
start = method.source.new_block("while_start" ) start = method.source.new_block("while_start" )
method.source.current start method.source.current start
cond = Compiler.compile(expression.condition, method) cond = process(expression.condition)
method.source.add_code IsTrueBranch.new(merge) method.source.add_code IsTrueBranch.new(merge)
last = cond last = cond
expression.body.each do |part| expression.body.each do |part|
last = Compiler.compile(part , method) last = process(part)
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
# unconditionally branch to the start # unconditionally branch to the start

View File

@ -4,11 +4,6 @@ require "stream_reader"
require "elf/object_writer" require "elf/object_writer"
require 'salama-reader' require 'salama-reader'
AST::Node.class_eval do AST::Node.class_eval do
def each
children.each do |child|
yield child
end
end
def first def first
children.first children.first
end end

View File

@ -2,7 +2,7 @@ require_relative '../helper'
require 'parslet/convenience' require 'parslet/convenience'
module CompilerHelper Compiler.class_eval doHelper
def check def check
Virtual.machine.boot.compile_main @string_input Virtual.machine.boot.compile_main @string_input

View File

@ -6,7 +6,7 @@ class CompilerTest < MiniTest::Test
Virtual.machine.boot Virtual.machine.boot
end end
def check 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}" assert res.is_a?(Virtual::Slot) , "compiler must compile to slot, not #{res.class}"
end end
def true_ex def true_ex

View File

@ -8,9 +8,11 @@ module Fragments
def check def check
expressions = Virtual.machine.boot.compile_main @string_input expressions = Virtual.machine.boot.compile_main @string_input
puts expressions
@expect.each_with_index do | should , i | @expect.each_with_index do | should , i |
assert expressions[i].is_a?(Virtual::Slot) , "compiles should return slots, not #{should.class}" exp_i = expressions[i]
assert_equal should , expressions[i].class assert exp_i.is_a?(Virtual::Slot) , "compiles should return #{should}, not #{exp_i}"
assert_equal should , exp_i.class
end end
# Virtual.machine.run_passes # Virtual.machine.run_passes
end end

View File

@ -5,7 +5,7 @@ class AddTest < MiniTest::Test
def setup def setup
Virtual.machine.boot Virtual.machine.boot
code =Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5)) 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" Virtual.machine.run_before "Register::CallImplementation"
@interpreter = Interpreter::Interpreter.new @interpreter = Interpreter::Interpreter.new
@interpreter.start Virtual.machine.init @interpreter.start Virtual.machine.init

View File

@ -5,7 +5,7 @@ class TestPuts < MiniTest::Test
def setup def setup
Virtual.machine.boot Virtual.machine.boot
code = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:putstring, [] ,Ast::StringExpression.new("Hello again"))]) 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" Virtual.machine.run_before "Register::CallImplementation"
@interpreter = Interpreter::Interpreter.new @interpreter = Interpreter::Interpreter.new
@interpreter.start Virtual.machine.init @interpreter.start Virtual.machine.init