Skip to content

Instantly share code, notes, and snippets.

@eljojo
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eljojo/53f145473c1d30244efa to your computer and use it in GitHub Desktop.
Save eljojo/53f145473c1d30244efa to your computer and use it in GitHub Desktop.
Almost Rubinius
module Rules
class Processor
def initialize(context)
require 'parser/current'
@context = context
end
def process(command)
node = node_for_command(command)
process_node(node)
end
def node_for_command(command)
Parser::CurrentRuby.parse(command)
end
private
attr_reader :context
def process_node(node)
raise "object is not a AST::Node" unless node.is_a?(Parser::AST::Node)
node = clean_begin(node)
children = node.children
case node.type
when :int
children.first
when :str
children.first.to_s
when :float
children.first
when :true
true
when :false
false
when :sym
children.first
when :nil
nil
when :send
receiver = children.first.nil? ? context : process_node(children.first)
method_name = children[1]
args = children[2..-1].map{|arg| process_node(arg)}
receiver.send(method_name, *args)
when :or
process_node(children.first) || process_node(children.last)
when :and
process_node(children.first) && process_node(children.last)
when :dstr
children.map{|child| process_node(child)}.join
else
raise "Found another type of node! #{node.type}"
end
end
def clean_begin(node)
return node unless node.type == :begin
raise "begin has more than one children!" if node.children.length > 1
node.children.first
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment