2014-04-24 14:43:20 +02:00
|
|
|
require 'parslet'
|
2014-04-24 14:53:48 +02:00
|
|
|
require 'vm/nodes'
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 14:44:34 +02:00
|
|
|
module Parser
|
2014-04-24 14:43:20 +02:00
|
|
|
class Transform < Parslet::Transform
|
2014-04-27 17:13:34 +02:00
|
|
|
rule(:integer => simple(:value)) { Vm::IntegerExpression.new(value.to_i) }
|
2014-04-27 14:44:34 +02:00
|
|
|
rule(:name => simple(:name)) { Vm::NameExpression.new(name.to_s) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
rule(:arg => simple(:arg)) { arg }
|
|
|
|
rule(:args => sequence(:args)) { args }
|
|
|
|
|
|
|
|
rule(:funcall => simple(:funcall),
|
2014-04-27 14:44:34 +02:00
|
|
|
:args => simple(:args)) { Vm::FuncallExpression.new(funcall.name, [args]) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
rule(:funcall => simple(:funcall),
|
2014-04-27 14:44:34 +02:00
|
|
|
:args => sequence(:args)) { Vm::FuncallExpression.new(funcall.name, args) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
rule(:cond => simple(:cond),
|
|
|
|
:if_true => {:body => simple(:if_true)},
|
2014-04-27 14:44:34 +02:00
|
|
|
:if_false => {:body => simple(:if_false)}) { Vm::ConditionalExpression.new(cond, if_true, if_false) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
rule(:param => simple(:param)) { param }
|
|
|
|
rule(:params => sequence(:params)) { params }
|
|
|
|
|
|
|
|
rule(:func => simple(:func),
|
|
|
|
:params => simple(:name),
|
2014-04-27 14:44:34 +02:00
|
|
|
:body => simple(:body)) { Vm::FunctionExpression.new(func.name, [name], body) }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
|
|
|
rule(:func => simple(:func),
|
|
|
|
:params => sequence(:params),
|
2014-04-27 14:44:34 +02:00
|
|
|
:body => simple(:body)) { Vm::FunctionExpression.new(func.name, params, body) }
|
2014-04-27 15:30:32 +02:00
|
|
|
|
|
|
|
#shortcut to get the ast tree for a given string
|
|
|
|
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
|
|
|
def self.ast string , rule = :root
|
|
|
|
syntax = Parser.new.send(rule).parse(string)
|
|
|
|
tree = Transform.new.apply(syntax)
|
|
|
|
tree
|
|
|
|
end
|
2014-04-24 14:43:20 +02:00
|
|
|
end
|
|
|
|
end
|