adds variable, something compliles , but...

This commit is contained in:
Torsten Ruger 2014-05-10 17:55:02 +03:00
parent 5b002c0ff6
commit e600911fe8
6 changed files with 33 additions and 9 deletions

View File

@ -32,7 +32,7 @@ module Arm
end end
def word_load value , reg def word_load value , reg
raise "nnn " unless reg.class == Symbol raise "not a register :#{reg}:" unless reg.class == Symbol
mov( :left => reg , :right => value ) mov( :left => reg , :right => value )
end end
def string_load str_lit , reg def string_load str_lit , reg

View File

@ -23,6 +23,11 @@ module Ast
def initialize name def initialize name
@name = name @name = name
end end
def compile context
variable = Vm::Variable.new(@name)
context.locals[@name] = variable
variable
end
def inspect def inspect
self.class.name + ".new(" + name + ")" self.class.name + ".new(" + name + ")"
end end

View File

@ -14,11 +14,15 @@ module Ast
end end
def compile context def compile context
args = params.collect{|p| Vm::Value.type p.name }
function = Vm::Function.new(name ,args )
context.program.add_function function
parent_locals = context.locals parent_locals = context.locals
context.locals = {} context.locals = {}
args = []
params.each do |param|
args << param.compile(context) # making the argument a local
end
# args = params.collect{|p| Vm::Value.type p.name }
function = Vm::Function.new(name ,args )
context.program.add_function function
block.each do |b| block.each do |b|
compiled = b.compile context compiled = b.compile context
if compiled.is_a? Vm::Block if compiled.is_a? Vm::Block

View File

@ -35,8 +35,10 @@ module Ast
end end
def compile context def compile context
var = @assigned.compile(context) value = @assigned.compile(context)
context.locals[@assignee] = var variable = Vm::Variable.new @assignee , :r0 , value
context.locals[@assignee] = variable
variable
end end
def attributes def attributes

View File

@ -56,7 +56,6 @@ module Vm
def assemble io def assemble io
@entry.assemble(io) @entry.assemble(io)
raise @body.inspect
@body.assemble(io) @body.assemble(io)
@exit.assemble(io) @exit.assemble(io)
end end

View File

@ -36,7 +36,6 @@ module Vm
end end
t t
end end
end end
class Word < Value class Word < Value
@ -60,7 +59,22 @@ module Vm
Machine.instance.signed_plus self , signed Machine.instance.signed_plus self , signed
end end
end end
class Variable < Value
attr_reader :name , :register
def initialize name , register = nil , val = nil
super(val)
@register = register
@name = name
end
def length
@value.length
end
def assemble io
@value.load @register
end
end
# The name really says it all. # The name really says it all.
# The only interesting thing is storage. # The only interesting thing is storage.
# Currently string are stored "inline" , ie in the code segment. # Currently string are stored "inline" , ie in the code segment.