fixing fragment tests, most done
This commit is contained in:
parent
6b715bbb1b
commit
0a14cffefb
@ -7,7 +7,7 @@ GIT
|
|||||||
|
|
||||||
GIT
|
GIT
|
||||||
remote: https://github.com/ruby-in-ruby/crystal-reader.git
|
remote: https://github.com/ruby-in-ruby/crystal-reader.git
|
||||||
revision: b25d5ab99250965883d3a71163961ea75431778f
|
revision: 06d4ab2fe5af054df6c3a247528b1088e78e24c9
|
||||||
specs:
|
specs:
|
||||||
crystal-reader (0.1.0)
|
crystal-reader (0.1.0)
|
||||||
|
|
||||||
|
@ -12,14 +12,24 @@ module Ast
|
|||||||
class NameExpression < Expression
|
class NameExpression < Expression
|
||||||
# attr_reader :name
|
# attr_reader :name
|
||||||
|
|
||||||
# compiling a variable resolves it.
|
# compiling a variable resolves it. if it wasn't defined, raise an exception
|
||||||
# if it wasn't defined, nli is returned
|
|
||||||
def compile context , into
|
def compile context , into
|
||||||
|
raise "Undefined variable #{name}, defined locals #{context.locals.keys.join('-')}" unless context.locals.has_key?(name)
|
||||||
context.locals[name]
|
context.locals[name]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ModuleName < NameExpression
|
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
|
end
|
||||||
|
|
||||||
class StringExpression < Expression
|
class StringExpression < Expression
|
||||||
|
@ -7,28 +7,20 @@ module Ast
|
|||||||
def compile context , into
|
def compile context , into
|
||||||
params = args.collect{ |a| a.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)
|
function = context.current_class.get_or_create_function(name)
|
||||||
value = Vm::Integer.new(Vm::Function::RECEIVER_REG)
|
value_receiver = 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}"
|
|
||||||
else
|
else
|
||||||
# should be case switch for basic tyes and dynamic dispatch for objects reference
|
value_receiver = receiver.compile(context , into)
|
||||||
value = context.locals[receiver.name]
|
|
||||||
raise "no value" unless value
|
|
||||||
function = context.current_class.get_or_create_function(name)
|
function = context.current_class.get_or_create_function(name)
|
||||||
end
|
end
|
||||||
raise "No such method error #{clazz.to_s}:#{name}" if function == nil
|
# this lot below should go, since the compile should handle all
|
||||||
call = Vm::CallSite.new( name , value , params , function)
|
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 = context.function
|
||||||
current_function.save_locals(context , into) if current_function
|
current_function.save_locals(context , into) if current_function
|
||||||
call.load_args into
|
call.load_args into
|
||||||
|
@ -25,7 +25,9 @@ module Ast
|
|||||||
|
|
||||||
parent_locals = context.locals
|
parent_locals = context.locals
|
||||||
parent_function = context.function
|
parent_function = context.function
|
||||||
|
puts "Locals #{locals.keys.join('-')}, #{locals.object_id}"
|
||||||
context.locals = locals
|
context.locals = locals
|
||||||
|
puts "Locals #{context.locals.keys.join('-')}, #{context.locals.object_id}"
|
||||||
context.function = function
|
context.function = function
|
||||||
|
|
||||||
into = function.body
|
into = function.body
|
||||||
|
@ -7,6 +7,7 @@ module Ast
|
|||||||
#puts "compiled right #{r_val.inspect}"
|
#puts "compiled right #{r_val.inspect}"
|
||||||
if operator == "=" # assignment, value based
|
if operator == "=" # assignment, value based
|
||||||
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
||||||
|
puts context.inspect unless context.locals
|
||||||
l_val = context.locals[left.name]
|
l_val = context.locals[left.name]
|
||||||
if( l_val ) #variable existed, move data there
|
if( l_val ) #variable existed, move data there
|
||||||
l_val = l_val.move( into , r_val)
|
l_val = l_val.move( into , r_val)
|
||||||
@ -18,7 +19,6 @@ module Ast
|
|||||||
end
|
end
|
||||||
|
|
||||||
l_val = left.compile(context , into)
|
l_val = left.compile(context , into)
|
||||||
|
|
||||||
case operator
|
case operator
|
||||||
when ">"
|
when ">"
|
||||||
code = l_val.greater_than into , r_val
|
code = l_val.greater_than into , r_val
|
||||||
|
@ -5,6 +5,7 @@ module Ast
|
|||||||
while_block = into.new_block "#{into.name}_while"
|
while_block = into.new_block "#{into.name}_while"
|
||||||
ret = while_block.new_block "#{into.name}_return"
|
ret = while_block.new_block "#{into.name}_return"
|
||||||
puts "compiling while condition #{condition}"
|
puts "compiling while condition #{condition}"
|
||||||
|
puts "Locals #{context.locals.keys.join('-')}, #{context.locals.object_id}"
|
||||||
cond_val = condition.compile(context , while_block)
|
cond_val = condition.compile(context , while_block)
|
||||||
while_block.b ret , condition_code: cond_val.not_operator
|
while_block.b ret , condition_code: cond_val.not_operator
|
||||||
last = nil
|
last = nil
|
||||||
|
@ -23,8 +23,8 @@ module Core
|
|||||||
|
|
||||||
#TODO this is in the wrong place. It is a function that returns a function object
|
#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
|
# while all other methods add their code into some block. --> kernel
|
||||||
def putstring context , string = Vm::Integer , length = Vm::Integer
|
def putstring context
|
||||||
function = Vm::Function.new(:putstring , [string , length ] , string)
|
function = Vm::Function.new(:putstring , Vm::Integer , [] )
|
||||||
block = function.body
|
block = function.body
|
||||||
# should be another level of indirection, ie write(io,str)
|
# should be another level of indirection, ie write(io,str)
|
||||||
ret = Vm::RegisterMachine.instance.write_stdout(block)
|
ret = Vm::RegisterMachine.instance.write_stdout(block)
|
||||||
|
@ -45,7 +45,7 @@ module Vm
|
|||||||
args.each_with_index do |arg , i|
|
args.each_with_index do |arg , i|
|
||||||
if arg.is_a?(Value)
|
if arg.is_a?(Value)
|
||||||
@args[i] = arg
|
@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
|
else
|
||||||
@args[i] = arg.new(RegisterUse.new(RECEIVER_REG).next_reg(i + 1))
|
@args[i] = arg.new(RegisterUse.new(RECEIVER_REG).next_reg(i + 1))
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
require_relative "code"
|
require_relative "code"
|
||||||
require "support/hash_attributes"
|
|
||||||
module Vm
|
module Vm
|
||||||
|
|
||||||
# Because the idea of what one instruction does, does not always map one to one to real machine
|
# 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
|
def less_than block , right
|
||||||
RegisterMachine.instance.integer_less_than block , self , right
|
RegisterMachine.instance.integer_less_than block , self , right
|
||||||
end
|
end
|
||||||
def equals block , right
|
|
||||||
RegisterMachine.instance.integer_equals block , self , right
|
|
||||||
end
|
|
||||||
def == other
|
def == other
|
||||||
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
|
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
|
||||||
end
|
end
|
||||||
@ -146,6 +143,9 @@ module Vm
|
|||||||
def left_shift block , first , right
|
def left_shift block , first , right
|
||||||
RegisterMachine.instance.integer_left_shift block , self , first , right
|
RegisterMachine.instance.integer_left_shift block , self , first , right
|
||||||
end
|
end
|
||||||
|
def equals block , right
|
||||||
|
RegisterMachine.instance.integer_equals block , self , right
|
||||||
|
end
|
||||||
|
|
||||||
def load block , right
|
def load block , right
|
||||||
if(right.is_a? IntegerConstant)
|
if(right.is_a? IntegerConstant)
|
||||||
|
@ -23,11 +23,10 @@ module Fragments
|
|||||||
# file is a list of expressions, all but the last must be a function
|
# file is a list of expressions, all but the last must be a function
|
||||||
# and the last is wrapped as a main
|
# and the last is wrapped as a main
|
||||||
parts.each_with_index do |part,index|
|
parts.each_with_index do |part,index|
|
||||||
if index == (parts.length - 1)
|
if part.is_a? Ast::FunctionExpression
|
||||||
expr = part.compile( @object_space.context , @object_space.main )
|
|
||||||
else
|
|
||||||
expr = part.compile( @object_space.context , nil )
|
expr = part.compile( @object_space.context , nil )
|
||||||
raise "should be function definition for now, not #{part.inspect}#{expr.inspect}" unless expr.is_a? Vm::Function
|
else
|
||||||
|
expr = part.compile( @object_space.context , @object_space.main )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,7 +8,7 @@ class TestFoo < MiniTest::Test
|
|||||||
def foo(x)
|
def foo(x)
|
||||||
a = 5
|
a = 5
|
||||||
end
|
end
|
||||||
foo( 3 )
|
3.foo( 4 )
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0x40,0x2d,0xe9,0x5,0x20,0xa0,0xe3,0x2,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x5,0x20,0xa0,0xe3,0x2,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
@should = [0x0,0x40,0x2d,0xe9,0x5,0x20,0xa0,0xe3,0x2,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x5,0x20,0xa0,0xe3,0x2,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
||||||
@output = ""
|
@output = ""
|
||||||
|
@ -21,8 +21,12 @@ def minus(a,b)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def t_seven()
|
||||||
tim = times(7,6)
|
tim = times(7,6)
|
||||||
tim.putint()
|
tim.putint()
|
||||||
|
end
|
||||||
|
|
||||||
|
t_seven()
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
@should = [0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
||||||
@output = " 42 "
|
@output = " 42 "
|
||||||
|
@ -5,7 +5,7 @@ class TestHello < MiniTest::Test
|
|||||||
|
|
||||||
def test_hello
|
def test_hello
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
putstring( "Hello Raisa, I am crystal" )
|
"Hello Raisa, I am crystal".putstring()
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
@should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
||||||
@output = "Hello Raisa, I am crystal "
|
@output = "Hello Raisa, I am crystal "
|
||||||
|
@ -5,8 +5,7 @@ class TestPutint < MiniTest::Test
|
|||||||
|
|
||||||
def test_putint
|
def test_putint
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
fourty_two = 42
|
42.putint()
|
||||||
fourty_two.putint()
|
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
@should = [0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
||||||
@output = " 42 "
|
@output = " 42 "
|
||||||
|
@ -14,10 +14,11 @@ def fibonaccit(n) # n == r0
|
|||||||
b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4
|
b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4
|
||||||
n = n - 1 # r0 <- r2 for call, #call ok
|
n = n - 1 # r0 <- r2 for call, #call ok
|
||||||
end #r5 <- r0 - 1 n=n-1 through r5 tmp
|
end #r5 <- r0 - 1 n=n-1 through r5 tmp
|
||||||
|
b.putint()
|
||||||
return b
|
return b
|
||||||
end # r0 <- r5
|
end # r0 <- r5
|
||||||
fibo = fibonaccit( 10 )
|
|
||||||
fibo.putint()
|
fibonaccit( 10 )
|
||||||
HERE
|
HERE
|
||||||
@should = [0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
@should = [0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
||||||
@output = " 55 "
|
@output = " 55 "
|
||||||
|
Loading…
x
Reference in New Issue
Block a user