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
# attr_reader :value
def compile frame , method
def compile method , frame
Virtual::IntegerConstant.new value
end
end
class TrueExpression
def compile frame , method
def compile method , frame
Virtual::TrueValue.new
end
end
class FalseExpression
def compile frame , method
def compile method , frame
Virtual::FalseValue.new
end
end
class NilExpression
def compile frame , method
def compile method , frame
Virtual::NilValue.new
end
end
@ -33,7 +33,7 @@ module Ast
# 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.
# 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
if method.has_var(name)
frame.compile_get(method , name )
@ -45,7 +45,7 @@ module Ast
class ModuleName < NameExpression
def compile frame , method
def compile method , frame
clazz = ::Virtual::Object.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
#class qualifier, means call from metaclass
@ -56,7 +56,7 @@ module Ast
class StringExpression < Expression
# attr_reader :string
def compile frame , method
def compile method , frame
value = Virtual::StringConstant.new(string)
::Virtual::Object.space.add_object value
value
@ -65,9 +65,9 @@ module Ast
class AssignmentExpression < Expression
#attr_reader :left, :right
def compile frame , method
def compile method , frame
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 )
end
def old_scratch
@ -93,7 +93,7 @@ module Ast
end
class VariableExpression < NameExpression
def compile frame ,method
def compile method , frame
method.add Virtual::ObjectGet.new(name)
Virtual::Return.new( Virtual::Mystery.new )
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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