fixing fragment tests, most done
This commit is contained in:
@ -12,14 +12,24 @@ module Ast
|
||||
class NameExpression < Expression
|
||||
# attr_reader :name
|
||||
|
||||
# compiling a variable resolves it.
|
||||
# if it wasn't defined, nli is returned
|
||||
# compiling a variable resolves it. if it wasn't defined, raise an exception
|
||||
def compile context , into
|
||||
raise "Undefined variable #{name}, defined locals #{context.locals.keys.join('-')}" unless context.locals.has_key?(name)
|
||||
context.locals[name]
|
||||
end
|
||||
end
|
||||
|
||||
class ModuleName < NameExpression
|
||||
|
||||
def compile context , into
|
||||
clazz = context.object_space.get_or_create_class name
|
||||
raise "uups #{clazz}.#{name}" unless clazz
|
||||
#class qualifier, means call from metaclass
|
||||
clazz = clazz.meta_class
|
||||
value = clazz
|
||||
puts "CLAZZ #{value}"
|
||||
function = clazz.get_or_create_function(name)
|
||||
end
|
||||
end
|
||||
|
||||
class StringExpression < Expression
|
||||
|
@ -7,28 +7,20 @@ module Ast
|
||||
def compile context , into
|
||||
params = args.collect{ |a| a.compile(context, into) }
|
||||
|
||||
if receiver.name == :self
|
||||
if receiver.is_a?(NameExpression) and (receiver.name == :self)
|
||||
function = context.current_class.get_or_create_function(name)
|
||||
value = Vm::Integer.new(Vm::Function::RECEIVER_REG)
|
||||
elsif receiver.is_a? ModuleName
|
||||
c_name = receiver.name
|
||||
clazz = context.object_space.get_or_create_class c_name
|
||||
raise "uups #{clazz}.#{c_name}" unless clazz
|
||||
#class qualifier, means call from metaclass
|
||||
clazz = clazz.meta_class
|
||||
value = clazz
|
||||
puts "CLAZZ #{value}"
|
||||
function = clazz.get_or_create_function(name)
|
||||
elsif receiver.is_a? VariableExpression
|
||||
raise "not implemented instance var:#{receiver}"
|
||||
value_receiver = Vm::Integer.new(Vm::Function::RECEIVER_REG)
|
||||
else
|
||||
# should be case switch for basic tyes and dynamic dispatch for objects reference
|
||||
value = context.locals[receiver.name]
|
||||
raise "no value" unless value
|
||||
value_receiver = receiver.compile(context , into)
|
||||
function = context.current_class.get_or_create_function(name)
|
||||
end
|
||||
raise "No such method error #{clazz.to_s}:#{name}" if function == nil
|
||||
call = Vm::CallSite.new( name , value , params , function)
|
||||
# this lot below should go, since the compile should handle all
|
||||
if receiver.is_a? VariableExpression
|
||||
raise "not implemented instance var:#{receiver}"
|
||||
end
|
||||
raise "No such method error #{3.to_s}:#{name}" if (function.nil?)
|
||||
raise "No receiver error #{inspect}:#{value_receiver}:#{name}" if (value_receiver.nil?)
|
||||
call = Vm::CallSite.new( name , value_receiver , params , function)
|
||||
current_function = context.function
|
||||
current_function.save_locals(context , into) if current_function
|
||||
call.load_args into
|
||||
|
@ -25,7 +25,9 @@ module Ast
|
||||
|
||||
parent_locals = context.locals
|
||||
parent_function = context.function
|
||||
puts "Locals #{locals.keys.join('-')}, #{locals.object_id}"
|
||||
context.locals = locals
|
||||
puts "Locals #{context.locals.keys.join('-')}, #{context.locals.object_id}"
|
||||
context.function = function
|
||||
|
||||
into = function.body
|
||||
|
@ -7,6 +7,7 @@ module Ast
|
||||
#puts "compiled right #{r_val.inspect}"
|
||||
if operator == "=" # assignment, value based
|
||||
raise "Can only assign variables, not #{left}" unless 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)
|
||||
@ -18,7 +19,6 @@ module Ast
|
||||
end
|
||||
|
||||
l_val = left.compile(context , into)
|
||||
|
||||
case operator
|
||||
when ">"
|
||||
code = l_val.greater_than into , r_val
|
||||
|
@ -5,6 +5,7 @@ module Ast
|
||||
while_block = into.new_block "#{into.name}_while"
|
||||
ret = while_block.new_block "#{into.name}_return"
|
||||
puts "compiling while condition #{condition}"
|
||||
puts "Locals #{context.locals.keys.join('-')}, #{context.locals.object_id}"
|
||||
cond_val = condition.compile(context , while_block)
|
||||
while_block.b ret , condition_code: cond_val.not_operator
|
||||
last = nil
|
||||
|
@ -23,8 +23,8 @@ module Core
|
||||
|
||||
#TODO this is in the wrong place. It is a function that returns a function object
|
||||
# while all other methods add their code into some block. --> kernel
|
||||
def putstring context , string = Vm::Integer , length = Vm::Integer
|
||||
function = Vm::Function.new(:putstring , [string , length ] , string)
|
||||
def putstring context
|
||||
function = Vm::Function.new(:putstring , Vm::Integer , [] )
|
||||
block = function.body
|
||||
# should be another level of indirection, ie write(io,str)
|
||||
ret = Vm::RegisterMachine.instance.write_stdout(block)
|
||||
|
@ -45,7 +45,7 @@ module Vm
|
||||
args.each_with_index do |arg , i|
|
||||
if arg.is_a?(Value)
|
||||
@args[i] = arg
|
||||
raise "arg in non std register #{arg.inspect}" unless RECEIVER_REG == arg.used_register.next_reg(i - 1)
|
||||
raise "arg #{i}in non std register #{arg.inspect}" unless RECEIVER_REG == arg.used_register.next_reg(-1-i)
|
||||
else
|
||||
@args[i] = arg.new(RegisterUse.new(RECEIVER_REG).next_reg(i + 1))
|
||||
end
|
||||
|
@ -1,5 +1,4 @@
|
||||
require_relative "code"
|
||||
require "support/hash_attributes"
|
||||
module Vm
|
||||
|
||||
# Because the idea of what one instruction does, does not always map one to one to real machine
|
||||
|
@ -122,9 +122,6 @@ module Vm
|
||||
def less_than block , right
|
||||
RegisterMachine.instance.integer_less_than block , self , right
|
||||
end
|
||||
def equals block , right
|
||||
RegisterMachine.instance.integer_equals block , self , right
|
||||
end
|
||||
def == other
|
||||
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
|
||||
end
|
||||
@ -146,6 +143,9 @@ module Vm
|
||||
def left_shift block , first , right
|
||||
RegisterMachine.instance.integer_left_shift block , self , first , right
|
||||
end
|
||||
def equals block , right
|
||||
RegisterMachine.instance.integer_equals block , self , right
|
||||
end
|
||||
|
||||
def load block , right
|
||||
if(right.is_a? IntegerConstant)
|
||||
|
Reference in New Issue
Block a user