rubyx/lib/ast/basic_expressions.rb

81 lines
2.0 KiB
Ruby
Raw Normal View History

# collection of the simple ones, int and strings and such
module Ast
class IntegerExpression < Expression
# attr_reader :value
def compile frame , method
2014-06-26 16:52:15 +02:00
Virtual::IntegerConstant.new value
end
end
2014-07-01 14:57:13 +02:00
class TrueExpression
def compile frame , method
2014-07-01 14:57:13 +02:00
Virtual::TrueValue.new
end
end
2014-07-10 16:14:38 +02:00
2014-07-01 14:57:13 +02:00
class FalseExpression
def compile frame , method
2014-07-01 14:57:13 +02:00
Virtual::FalseValue.new
end
end
2014-07-10 16:14:38 +02:00
2014-07-01 14:57:13 +02:00
class NilExpression
def compile frame , method
2014-07-01 14:57:13 +02:00
Virtual::NilValue.new
end
end
class NameExpression < Expression
# attr_reader :name
2014-07-10 16:14:38 +02:00
# 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
2014-07-15 09:35:29 +02:00
return Virtual::Self.new( Virtual::Mystery.new ) if name == :self
2014-07-10 16:14:38 +02:00
if method.has_var(name)
2014-07-13 15:00:48 +02:00
frame.compile_get(method , name )
2014-07-10 16:14:38 +02:00
else
2014-07-13 15:00:48 +02:00
frame.compile_send( method , name )
2014-07-10 16:14:38 +02:00
end
end
end
class ModuleName < NameExpression
def compile frame , method
2014-07-13 13:12:43 +02:00
clazz = ::Virtual::Object.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
#class qualifier, means call from metaclass
2014-07-14 13:06:09 +02:00
#clazz = clazz.meta_class
clazz
end
end
class StringExpression < Expression
# attr_reader :string
def compile frame , method
2014-06-26 16:52:15 +02:00
value = Virtual::StringConstant.new(string)
::Virtual::Object.space.add_object value
value
end
end
2014-07-14 15:19:47 +02:00
class AssignmentExpression < Expression
#attr_reader :left, :right
2014-07-14 15:19:47 +02:00
def compile frame , method
raise "must assign to NameExpression , not #{left}" unless left.instance_of? NameExpression
r = right.compile(frame,method)
frame.compile_set( method , left.name , r )
end
end
class VariableExpression < NameExpression
def compile frame ,method
2014-07-14 23:00:00 +02:00
method.add Virtual::ObjectGet.new(name)
2014-07-15 09:35:29 +02:00
Virtual::Return.new( Virtual::Mystery.new )
2014-07-14 15:19:47 +02:00
end
end
end