add more functionality to get while to work (not there yet)

This commit is contained in:
Torsten Ruger
2014-05-14 11:33:23 +03:00
parent 2230a4f25e
commit d50c38f5ad
6 changed files with 46 additions and 23 deletions

View File

@ -1,20 +1,20 @@
module Ast
class FunctionExpression < Expression
attr_reader :name, :params, :block
def initialize name, params, block
@name, @params, @block = name.to_sym, params, block
attr_reader :name, :params, :body
def initialize name, params, body
@name, @params, @body = name.to_sym, params, body
end
def attributes
[:name, :params, :block]
[:name, :params, :body]
end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
params.collect{|m| m.inspect }.join( ",") +"] , [" +
block.collect{|m| m.inspect }.join( ",") +"] )"
body.collect{|m| m.inspect }.join( ",") +"] )"
end
def to_s
"def #{name}( " + params.join(",") + ") \n" + block.join("\n") + "end\n"
"def #{name}( " + params.join(",") + ") \n" + body.join("\n") + "end\n"
end
def compile context , into
raise "function does not compile into anything #{self}" if into
@ -35,11 +35,11 @@ module Ast
context.function = function
into = function.body
block.each do |b|
body.each do |b|
compiled = b.compile(context , into)
function.return_type = compiled
raise "alarm " unless compiled.is_a? Vm::Word
puts compiled.inspect
puts "compiled in function #{compiled.inspect}"
raise "alarm #{compiled} \n #{b}" unless compiled.is_a? Vm::Word
end
context.locals = parent_locals
context.function = parent_function

View File

@ -16,18 +16,25 @@ module Ast
"#{left} #{operator} #{right}"
end
def compile context , into
puts "compile #{to_s}"
puts "compile operator #{to_s}"
r_val = right.compile(context , into)
if operator == "=" # assignemnt
if operator == "=" # assignment, value based
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
if r_val.is_a? Vm::IntegerConstant
puts context.attributes.keys.join(" ")
next_register = context.function.next_register
r_val = Vm::Integer.new(next_register).load( into , r_val )
l_val = context.locals[left.name]
if( l_val ) #variable existed, move data there
l_val = l_val.move( into , r_val)
else
if r_val.is_a? Vm::IntegerConstant
next_register = context.function.next_register
l_val = Vm::Integer.new(next_register).load( into , r_val )
else
l_val = r_val
end
end
context.locals[left.name] = r_val
return r_val
context.locals[left.name] = l_val
return l_val
end
l_val = left.compile(context , into)
case operator
@ -35,6 +42,8 @@ module Ast
code = l_val.less_or_equal into , r_val
when "+"
code = l_val.plus into , r_val
when "-"
code = l_val.minus into , r_val
else
raise "unimplemented operator #{operator} #{self}"
end

View File

@ -16,10 +16,12 @@ module Ast
def compile context , into
cond_val = condition.compile(context , into)
#set up branches for bodies
last = nil
body.each do |part|
part.compile(context , into )
last = part.compile(context , into )
puts "compiled in while #{last.inspect}"
end
return cond_val
return last
end
end