Reworking if statement
Using 2 phase approach Flattening tbd
This commit is contained in:
parent
db1549e0ee
commit
dab4e74659
9
lib/mom/check.rb
Normal file
9
lib/mom/check.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
module Mom
|
||||||
|
|
||||||
|
# A base class for conditions in MOM
|
||||||
|
# Just a marker, no real functionality for now
|
||||||
|
|
||||||
|
class Check < Instruction
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
14
lib/mom/if_statement.rb
Normal file
14
lib/mom/if_statement.rb
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
module Mom
|
||||||
|
class IfStatement < Instruction
|
||||||
|
attr_reader :condition , :if_true , :if_false
|
||||||
|
|
||||||
|
attr_accessor :hoisted
|
||||||
|
|
||||||
|
def initialize( cond , if_true , if_false = nil)
|
||||||
|
@condition = cond
|
||||||
|
@if_true = if_true
|
||||||
|
@if_false = if_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -2,10 +2,17 @@ module Mom
|
|||||||
|
|
||||||
# Base class for MOM instructions
|
# Base class for MOM instructions
|
||||||
class Instruction
|
class Instruction
|
||||||
|
attr :next_instruction
|
||||||
|
|
||||||
|
# flattening will change the structure from a tree to a linked list (and use
|
||||||
|
# next_instruction to do so)
|
||||||
|
def flatten
|
||||||
|
raise "not implemented"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# basically a label
|
# A label with a name
|
||||||
class Noop
|
class Label
|
||||||
attr_reader :name
|
attr_reader :name
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name
|
||||||
@ -16,6 +23,7 @@ module Mom
|
|||||||
end
|
end
|
||||||
|
|
||||||
require_relative "simple_call"
|
require_relative "simple_call"
|
||||||
|
require_relative "if_statement"
|
||||||
require_relative "truth_check"
|
require_relative "truth_check"
|
||||||
require_relative "jump"
|
require_relative "jump"
|
||||||
require_relative "slot_load"
|
require_relative "slot_load"
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require_relative "check"
|
||||||
|
|
||||||
module Mom
|
module Mom
|
||||||
|
|
||||||
# The funny thing about the ruby truth is that is is anything but false or nil
|
# The funny thing about the ruby truth is that is is anything but false or nil
|
||||||
@ -5,17 +7,11 @@ module Mom
|
|||||||
# To implement the normal ruby logic, we check for false or nil and jump
|
# To implement the normal ruby logic, we check for false or nil and jump
|
||||||
# to the false branch. true_block follows implicitly
|
# to the false branch. true_block follows implicitly
|
||||||
#
|
#
|
||||||
# The class only carries the blocks for analysis, and does
|
class TruthCheck < Check
|
||||||
# - NOT imply any order
|
attr_reader :condition
|
||||||
# - 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)
|
def initialize(condition)
|
||||||
@condition , @true_block , @false_block , @merge_block = condition , true_block , false_block , merge_block
|
@condition = condition
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -10,19 +10,19 @@ module Vool
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_mom( method )
|
def to_mom( method )
|
||||||
merge = Mom::Noop.new(:merge)
|
if_true = @if_true.to_mom( method )
|
||||||
if_true = add_jump(@if_true.to_mom( method ) , merge)
|
if_false = @if_false.to_mom( method )
|
||||||
if_false = add_jump(@if_false.to_mom( method ) , merge)
|
condition , hoisted = hoist_condition( method )
|
||||||
cond = hoist_condition( method )
|
check = Mom::IfStatement.new( Mom::TruthCheck.new(condition) , if_true , if_false )
|
||||||
check = Mom::TruthCheck.new( cond.pop , if_true , if_false , merge)
|
check.hoisted = hoisted.to_mom(method) if hoisted
|
||||||
[ *cond , check , if_true , if_false , merge ]
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def hoist_condition( method )
|
def hoist_condition( method )
|
||||||
return [@condition] if @condition.is_a?(Vool::Named)
|
return [@condition] if @condition.is_a?(Vool::Named)
|
||||||
local = method.create_tmp
|
local = method.create_tmp
|
||||||
assign = LocalAssignment.new( local , @condition).to_mom(method)
|
assign = LocalAssignment.new( local , @condition)
|
||||||
[assign , Vool::LocalVariable.new(local)]
|
[Vool::LocalVariable.new(local) , assign]
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect(arr)
|
def collect(arr)
|
||||||
@ -44,11 +44,5 @@ module Vool
|
|||||||
@if_true != nil
|
@if_true != nil
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def add_jump( block , merge)
|
|
||||||
block = [block] unless block.is_a?(Array)
|
|
||||||
block << Mom::Jump.new(merge)
|
|
||||||
block
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user