more test working
operator wip
This commit is contained in:
parent
77f0a08060
commit
5f628744d6
@ -1,8 +1,5 @@
|
||||
module Bosl
|
||||
Compiler.class_eval do
|
||||
# operators are really function calls
|
||||
|
||||
# call_site - attr_reader :name, :args , :receiver
|
||||
|
||||
def on_call expression
|
||||
name , arguments , receiver = *expression
|
||||
|
@ -6,7 +6,7 @@ module Bosl
|
||||
end
|
||||
|
||||
def on_class expression
|
||||
puts expression.inspect
|
||||
#puts expression.inspect
|
||||
name , derives , expressions = *expression
|
||||
clazz = Parfait::Space.object_space.get_class_by_name! name
|
||||
#puts "Compiling class #{clazz.name.inspect}"
|
||||
|
@ -3,15 +3,13 @@ module Bosl
|
||||
# operator attr_reader :operator, :left, :right
|
||||
def on_operator expression
|
||||
operator , left , right = *expression
|
||||
nil
|
||||
Virtual::Return.new()
|
||||
end
|
||||
|
||||
def on_assign expression
|
||||
puts "assign"
|
||||
puts expression.inspect
|
||||
name , value = *expression
|
||||
name = name.to_a.first
|
||||
v = process(value )
|
||||
v = process(value)
|
||||
index = method.ensure_local( name )
|
||||
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index ) , v )
|
||||
end
|
||||
|
@ -1,25 +1,25 @@
|
||||
module Bosl
|
||||
Compiler.class_eval do
|
||||
|
||||
# while- attr_reader :condition, :body
|
||||
def on_while expression
|
||||
puts expression.inspect
|
||||
condition , expressions = *expression
|
||||
condition = condition.first
|
||||
|
||||
# this is where the while ends and both branches meet
|
||||
merge = method.source.new_block("while merge")
|
||||
# this comes after the current and beofre the merge
|
||||
start = method.source.new_block("while_start" )
|
||||
method.source.current start
|
||||
|
||||
cond = process(expression.condition)
|
||||
cond = process(condition)
|
||||
|
||||
method.source.add_code IsTrueBranch.new(merge)
|
||||
method.source.add_code Virtual::IsTrueBranch.new(merge)
|
||||
|
||||
last = process_all(expressions).last
|
||||
|
||||
last = cond
|
||||
expression.body.each do |part|
|
||||
last = process(part)
|
||||
raise part.inspect if last.nil?
|
||||
end
|
||||
# unconditionally branch to the start
|
||||
method.source.add_code UnconditionalBranch.new(start)
|
||||
method.source.add_code Virtual::UnconditionalBranch.new(start)
|
||||
|
||||
# continue execution / compiling at the merge block
|
||||
method.source.current merge
|
||||
|
@ -6,7 +6,7 @@ module Virtual
|
||||
# from may be a Constant (Object,Integer,String,Class)
|
||||
class Set < Instruction
|
||||
def initialize from , to
|
||||
raise "no to slot #{to}" unless to.is_a? Slot
|
||||
raise "no to slot #{to.inspect}" unless to.is_a? Slot
|
||||
@from = from
|
||||
@to = to
|
||||
end
|
||||
|
@ -8,7 +8,6 @@ module Fragments
|
||||
|
||||
def check
|
||||
expressions = Virtual.machine.boot.compile_main @string_input
|
||||
puts expressions
|
||||
@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}"
|
||||
|
@ -1,9 +1,9 @@
|
||||
|
||||
require_relative "test_foo"
|
||||
require_relative "test_if"
|
||||
require_relative "test_hello"
|
||||
require_relative "test_putint"
|
||||
require_relative "test_functions"
|
||||
require_relative "test_recursive_fibo"
|
||||
require_relative "test_while_fibo"
|
||||
#require_relative "test_string_class"
|
||||
#require_relative "test_hello"
|
||||
#require_relative "test_putint"
|
||||
#require_relative "test_recursive_fibo"
|
||||
#require_relative "test_while_fibo"
|
||||
|
@ -3,14 +3,11 @@ require_relative 'helper'
|
||||
class TestPutint < MiniTest::Test
|
||||
include Fragments
|
||||
|
||||
def ttest_putint
|
||||
def test_putint
|
||||
@string_input = <<HERE
|
||||
42.putint()
|
||||
HERE
|
||||
@should = [0x0,0x40,0x2d,0xe9,0x2,0x30,0xa0,0xe1,0x78,0x20,0x8f,0xe2,0x9,0x20,0x82,0xe2,0xe2,0xff,0xff,0xeb,0x6c,0x20,0x8f,0xe2,0xc,0x30,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x2,0x10,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x80,0xbd,0xe8]
|
||||
@output = " 42 "
|
||||
@target = [:Object , :putint]
|
||||
parse
|
||||
write "putint"
|
||||
@expect = []
|
||||
check
|
||||
end
|
||||
end
|
||||
|
@ -5,19 +5,21 @@ class TestRecursinveFibo < MiniTest::Test
|
||||
|
||||
def test_recursive_fibo
|
||||
@string_input = <<HERE
|
||||
def fibonaccir( n )
|
||||
if( n <= 1 )
|
||||
return n
|
||||
else
|
||||
a = fibonaccir( n - 1 )
|
||||
b = fibonaccir( n - 2 )
|
||||
return a + b
|
||||
end
|
||||
end
|
||||
def fib_print(n)
|
||||
int fib_print(int n)
|
||||
fib = fibonaccir( n )
|
||||
fib.putint()
|
||||
end
|
||||
int fibonaccir( int n )
|
||||
if( n <= 1 )
|
||||
return n
|
||||
else
|
||||
int tmp = n - 1
|
||||
a = fibonaccir( tmp )
|
||||
tmp = n - 2
|
||||
b = fibonaccir( tmp )
|
||||
return a + b
|
||||
end
|
||||
end
|
||||
|
||||
fib_print(10)
|
||||
HERE
|
||||
|
@ -5,18 +5,18 @@ class TestWhileFragment < MiniTest::Test
|
||||
|
||||
def test_while_fibo
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n) # n == r0
|
||||
a = 0 # a == r1
|
||||
b = 1 # b = r2
|
||||
while( n > 1 ) do #BUG comment lines + comments behind function calls
|
||||
tmp = a # r3 <- r1
|
||||
a = b # r1 <- r2
|
||||
b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4
|
||||
n = n - 1 # r0 <- r2 for call, #call ok
|
||||
end #r5 <- r0 - 1 n=n-1 through r5 tmp
|
||||
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 # r0 <- r5
|
||||
end
|
||||
|
||||
fibonaccit( 10 )
|
||||
HERE
|
||||
|
Loading…
Reference in New Issue
Block a user