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

@ -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