fix fibo with new syntax. certainly works for operators, but not everything

This commit is contained in:
Torsten Ruger 2014-05-21 12:47:40 +03:00
parent 16a07d5aa2
commit e1f889fd10
5 changed files with 50 additions and 22 deletions

View File

@ -88,7 +88,7 @@ module Arm
io.write_uint32 val io.write_uint32 val
end end
def shift val , by def shift val , by
raise "Not integer #{val}:#{val.class}" unless val.is_a? Fixnum raise "Not integer #{val}:#{val.class} #{inspect}" unless val.is_a? Fixnum
val << by val << by
end end
end end

View File

@ -80,30 +80,31 @@ module Core
# not my hand off course, found in the net from a basic introduction # not my hand off course, found in the net from a basic introduction
def fibo context def fibo context
fibo_function = Vm::Function.new(:fibo , [Vm::Integer , Vm::Integer] , Vm::Integer ) fibo_function = Vm::Function.new(:fibo , [Vm::Integer , Vm::Integer] , Vm::Integer )
result , int = fibo_function.args result = fibo_function.args[0]
i = Vm::Integer.new(2) int = fibo_function.args[1]
count = Vm::Integer.new(2)
loop_block = Vm::Block.new("loop") loop_block = Vm::Block.new("loop")
f1 = Vm::Integer.new(3) f1 = Vm::Integer.new(3)
f2 = Vm::Integer.new(4) f2 = Vm::Integer.new(4)
fibo_function.body.instance_eval do b = fibo_function.body.scope binding
cmp( int , 1)
mov( result, int , condition_code: :le) b.a int == 1
mov( :pc , :lr , condition_code: :le) b.mov( result, int , condition_code: :le)
push [ i , f1 , f2 , :lr] b.mov( :pc , :lr , condition_code: :le)
mov( f1 , 1) b.push [ count , f1 , f2 , :lr]
mov(f2 , 0) b.f1 = 1
sub( i , int , 2) b.f2 = 0
add_code loop_block b.count = int - 2
end
loop_block.instance_eval do b.add_code loop_block
add( f1 , f1 , f2) l = loop_block.scope binding
sub( f2 , f1 , f2)
sub( i , i , 1 , update_status: 1) l.f1 = f1 + f2
bpl( loop_block ) l.f2 = f1 - f2
mov( result , f1 ) l.count = (count - 1).set_update_status
pop [ i , f1 , f2 , :pc] l.bpl( loop_block )
end l.result = f1
l.pop [ count , f1 , f2 , :pc]
fibo_function fibo_function
end end

View File

@ -23,4 +23,15 @@ module Support
end end
end end
end end
end end
class Binding
#these are defined in 2.1 and thus the definitions should be conditional. TODO
def local_variable_defined? sym
vars = eval("local_variables")
vars.include? sym
end
def local_variable_get sym
eval(sym.to_s)
end
end

View File

@ -11,6 +11,10 @@ module Vm
# The first setting the position, the second assembling # The first setting the position, the second assembling
class Code class Code
def class_for clazz
CMachine.instance.class_for(clazz)
end
# set the position to zero, will have to reset later # set the position to zero, will have to reset later
def initialize def initialize
@position = 0 @position = 0

View File

@ -28,6 +28,18 @@ module Vm
def opcode def opcode
@attributes[:opcode] @attributes[:opcode]
end end
def method_missing name , *args , &block
return super unless (args.length <= 1) or block_given?
set , attribute = name.to_s.split("set_")
if set == ""
@attributes[attribute.to_sym] = args[0] || 1
return self
else
return super
end
return @attributes[name.to_sym]
end
end end
class StackInstruction < Instruction class StackInstruction < Instruction