2014-06-25 01:47:59 +02:00
|
|
|
# collection of the simple ones, int and strings and such
|
|
|
|
|
2015-05-04 13:22:22 +02:00
|
|
|
module Compiler
|
2014-06-25 01:47:59 +02:00
|
|
|
|
2014-09-14 17:14:57 +02:00
|
|
|
# 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
|
|
|
|
|
2015-04-10 13:02:28 +02:00
|
|
|
# The current approach moves the constant into a variable before using it
|
2014-09-14 17:14:57 +02:00
|
|
|
# But in the future (in the one that holds great things) we optimize those unneccesay moves away
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2014-06-25 01:47:59 +02:00
|
|
|
# attr_reader :value
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_integer expression , method
|
|
|
|
int = Virtual::IntegerConstant.new(expression.value)
|
2015-05-06 07:38:29 +02:00
|
|
|
to = Virtual::Return.new(Virtual::Integer , int)
|
2014-09-15 11:08:37 +02:00
|
|
|
method.add_code Virtual::Set.new( to , int)
|
2014-08-20 16:14:52 +02:00
|
|
|
to
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_true expression , method
|
2014-09-15 11:08:37 +02:00
|
|
|
value = Virtual::TrueConstant.new
|
|
|
|
to = Virtual::Return.new(Virtual::Reference , value)
|
|
|
|
method.add_code Virtual::Set.new( to , value )
|
2014-09-14 17:14:57 +02:00
|
|
|
to
|
2014-07-01 14:57:13 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_false expression , method
|
2014-09-15 11:08:37 +02:00
|
|
|
value = Virtual::FalseConstant.new
|
|
|
|
to = Virtual::Return.new(Virtual::Reference , value)
|
|
|
|
method.add_code Virtual::Set.new( to , value )
|
2014-09-14 17:14:57 +02:00
|
|
|
to
|
2014-07-01 14:57:13 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_nil expression , method
|
2014-09-15 11:08:37 +02:00
|
|
|
value = Virtual::NilConstant.new
|
|
|
|
to = Virtual::Return.new(Virtual::Reference , value)
|
|
|
|
method.add_code Virtual::Set.new( to , value )
|
2014-09-14 17:14:57 +02:00
|
|
|
to
|
2014-07-01 14:57:13 +02:00
|
|
|
end
|
|
|
|
|
2015-05-08 13:57:11 +02:00
|
|
|
# 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
|
2015-05-08 13:34:46 +02:00
|
|
|
# otherwise it's a method without args and a send is issued.
|
2015-05-08 13:57:11 +02:00
|
|
|
# whichever way this goes the result is stored in the return slot (as all compiles)
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_name expression , method
|
|
|
|
return Virtual::Self.new( Virtual::Mystery ) if expression.name == :self
|
2015-05-08 13:57:11 +02:00
|
|
|
name = expression.name
|
2015-05-08 13:34:46 +02:00
|
|
|
if method.has_var(expression.name)
|
2015-05-08 13:57:11 +02:00
|
|
|
# either an argument, so it's stored in message
|
|
|
|
if( index = method.has_arg(name))
|
|
|
|
method.add_code MessageGet.new(name , index)
|
|
|
|
else # or a local so it is in the frame
|
|
|
|
method.add_code FrameGet.new(name , index)
|
|
|
|
end
|
2014-07-10 16:14:38 +02:00
|
|
|
else
|
2015-05-08 13:34:46 +02:00
|
|
|
call = Ast::CallSiteExpression.new(expression.name , [] ) #receiver self is implicit
|
|
|
|
Compiler.compile(call, method)
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_module expression , method
|
2014-09-14 20:26:30 +02:00
|
|
|
clazz = Virtual::BootSpace.space.get_or_create_class name
|
2014-06-25 01:47:59 +02:00
|
|
|
raise "uups #{clazz}.#{name}" unless clazz
|
2014-09-15 11:08:37 +02:00
|
|
|
to = Virtual::Return.new(Virtual::Reference , clazz )
|
2014-09-14 17:14:57 +02:00
|
|
|
method.add_code Virtual::Set.new( to , clazz )
|
|
|
|
to
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
|
|
|
|
# attr_reader :string
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_string expression , method
|
|
|
|
value = Virtual::StringConstant.new(expression.string)
|
2014-09-15 11:08:37 +02:00
|
|
|
to = Virtual::Return.new(Virtual::Reference , value)
|
2015-05-04 13:22:22 +02:00
|
|
|
Virtual::BootSpace.space.add_object value
|
2014-09-14 17:14:57 +02:00
|
|
|
method.add_code Virtual::Set.new( to , value )
|
|
|
|
to
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
2014-07-14 20:28:21 +02:00
|
|
|
|
2015-05-04 13:22:22 +02:00
|
|
|
#attr_reader :left, :right
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_assignment expression , method
|
|
|
|
raise "must assign to NameExpression , not #{expression.left}" unless expression.left.instance_of? Ast::NameExpression
|
|
|
|
r = Compiler.compile(expression.right , method )
|
|
|
|
raise "oh noo, nil from where #{expression.right.inspect}" unless r
|
2015-05-06 14:14:47 +02:00
|
|
|
index = method.has_arg(name)
|
|
|
|
if index
|
|
|
|
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::MessageSlot.new(index , r,type , r ))
|
|
|
|
else
|
2015-05-08 13:34:46 +02:00
|
|
|
index = method.ensure_local(expression.left.name)
|
2015-05-06 14:14:47 +02:00
|
|
|
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::FrameSlot.new(index , r.type , r ))
|
|
|
|
end
|
|
|
|
r
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
|
|
|
|
2015-05-08 13:34:46 +02:00
|
|
|
def self.compile_variable expression, method
|
|
|
|
method.add_code Virtual::InstanceGet.new(expression.name)
|
2015-05-06 07:38:29 +02:00
|
|
|
Virtual::Return.new( Virtual::Mystery )
|
2014-07-14 15:19:47 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|