remove message as third arg to compile

This commit is contained in:
Torsten Ruger 2015-05-06 15:14:47 +03:00
parent 1feed6af44
commit 8a7db6d4f2
8 changed files with 40 additions and 33 deletions

View File

@ -11,28 +11,28 @@ module Compiler
# But in the future (in the one that holds great things) we optimize those unneccesay moves away
# attr_reader :value
def self.compile_integer expession , method , message
def self.compile_integer expession , method
int = Virtual::IntegerConstant.new(expession.value)
to = Virtual::Return.new(Virtual::Integer , int)
method.add_code Virtual::Set.new( to , int)
to
end
def self.compile_true expession , method , message
def self.compile_true expession , method
value = Virtual::TrueConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
end
def self.compile_false expession , method , message
def self.compile_false expession , method
value = Virtual::FalseConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
end
def self.compile_nil expession , method , message
def self.compile_nil expession , method
value = Virtual::NilConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
@ -43,7 +43,7 @@ module Compiler
# 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 usued.
# this makes the namespace static, ie when eval and co are implemented method needs recompilation
def self.compile_name expession , method , message
def self.compile_name expession , method
return Virtual::Self.new( Virtual::Mystery ) if expession.name == :self
if method.has_var(expession.name)
message.compile_get(method , expession.name )
@ -54,7 +54,7 @@ module Compiler
end
def self.compile_module expession , method , message
def self.compile_module expession , method
clazz = Virtual::BootSpace.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
to = Virtual::Return.new(Virtual::Reference , clazz )
@ -63,7 +63,7 @@ module Compiler
end
# attr_reader :string
def self.compile_string expession , method , message
def self.compile_string expession , method
value = Virtual::StringConstant.new(expession.string)
to = Virtual::Return.new(Virtual::Reference , value)
Virtual::BootSpace.space.add_object value
@ -72,14 +72,21 @@ module Compiler
end
#attr_reader :left, :right
def self.compile_assignment expession , method , message
def self.compile_assignment expession , method
raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? Ast::NameExpression
r = Compiler.compile(expession.right , method,message)
r = Compiler.compile(expession.right , method )
raise "oh noo, nil from where #{expession.right.inspect}" unless r
message.compile_set( method , expession.left.name , r )
index = method.has_arg(name)
if index
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::MessageSlot.new(index , r,type , r ))
else
index = method.ensure_local(expession.left.name)
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::FrameSlot.new(index , r.type , r ))
end
r
end
def self.compile_variable expession, method , message
def self.compile_variable expession, method
method.add_code Virtual::InstanceGet.new(expession.name)
Virtual::Return.new( Virtual::Mystery )
end

View File

@ -3,15 +3,15 @@ module Compiler
# call_site - attr_reader :name, :args , :receiver
def self.compile_callsite expession , method , message
me = Compiler.compile( expession.receiver , method, message )
def self.compile_callsite expession , method
me = Compiler.compile( expession.receiver , method )
method.add_code Virtual::NewMessage.new
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me)
method.add_code Virtual::Set.new(Virtual::NewName.new(), Virtual::StringConstant.new(expession.name))
compiled_args = []
expession.args.each_with_index do |arg , i|
#compile in the running method, ie before passing control
val = Compiler.compile( arg , method, message)
val = Compiler.compile( arg , method)
# move the compiled value to it's slot in the new message
to = Virtual::NewMessageSlot.new(i ,val.type , val)
# (doing this immediately, not after the loop, so if it's a return it won't get overwritten)

View File

@ -1,8 +1,8 @@
module Compiler
# list - attr_reader :expressions
def self.compile_list expession , method , message
def self.compile_list expession , method
expession.expressions.collect do |part|
Compiler.compile( part , method, message )
Compiler.compile( part , method )
end
end
end

View File

