rubyx/lib/slot_language/slot_compiler.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2019-10-04 16:38:51 +02:00
require "parser/current"
require "ast"
module SlotLanguage
class SlotCompiler < AST::Processor
def self.compile(input)
ast = Parser::CurrentRuby.parse( input )
self.new.process(ast)
end
def not_implemented(node)
raise "Not implemented #{node.type}"
end
# default to error, so non implemented stuff shows early
def handler_missing(node)
not_implemented(node)
end
def on_send(statement)
2019-10-04 20:06:22 +02:00
#puts statement
2019-10-04 16:38:51 +02:00
kids = statement.children.dup
receiver = process(kids.shift) || MessageSlot.new
name = kids.shift
2019-10-04 20:06:22 +02:00
return label(name) if(name.to_s.end_with?("_label"))
2019-10-04 16:38:51 +02:00
SlotMaker.new( name , receiver )
end
2019-10-04 20:06:22 +02:00
def on_lvasgn expression
#puts expression
name = expression.children[0]
value = process(expression.children[1])
Sol::LocalAssignment.new(name,value)
end
def on_ivar expression
Sol::InstanceVariable.new(instance_name(expression.children.first))
end
private
def instance_name(sym)
sym.to_s[1 .. -1].to_sym
end
def label(name)
SlotMachine::Label.new(name.to_s , name)
end
2019-10-04 16:38:51 +02:00
end
end
require_relative "named_slot"
require_relative "message_slot"
require_relative "slot_maker"