phase 2, add/sub instructions work with +- operator
This commit is contained in:
parent
c3c6928dc8
commit
c7a2ce5af9
@ -43,13 +43,27 @@ module Vm
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
class LogicInstruction < Instruction
|
class LogicInstruction < Instruction
|
||||||
|
# result = left op right
|
||||||
|
#
|
||||||
|
# Logic instruction are your basic operator implementation. But unlike the (normal) code we write
|
||||||
|
# these Instructions must have "place" to write their results. Ie when you write 4 + 5 in ruby
|
||||||
|
# the result is sort of up in the air, but with Instructions the result must be assigned
|
||||||
def initialize result , left , right , options = {}
|
def initialize result , left , right , options = {}
|
||||||
@result = result
|
@result = result
|
||||||
@left = left
|
@left = left
|
||||||
@right = right
|
@right = right
|
||||||
super(options)
|
super(options)
|
||||||
end
|
end
|
||||||
attr_accessor :result
|
# this is used to write code that looks like assignment
|
||||||
|
# So instructions can be created without the result (register) set, and this assigns where
|
||||||
|
# the reuslt after the fact, but usually in the same line
|
||||||
|
# Example (with block b, and variables int,a,b): b.int = a + b
|
||||||
|
# a + b actually creates an add instruction while the b.int= assigns the result to int
|
||||||
|
# b.add( int , a , b) is an alternative (assmbler style) way of writing the same.
|
||||||
|
def assign left
|
||||||
|
@result = left
|
||||||
|
self
|
||||||
|
end
|
||||||
end
|
end
|
||||||
class MathInstruction < Instruction
|
class MathInstruction < Instruction
|
||||||
def initialize first , options = {}
|
def initialize first , options = {}
|
||||||
|
@ -88,7 +88,10 @@ module Vm
|
|||||||
end
|
end
|
||||||
|
|
||||||
def + other
|
def + other
|
||||||
class_for(LogicInstruction).new(nil , self , other , :opcode => :add)
|
class_for(LogicInstruction).new(nil , self , other , opcode: :add)
|
||||||
|
end
|
||||||
|
def - other
|
||||||
|
class_for(LogicInstruction).new(nil , self , other , opcode: :sub , update_status: 1 )
|
||||||
end
|
end
|
||||||
def plus block , first , right
|
def plus block , first , right
|
||||||
CMachine.instance.integer_plus block , self , first , right
|
CMachine.instance.integer_plus block , self , first , right
|
||||||
|
@ -13,15 +13,13 @@ class TestSmallProg < MiniTest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_loop
|
def test_loop
|
||||||
start = Vm::Block.new("start")
|
s = Vm::Block.new("start").scope binding
|
||||||
m = @program.main.scope binding
|
m = @program.main.scope binding
|
||||||
r0 = Vm::Integer.new(0)
|
r0 = Vm::Integer.new(0)
|
||||||
m.r0 = 5 #1
|
m.r0 = 5 #1
|
||||||
m << start
|
m << s
|
||||||
start.instance_eval do
|
s.r0 = r0 - 1 #2
|
||||||
sub :r0, :r0, 1 , :update_status => 1 #2
|
s.bne s #3
|
||||||
bne start #3
|
|
||||||
end
|
|
||||||
@should = [0,176,160,227,5,0,160,227,1,0,80,226,253,255,255,26,1,112,160,227,0,0,0,239]
|
@should = [0,176,160,227,5,0,160,227,1,0,80,226,253,255,255,26,1,112,160,227,0,0,0,239]
|
||||||
write "loop"
|
write "loop"
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user