first stab at moms if

This commit is contained in:
Torsten Ruger 2017-08-30 17:21:13 +03:00
parent ffd7a32ae3
commit b6fa8261e6
7 changed files with 96 additions and 3 deletions

View File

@ -4,8 +4,19 @@ module Mom
class Instruction
end
# basically a label
class Noop
attr_reader :name
def initialize(name)
@name = name
end
end
end
require_relative "simple_call"
require_relative "truth_check"
require_relative "jump"
require_relative "slot_load"
require_relative "return_sequence"

14
lib/mom/jump.rb Normal file
View 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
View 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

View File

@ -1,6 +1,6 @@
# Virtual
# Object Oriented
# Langiage
# Language
#
# 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
@ -12,6 +12,7 @@
#
# This allows us to write compilers or passes of the compiler(s) as functions on the
# classes.
#
module Vool
# Base class for all statements in the tree. Derived classes correspond to known language

View File

@ -2,13 +2,32 @@ module Vool
class IfStatement < Statement
attr_reader :condition , :if_true , :if_false
def initialize( cond , if_true , if_false = [])
def initialize( cond , if_true , if_false = nil)
@condition = cond
@if_true = if_true
@if_false = if_false
simplify_condition
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)
@if_true.collect(arr)
@if_false.collect(arr)

View File

@ -2,7 +2,7 @@ require_relative "ruby_compiler"
module Vool
class VoolCompiler
def self.compile( ruby_source )
statements = RubyCompiler.compile( ruby_source )
statements.create_objects

View 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