remove the parse code and fix the rest to work with the gem (from git for now)

This commit is contained in:
Torsten Ruger
2014-06-04 22:03:45 +03:00
parent e9d2724f62
commit 7cc4c6344c
45 changed files with 33 additions and 1708 deletions

10
lib/ast/all.rb Normal file
View File

@ -0,0 +1,10 @@
require "ast/expression"
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"

View File

@ -3,67 +3,31 @@
module Ast
class IntegerExpression < Expression
attr_reader :value
def initialize val
@value = val
end
def inspect
self.class.name + ".new(" + value.to_s+ ")"
end
def to_s
value.to_s
end
# attr_reader :value
def compile context , into
Vm::IntegerConstant.new value
end
def attributes
[:value]
end
end
class NameExpression < Expression
attr_reader :name
def initialize name
@name = name.to_sym
end
# attr_reader :name
# 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})"
end
def to_s
name.to_s
end
def attributes
[:name]
end
end
class ModuleName < NameExpression
end
class StringExpression < Expression
attr_reader :string
def initialize str
@string = str
end
def inspect
self.class.name + '.new("' + string + '")'
end
def to_s
'"' + string.to_s + '"'
end
# attr_reader :string
def compile context , into
value = Vm::StringConstant.new(string)
context.object_space.add_object value
value
end
def attributes
[:string]
end
end
end

View File

@ -2,13 +2,7 @@ module Ast
# assignment, like operators are really function calls
class CallSiteExpression < Expression
attr_reader :name, :args , :receiver
def initialize name, args , receiver = Ast::NameExpression.new(:self)
@name = name.to_sym
@args = args
@receiver = receiver
end
# attr_reader :name, :args , :receiver
def compile context , into
params = args.collect{ |a| a.compile(context, into) }
@ -37,28 +31,10 @@ module Ast
puts "compile call #{function.return_type}"
function.return_type
end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") + "] ," + receiver.inspect + ")"
end
def to_s
"#{name}(" + args.join(",") + ")"
end
def attributes
[:name , :args , :receiver]
end
end
class VariableExpression < CallSiteExpression
def initialize name
super( :_get_instance_variable , [StringExpression.new(name)] )
end
def inspect
self.class.name + ".new(" + args[0].string.inspect + ")"
end
# super( :_get_instance_variable , [StringExpression.new(name)] )
end
end

View File

@ -1,35 +1,16 @@
module Ast
class ArrayExpression < Expression
attr_reader :values
def initialize vals
@values = vals
end
def inspect
self.class.name + ".new(" + values.to_s+ ")"
end
# attr_reader :values
def compile context
to.do
end
def attributes
[:values]
end
end
class AssociationExpression < Expression
attr_reader :key , :value
def initialize key , value
@key , @value = key , value
end
def inspect
self.class.name + ".new(" + key.inspect + " , " + value.inspect + ")"
end
# attr_reader :key , :value
def compile context
to.do
end
def attributes
[:key , :value]
end
end
class HashExpression < ArrayExpression
def compile context

View File

@ -1,43 +0,0 @@
# 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
module Ast
class Expression
def compile context , into
raise "abstract #{self}"
end
def compile context , into
raise "abstract #{self}"
end
def attributes
raise "abstract #{self}"
end
def == other
return false unless other.class == self.class
attributes.each do |a|
left = send(a)
right = other.send(a)
return false unless left.class == right.class
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"

View File

@ -1,26 +1,6 @@
module Ast
class FunctionExpression < Expression
attr_reader :name, :params, :body , :receiver
def initialize name, params, body , receiver = nil
@name = name.to_sym
@params = params
@body = body
@receiver = receiver
end
def attributes
[:name, :params, :body , :receiver]
end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
params.collect{|m| m.inspect }.join( ",") +"] , [" +
body.collect{|m| m.inspect }.join( ",") +"] ,"+ receiver.inspect + " )"
end
def to_s
ret = "def "
ret += "#{receiver}." if receiver
ret + "#{name}( " + params.join(",") + ") \n" + body.join("\n") + "end\n"
end
# attr_reader :name, :params, :body , :receiver
def compile context , into
raise "function does not compile into anything #{self}" if into
args = []
@ -67,6 +47,5 @@ module Ast
context.function = parent_function
function
end
end
end

View File

@ -1,16 +1,6 @@
module Ast
class IfExpression < Expression
attr_reader :cond, :if_true, :if_false
def initialize cond, if_true, if_false
@cond, @if_true, @if_false = cond, if_true, if_false
end
def inspect
self.class.name + ".new(" + cond.inspect + ", "+
if_true.inspect + "," + if_false.inspect + " )"
end
def attributes
[:cond, :if_true, :if_false]
end
# attr_reader :cond, :if_true, :if_false
def compile context , into
# to execute the logic as the if states it, the blocks are the other way around
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block

View File

@ -1,21 +1,6 @@
module Ast
class ModuleExpression < Expression
attr_reader :name ,:expressions
def initialize name , expressions
@name = name.to_sym
@expressions = expressions
end
def inspect
self.class.name + ".new(" + @name.inspect + " ," + @expressions.inspect + " )"
end
def to_s
"module #{name}\n #{expressions}\nend\n"
end
def attributes
[:name , :expressions]
end
# attr_reader :name ,:expressions
def compile context , into
clazz = context.object_space.get_or_create_class name
puts "Created class #{clazz.name.inspect}"

View File

@ -1,20 +1,6 @@
module Ast
class OperatorExpression < Expression
attr_reader :operator, :left, :right
def initialize operator, left, right
@operator, @left, @right = operator, left, right
end
def attributes
[:operator, :left, :right]
end
def inspect
self.class.name + ".new(" + operator.inspect + ", " + left.inspect + "," + right.inspect + ")"
end
def to_s
"#{left} #{operator} #{right}"
end
# attr_reader :operator, :left, :right
def compile context , into
puts "compiling operator #{to_s}"
r_val = right.compile(context , into)

View File

@ -1,18 +1,6 @@
module Ast
class ReturnExpression < Expression
attr_reader :expression
def initialize expression
@expression = expression
end
def inspect
self.class.name + ".new(" + expression.inspect + " )"
end
def to_s
"return #{expression}\n"
end
def attributes
[:expression]
end
# attr_reader :expression
def compile context , into
puts "compiling return expression #{expression}, now return in 7"
expression_value = expression.compile(context , into)

View File

@ -1,18 +1,6 @@
module Ast
class WhileExpression < Expression
attr_reader :condition, :body
def initialize condition, body
@condition , @body = condition , body
end
def inspect
self.class.name + ".new(" + condition.inspect + ", " + body.inspect + " )"
end
def to_s
"while(#{condition}) do\n" + body.join("\n") + "\nend\n"
end
def attributes
[:condition, :body]
end
# attr_reader :condition, :body
def compile context , into
while_block = into.new_block "#{into.name}_while"
ret = while_block.new_block "#{into.name}_return"