change method and frame around in calling, easier to understand static first

This commit is contained in:
Torsten Ruger 2014-07-24 14:56:27 +03:00
parent e408b0e4b9
commit e427bcef43
9 changed files with 29 additions and 29 deletions

View File

@ -4,25 +4,25 @@ module Ast
class IntegerExpression < Expression class IntegerExpression < Expression
# attr_reader :value # attr_reader :value
def compile frame , method def compile method , frame
Virtual::IntegerConstant.new value Virtual::IntegerConstant.new value
end end
end end
class TrueExpression class TrueExpression
def compile frame , method def compile method , frame
Virtual::TrueValue.new Virtual::TrueValue.new
end end
end end
class FalseExpression class FalseExpression
def compile frame , method def compile method , frame
Virtual::FalseValue.new Virtual::FalseValue.new
end end
end end
class NilExpression class NilExpression
def compile frame , method def compile method , frame
Virtual::NilValue.new Virtual::NilValue.new
end end
end end
@ -33,7 +33,7 @@ module Ast
# 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 ussued. # otherwise it's a method without args and a send is ussued.
# this makes the namespace static, ie when eval and co are implemented method needs recompilation # this makes the namespace static, ie when eval and co are implemented method needs recompilation
def compile frame , method def compile method , frame
return Virtual::Self.new( Virtual::Mystery.new ) if name == :self return Virtual::Self.new( Virtual::Mystery.new ) if name == :self
if method.has_var(name) if method.has_var(name)
frame.compile_get(method , name ) frame.compile_get(method , name )
@ -45,7 +45,7 @@ module Ast
class ModuleName < NameExpression class ModuleName < NameExpression
def compile frame , method def compile method , frame
clazz = ::Virtual::Object.space.get_or_create_class name clazz = ::Virtual::Object.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz raise "uups #{clazz}.#{name}" unless clazz
#class qualifier, means call from metaclass #class qualifier, means call from metaclass
@ -56,7 +56,7 @@ module Ast
class StringExpression < Expression class StringExpression < Expression
# attr_reader :string # attr_reader :string
def compile frame , method def compile method , frame
value = Virtual::StringConstant.new(string) value = Virtual::StringConstant.new(string)
::Virtual::Object.space.add_object value ::Virtual::Object.space.add_object value
value value
@ -65,9 +65,9 @@ module Ast
class AssignmentExpression < Expression class AssignmentExpression < Expression
#attr_reader :left, :right #attr_reader :left, :right
def compile frame , method def compile method , frame
raise "must assign to NameExpression , not #{left}" unless left.instance_of? NameExpression raise "must assign to NameExpression , not #{left}" unless left.instance_of? NameExpression
r = right.compile(frame , method) r = right.compile(method,frame)
frame.compile_set( method , left.name , r ) frame.compile_set( method , left.name , r )
end end
def old_scratch def old_scratch
@ -93,7 +93,7 @@ module Ast
end end
class VariableExpression < NameExpression class VariableExpression < NameExpression
def compile frame ,method def compile method , frame
method.add Virtual::ObjectGet.new(name) method.add Virtual::ObjectGet.new(name)
Virtual::Return.new( Virtual::Mystery.new ) Virtual::Return.new( Virtual::Mystery.new )
end end

View File

@ -4,9 +4,9 @@ module Ast
class CallSiteExpression < Expression class CallSiteExpression < Expression
# attr_reader :name, :args , :receiver # attr_reader :name, :args , :receiver
@@counter = 0 @@counter = 0
def compile frame , method def compile method , frame
me = receiver.compile(frame , method ) me = receiver.compile( method, frame )
with = args.collect{|a| a.compile(frame , method)} with = args.collect{|a| a.compile( method,frame)}
frame.compile_send( method , name , me , with ) frame.compile_send( method , name , me , with )
end end

View File

@ -1,8 +1,8 @@
module Ast module Ast
class ExpressionList < Expression class ExpressionList < Expression
# attr_reader :expressions # attr_reader :expressions
def compile binding , method def compile method , frame
expressions.collect { |part| part.compile( binding , method ) } expressions.collect { |part| part.compile( method, frame ) }
end end
end end
end end

View File

