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