refactor while statement

This commit is contained in:
Torsten Ruger 2016-12-09 14:29:06 +02:00
parent da4a1af074
commit 29fb2a50f5
3 changed files with 34 additions and 21 deletions

View File

@ -5,7 +5,8 @@ module Typed
CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" , CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" ,
"class_statement" , "collections" , "field_def" , "field_access", "class_statement" , "collections" , "field_def" , "field_access",
"function_statement" , "if_statement" , "name_expression" , "function_statement" , "if_statement" , "name_expression" ,
"operator_expression" , "return_statement", "statement_list"] "operator_expression" , "return_statement", "statement_list",
"while_statement"]
CompilerModules.each do |mod| CompilerModules.each do |mod|
require_relative "compiler/" + mod require_relative "compiler/" + mod
@ -190,5 +191,3 @@ module Typed
end end
end end
end end
require_relative "compiler/while_statement"

View File

@ -7,17 +7,17 @@ module Typed
def on_IfStatement( statement ) def on_IfStatement( statement )
# branch_type , condition , if_true , if_false = *statement # branch_type , condition , if_true , if_false = *statement
true_block = compile_condition( statement ) true_block = compile_if_condition( statement )
merge = compile_false( statement ) merge = compile_if_false( statement )
add_code true_block add_code true_block
compile_true(statement) compile_if_true(statement)
add_code merge add_code merge
nil # statements don't return anything nil # statements don't return anything
end end
private private
def compile_condition( statement ) def compile_if_condition( statement )
reset_regs reset_regs
process(statement.condition) process(statement.condition)
branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}" branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}"
@ -25,12 +25,12 @@ module Typed
add_code branch_class.new( statement.condition , true_block ) add_code branch_class.new( statement.condition , true_block )
return true_block return true_block
end end
def compile_true( statement ) def compile_if_true( statement )
reset_regs reset_regs
process(statement.if_true) process(statement.if_true)
end end
def compile_false( statement ) def compile_if_false( statement )
reset_regs reset_regs
process(statement.if_false) if statement.if_false.statements process(statement.if_false) if statement.if_false.statements
merge = Register::Label.new(statement , "if_merge") merge = Register::Label.new(statement , "if_merge")

View File

@ -1,22 +1,17 @@
module Typed module Typed
Compiler.class_eval do module WhileStatement
def on_WhileStatement statement def on_WhileStatement statement
#puts statement.inspect
#branch_type , condition , statements = *statement #branch_type , condition , statements = *statement
condition_label = Register::Label.new(statement.condition , "condition_label") condition_label = compile_while_preamble( statement ) #jump there
# unconditionally branch to the condition upon entering the loop
add_code Register::Branch.new(statement.condition,condition_label)
add_code start = Register::Label.new(statement , "while_start" ) start = compile_while_body( statement )
reset_regs
process(statement.statements)
# This is where the loop starts, though in subsequent iterations it's in the middle # This is where the loop starts, though in subsequent iterations it's in the middle
add_code condition_label add_code condition_label
reset_regs
process(statement.condition) compile_while_condition( statement )
branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}" branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}"
# this is where the while ends and both branches meet # this is where the while ends and both branches meet
@ -24,5 +19,24 @@ module Typed
nil # statements don't return anything nil # statements don't return anything
end end
private
def compile_while_preamble( statement )
condition_label = Register::Label.new(statement.condition , "condition_label")
# unconditionally branch to the condition upon entering the loop
add_code Register::Branch.new(statement.condition , condition_label)
condition_label
end
def compile_while_body( statement )
start = Register::Label.new(statement , "while_start" )
add_code start
reset_regs
process(statement.statements)
start
end
def compile_while_condition( statement )
reset_regs
process(statement.condition)
end
end end
end end