rubyx/lib/compiler/basic_expressions.rb

94 lines
3.6 KiB
Ruby
Raw Normal View History

# 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
2015-04-10 13:02:28 +02:00
# 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
2015-05-06 14:14:47 +02:00
def self.compile_integer expession , method
2015-05-05 13:04:37 +02:00
int = Virtual::IntegerConstant.new(expession.value)
2015-05-06 07:38:29 +02:00
to = Virtual::Return.new(Virtual::Integer , int)
method.add_code Virtual::Set.new( to , int)
to
end
2015-05-06 14:14:47 +02:00
def self.compile_true expession , method
value = Virtual::TrueConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
2014-07-01 14:57:13 +02:00
end
2015-05-06 14:14:47 +02:00
def self.compile_false expession , method
value = Virtual::FalseConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
2014-07-01 14:57:13 +02:00
end
2015-05-06 14:14:47 +02:00
def self.compile_nil expession , method
value = Virtual::NilConstant.new
to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value )
to
2014-07-01 14:57:13 +02:00
end
# 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-05 13:04:37 +02:00
# otherwise it's a method without args and a send is usued.
2014-07-10 16:14:38 +02:00
# this makes the namespace static, ie when eval and co are implemented method needs recompilation
2015-05-06 14:14:47 +02:00
def self.compile_name expession , method
return Virtual::Self.new( Virtual::Mystery ) if expession.name == :self
if method.has_var(expession.name)
message.compile_get(method , expession.name )
2014-07-10 16:14:38 +02:00
else
2015-05-05 13:04:37 +02:00
raise "TODO unimplemented branch #{expession.class}(#{expession})"
message.compile_send( method , expession.name , Virtual::Self.new( Virtual::Mystery ) )
2014-07-10 16:14:38 +02:00
end
end
2015-05-06 14:14:47 +02:00
def self.compile_module expession , method
clazz = Virtual::BootSpace.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
to = Virtual::Return.new(Virtual::Reference , clazz )
method.add_code Virtual::Set.new( to , clazz )
to
end
# attr_reader :string
2015-05-06 14:14:47 +02:00
def self.compile_string expession , method
value = Virtual::StringConstant.new(expession.string)
to = Virtual::Return.new(Virtual::Reference , value)
Virtual::BootSpace.space.add_object value
method.add_code Virtual::Set.new( to , value )
to
end
#attr_reader :left, :right
2015-05-06 14:14:47 +02:00
def self.compile_assignment expession , method
2015-05-05 14:11:09 +02:00
raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? Ast::NameExpression
2015-05-06 14:14:47 +02:00
r = Compiler.compile(expession.right , method )
raise "oh noo, nil from where #{expession.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
index = method.ensure_local(expession.left.name)
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::FrameSlot.new(index , r.type , r ))
end
r
end
2015-05-06 14:14:47 +02:00
def self.compile_variable expession, method
method.add_code Virtual::InstanceGet.new(expession.name)
2015-05-06 07:38:29 +02:00
Virtual::Return.new( Virtual::Mystery )
2014-07-14 15:19:47 +02:00
end
end