rubyx/lib/ast/basic_expressions.rb

101 lines
3.2 KiB
Ruby
Raw Normal View History

# collection of the simple ones, int and strings and such
module Ast
# 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 varaible before using it
# But in the future (in the one that holds great things) we optimize those unneccesay moves away
class IntegerExpression < Expression
# attr_reader :value
2014-07-25 09:49:34 +02:00
def compile method , message
to = Virtual::NewReturn.new(Virtual::Integer)
method.add_code Virtual::Set.new( to , Virtual::IntegerConstant.new(value))
to
end
end
2014-07-01 14:57:13 +02:00
class TrueExpression
2014-07-25 09:49:34 +02:00
def compile method , message
to = Virtual::Return.new(Virtual::Reference)
method.add_code Virtual::Set.new( to , Virtual::TrueConstant.new )
to
2014-07-01 14:57:13 +02:00
end
end
2014-07-10 16:14:38 +02:00
2014-07-01 14:57:13 +02:00
class FalseExpression
2014-07-25 09:49:34 +02:00
def compile method , message
to = Virtual::Return.new(Virtual::Reference)
method.add_code Virtual::Set.new( to , Virtual::FalseConstant.new )
to
2014-07-01 14:57:13 +02:00
end
end
2014-07-10 16:14:38 +02:00
2014-07-01 14:57:13 +02:00
class NilExpression
2014-07-25 09:49:34 +02:00
def compile method , message
to = Virtual::Return.new(Virtual::Reference)
method.add_code Virtual::Set.new( to , Virtual::NilConstant.new )
to
2014-07-01 14:57:13 +02:00
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
2014-07-25 09:49:34 +02:00
def compile method , message
return Virtual::Self.new( Virtual::Mystery ) if name == :self
2014-07-10 16:14:38 +02:00
if method.has_var(name)
2014-07-25 09:49:34 +02:00
message.compile_get(method , name )
2014-07-10 16:14:38 +02:00
else
raise "Unimplemented #{self}"
message.compile_send( method , name , Virtual::Self.new( Virtual::Mystery ) )
2014-07-10 16:14:38 +02:00
end
end
end
class ModuleName < NameExpression
2014-07-25 09:49:34 +02:00
def compile method , message
to = Virtual::Return.new(Virtual::Reference)
2014-08-24 20:32:21 +02:00
clazz = ::Virtual::BootSpace.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
method.add_code Virtual::Set.new( to , clazz )
to
end
end
class StringExpression < Expression
# attr_reader :string
2014-07-25 09:49:34 +02:00
def compile method , message
to = Virtual::Return.new(Virtual::Reference)
2014-06-26 16:52:15 +02:00
value = Virtual::StringConstant.new(string)
Virtual::BootSpace.space.add_object value
method.add_code Virtual::Set.new( to , value )
to
end
end
2014-07-14 15:19:47 +02:00
class AssignmentExpression < Expression
#attr_reader :left, :right
2014-07-25 09:49:34 +02:00
def compile method , message
raise "must assign to NameExpression , not #{left}" unless left.instance_of? NameExpression
2014-07-25 09:49:34 +02:00
r = right.compile(method,message)
2014-07-27 09:09:31 +02:00
raise "oh noo, nil from where #{right.inspect}" unless r
2014-07-25 09:49:34 +02:00
message.compile_set( method , left.name , r )
end
end
class VariableExpression < NameExpression
2014-07-25 09:49:34 +02:00
def compile method , message
method.add_code Virtual::InstanceGet.new(name)
Virtual::NewReturn.new( Virtual::Mystery )
2014-07-14 15:19:47 +02:00
end
end
end