From b6fa8261e69f1d44614e40f628e7102c47844ec9 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 30 Aug 2017 17:21:13 +0300 Subject: [PATCH] first stab at moms if --- lib/mom/instruction.rb | 11 +++++++++++ lib/mom/jump.rb | 14 ++++++++++++++ lib/mom/truth_check.rb | 21 +++++++++++++++++++++ lib/vool/statement.rb | 3 ++- lib/vool/statements/if_statement.rb | 21 ++++++++++++++++++++- lib/vool/vool_compiler.rb | 2 +- test/vool/to_mom/test_if.rb | 27 +++++++++++++++++++++++++++ 7 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 lib/mom/jump.rb create mode 100644 lib/mom/truth_check.rb create mode 100644 test/vool/to_mom/test_if.rb diff --git a/lib/mom/instruction.rb b/lib/mom/instruction.rb index df1cce24..29af863d 100644 --- a/lib/mom/instruction.rb +++ b/lib/mom/instruction.rb @@ -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" diff --git a/lib/mom/jump.rb b/lib/mom/jump.rb new file mode 100644 index 00000000..05094710 --- /dev/null +++ b/lib/mom/jump.rb @@ -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 diff --git a/lib/mom/truth_check.rb b/lib/mom/truth_check.rb new file mode 100644 index 00000000..dd9202b7 --- /dev/null +++ b/lib/mom/truth_check.rb @@ -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 diff --git a/lib/vool/statement.rb b/lib/vool/statement.rb index e8b9a8b7..57d1069b 100644 --- a/lib/vool/statement.rb +++ b/lib/vool/statement.rb @@ -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 diff --git a/lib/vool/statements/if_statement.rb b/lib/vool/statements/if_statement.rb index 2185c5c6..62620057 100644 --- a/lib/vool/statements/if_statement.rb +++ b/lib/vool/statements/if_statement.rb @@ -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) diff --git a/lib/vool/vool_compiler.rb b/lib/vool/vool_compiler.rb index 721fcd3b..23c9870c 100644 --- a/lib/vool/vool_compiler.rb +++ b/lib/vool/vool_compiler.rb @@ -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 diff --git a/test/vool/to_mom/test_if.rb b/test/vool/to_mom/test_if.rb new file mode 100644 index 00000000..033c61d8 --- /dev/null +++ b/test/vool/to_mom/test_if.rb @@ -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