@ -1,11 +1,11 @@
module Compiler
# function attr_reader :name, :params, :body , :receiver
def self.compile_function expression, method , message
def self.compile_function expression, method
args = expression.params.collect do |p|
raise "error, argument must be a identifier, not #{p}" unless p.is_a? Ast::NameExpression
p.name
end
r = expression.receiver ? expression.receiver.compile(method,message) : Virtual::Self.new()
r = expression.receiver ? Compiler.compile(expression.receiver, method ) : Virtual::Self.new()
new_method = Virtual::CompiledMethod.new(expression.name , args , r )
new_method.class_name = r.is_a?(Virtual::BootClass) ? r.name : method.class_name
clazz = Virtual::BootSpace.space.get_or_create_class(new_method.class_name)
@ -14,7 +14,7 @@ module Compiler
#frame = frame.new_frame
return_type = nil
expression.body.each do |ex|
return_type = Compiler.compile(ex,new_method,message )
return_type = Compiler.compile(ex,new_method )
raise return_type.inspect if return_type.is_a? Virtual::Instruction
end
new_method.return_type = return_type

View File

@ -1,7 +1,7 @@
module Compiler
# if - attr_reader :cond, :if_true, :if_false
def self.compile_if expression , method , message
def self.compile_if expression , method
# to execute the logic as the if states it, the blocks are the other way around
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block
merge_block = method.new_block "if_merge" # last one, created first
@ -9,7 +9,7 @@ module Compiler
false_block = method.new_block "if_false" # directly next in order, ie if we don't jump we land here
is = cond.compile(method,message)
is = Compiler.compile(expression.cond, method )
# TODO should/will use different branches for different conditions.
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? Virtual::BranchCondition
method.add_code Virtual::IsTrueBranch.new( true_block )
@ -17,16 +17,16 @@ module Compiler
# compile the true block (as we think of it first, even it is second in sequential order)
method.current true_block
last = is
if_true.each do |part|
last = part.compile(method,message )
expression.if_true.each do |part|
last = Compiler.compile(part,method )
raise part.inspect if last.nil?
end
# compile the false block
method.current false_block
if_false.each do |part|
expression.if_false.each do |part|
puts "compiling in if false #{part}"
last = part.compile(method,message)
last = Compiler.compile(part,method )
raise part.inspect if last.nil?
end
method.add_code Virtual::UnconditionalBranch.new( merge_block )

View File

@ -4,7 +4,7 @@ module Compiler
return clazz
end
def self.compile_class expression , method , message
def self.compile_class expression , method
clazz = ::Virtual::BootSpace.space.get_or_create_class expression.name
puts "Created class #{clazz.name.inspect}"
expression.expressions.each do |expr|
@ -12,7 +12,7 @@ module Compiler
# if not, execute it, but that does means we should be in salama (executable), not ruby. 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,message )
expression_value = Compiler.compile(expr,method )
clazz.add_instance_method(expression_value)
#puts "compiled expression #{expression_value.inspect}"
end

View File

@ -1,7 +1,7 @@
module Compiler
# operator attr_reader :operator, :left, :right
def self.compile_operator expression, method , message
call = CallSiteExpression.new( operator , [right] , left )
call.compile(method,message)
def self.compile_operator expression, method
call = Ast::CallSiteExpression.new(expression.operator , [expression.right] , expression.left )
Compiler.compile(call, method)
end
end

View File

@ -1,17 +1,17 @@
module Compiler
# while- attr_reader :condition, :body
def self.compile_while expression, method , message
def self.compile_while expression, method
start = Virtual::Label.new("while_start")
method.add_code start
is = expression.condition.compile(method,message)
is = expression.condition.compile(method )
branch = Virtual::IsTrueBranch.new "while"
merge = Virtual::Label.new(branch.name)
branch.other = merge #false jumps to end of while
method.add_code branch
last = is
expression.body.each do |part|
last = part.compile(method,message )
last = part.compile(method )
raise part.inspect if last.nil?
end
# unconditionally brnach to the start