2015-05-08 14:10:30 +02:00
|
|
|
module Virtual
|
|
|
|
# collection of the simple ones, int and strings and such
|
|
|
|
|
|
|
|
module Compiler
|
|
|
|
|
|
|
|
# Constant expressions can by definition be evaluated at compile time.
|
|
|
|
# But that does not solve their storage, ie they need to be accessible at runtime from _somewhere_
|
|
|
|
# So we view ConstantExpressions like functions that return the value of the constant.
|
|
|
|
# In other words, their storage is the return slot as it would be for a method
|
|
|
|
|
|
|
|
# The current approach moves the constant into a variable before using it
|
|
|
|
# But in the future (in the one that holds great things) we optimize those unneccesay moves away
|
|
|
|
|
|
|
|
# attr_reader :value
|
|
|
|
def self.compile_integer expression , method
|
2015-06-02 08:05:27 +02:00
|
|
|
int = expression.value
|
2015-05-08 14:19:30 +02:00
|
|
|
to = Return.new(Integer , int)
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new( int , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compile_true expression , method
|
2015-06-02 08:05:27 +02:00
|
|
|
to = Return.new(Reference , true )
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new( true , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compile_false expression , method
|
2015-06-02 08:05:27 +02:00
|
|
|
to = Return.new(Reference , false)
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new( false , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compile_nil expression , method
|
2015-06-02 08:05:27 +02:00
|
|
|
to = Return.new(Reference , nil)
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new( nil , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
# attr_reader :name
|
|
|
|
# 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 issued.
|
|
|
|
# whichever way this goes the result is stored in the return slot (as all compiles)
|
|
|
|
def self.compile_name expression , method
|
2015-05-24 12:54:17 +02:00
|
|
|
return Self.new( Reference.new(method.for_class)) if expression.name == :self
|
2015-05-31 17:34:18 +02:00
|
|
|
name = expression.name.to_sym
|
2015-05-20 16:29:08 +02:00
|
|
|
if method.has_var(name)
|
2015-05-08 14:10:30 +02:00
|
|
|
# either an argument, so it's stored in message
|
|
|
|
if( index = method.has_arg(name))
|
2015-07-19 10:15:38 +02:00
|
|
|
method.source.add_code Set.new( MessageSlot.new(expression.name) , Return.new)
|
2015-05-08 14:10:30 +02:00
|
|
|
else # or a local so it is in the frame
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code FrameGet.new(expression.name , index)
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
call = Ast::CallSiteExpression.new(expression.name , [] ) #receiver self is implicit
|
|
|
|
Compiler.compile(call, method)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.compile_module expression , method
|
2015-05-16 11:54:11 +02:00
|
|
|
clazz = Space.space.get_class_by_name name
|
2015-05-08 14:10:30 +02:00
|
|
|
raise "uups #{clazz}.#{name}" unless clazz
|
2015-05-08 14:19:30 +02:00
|
|
|
to = Return.new(Reference , clazz )
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new( clazz , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
# attr_reader :string
|
|
|
|
def self.compile_string expression , method
|
2015-06-11 07:04:14 +02:00
|
|
|
# Clearly a TODO here to implement strings rather than reusing symbols
|
2015-05-31 17:34:18 +02:00
|
|
|
value = expression.string.to_sym
|
2015-05-08 14:19:30 +02:00
|
|
|
to = Return.new(Reference , value)
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.constants << value
|
|
|
|
method.source.add_code Set.new( value , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
#attr_reader :left, :right
|
|
|
|
def self.compile_assignment expression , method
|
2015-05-30 11:20:39 +02:00
|
|
|
unless expression.left.instance_of? Ast::NameExpression
|
|
|
|
raise "must assign to NameExpression , not #{expression.left}"
|
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
r = Compiler.compile(expression.right , method )
|
|
|
|
raise "oh noo, nil from where #{expression.right.inspect}" unless r
|
2015-06-02 08:05:27 +02:00
|
|
|
index = method.has_arg(expression.left.name.to_sym)
|
2015-05-08 14:10:30 +02:00
|
|
|
if index
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new(MessageSlot.new(index , r,type , r ) , Return.new)
|
2015-05-08 14:10:30 +02:00
|
|
|
else
|
2015-05-31 17:34:18 +02:00
|
|
|
index = method.ensure_local(expression.left.name.to_sym)
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code Set.new(FrameSlot.new(index , r.type , r ) , Return.new)
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compile_variable expression, method
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code InstanceGet.new(expression.name)
|
2015-06-11 07:04:14 +02:00
|
|
|
Return.new( Unknown )
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|