several larger changes came together, bit of cleaning too

- all code must be in functions (which must be in classes).
— changes a fair few tests
— also changes api, as method is not recursive, not passed around
- all state in instance vars in compiler (no accessors)
- class is another such variable, surely more coming
all green again
This commit is contained in:
Torsten Ruger
2015-10-06 00:27:13 +03:00
parent 3d36fd1746
commit f4a4ccb98e
37 changed files with 302 additions and 205 deletions

View File

@ -1,6 +1,6 @@
module CodeChecker
def check
Virtual.machine.boot.compile_main @string_input
Virtual.machine.boot.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert @output , "No output given"
assert_equal @output.length , produced.blocks.length , "Block length"

View File

@ -5,7 +5,7 @@ require 'parslet/convenience'
Bosl::Compiler.class_eval do
def check
Virtual.machine.boot.compile_main @string_input
Virtual.machine.boot.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert_equal @output , produced
Virtual.machine.run_passes

View File

@ -1,4 +1,3 @@
require_relative "test_basic"
require_relative "test_methods"
require_relative "test_hello"
require_relative "test_compiler"

View File

@ -3,7 +3,15 @@ require_relative "compiler_helper"
class TestBasic < MiniTest::Test
def check
expressions = Virtual.machine.boot.compile_main @string_input
input = <<HERE
class Object
int main()
#{@string_input}
end
end
HERE
expressions = Virtual.machine.boot.parse_and_compile input
if( expressions.first.is_a? Virtual::Self )
expressions.first.type.instance_variable_set :@of_class , nil
end

View File

