upgrades ast to first class

This commit is contained in:
Torsten Ruger
2014-05-05 09:51:16 +03:00
parent 7c0aa8ae7d
commit 7c7e58ea62
10 changed files with 40 additions and 40 deletions

View File

@ -1,87 +0,0 @@
# ast classes
module Parser
class Expression
def eval
raise "abstract"
end
def compare other , attributes
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
class IntegerExpression < Expression
attr_reader :value
def initialize val
@value = val
end
def == other
compare other , [:value]
end
end
class NameExpression < Expression
attr_reader :name
def initialize name
@name = name
end
def == other
compare other , [:name]
end
end
class StringExpression < Expression
attr_reader :string
def initialize str
@string = str
end
def == other
compare other , [:string]
end
end
class FuncallExpression < Expression
attr_reader :name, :args
def initialize name, args
@name , @args = name , args
end
def == other
compare other , [:name , :args]
end
end
class ConditionalExpression < 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 == other
compare other , [:cond, :if_true, :if_false]
end
end
class AssignmentExpression < Expression
attr_reader :assignee, :assigned
def initialize assignee, assigned
@assignee, @assigned = assignee, assigned
end
def == other
compare other , [:assignee, :assigned]
end
end
class FunctionExpression < Expression
attr_reader :name, :params, :block
def initialize name, params, block
@name, @params, @block = name, params, block
end
def == other
compare other , [:name, :params, :block]
end
end
end

View File

@ -1,11 +1,11 @@
require 'parslet'
require_relative 'nodes'
require 'ast/expression'
module Parser
class Transform < Parslet::Transform
rule(:integer => simple(:value)) { IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { NameExpression.new(name.to_s) }
rule(:string => simple(:string)) { StringExpression.new(string.to_s) }
rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
rule(:string => simple(:string)) { Ast::StringExpression.new(string.to_s) }
rule(:argument => simple(:argument)) { argument }
rule(:argument_list => sequence(:argument_list)) { argument_list }
@ -13,17 +13,17 @@ module Parser
# need TWO transform rules, for one/many arguments (see the[] wrapping in the first)
rule(:function_call => simple(:function_call),
:argument_list => simple(:argument)) do
FuncallExpression.new(function_call.name, [argument])
Ast::FuncallExpression.new(function_call.name, [argument])
end
rule( :function_call => simple(:function_call),
:argument_list => sequence(:argument_list)) do
FuncallExpression.new(function_call.name, argument_list)
Ast::FuncallExpression.new(function_call.name, argument_list)
end
rule(:conditional => simple(:conditional),
:if_true => {:expressions => sequence(:if_true)},
:if_false => {:expressions => sequence(:if_false)}) do
ConditionalExpression.new(conditional, if_true, if_false)
Ast::ConditionalExpression.new(conditional, if_true, if_false)
end
rule(:parmeter => simple(:parmeter)) { parmeter }
@ -33,17 +33,17 @@ module Parser
rule(:function_definition => simple(:function_definition),
:parmeter_list => simple(:parmeter),
:expressions => sequence(:expressions)) do
FunctionExpression.new(function_definition.name, [parmeter], expressions)
Ast::FunctionExpression.new(function_definition.name, [parmeter], expressions)
end
rule(:function_definition => simple(:function_definition),
:parmeter_list => sequence(:parmeter_list),
:expressions => sequence(:expressions)) do
FunctionExpression.new(function_definition.name, parmeter_list, expressions)
Ast::FunctionExpression.new(function_definition.name, parmeter_list, expressions)
end
rule(:asignee => simple(:left) , :asigned => simple(:right) ) do
AssignmentExpression.new(left.name, right )
Ast::AssignmentExpression.new(left.name, right )
end
#shortcut to get the ast tree for a given string
# optional second arguement specifies a rule that will be parsed (mainly for testing)