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