working on if
This commit is contained in:
parent
3a152c1295
commit
55cb6bd2d6
@ -2,9 +2,12 @@ module Ast
|
||||
class FunctionExpression < Expression
|
||||
# attr_reader :name, :params, :body , :receiver
|
||||
def compile frame , method
|
||||
args = params.collect{ |p| p.compile(frame , method )}
|
||||
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
|
||||
method = Virtual::Method.new(name , params , r )
|
||||
method = Virtual::Method.new(name , args , r )
|
||||
frame = frame.new_frame
|
||||
return_type = nil
|
||||
body.each do |ex|
|
||||
|
@ -2,7 +2,14 @@ module Ast
|
||||
class IfExpression < Expression
|
||||
# attr_reader :cond, :if_true, :if_false
|
||||
def compile frame , method
|
||||
Virtual::Reference
|
||||
is = cond.compile(frame , method)
|
||||
# is.is_false(frame,method)
|
||||
last = is
|
||||
if_true.each do |part|
|
||||
last = part.compile(frame,method )
|
||||
raise part.inspect if last.nil?
|
||||
end
|
||||
last
|
||||
end
|
||||
def old
|
||||
f = context.function
|
||||
|
@ -40,6 +40,7 @@ module Virtual
|
||||
end
|
||||
|
||||
def compile_set method , name , val
|
||||
method.set_var(name,val)
|
||||
method.add FrameSet.new(name , val )
|
||||
method.get_var(name)
|
||||
end
|
||||
|
@ -40,12 +40,29 @@ module Virtual
|
||||
# variables are locals and and arguments
|
||||
# used to determine if a send must be issued
|
||||
def has_var name
|
||||
var = @args.find {|a| a == name }
|
||||
var = @locals.find {|a| a == name } unless var
|
||||
var = @tmps.find {|a| a == name } unless var
|
||||
name = name.to_sym
|
||||
var = @args.find {|a| a.name == name }
|
||||
var = @locals.find {|a| a.name == name } unless var
|
||||
var = @tmps.find {|a| a.name == name } unless var
|
||||
var
|
||||
end
|
||||
|
||||
def set_var name , var
|
||||
v = has_var name
|
||||
if( v )
|
||||
puts "resetting local #{v}"
|
||||
else
|
||||
v = Local.new(name , var)
|
||||
@locals << v
|
||||
end
|
||||
v
|
||||
end
|
||||
|
||||
def get_var name
|
||||
var = has_var name
|
||||
raise "no var #{name} in method #{self.name} , #{@locals} #{@args}" unless var
|
||||
var
|
||||
end
|
||||
alias :get_var :has_var
|
||||
|
||||
def get_tmp
|
||||
name = "__tmp__#{@tmps.length}"
|
||||
|
@ -36,7 +36,7 @@ module Virtual
|
||||
class Variable < Value
|
||||
|
||||
def initialize name , type
|
||||
@name = name
|
||||
@name = name.to_sym
|
||||
@type = type
|
||||
end
|
||||
attr_accessor :name , :type
|
||||
|
@ -9,7 +9,7 @@ def foo(x)
|
||||
5
|
||||
end
|
||||
HERE
|
||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(nil),Virtual::IntegerConstant.new(5),Virtual::MethodEnter.new(nil))]
|
||||
@output = [Virtual::Method.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::IntegerConstant.new(5),Virtual::MethodEnter.new(nil))]
|
||||
check
|
||||
end
|
||||
|
||||
@ -19,7 +19,7 @@ def String.length(x)
|
||||
@length
|
||||
end
|
||||
HERE
|
||||
@output = [Virtual::Method.new(:length,[Ast::NameExpression.new(:x)],Boot::BootClass.new(:String,:Object),Virtual::Return.new( Virtual::Mystery.new()),Virtual::MethodEnter.new(Virtual::ObjectGet.new(:length)))]
|
||||
@output = [Virtual::Method.new(:length,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Boot::BootClass.new(:String,:Object),Virtual::Return.new(Virtual::Mystery.new()),Virtual::MethodEnter.new(Virtual::ObjectGet.new(:length,nil)))]
|
||||
check
|
||||
end
|
||||
|
||||
@ -30,7 +30,7 @@ def foo(x)
|
||||
2 + 5
|
||||
end
|
||||
HERE
|
||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5),Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil)))))]
|
||||
@output = [Virtual::Method.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5),Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil)))))]
|
||||
check
|
||||
end
|
||||
|
||||
@ -44,7 +44,7 @@ HERE
|
||||
check
|
||||
end
|
||||
|
||||
def ttest_function_if
|
||||
def test_function_if
|
||||
@string_input = <<HERE
|
||||
def ofthen(n)
|
||||
if(0)
|
||||
@ -54,7 +54,7 @@ def ofthen(n)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
||||
@output = nil
|
||||
check
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user