rubyx/lib/vm/transform.rb

34 lines
1.1 KiB
Ruby
Raw Normal View History

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-24 14:53:48 +02:00
module Vm
2014-04-24 14:43:20 +02:00
class Transform < Parslet::Transform
rule(:number => simple(:value)) { Number.new(value.to_i) }
rule(:name => simple(:name)) { Name.new(name.to_s) }
rule(:arg => simple(:arg)) { arg }
rule(:args => sequence(:args)) { args }
rule(:funcall => simple(:funcall),
:args => simple(:args)) { Funcall.new(funcall.name, [args]) }
rule(:funcall => simple(:funcall),
:args => sequence(:args)) { Funcall.new(funcall.name, args) }
rule(:cond => simple(:cond),
:if_true => {:body => simple(:if_true)},
:if_false => {:body => simple(:if_false)}) { Conditional.new(cond, if_true, if_false) }
rule(:param => simple(:param)) { param }
rule(:params => sequence(:params)) { params }
rule(:func => simple(:func),
:params => simple(:name),
:body => simple(:body)) { Function.new(func.name, [name], body) }
rule(:func => simple(:func),
:params => sequence(:params),
:body => simple(:body)) { Function.new(func.name, params, body) }
end
end