small interpreter refactor
This commit is contained in:
parent
e479b00b29
commit
6bb23eac56
@ -214,17 +214,7 @@ module Register
|
|||||||
ret_value = 0
|
ret_value = 0
|
||||||
case name
|
case name
|
||||||
when :putstring
|
when :putstring
|
||||||
str = get_register( :r1 ) # should test length, ie r2
|
ret_value = handle_putstring
|
||||||
case str
|
|
||||||
when Symbol
|
|
||||||
@stdout += str.to_s
|
|
||||||
ret_value = str.to_s.length
|
|
||||||
when Parfait::Word
|
|
||||||
@stdout += str.to_string
|
|
||||||
ret_value = str.char_length
|
|
||||||
else
|
|
||||||
raise "NO string for putstring #{str.class}:#{str.object_id}" unless str.is_a?(Symbol)
|
|
||||||
end
|
|
||||||
when :exit
|
when :exit
|
||||||
set_instruction(nil)
|
set_instruction(nil)
|
||||||
return false
|
return false
|
||||||
@ -235,31 +225,26 @@ module Register
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_putstring
|
||||||
|
str = get_register( :r1 ) # should test length, ie r2
|
||||||
|
case str
|
||||||
|
when Symbol
|
||||||
|
@stdout += str.to_s
|
||||||
|
return str.to_s.length
|
||||||
|
when Parfait::Word
|
||||||
|
@stdout += str.to_string
|
||||||
|
return str.char_length
|
||||||
|
else
|
||||||
|
raise "NO string for putstring #{str.class}:#{str.object_id}" unless str.is_a?(Symbol)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def execute_OperatorInstruction
|
def execute_OperatorInstruction
|
||||||
left = get_register(@instruction.left) || 0
|
left = get_register(@instruction.left) || 0
|
||||||
rr = @instruction.right
|
rr = @instruction.right
|
||||||
right = get_register(rr) || 0
|
right = get_register(rr) || 0
|
||||||
@flags[:overflow] = false
|
@flags[:overflow] = false
|
||||||
case @instruction.operator.to_s
|
result = handle_operator(left,right)
|
||||||
when "+"
|
|
||||||
result = left + right
|
|
||||||
when "-"
|
|
||||||
result = left - right
|
|
||||||
when ">>"
|
|
||||||
result = left / (2**right)
|
|
||||||
when "<<"
|
|
||||||
result = left * (2**right)
|
|
||||||
when "*"
|
|
||||||
result = left * right
|
|
||||||
when "&"
|
|
||||||
result = left & right
|
|
||||||
when "|"
|
|
||||||
result = left | right
|
|
||||||
when "=="
|
|
||||||
result = (left == right) ? 1 : 0
|
|
||||||
else
|
|
||||||
raise "unimplemented '#{@instruction.operator}' #{@instruction}"
|
|
||||||
end
|
|
||||||
if( result > 2**32 )
|
if( result > 2**32 )
|
||||||
@flags[:overflow] = true
|
@flags[:overflow] = true
|
||||||
result = result % 2**32
|
result = result % 2**32
|
||||||
@ -270,5 +255,28 @@ module Register
|
|||||||
right = set_register(@instruction.left , result)
|
right = set_register(@instruction.left , result)
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_operator(left, right)
|
||||||
|
case @instruction.operator.to_s
|
||||||
|
when "+"
|
||||||
|
return left + right
|
||||||
|
when "-"
|
||||||
|
return left - right
|
||||||
|
when ">>"
|
||||||
|
return left / (2**right)
|
||||||
|
when "<<"
|
||||||
|
return left * (2**right)
|
||||||
|
when "*"
|
||||||
|
return left * right
|
||||||
|
when "&"
|
||||||
|
return left & right
|
||||||
|
when "|"
|
||||||
|
return left | right
|
||||||
|
when "=="
|
||||||
|
return (left == right) ? 1 : 0
|
||||||
|
else
|
||||||
|
raise "unimplemented '#{@instruction.operator}' #{@instruction}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user