@ -7,11 +7,11 @@ class CompilerTest < MiniTest::Test
Virtual.machine.boot
end
def check
res = Bosl::Compiler.compile( @expression , Virtual.machine.space.get_main )
res = Bosl::Compiler.compile( @expression )
assert res.is_a?(Virtual::Slot) , "compiler must compile to slot, not #{res.inspect}"
end
def ttest_if_expression
#TODO review constant : all expressions return a slot
#TODO review constant : all expressions return a slot
@expression = s(:if,
s(:condition,
s(:int, 0)),
@ -21,9 +21,12 @@ class CompilerTest < MiniTest::Test
check
end
def test_function_expression
@expression = s(:function, :int, s(:name, :foo),
s(:parameters, s(:parameter, :ref, :x)),
s(:expressions, s(:int, 5)))
@expression = s(:class, :Foo,
s(:derives, :Object),
s(:expressions,
s(:function, :int, s(:name, :foo),
s(:parameters, s(:parameter, :ref, :x)),
s(:expressions, s(:int, 5)))))
check
end
end

View File

@ -7,10 +7,12 @@ module Virtual
def test_foo3
@string_input = <<HERE
field int a
int foo(int x)
int b = self.a
return b +x
class Object
field int a
int foo(int x)
int b = self.a
return b +x
end
end
HERE
@output = [ [Virtual::MethodEnter] , [Virtual::MethodReturn] ]

View File

@ -6,7 +6,7 @@ class HelloTest < MiniTest::Test
machine = Virtual.machine.boot
Parfait::Space.object_space.get_class_by_name(:Integer).remove_instance_method :plus
#TODO remove this hack: write proper aliases
expressions = machine.compile_main @string_input
expressions = machine.parse_and_compile @string_input
output_at = "Register::CallImplementation"
#{}"Register::CallImplementation"
machine.run_before output_at
@ -34,7 +34,11 @@ HERE
def test_string_put
@string_input = <<HERE
"Hello again\n".putstring()
class Object
int main()
"Hello again\n".putstring()
end
end
HERE
check
end

View File

@ -19,8 +19,10 @@ HERE
def test_simplest_function
@string_input = <<HERE
int foo(int x)
return x
class Object
int foo(int x)
return x
end
end
HERE
@output = [[MethodEnter] ,[MethodReturn]]
@ -29,8 +31,10 @@ HERE
def test_second_simplest_function
@string_input = <<HERE
ref foo(ref x)
return x
class Object
ref foo(ref x)
return x
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
@ -39,24 +43,28 @@ HERE
def test_puts_string
@string_input = <<HERE
int foo()
puts("Hello")
class Object
int main()
puts("Hello")
end
end
foo()
HERE
@output = [[Virtual::MethodEnter , Virtual::NewMessage, Virtual::Set, Virtual::Set, Virtual::MethodCall],
[Virtual::MethodReturn]]
@output = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
check
end
def ttest_class_function
def test_int_function
@string_input = <<HERE
int self.length(int x)
self.length
class Integer < Object
int times(int x)
self * x
end
end
HERE
@output = nil
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
check
cla = Virtual.machine.space.get_class_by_name :Integer
assert cla.get_instance_method( :times )
end
def ttest_function_ops
@ -106,11 +114,15 @@ HERE
def test_while
@string_input = <<HERE
while(1)
3
class Object
int foo()
while(1)
3
end
end
end
HERE
@output = [[MethodEnter],[Set,IsTrueBranch,Set,UnconditionalBranch],[],[MethodReturn]]
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end

View File

@ -7,7 +7,7 @@ require_relative '../helper'
module Fragments
def check
expressions = Virtual.machine.boot.compile_main @string_input
expressions = Virtual.machine.boot.parse_and_compile @string_input
@expect.each_with_index do | should , i |
exp_i = expressions[i]
assert exp_i.is_a?(Virtual::Slot) , "compiles should return #{should}, not #{exp_i}"

View File

@ -5,11 +5,16 @@ class TestFoo < MiniTest::Test
def test_foo2
@string_input = <<HERE
int foo(int x)
int a = 5
return a
class Object
int foo(int x)
int a = 5
return a
end
int main()
foo( 4 )
end
end
foo( 4 )
HERE
@expect = [ Virtual::Return ]
check

View File

@ -5,30 +5,35 @@ class TestFunctions < MiniTest::Test
def test_functions
@string_input = <<HERE
int minus(int a,int b)
return a - b
end
class Object
int plus(int a, int b)
return a + b
end
int minus(int a,int b)
return a - b
end
int times(int a, int b)
if( b == 0 )
a = 0
else
int m = minus(b, 1)
int t = times(a, m)
a = plus(a,t)
int plus(int a, int b)
return a + b
end
int times(int a, int b)
if( b == 0 )
a = 0
else
int m = minus(b, 1)
int t = times(a, m)
a = plus(a,t)
end
end
int t_seven()
int tim = times(7,6)
tim.putint()
end
int main()
t_seven()
end
end
int t_seven()
int tim = times(7,6)
tim.putint()
end
t_seven()
HERE
@expect = [Virtual::Return ]
check

View File

@ -5,7 +5,11 @@ class TestHello < MiniTest::Test
def test_hello
@string_input = <<HERE
"Hello Raisa, I am salama".putstring()
class Object
int main()
"Hello Raisa, I am salama".putstring()
end
end
HERE
@expect = []
check

View File

@ -5,10 +5,14 @@ class TestIf < MiniTest::Test
def test_if_basic
@string_input = <<HERE
if( n < 12)
3
else
4
class Object
int main()
if( n < 12)
3
else
4
end
end
end
HERE
@expect = [Virtual::Return ]
@ -17,7 +21,11 @@ HERE
def test_return
@string_input = <<HERE
return 5
class Object
int main()
return 5
end
end
HERE
@expect = [Virtual::Return ]
check
@ -26,15 +34,19 @@ HERE
def test_if_function
@string_input = <<HERE
int itest(int n)
if( n < 12)
"then".putstring()
else
"else".putstring()
class Object
int itest(int n)
if( n < 12)
"then".putstring()
else
"else".putstring()
end
end
int main()
itest(20)
end
end
itest(20)
HERE
@expect = [Virtual::Return ]
check

View File

@ -5,7 +5,11 @@ class TestPutint < MiniTest::Test
def test_putint
@string_input = <<HERE
42.putint()
class Object
int main()
42.putint()
end
end
HERE
@expect = []
check

View File

@ -5,24 +5,29 @@ class TestRecursinveFibo < MiniTest::Test
def test_recursive_fibo
@string_input = <<HERE
int fib_print(int n)
int fib = fibonaccir( n )
fib.putint()
end
int fibonaccir( int n )
if( n <= 1 )
return n
else
int tmp
tmp = n - 1
int a = fibonaccir( tmp )
tmp = n - 2
int b = fibonaccir( tmp )
return a + b
class Integer
int fib_print(int n)
int fib = fibonaccir( n )
fib.putint()
end
int fibonaccir( int n )
if( n <= 1 )
return n
else
int tmp
tmp = n - 1
int a = fibonaccir( tmp )
tmp = n - 2
int b = fibonaccir( tmp )
return a + b
end
end
end
class Object
int main()
fib_print(10)
end
end
fib_print(10)
HERE
@expect = [Virtual::Return ]
check

View File

@ -5,20 +5,24 @@ class TestWhileFragment < MiniTest::Test
def test_while_fibo
@string_input = <<HERE
int fibonaccit(int n)
int a = 0
int b = 1
while( n > 1 )
int tmp = a
a = b
b = tmp + b
n = n - 1
end
b.putint()
return b
end
class Object
int fibonaccit(int n)
int a = 0
int b = 1
while( n > 1 )
int tmp = a
a = b
b = tmp + b
n = n - 1
end
b.putint()
return b
end
fibonaccit( 10 )
int main()
fibonaccit( 10 )
end
end
HERE
@expect = [Virtual::Return ]
check

View File

@ -3,14 +3,21 @@ require_relative "helper"
class AddTest < MiniTest::Test
include AST::Sexp
include Ticker
def setup
Virtual.machine.boot
code = s(:call,
s(:name, :plus),
s(:arguments , s(:int , 5)),
s(:receiver, s(:int, 2)))
Bosl::Compiler.compile( code , Virtual.machine.space.get_main )
code = s(:class, :Object,
s(:derives, nil),
s(:expressions,
s(:function, :int,
s(:name, :main),
s(:parameters),
s(:expressions,
s(:call,
s(:name, :plus),
s(:arguments , s(:int , 5)),
s(:receiver, s(:int, 2)))))))
Bosl::Compiler.compile( code )
Virtual.machine.run_before "Register::CallImplementation"
@interpreter = Interpreter::Interpreter.new
@interpreter.start Virtual.machine.init

View File

@ -2,6 +2,7 @@ require_relative "helper"
class AddTest < MiniTest::Test
include Ticker
include AST::Sexp
def test_puti
@string_input = <<HERE
@ -23,10 +24,19 @@ class Integer < Object
return add_string( start )
end
end
class Object
int main()
5.to_string()
end
end
HERE
expressions = Virtual.machine.boot.compile_main @string_input
puts expressions
Virtual.machine.boot
syntax = Parser::Salama.new.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax)
puts parts.inspect
Bosl::Compiler.compile( parts )
# expressions = Virtual.machine.boot.parse_and_compile @string_input
# Bosl::Compiler.compile( expressions , Virtual.machine.space.get_main )
Virtual.machine.run_before "Register::CallImplementation"
@interpreter = Interpreter::Interpreter.new
@ -38,7 +48,7 @@ HERE
"LoadConstant" , "SetSlot" , "RegisterTransfer" , "GetSlot" , "FunctionCall" ,
"SaveReturn" , "GetSlot", "OperatorInstruction" , "RegisterTransfer" , "GetSlot" , "GetSlot" ,
"GetSlot" , "FunctionReturn" ,"RegisterTransfer" , "Syscall", "NilClass"].each_with_index do |name , index|
return if index == 10
return if index == 11
got = ticks(1)
puts got
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"

View File

@ -5,13 +5,20 @@ class TestPuts < MiniTest::Test
include Ticker
def setup
Virtual.machine.boot
code = s(:call,
s(:name, :putstring),
s(:arguments),
s(:receiver,
s(:string, "Hello again")))
code = s(:class, :Object,
s(:derives, nil),
s(:expressions,
s(:function, :int,
s(:name, :main),
s(:parameters),
s(:expressions,
s(:call,
s(:name, :putstring),
s(:arguments),
s(:receiver,
s(:string, "Hello again")))))))
Bosl::Compiler.compile( code , Virtual.machine.space.get_main )
Bosl::Compiler.compile( code )
Virtual.machine.run_before "Register::CallImplementation"
@interpreter = Interpreter::Interpreter.new
@interpreter.start Virtual.machine.init