2015-09-19 15:28:41 +02:00
|
|
|
module Bosl
|
2015-05-08 14:10:30 +02:00
|
|
|
# collection of the simple ones, int and strings and such
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-05-08 14:10:30 +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
|
|
|
|
|
|
|
|
# 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-09-19 17:56:18 +02:00
|
|
|
def on_int expression
|
2015-09-19 15:28:41 +02:00
|
|
|
int = *expression
|
|
|
|
to = Virtual::Return.new(Integer , int)
|
|
|
|
method.source.add_code Virtual::Set.new( int , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_true expression
|
2015-09-19 15:28:41 +02:00
|
|
|
to = Virtual::Return.new(Reference , true )
|
|
|
|
method.source.add_code Virtual::Set.new( true , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_false expression
|
2015-09-19 15:28:41 +02:00
|
|
|
to = Virtual::Return.new(Reference , false)
|
|
|
|
method.source.add_code Virtual::Set.new( false , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_nil expression
|
2015-09-19 15:28:41 +02:00
|
|
|
to = Virtual::Return.new(Reference , nil)
|
|
|
|
method.source.add_code Virtual::Set.new( nil , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_modulename expression
|
2015-07-19 12:31:13 +02:00
|
|
|
clazz = Parfait::Space.object_space.get_class_by_name expression.name
|
2015-08-17 01:37:07 +02:00
|
|
|
raise "compile_modulename #{clazz}.#{name}" unless clazz
|
2015-09-19 15:28:41 +02:00
|
|
|
to = Virtual::Return.new(Reference , clazz )
|
|
|
|
method.source.add_code Virtual::Set.new( clazz , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
# attr_reader :string
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_string expression
|
2015-06-11 07:04:14 +02:00
|
|
|
# Clearly a TODO here to implement strings rather than reusing symbols
|
2015-09-19 16:57:44 +02:00
|
|
|
value = expression.first.to_sym
|
|
|
|
to = Virtual::Return.new(Virtual::Reference , value)
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.constants << value
|
2015-09-19 15:28:41 +02:00
|
|
|
method.source.add_code Virtual::Set.new( value , to )
|
2015-05-08 14:10:30 +02:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
|
|
|
#attr_reader :left, :right
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_assignment expression
|
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-09-19 17:56:18 +02:00
|
|
|
r = process(expression.right )
|
2015-05-08 14:10:30 +02:00
|
|
|
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-09-19 15:28:41 +02:00
|
|
|
method.source.add_code Virtual::Set.new(ArgSlot.new(index , r.type , r ) , Virtual::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-09-19 15:28:41 +02:00
|
|
|
method.source.add_code Virtual::Set.new(FrameSlot.new(index , r.type , r ) , Virtual::Return.new)
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2015-09-19 17:56:18 +02:00
|
|
|
def on_variable expression
|
2015-07-03 19:13:03 +02:00
|
|
|
method.source.add_code InstanceGet.new(expression.name)
|
2015-09-19 15:28:41 +02:00
|
|
|
Virtual::Return.new( Unknown )
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|