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 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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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