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
|
|
|
|
2014-04-27 20:12:42 +02:00
|
|
|
rule(:argument => simple(:argument)) { argument }
|
|
|
|
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 20:51:06 +02:00
|
|
|
# need TWO transform rules, for one/many arguments (see the[] wrapping in the first)
|
2014-04-27 20:41:38 +02:00
|
|
|
rule(:function_call => simple(:function_call),
|
|
|
|
:argument_list => simple(:argument)) do
|
|
|
|
Vm::FuncallExpression.new(function_call.name, [argument])
|
|
|
|
end
|
|
|
|
rule( :function_call => simple(:function_call),
|
|
|
|
:argument_list => sequence(:argument_list)) do
|
2014-04-27 20:12:42 +02:00
|
|
|
Vm::FuncallExpression.new(function_call.name, argument_list)
|
|
|
|
end
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 20:41:38 +02:00
|
|
|
rule(:conditional => simple(:conditional),
|
2014-04-28 15:07:34 +02:00
|
|
|
:if_true => {:expressions => sequence(:if_true)},
|
|
|
|
:if_false => {:expressions => sequence(:if_false)}) do
|
|
|
|
Vm::ConditionalExpression.new(conditional, if_true, if_false)
|
|
|
|
end
|
2014-04-27 20:41:38 +02:00
|
|
|
|
|
|
|
rule(:parmeter => simple(:parmeter)) { parmeter }
|
|
|
|
rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list }
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 20:51:06 +02:00
|
|
|
# need TWO transform rules, for one/many arguments (see the[] wrapping in the first)
|
2014-04-27 20:41:38 +02:00
|
|
|
rule(:function_definition => simple(:function_definition),
|
|
|
|
:parmeter_list => simple(:parmeter),
|
2014-04-28 15:07:34 +02:00
|
|
|
:expressions => sequence(:expressions)) do
|
|
|
|
Vm::FunctionExpression.new(function_definition.name, [parmeter], expressions)
|
2014-04-27 20:41:38 +02:00
|
|
|
end
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-27 20:12:42 +02:00
|
|
|
rule(:function_definition => simple(:function_definition),
|
2014-04-27 20:41:38 +02:00
|
|
|
:parmeter_list => sequence(:parmeter_list),
|
2014-04-28 15:07:34 +02:00
|
|
|
:expressions => sequence(:expressions)) do
|
|
|
|
Vm::FunctionExpression.new(function_definition.name, parmeter_list, expressions)
|
2014-04-27 20:12:42 +02:00
|
|
|
end
|
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
|