more expressions and if test

This commit is contained in:
Torsten Ruger 2015-09-19 17:57:44 +03:00
parent 9bacb07f1c
commit 3a885a8e46
5 changed files with 17 additions and 12 deletions

View File

@ -48,8 +48,8 @@ module Bosl
# attr_reader :string # attr_reader :string
def self.compile_string expression , method def self.compile_string expression , method
# Clearly a TODO here to implement strings rather than reusing symbols # Clearly a TODO here to implement strings rather than reusing symbols
value = expression.string.to_sym value = expression.first.to_sym
to = Virtual::Return.new(Reference , value) to = Virtual::Return.new(Virtual::Reference , value)
method.source.constants << value method.source.constants << value
method.source.add_code Virtual::Set.new( value , to ) method.source.add_code Virtual::Set.new( value , to )
to to

View File

@ -8,8 +8,11 @@ module Bosl
name , arguments , receiver = *expession name , arguments , receiver = *expession
name = name.to_a.first name = name.to_a.first
me = Compiler.compile( receiver.to_a.first , method ) if receiver
me = Compiler.compile( receiver.to_a.first , method )
else
me = Virtual::Self.new
end
## need two step process, compile and save to frame ## need two step process, compile and save to frame
# then move from frame to new message # then move from frame to new message
method.source.add_code Virtual::NewMessage.new method.source.add_code Virtual::NewMessage.new

View File

@ -3,6 +3,8 @@ module Bosl
# if - attr_reader :cond, :if_true, :if_false # if - attr_reader :cond, :if_true, :if_false
def self.compile_if expression , method def self.compile_if expression , method
condition , if_true , if_false = *expression
condition = condition.first
# to execute the logic as the if states it, the blocks are the other way around # to execute the logic as the if states it, the blocks are the other way around
# so we can the jump over the else if true , # so we can the jump over the else if true ,
# and the else joins unconditionally after the true_block # and the else joins unconditionally after the true_block
@ -11,27 +13,27 @@ module Bosl
false_block = method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here false_block = method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here
is = Compiler.compile(expression.cond, method ) is = Compiler.compile(condition, method )
# TODO should/will use different branches for different conditions. # TODO should/will use different branches for different conditions.
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition # just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition
method.source.add_code IsTrueBranch.new( true_block ) method.source.add_code Virtual::IsTrueBranch.new( true_block )
# compile the true block (as we think of it first, even it is second in sequential order) # compile the true block (as we think of it first, even it is second in sequential order)
method.source.current true_block method.source.current true_block
last = is last = is
expression.if_true.each do |part| if_true.each do |part|
last = Compiler.compile(part,method ) last = Compiler.compile(part,method )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
# compile the false block # compile the false block
method.source.current false_block method.source.current false_block
expression.if_false.each do |part| if_false.each do |part|
#puts "compiling in if false #{part}" #puts "compiling in if false #{part}"
last = Compiler.compile(part,method ) last = Compiler.compile(part,method )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
method.source.add_code UnconditionalBranch.new( merge_block ) method.source.add_code Virtual::UnconditionalBranch.new( merge_block )
#puts "compiled if: end" #puts "compiled if: end"
method.source.current merge_block method.source.current merge_block

View File

@ -2,8 +2,8 @@ module Bosl
module Compiler module Compiler
# operator attr_reader :operator, :left, :right # operator attr_reader :operator, :left, :right
def self.compile_operator expression, method def self.compile_operator expression, method
call = Ast::CallSiteExpression.new(expression.operator , [expression.right] , expression.left ) operator , left , right = *expression
Compiler.compile(call, method) nil
end end
def self.compile_assign expression, method def self.compile_assign expression, method

View File

@ -26,7 +26,7 @@ HERE
def test_if_function def test_if_function
@string_input = <<HERE @string_input = <<HERE
def itest(n) int itest(int n)
if( n < 12) if( n < 12)
"then".putstring() "then".putstring()
else else