working on if
This commit is contained in:
parent
3a152c1295
commit
55cb6bd2d6
@ -2,9 +2,12 @@ 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 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
|
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
|
frame = frame.new_frame
|
||||||
return_type = nil
|
return_type = nil
|
||||||
body.each do |ex|
|
body.each do |ex|
|
||||||
|
@ -2,7 +2,14 @@ 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 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
|
end
|
||||||
def old
|
def old
|
||||||
f = context.function
|
f = context.function
|
||||||
|
@ -40,6 +40,7 @@ module Virtual
|
|||||||
end
|
end
|
||||||
|
|
||||||
def compile_set method , name , val
|
def compile_set method , name , val
|
||||||
|
method.set_var(name,val)
|
||||||
method.add FrameSet.new(name , val )
|
method.add FrameSet.new(name , val )
|
||||||
method.get_var(name)
|
method.get_var(name)
|
||||||
end
|
end
|
||||||
|
@ -40,12 +40,29 @@ module Virtual
|
|||||||
# variables are locals and and arguments
|
# variables are locals and and arguments
|
||||||
# used to determine if a send must be issued
|
# used to determine if a send must be issued
|
||||||
def has_var name
|
def has_var name
|
||||||
var = @args.find {|a| a == name }
|
name = name.to_sym
|
||||||
var = @locals.find {|a| a == name } unless var
|
var = @args.find {|a| a.name == name }
|
||||||
var = @tmps.find {|a| a == name } unless var
|
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
|
var
|
||||||
end
|
end
|
||||||
alias :get_var :has_var
|
|
||||||
|
|
||||||
def get_tmp
|
def get_tmp
|
||||||
name = "__tmp__#{@tmps.length}"
|
name = "__tmp__#{@tmps.length}"
|
||||||
|
@ -36,7 +36,7 @@ module Virtual
|
|||||||
class Variable < Value
|
class Variable < Value
|
||||||
|
|
||||||
def initialize name , type
|
def initialize name , type
|
||||||
@name = name
|
@name = name.to_sym
|
||||||
@type = type
|
@type = type
|
||||||
end
|
end
|
||||||
attr_accessor :name , :type
|
attr_accessor :name , :type
|
||||||
|
@ -9,7 +9,7 @@ def foo(x)
|
|||||||
5
|
5
|
||||||
end
|
end
|
||||||
HERE
|
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
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ def String.length(x)
|
|||||||
@length
|
@length
|
||||||
end
|
end
|
||||||
HERE
|
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
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ def foo(x)
|
|||||||
2 + 5
|
2 + 5
|
||||||
end
|
end
|
||||||
HERE
|
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
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ HERE
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def ttest_function_if
|
def test_function_if
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
def ofthen(n)
|
def ofthen(n)
|
||||||
if(0)
|
if(0)
|
||||||
@ -54,7 +54,7 @@ def ofthen(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])]
|
@output = nil
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user