add more functionality to get while to work (not there yet)
This commit is contained in:
parent
2230a4f25e
commit
d50c38f5ad
@ -20,6 +20,11 @@ module Arm
|
|||||||
left
|
left
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def integer_minus block , left , right
|
||||||
|
block.add_code sub(:left => left , :right => right )
|
||||||
|
left
|
||||||
|
end
|
||||||
|
|
||||||
def integer_load block , left , right
|
def integer_load block , left , right
|
||||||
reg = "r#{left.register}".to_sym
|
reg = "r#{left.register}".to_sym
|
||||||
block.add_code mov( :left => reg , :right => right )
|
block.add_code mov( :left => reg , :right => right )
|
||||||
@ -35,7 +40,7 @@ module Arm
|
|||||||
def function_call into , call
|
def function_call into , call
|
||||||
raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite
|
raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite
|
||||||
raise "Not linked #{call.inspect}" unless call.function
|
raise "Not linked #{call.inspect}" unless call.function
|
||||||
into.add_code bl( :left => call.function )
|
into.add_code call( :left => call.function )
|
||||||
call.function.return_type
|
call.function.return_type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
module Ast
|
module Ast
|
||||||
class FunctionExpression < Expression
|
class FunctionExpression < Expression
|
||||||
attr_reader :name, :params, :block
|
attr_reader :name, :params, :body
|
||||||
def initialize name, params, block
|
def initialize name, params, body
|
||||||
@name, @params, @block = name.to_sym, params, block
|
@name, @params, @body = name.to_sym, params, body
|
||||||
end
|
end
|
||||||
def attributes
|
def attributes
|
||||||
[:name, :params, :block]
|
[:name, :params, :body]
|
||||||
end
|
end
|
||||||
def inspect
|
def inspect
|
||||||
self.class.name + ".new(" + name.inspect + ", ["+
|
self.class.name + ".new(" + name.inspect + ", ["+
|
||||||
params.collect{|m| m.inspect }.join( ",") +"] , [" +
|
params.collect{|m| m.inspect }.join( ",") +"] , [" +
|
||||||
block.collect{|m| m.inspect }.join( ",") +"] )"
|
body.collect{|m| m.inspect }.join( ",") +"] )"
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"def #{name}( " + params.join(",") + ") \n" + block.join("\n") + "end\n"
|
"def #{name}( " + params.join(",") + ") \n" + body.join("\n") + "end\n"
|
||||||
end
|
end
|
||||||
def compile context , into
|
def compile context , into
|
||||||
raise "function does not compile into anything #{self}" if into
|
raise "function does not compile into anything #{self}" if into
|
||||||
@ -35,11 +35,11 @@ module Ast
|
|||||||
context.function = function
|
context.function = function
|
||||||
|
|
||||||
into = function.body
|
into = function.body
|
||||||
block.each do |b|
|
body.each do |b|
|
||||||
compiled = b.compile(context , into)
|
compiled = b.compile(context , into)
|
||||||
function.return_type = compiled
|
function.return_type = compiled
|
||||||
raise "alarm " unless compiled.is_a? Vm::Word
|
puts "compiled in function #{compiled.inspect}"
|
||||||
puts compiled.inspect
|
raise "alarm #{compiled} \n #{b}" unless compiled.is_a? Vm::Word
|
||||||
end
|
end
|
||||||
context.locals = parent_locals
|
context.locals = parent_locals
|
||||||
context.function = parent_function
|
context.function = parent_function
|
||||||
|
@ -16,18 +16,25 @@ module Ast
|
|||||||
"#{left} #{operator} #{right}"
|
"#{left} #{operator} #{right}"
|
||||||
end
|
end
|
||||||
def compile context , into
|
def compile context , into
|
||||||
puts "compile #{to_s}"
|
puts "compile operator #{to_s}"
|
||||||
r_val = right.compile(context , into)
|
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)
|
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
||||||
if r_val.is_a? Vm::IntegerConstant
|
l_val = context.locals[left.name]
|
||||||
puts context.attributes.keys.join(" ")
|
if( l_val ) #variable existed, move data there
|
||||||
next_register = context.function.next_register
|
l_val = l_val.move( into , r_val)
|
||||||
r_val = Vm::Integer.new(next_register).load( 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
|
end
|
||||||
context.locals[left.name] = r_val
|
context.locals[left.name] = l_val
|
||||||
return r_val
|
return l_val
|
||||||
end
|
end
|
||||||
|
|
||||||
l_val = left.compile(context , into)
|
l_val = left.compile(context , into)
|
||||||
|
|
||||||
case operator
|
case operator
|
||||||
@ -35,6 +42,8 @@ module Ast
|
|||||||
code = l_val.less_or_equal into , r_val
|
code = l_val.less_or_equal into , r_val
|
||||||
when "+"
|
when "+"
|
||||||
code = l_val.plus into , r_val
|
code = l_val.plus into , r_val
|
||||||
|
when "-"
|
||||||
|
code = l_val.minus into , r_val
|
||||||
else
|
else
|
||||||
raise "unimplemented operator #{operator} #{self}"
|
raise "unimplemented operator #{operator} #{self}"
|
||||||
end
|
end
|
||||||
|
@ -16,10 +16,12 @@ module Ast
|
|||||||
def compile context , into
|
def compile context , into
|
||||||
cond_val = condition.compile(context , into)
|
cond_val = condition.compile(context , into)
|
||||||
#set up branches for bodies
|
#set up branches for bodies
|
||||||
|
last = nil
|
||||||
body.each do |part|
|
body.each do |part|
|
||||||
part.compile(context , into )
|
last = part.compile(context , into )
|
||||||
|
puts "compiled in while #{last.inspect}"
|
||||||
end
|
end
|
||||||
return cond_val
|
return last
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,13 +13,17 @@ module Vm
|
|||||||
|
|
||||||
def load_args into
|
def load_args into
|
||||||
args.each_with_index do |arg , index|
|
args.each_with_index do |arg , index|
|
||||||
puts "load " + arg.inspect
|
if arg.is_a? IntegerConstant
|
||||||
arg.load( into , index )
|
Vm::Integer.new(index).load into , arg
|
||||||
|
else
|
||||||
|
raise "no #{arg.inspect}" if arg.register != index
|
||||||
|
#arg.load( into , index )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_call into
|
def do_call into
|
||||||
into.add_code CMachine.instance.function_call into , self
|
CMachine.instance.function_call into , self
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -66,6 +66,9 @@ module Vm
|
|||||||
def plus block , right
|
def plus block , right
|
||||||
CMachine.instance.integer_plus block , self , right
|
CMachine.instance.integer_plus block , self , right
|
||||||
end
|
end
|
||||||
|
def minus block , right
|
||||||
|
CMachine.instance.integer_minus block , self , right
|
||||||
|
end
|
||||||
|
|
||||||
def load block , right
|
def load block , right
|
||||||
CMachine.instance.integer_load block , self , right
|
CMachine.instance.integer_load block , self , right
|
||||||
|
Loading…
Reference in New Issue
Block a user