2014-05-05 09:02:02 +02:00
|
|
|
module Ast
|
|
|
|
|
|
|
|
# assignment, like operators are really function calls
|
|
|
|
|
|
|
|
class FuncallExpression < Expression
|
|
|
|
attr_reader :name, :args
|
|
|
|
def initialize name, args
|
|
|
|
@name , @args = name , args
|
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
def compile context , into
|
|
|
|
params = args.collect{ |a| a.compile(context, into) }
|
|
|
|
fun = Vm::FunctionCall.new( name , params )
|
|
|
|
fun.load_args into
|
|
|
|
fun.do_call into
|
2014-05-06 20:36:28 +02:00
|
|
|
fun
|
2014-05-05 09:13:49 +02:00
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
|
2014-05-10 11:54:10 +02:00
|
|
|
def inspect
|
|
|
|
self.class.name + ".new(" + name.inspect + ", ["+
|
|
|
|
args.collect{|m| m.inspect }.join( ",") +"] )"
|
|
|
|
end
|
2014-05-13 09:49:26 +02:00
|
|
|
def to_s
|
|
|
|
"#{name}(" + args.join(",") + ")"
|
|
|
|
end
|
2014-05-10 11:54:10 +02:00
|
|
|
def attributes
|
|
|
|
[:name , :args]
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-10 20:41:46 +02:00
|
|
|
class OperatorExpression < Expression
|
|
|
|
attr_reader :operator, :left, :right
|
|
|
|
|
|
|
|
def initialize operator, left, right
|
|
|
|
@operator, @left, @right = operator, left, right
|
|
|
|
end
|
|
|
|
def attributes
|
|
|
|
[:operator, :left, :right]
|
|
|
|
end
|
|
|
|
def inspect
|
|
|
|
self.class.name + ".new(" + operator.inspect + ", " + left.inspect + "," + right.inspect + ")"
|
|
|
|
end
|
2014-05-13 09:49:26 +02:00
|
|
|
def to_s
|
|
|
|
"#{left} #{operator} #{right}"
|
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
def compile context , into
|
|
|
|
r_val = right.compile(context , into)
|
|
|
|
|
|
|
|
if operator == "=" # assignemnt
|
|
|
|
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
|
|
|
context.locals[left.name] = r_val
|
|
|
|
return r_val
|
2014-05-10 20:41:46 +02:00
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
l_val = left.compile(context , into)
|
|
|
|
|
|
|
|
case operator
|
|
|
|
when ">"
|
|
|
|
code = l_val.less_or_equal r_val
|
|
|
|
when "+"
|
|
|
|
code = l_val.plus r_val
|
|
|
|
else
|
|
|
|
raise "unimplemented operator #{operator} #{self}"
|
2014-05-10 20:41:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|