2014-06-04 18:59:38 +02:00
|
|
|
# abstract syntax tree (ast)
|
|
|
|
# This Layer is semi automagically created by parslet using the transform
|
|
|
|
|
|
|
|
# It in turn is responsible for the transformation to the next layer, vm code
|
|
|
|
|
|
|
|
# This happens in the compile function which must return a Vm::Code derivative
|
|
|
|
|
|
|
|
# PS: compare is only for tests and should be factored out to there
|
|
|
|
|
2015-07-12 09:00:29 +02:00
|
|
|
Array.class_eval do
|
|
|
|
def to_basic
|
|
|
|
collect do |item|
|
|
|
|
item.to_basic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Symbol.class_eval do
|
|
|
|
def to_basic
|
|
|
|
to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
String.class_eval do
|
|
|
|
def to_basic
|
|
|
|
to_s
|
|
|
|
end
|
|
|
|
end
|
2014-06-04 18:59:38 +02:00
|
|
|
module Ast
|
|
|
|
class Expression
|
|
|
|
def attributes
|
2015-07-11 20:59:36 +02:00
|
|
|
raise "abstract called for #{self}"
|
2014-06-30 16:51:07 +02:00
|
|
|
end
|
2015-07-12 09:00:29 +02:00
|
|
|
def to_basic()
|
|
|
|
data = { "class" => self.class.name }
|
2015-07-11 20:59:36 +02:00
|
|
|
attributes.each do |name|
|
2015-07-12 09:00:29 +02:00
|
|
|
val = instance_variable_get("@#{name}".to_sym)
|
|
|
|
res = val.to_basic
|
|
|
|
data[name] = res
|
2015-07-11 20:59:36 +02:00
|
|
|
end
|
2015-07-12 09:00:29 +02:00
|
|
|
data
|
2015-07-11 20:59:36 +02:00
|
|
|
end
|
|
|
|
|
2015-07-12 09:00:29 +02:00
|
|
|
def self.from_basic(hash)
|
|
|
|
clazz = hash.delete("class")
|
|
|
|
keys = hash.keys
|
|
|
|
klass = clazz.split("::").inject(Object) {|o,c| o.const_get(c)}
|
|
|
|
keys.delete("class")
|
|
|
|
values = keys.collect{|k| read_basic(hash[k]) }
|
|
|
|
klass.new(*values)
|
|
|
|
end
|
|
|
|
def self.read_basic val
|
|
|
|
return from_basic(val) if val.is_a?(Hash)
|
|
|
|
return val.collect{|i| from_basic(i)} if(val.is_a? Array )
|
|
|
|
val
|
2015-07-11 20:59:36 +02:00
|
|
|
end
|
|
|
|
|
2014-06-30 16:51:07 +02:00
|
|
|
def inspect
|
|
|
|
self.class.name + ".new()"
|
2014-06-04 18:59:38 +02:00
|
|
|
end
|
|
|
|
def == other
|
2015-07-11 20:59:36 +02:00
|
|
|
return false unless other.class == self.class
|
2014-06-04 18:59:38 +02:00
|
|
|
attributes.each do |a|
|
|
|
|
left = send(a)
|
|
|
|
right = other.send(a)
|
2015-07-11 20:59:36 +02:00
|
|
|
return false unless left.class == right.class
|
2014-06-04 18:59:38 +02:00
|
|
|
return false unless left == right
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
require_relative "basic_expressions"
|
|
|
|
require_relative "call_site_expression"
|
|
|
|
require_relative "compound_expressions"
|
|
|
|
require_relative "if_expression"
|
|
|
|
require_relative "function_expression"
|
|
|
|
require_relative "module_expression"
|
|
|
|
require_relative "operator_expressions"
|
|
|
|
require_relative "return_expression"
|
|
|
|
require_relative "while_expression"
|
2014-06-25 21:45:09 +02:00
|
|
|
require_relative "expression_list"
|