2014-06-25 01:47:59 +02:00
|
|
|
# collection of the simple ones, int and strings and such
|
|
|
|
|
|
|
|
module Ast
|
|
|
|
|
|
|
|
class IntegerExpression < Expression
|
|
|
|
# attr_reader :value
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-08-20 16:14:52 +02:00
|
|
|
to = Virtual::Return.new(Integer)
|
|
|
|
method.add_code Virtual::Set.new( to , Virtual::IntegerConstant.new(value))
|
|
|
|
to
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-01 14:57:13 +02:00
|
|
|
class TrueExpression
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-07-01 14:57:13 +02:00
|
|
|
Virtual::TrueValue.new
|
|
|
|
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
|
2014-07-01 14:57:13 +02:00
|
|
|
Virtual::FalseValue.new
|
|
|
|
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
|
2014-07-01 14:57:13 +02:00
|
|
|
Virtual::NilValue.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-25 01:47:59 +02:00
|
|
|
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
|
2014-08-20 16:14:52 +02:00
|
|
|
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
|
2014-08-20 16:14:52 +02:00
|
|
|
raise "Unimplemented"
|
2014-07-25 09:49:34 +02:00
|
|
|
message.compile_send( method , name , Virtual::Self.new( Virtual::Mystery.new ) )
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ModuleName < NameExpression
|
|
|
|
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-07-13 13:12:43 +02:00
|
|
|
clazz = ::Virtual::Object.space.get_or_create_class name
|
2014-06-25 01:47:59 +02:00
|
|
|
raise "uups #{clazz}.#{name}" unless clazz
|
|
|
|
#class qualifier, means call from metaclass
|
2014-07-14 13:06:09 +02:00
|
|
|
#clazz = clazz.meta_class
|
2014-06-25 01:47:59 +02:00
|
|
|
clazz
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class StringExpression < Expression
|
|
|
|
# attr_reader :string
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-06-26 16:52:15 +02:00
|
|
|
value = Virtual::StringConstant.new(string)
|
2014-07-12 20:59:17 +02:00
|
|
|
::Virtual::Object.space.add_object value
|
2014-06-25 01:47:59 +02:00
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2014-07-14 15:19:47 +02:00
|
|
|
class AssignmentExpression < Expression
|
2014-07-14 20:28:21 +02:00
|
|
|
#attr_reader :left, :right
|
|
|
|
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-07-14 20:28:21 +02:00
|
|
|
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 )
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
2014-07-16 21:36:24 +02:00
|
|
|
def old_scratch
|
|
|
|
if operator == "=" # assignment, value based
|
|
|
|
if(left.is_a? VariableExpression)
|
|
|
|
left.make_setter
|
|
|
|
l_val = left.compile(context)
|
|
|
|
elsif left.is_a?(NameExpression)
|
|
|
|
puts context.inspect unless context.locals
|
|
|
|
l_val = context.locals[left.name]
|
|
|
|
if( l_val ) #variable existed, move data there
|
|
|
|
l_val = l_val.move( into , r_val)
|
|
|
|
else
|
|
|
|
l_val = context.function.new_local.move( into , r_val )
|
|
|
|
end
|
|
|
|
context.locals[left.name] = l_val
|
|
|
|
else
|
|
|
|
raise "Can only assign variables, not #{left}"
|
|
|
|
end
|
|
|
|
return l_val
|
|
|
|
end
|
|
|
|
end
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
class VariableExpression < NameExpression
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-08-13 10:59:51 +02:00
|
|
|
method.add_code Virtual::ObjectGet.new(name)
|
2014-07-15 09:35:29 +02:00
|
|
|
Virtual::Return.new( Virtual::Mystery.new )
|
2014-07-14 15:19:47 +02:00
|
|
|
end
|
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|