first stab at moms if
This commit is contained in:
parent
ffd7a32ae3
commit
b6fa8261e6
@ -4,8 +4,19 @@ module Mom
|
|||||||
class Instruction
|
class Instruction
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# basically a label
|
||||||
|
class Noop
|
||||||
|
attr_reader :name
|
||||||
|
def initialize(name)
|
||||||
|
@name = name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
require_relative "simple_call"
|
require_relative "simple_call"
|
||||||
|
require_relative "truth_check"
|
||||||
|
require_relative "jump"
|
||||||
require_relative "slot_load"
|
require_relative "slot_load"
|
||||||
require_relative "return_sequence"
|
require_relative "return_sequence"
|
||||||
|
14
lib/mom/jump.rb
Normal file
14
lib/mom/jump.rb
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
module Mom
|
||||||
|
|
||||||
|
# unconditional jump to the instruction given as target
|
||||||
|
#
|
||||||
|
class Jump < Instruction
|
||||||
|
attr_reader :target
|
||||||
|
|
||||||
|
def initialize(target)
|
||||||
|
@target = target
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
21
lib/mom/truth_check.rb
Normal file
21
lib/mom/truth_check.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
module Mom
|
||||||
|
|
||||||
|
# The funny thing about the ruby truth is that is is anything but false or nil
|
||||||
|
#
|
||||||
|
# To implement the normal ruby logic, we check for false or nil and jump
|
||||||
|
# to the false branch. true_block follows implicitly
|
||||||
|
#
|
||||||
|
# The class only carries the blocks for analysis, and does
|
||||||
|
# - NOT imply any order
|
||||||
|
# - will not "handle" the blocks in subsequent processing.
|
||||||
|
#
|
||||||
|
class TruthCheck < Instruction
|
||||||
|
attr_reader :condition , :true_block , :false_block , :merge_block
|
||||||
|
|
||||||
|
def initialize(condition , true_block , false_block , merge_block)
|
||||||
|
@condition , @true_block , @false_block , @merge_block = condition , true_block , false_block , merge_block
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
@ -1,6 +1,6 @@
|
|||||||
# Virtual
|
# Virtual
|
||||||
# Object Oriented
|
# Object Oriented
|
||||||
# Langiage
|
# Language
|
||||||
#
|
#
|
||||||
# VOOL is the abstraction of ruby, ruby minus some of the fluff
|
# VOOL is the abstraction of ruby, ruby minus some of the fluff
|
||||||
# fluff is generally what makes ruby nice to use, like 3 ways to achieve the same thing
|
# fluff is generally what makes ruby nice to use, like 3 ways to achieve the same thing
|
||||||
@ -12,6 +12,7 @@
|
|||||||
#
|
#
|
||||||
# This allows us to write compilers or passes of the compiler(s) as functions on the
|
# This allows us to write compilers or passes of the compiler(s) as functions on the
|
||||||
# classes.
|
# classes.
|
||||||
|
#
|
||||||
module Vool
|
module Vool
|
||||||
|
|
||||||
# Base class for all statements in the tree. Derived classes correspond to known language
|
# Base class for all statements in the tree. Derived classes correspond to known language
|
||||||
|
@ -2,13 +2,32 @@ module Vool
|
|||||||
class IfStatement < Statement
|
class IfStatement < Statement
|
||||||
attr_reader :condition , :if_true , :if_false
|
attr_reader :condition , :if_true , :if_false
|
||||||
|
|
||||||
def initialize( cond , if_true , if_false = [])
|
def initialize( cond , if_true , if_false = nil)
|
||||||
@condition = cond
|
@condition = cond
|
||||||
@if_true = if_true
|
@if_true = if_true
|
||||||
@if_false = if_false
|
@if_false = if_false
|
||||||
simplify_condition
|
simplify_condition
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_mom( method )
|
||||||
|
if_true = @if_true.to_mom( method )
|
||||||
|
if_false = @if_false.to_mom( method )
|
||||||
|
make_condition( if_true , if_false )
|
||||||
|
end
|
||||||
|
|
||||||
|
# conditions in ruby are almost always method sends (as even comparisons are)
|
||||||
|
# currently we just deal with straight up values which get tested
|
||||||
|
# for the funny ruby logic (everything but false and nil is true)
|
||||||
|
def make_condition( if_true , if_false )
|
||||||
|
merge = Mom::Noop.new(:merge)
|
||||||
|
if_true = [if_true] unless if_true.is_a?(Array)
|
||||||
|
if_true << Mom::Jump.new(merge)
|
||||||
|
if_false = [if_false] unless if_false.is_a?(Array)
|
||||||
|
if_false << Mom::Jump.new(merge)
|
||||||
|
check = Mom::TruthCheck.new( @condition , if_true , if_false , merge)
|
||||||
|
[ check , if_true , if_false , merge ]
|
||||||
|
end
|
||||||
|
|
||||||
def collect(arr)
|
def collect(arr)
|
||||||
@if_true.collect(arr)
|
@if_true.collect(arr)
|
||||||
@if_false.collect(arr)
|
@if_false.collect(arr)
|
||||||
|
27
test/vool/to_mom/test_if.rb
Normal file
27
test/vool/to_mom/test_if.rb
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
|
||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module Vool
|
||||||
|
class TestIfMom < MiniTest::Test
|
||||||
|
include MomCompile
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Risc.machine.boot
|
||||||
|
@stats = compile_first_method( "if(@a) ; 5.mod4 ; else; 4.mod4 ; end")
|
||||||
|
@first = @stats.first
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_if_compiles_as_array
|
||||||
|
assert_equal Array , @first.class , @stats
|
||||||
|
end
|
||||||
|
def test_condition_compiles_to_check
|
||||||
|
assert_equal Mom::TruthCheck , @first.first.class , @stats
|
||||||
|
end
|
||||||
|
def test_condition_is_instance
|
||||||
|
assert_equal Vool::InstanceVariable , @first.first.condition.class , @stats
|
||||||
|
end
|
||||||
|
def test_true_block_is_second
|
||||||
|
assert_equal @first[1] , @first.first.true_block , @stats
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user