new function test and scoping fix

This commit is contained in:
Torsten Ruger 2015-10-14 15:17:33 +03:00
parent 1ea34481c1
commit d8a5dc147b
6 changed files with 38 additions and 31 deletions

View File

@ -45,7 +45,8 @@ module Phisol
raise ret.inspect if ret.is_a? Virtual::Instruction
end
@method = nil
Virtual::Return.new(return_type)
# function definition is a statement, does not return any value
return nil
end
end
end

View File

@ -1,14 +1,15 @@
require_relative '../helper'
require 'parslet/convenience'
Phisol::Compiler.class_eval do
def set_main main
@clazz = Virtual.machine.space.get_class_by_name :Object
@method = main
end
end
module CompilerHelper
Phisol::Compiler.class_eval do
def set_main main
@clazz = Virtual.machine.space.get_class_by_name :Object
@method = main
end
end
def set_main compiler
compiler.set_main Virtual.machine.space.get_main
end

View File

@ -1,4 +1,4 @@
require_relative "test_basic"
require_relative "test_call"
require_relative "test_compiler"
require_relative "test_field_access"
require_relative "test_function"

View File

@ -1,7 +1,7 @@
require_relative "compiler_helper"
module Virtual
class TestFields < MiniTest::Test
class TestCall < MiniTest::Test
include CompilerHelper
def setup

View File

@ -1,22 +0,0 @@
require_relative "compiler_helper"
class CompilerTest < MiniTest::Test
include AST::Sexp
def setup
Virtual.machine.boot
end
def check
res = Phisol::Compiler.compile( @statement )
assert res.is_a?(Virtual::Slot) , "compiler must compile to slot, not #{res.inspect}"
end
def test_function_statement
@statement = s(:class, :Foo,
s(:derives, :Object),
s(:statements,
s(:function, :int, s(:name, :foo),
s(:parameters, s(:parameter, :ref, :x)),
s(:statements, s(:int, 5)))))
check
end
end

View File

@ -0,0 +1,27 @@
require_relative "compiler_helper"
module Virtual
class TestFunctions < MiniTest::Test
include CompilerHelper
def setup
Virtual.machine.boot
end
#reset the compiler (other than other tests that need to fake their inside a method)
def set_main compiler
compiler.set_main(nil)
end
def test_puts
@root = :function_definition
@string_input = <<HERE
int puts(ref str)
main()
end
HERE
@output = AST::Node
check
end
end
end