adds the ast minus the compile code (ie no dependencies)

This commit is contained in:
Torsten Ruger 2014-06-04 19:59:38 +03:00
parent 7ab293cb62
commit b1010577ab
10 changed files with 276 additions and 0 deletions

View File

@ -0,0 +1,56 @@
# collection of the simple ones, int and strings and such
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
def attributes
[:value]
end
end
class NameExpression < Expression
attr_reader :name
def initialize name
@name = name.to_sym
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
def attributes
[:string]
end
end
end

View File

@ -0,0 +1,36 @@
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
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
end
end

View File

@ -0,0 +1,30 @@
module Ast
class ArrayExpression < Expression
attr_reader :values
def initialize vals
@values = vals
end
def inspect
self.class.name + ".new(" + values.to_s+ ")"
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
def attributes
[:key , :value]
end
end
class HashExpression < ArrayExpression
end
end

37
lib/ast/expression.rb Normal file
View File

@ -0,0 +1,37 @@
# 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 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

@ -0,0 +1,25 @@
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
end
end

15
lib/ast/if_expression.rb Normal file
View File

@ -0,0 +1,15 @@
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
end
end

View File

@ -0,0 +1,24 @@
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
end
class ClassExpression < ModuleExpression
end
end

View File

@ -0,0 +1,19 @@
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
end
end

View File

@ -0,0 +1,17 @@
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
end
end

View File

@ -0,0 +1,17 @@
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
end
end