gets rid of conversion approach, ast compiles
This commit is contained in:
parent
c67ee7f6f3
commit
99da6f5be3
@ -26,6 +26,11 @@ module Ast
|
|||||||
def initialize str
|
def initialize str
|
||||||
@string = str
|
@string = str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compile context
|
||||||
|
# TODO check if needst to be added?
|
||||||
|
ObjectReference.new( StringValue.new(string) )
|
||||||
|
end
|
||||||
def == other
|
def == other
|
||||||
compare other , [:string]
|
compare other , [:string]
|
||||||
end
|
end
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
module Ast
|
|
||||||
# Convert ast to vm-values via visitor pattern
|
|
||||||
# We do this (what would otherwise seem like foot-shuffling) to keep the layers seperated
|
|
||||||
# Ie towards the feature goal of reusing the same parse for several binary outputs
|
|
||||||
|
|
||||||
# scope of the funcitons is thus class scope ie self is the expression and all attributes work
|
|
||||||
# gets included into Value
|
|
||||||
module Conversion
|
|
||||||
def to_value
|
|
||||||
cl_name = self.class.name.to_s.split("::").last.gsub("Expression","").downcase
|
|
||||||
send "#{cl_name}_value"
|
|
||||||
end
|
|
||||||
def funcall_value
|
|
||||||
FunctionCall.new( name , args.collect{ |a| a.to_value } )
|
|
||||||
end
|
|
||||||
def string_value
|
|
||||||
ObjectReference.new( StringValue.new(string) )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
require_relative "expression"
|
|
||||||
|
|
||||||
Ast::Expression.class_eval do
|
|
||||||
include Ast::Conversion
|
|
||||||
end
|
|
@ -7,6 +7,15 @@ module Ast
|
|||||||
def initialize name, args
|
def initialize name, args
|
||||||
@name , @args = name , args
|
@name , @args = name , args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compile context
|
||||||
|
fun = Vm::FunctionCall.new( name , args.collect{ |a| a.compile(context) } )
|
||||||
|
fun.assign_function context
|
||||||
|
fun.load_args
|
||||||
|
#puts "funcall #{self.inspect}"
|
||||||
|
fun.do_call
|
||||||
|
end
|
||||||
|
|
||||||
def == other
|
def == other
|
||||||
compare other , [:name , :args]
|
compare other , [:name , :args]
|
||||||
end
|
end
|
||||||
|
@ -6,32 +6,24 @@ module Vm
|
|||||||
|
|
||||||
def initialize(name , args)
|
def initialize(name , args)
|
||||||
super(name)
|
super(name)
|
||||||
@name = name
|
@args = args
|
||||||
@values = args
|
|
||||||
@function = nil
|
@function = nil
|
||||||
end
|
end
|
||||||
attr_reader :function
|
attr_reader :function , :args
|
||||||
def args
|
|
||||||
values
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile context
|
def assign_function context
|
||||||
@function = context.program.get_function @name
|
@function = context.program.get_function @name
|
||||||
if @function
|
if @function
|
||||||
raise "error #{self}" unless @function.arity != @values.length
|
raise "error #{self}" unless @function.arity != args.length
|
||||||
else
|
else
|
||||||
@function = context.program.get_or_create_function @name
|
@function = context.program.get_or_create_function @name
|
||||||
end
|
end
|
||||||
args.each_with_index do |arg |
|
end
|
||||||
arg.compile context
|
def load_args
|
||||||
end
|
|
||||||
args.each_with_index do |arg , index|
|
args.each_with_index do |arg , index|
|
||||||
arg.load index
|
arg.load index
|
||||||
end
|
end
|
||||||
#puts "funcall #{self.inspect}"
|
|
||||||
self.do_call
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_call
|
def do_call
|
||||||
Machine.instance.function_call self
|
Machine.instance.function_call self
|
||||||
end
|
end
|
||||||
|
@ -96,5 +96,3 @@ module Vm
|
|||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
require "ast/conversion"
|
|
Loading…
Reference in New Issue
Block a user