fix and test assign

also fix field def and
externalized assign
This commit is contained in:
Torsten Ruger 2015-10-15 13:08:53 +03:00
parent 0179638f0e
commit 6f856315d7
6 changed files with 127 additions and 28 deletions

View File

@ -56,6 +56,7 @@ module Phisol
end
require_relative "ast_helper"
require_relative "compiler/assignment"
require_relative "compiler/basic_values"
require_relative "compiler/call_site"
require_relative "compiler/class_field"

View File

@ -0,0 +1,31 @@
module Phisol
Compiler.class_eval do
def on_assignment statement
reset_regs # statements reset registers, ie have all at their disposal
puts statement.inspect
name , value = *statement
name = name.to_a.first
v = process(value)
raise "Not register #{v}" unless v.is_a?(Register::RegisterValue)
code = nil
if( index = @method.has_arg(name))
# TODO, check type @method.arguments[index].type
code = Register.set_slot(statement , v , :message , index )
else # or a local so it is in the frame
index = @method.has_local( name )
if(index)
# TODO, check type @method.locals[index].type
code = Register.set_slot(statement , v , :frame , index )
end
end
if( code )
puts "addin code #{code}"
@method.source.add_code code
else
raise "must define variable #{name} before using it in #{@method.inspect}"
end
end
end
end

View File

@ -1,16 +1,14 @@
module Phisol
Compiler.class_eval do
include AST::Sexp
def on_field_def statement
#puts statement.inspect
reset_regs # field_def is a statement, no return and all regs
puts statement.inspect
type , name , value = *statement
index = @method.ensure_local( name , type )
if value
value = process( value )
end
# field_def is a statement, no return
@method.ensure_local( name, type ) unless( @method.has_arg(name))
# if there is a value assigned, process it as am assignemnt statement (kind of call on_assign)
process( s(:assignment , s(:name , name) , value ) ) if value
return nil
end
end

View File

@ -14,24 +14,5 @@ module Phisol
@method.source.add_code Register::OperatorInstruction.new(statement,operator,left_reg,right_reg)
return left_reg # though this has wrong value attached
end
def on_assignment statement
puts statement.inspect
name , value = *statement
name = name.to_a.first
v = process(value)
index = @method.has_local( name )
if(index)
@method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index, :int ) , v )
else
index = @method.has_arg( name )
if(index)
@method.source.add_code Virtual::Set.new(Virtual::ArgSlot.new(index , :int ) , v )
else
raise "must define variable #{name} before using it in #{@method.inspect}"
end
end
end
end
end

View File

@ -4,7 +4,8 @@ require_relative '../../helper'
module Statements
def check
machine = Virtual.machine.boot
machine = Virtual.machine
machine.boot unless machine.booted
machine.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert @expect , "No output given"

View File

@ -0,0 +1,87 @@
require_relative 'helper'
module Register
class TestAssignStatement < MiniTest::Test
include Statements
def setup
Virtual.machine.boot
end
def test_assign_arg
Virtual.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
@string_input = <<HERE
class Object
int main(int bar)
bar = 5
end
end
HERE
@expect = [[Virtual::MethodEnter,LoadConstant,SetSlot] , [Virtual::MethodReturn]]
check
end
def test_assign_int
@string_input = <<HERE
class Object
int main()
int r = 5
end
end
HERE
@expect = [[Virtual::MethodEnter,LoadConstant,SetSlot] , [Virtual::MethodReturn]]
check
end
def test_assign_op
@string_input = <<HERE
class Object
int main()
int n = 10 + 1
end
end
HERE
@expect = [[Virtual::MethodEnter,LoadConstant,LoadConstant,
OperatorInstruction,SetSlot],[Virtual::MethodReturn]]
check
end
def test_assign_local
@string_input = <<HERE
class Object
int main()
int runner
runner = 5
end
end
HERE
@expect = [[Virtual::MethodEnter,LoadConstant,SetSlot] , [Virtual::MethodReturn]]
check
end
def test_assign_local_assign
@string_input = <<HERE
class Object
int main()
int runner = 5
end
end
HERE
@expect = [[Virtual::MethodEnter,LoadConstant, SetSlot] , [Virtual::MethodReturn]]
check
end
def test_assign_call
@string_input = <<HERE
class Object
int main()
int r = main()
end
end
HERE
@expect = [[Virtual::MethodEnter,GetSlot,SetSlot, LoadConstant,
SetSlot,Virtual::MethodCall,GetSlot,SetSlot] , [Virtual::MethodReturn]]
check
end
end
end