@ -1,17 +1,17 @@
module Ast module Ast
class FunctionExpression < Expression class FunctionExpression < Expression
# attr_reader :name, :params, :body , :receiver # attr_reader :name, :params, :body , :receiver
def compile frame , method def compile method , frame
args = params.collect do |p| args = params.collect do |p|
raise "error, arguemnt must be a identifier, not #{p}" unless p.is_a? NameExpression raise "error, arguemnt must be a identifier, not #{p}" unless p.is_a? NameExpression
Virtual::Argument.new( p.name , Virtual::Mystery.new ) Virtual::Argument.new( p.name , Virtual::Mystery.new )
end end
r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new r = receiver ? receiver.compile(method,frame) : Virtual::SelfReference.new
method = Virtual::MethodDefinition.new(name , args , r ) method = Virtual::MethodDefinition.new(name , args , r )
frame = frame.new_frame frame = frame.new_frame
return_type = nil return_type = nil
body.each do |ex| body.each do |ex|
return_type = ex.compile(frame , method ) return_type = ex.compile(method,frame )
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.return_type = return_type method.return_type = return_type

View File

@ -1,15 +1,15 @@
module Ast module Ast
class IfExpression < Expression class IfExpression < Expression
# attr_reader :cond, :if_true, :if_false # attr_reader :cond, :if_true, :if_false
def compile frame , method def compile method , frame
is = cond.compile(frame , method) is = cond.compile(method,frame)
# is.is_false(frame,method) # is.is_false(frame,method)
# TODO should/will use different branches for different conditions. # TODO should/will use different branches for different conditions.
branch = Virtual::ImplicitBranch.new "if_merge" branch = Virtual::ImplicitBranch.new "if_merge"
method.add branch method.add branch
last = is last = is
if_true.each do |part| if_true.each do |part|
last = part.compile(frame,method ) last = part.compile(method,frame )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
merge = Virtual::Label.new(branch.name) merge = Virtual::Label.new(branch.name)
@ -17,7 +17,7 @@ module Ast
branch.swap branch.swap
method.current = branch method.current = branch
if_false.each do |part| if_false.each do |part|
last = part.compile(frame,method ) last = part.compile(method,frame )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
method.add merge method.add merge

View File

@ -1,9 +1,9 @@
module Ast module Ast
class OperatorExpression < Expression class OperatorExpression < Expression
# attr_reader :operator, :left, :right # attr_reader :operator, :left, :right
def compile frame , method def compile method , frame
call = CallSiteExpression.new( operator , [right] , left ) call = CallSiteExpression.new( operator , [right] , left )
call.compile(frame , method) call.compile(method,frame)
end end
end end
end end

View File

@ -1,7 +1,7 @@
module Ast module Ast
class ReturnExpression < Expression class ReturnExpression < Expression
# attr_reader :expression # attr_reader :expression
def compile frame ,method def compile scope ,method
Virtual::Reference.new Virtual::Reference.new
end end
def sc def sc

View File

@ -1,17 +1,17 @@
module Ast module Ast
class WhileExpression < Expression class WhileExpression < Expression
# attr_reader :condition, :body # attr_reader :condition, :body
def compile frame , method def compile method , frame
start = Virtual::Label.new("while_start") start = Virtual::Label.new("while_start")
method.add start method.add start
is = condition.compile(frame , method) is = condition.compile(method,frame)
branch = Virtual::ImplicitBranch.new "while" branch = Virtual::ImplicitBranch.new "while"
merge = Virtual::Label.new(branch.name) merge = Virtual::Label.new(branch.name)
branch.other = merge #false jumps to end of while branch.other = merge #false jumps to end of while
method.add branch method.add branch
last = is last = is
body.each do |part| body.each do |part|
last = part.compile(frame,method ) last = part.compile(method,frame )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
# unconditionally brnach to the start # unconditionally brnach to the start

View File

@ -13,7 +13,7 @@ module VirtualHelper
parts = Parser::Transform.new.apply(syntax) parts = Parser::Transform.new.apply(syntax)
machine = Virtual::Machine.new machine = Virtual::Machine.new
main = Virtual::MethodDefinition.main main = Virtual::MethodDefinition.main
expressions = parts.compile(machine.frame , main ) expressions = parts.compile( main, machine.frame )
should = YAML.load(@output.gsub("RETURN_MARKER" , "\n")) should = YAML.load(@output.gsub("RETURN_MARKER" , "\n"))
assert_equal should , expressions , expressions.to_yaml.gsub("\n" , "RETURN_MARKER") + "\n" + expressions.to_yaml assert_equal should , expressions , expressions.to_yaml.gsub("\n" , "RETURN_MARKER") + "\n" + expressions.to_yaml
end end