introduce constant class and add block to compile signature(wip, work in progress)
This commit is contained in:
@ -13,8 +13,8 @@ module Ast
|
||||
def to_s
|
||||
value.to_s
|
||||
end
|
||||
def compile context
|
||||
Vm::Signed.new value
|
||||
def compile context , into
|
||||
Vm::Integer.new value
|
||||
end
|
||||
def attributes
|
||||
[:value]
|
||||
@ -26,10 +26,10 @@ module Ast
|
||||
def initialize name
|
||||
@name = name
|
||||
end
|
||||
def compile context
|
||||
variable = Vm::Variable.new(@name)
|
||||
context.locals[@name] = variable
|
||||
variable
|
||||
# compiling a variable resolves it.
|
||||
# if it wasn't defined, nli is returned
|
||||
def compile context , into
|
||||
context.locals[name]
|
||||
end
|
||||
def inspect
|
||||
self.class.name + '.new("' + name + '")'
|
||||
@ -51,8 +51,8 @@ module Ast
|
||||
self.class.name + '.new("' + string + '")'
|
||||
end
|
||||
|
||||
def compile context
|
||||
value = Vm::StringLiteral.new(string)
|
||||
def compile context , into
|
||||
value = Vm::StringConstant.new(string)
|
||||
context.program.add_object value
|
||||
value
|
||||
end
|
||||
|
@ -12,7 +12,7 @@ module Ast
|
||||
def eval
|
||||
raise "abstract #{self}"
|
||||
end
|
||||
def compile context
|
||||
def compile context , into
|
||||
raise "abstract #{self}"
|
||||
end
|
||||
def attributes
|
||||
|
@ -16,27 +16,31 @@ module Ast
|
||||
def to_s
|
||||
"def #{name}( " + params.join(",") + ") \n" + block.join("\n") + "end\n"
|
||||
end
|
||||
def compile context
|
||||
raise self.to_s
|
||||
def compile context , into
|
||||
raise "function does not compile into anything #{self}" if into
|
||||
parent_locals = context.locals
|
||||
context.locals = {}
|
||||
args = []
|
||||
params.each do |param|
|
||||
args << param.compile(context) # making the argument a local
|
||||
params.each_with_index do |param , index|
|
||||
arg = param.name
|
||||
arg_value = Vm::Integer.new(index)
|
||||
context.locals[arg] = arg_value
|
||||
args << arg_value
|
||||
end
|
||||
# args = params.collect{|p| Vm::Value.type p.name }
|
||||
function = Vm::Function.new(name ,args )
|
||||
function = Vm::Function.new(name , args )
|
||||
context.program.add_function function
|
||||
into = function.entry
|
||||
block.each do |b|
|
||||
compiled = b.compile context
|
||||
compiled = b.compile(context , into)
|
||||
if compiled.is_a? Vm::Block
|
||||
into = compiled
|
||||
he.breaks.loose
|
||||
else
|
||||
function.body.add_code compiled
|
||||
end
|
||||
puts compiled.inspect
|
||||
end
|
||||
context.locals = parent_locals if parent_locals
|
||||
context.locals = parent_locals
|
||||
function
|
||||
end
|
||||
|
||||
|
@ -7,13 +7,14 @@ module Ast
|
||||
def initialize name, args
|
||||
@name , @args = name , args
|
||||
end
|
||||
def compile context
|
||||
fun = Vm::FunctionCall.new( name , args.collect{ |a| a.compile(context) } )
|
||||
fun.assign_function context
|
||||
fun.load_args
|
||||
fun.do_call
|
||||
def compile context , into
|
||||
params = args.collect{ |a| a.compile(context, into) }
|
||||
fun = Vm::FunctionCall.new( name , params )
|
||||
fun.load_args into
|
||||
fun.do_call into
|
||||
fun
|
||||
end
|
||||
|
||||
def inspect
|
||||
self.class.name + ".new(" + name.inspect + ", ["+
|
||||
args.collect{|m| m.inspect }.join( ",") +"] )"
|
||||
@ -41,35 +42,24 @@ module Ast
|
||||
def to_s
|
||||
"#{left} #{operator} #{right}"
|
||||
end
|
||||
def compile context
|
||||
parent_locals = context.locals
|
||||
context.locals = {}
|
||||
args = []
|
||||
|
||||
#assignemnt
|
||||
value = @assigned.compile(context)
|
||||
variable = Vm::Variable.new @assignee , :r0 , value
|
||||
context.locals[@assignee] = variable
|
||||
variable
|
||||
|
||||
|
||||
params.each do |param|
|
||||
args << param.compile(context) # making the argument a local
|
||||
def compile context , into
|
||||
r_val = right.compile(context , into)
|
||||
|
||||
if operator == "=" # assignemnt
|
||||
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
||||
context.locals[left.name] = r_val
|
||||
return r_val
|
||||
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|
|
||||
compiled = b.compile context
|
||||
if compiled.is_a? Vm::Block
|
||||
he.breaks.loose
|
||||
else
|
||||
function.body.add_code compiled
|
||||
end
|
||||
puts compiled.inspect
|
||||
l_val = left.compile(context , into)
|
||||
|
||||
case operator
|
||||
when ">"
|
||||
code = l_val.less_or_equal r_val
|
||||
when "+"
|
||||
code = l_val.plus r_val
|
||||
else
|
||||
raise "unimplemented operator #{operator} #{self}"
|
||||
end
|
||||
context.locals = parent_locals if parent_locals
|
||||
function
|
||||
end
|
||||
end
|
||||
end
|
@ -13,6 +13,14 @@ module Ast
|
||||
def attributes
|
||||
[:condition, :body]
|
||||
end
|
||||
def compile context , into
|
||||
cond_val = condition.compile(context , into)
|
||||
#set up branches for bodies
|
||||
body.each do |part|
|
||||
part.compile(context , into )
|
||||
end
|
||||
return cond_val
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user