2019-10-04 16:38:51 +02:00
|
|
|
require "parser/current"
|
|
|
|
require "ast"
|
|
|
|
|
|
|
|
module SlotLanguage
|
|
|
|
class SlotCompiler < AST::Processor
|
2020-02-09 15:33:34 +01:00
|
|
|
DEBUG = false
|
2019-10-04 16:38:51 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
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-05 13:49:45 +02:00
|
|
|
return goto(name,kids) if(name == :goto)
|
|
|
|
return check(name,receiver, kids) if(name == :==)
|
2019-10-05 18:37:24 +02:00
|
|
|
return assign(receiver, name , kids) if(name.to_s.end_with?("="))
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "Send #{name} , #{receiver} kids=#{kids}" if DEBUG
|
2019-10-07 19:14:40 +02:00
|
|
|
SlotMaker.new( name )
|
2019-10-04 16:38:51 +02:00
|
|
|
end
|
2019-10-05 18:37:24 +02:00
|
|
|
def on_lvar(lvar)
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "lvar #{lvar}" if DEBUG
|
2019-10-07 19:14:40 +02:00
|
|
|
SlotMaker.new(lvar.children.first )
|
2019-10-05 18:37:24 +02:00
|
|
|
end
|
2019-10-06 18:49:53 +02:00
|
|
|
def on_lvasgn( expression)
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "lvasgn #{expression}" if DEBUG
|
2019-10-04 20:06:22 +02:00
|
|
|
name = expression.children[0]
|
|
|
|
value = process(expression.children[1])
|
2019-10-07 19:14:40 +02:00
|
|
|
LoadMaker.new(SlotMaker.new(name),value)
|
2019-10-04 20:06:22 +02:00
|
|
|
end
|
2019-10-06 18:49:53 +02:00
|
|
|
alias :on_ivasgn :on_lvasgn
|
|
|
|
|
2019-10-05 13:49:45 +02:00
|
|
|
def on_if(expression)
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "if #{expression}" if DEBUG
|
2019-10-05 13:49:45 +02:00
|
|
|
condition = process(expression.children[0])
|
|
|
|
condition.set_goto( process(expression.children[1]) )
|
|
|
|
condition
|
|
|
|
end
|
|
|
|
def on_begin(exp)
|
2019-10-06 18:49:53 +02:00
|
|
|
if( exp.children.length == 1)
|
|
|
|
process(exp.first)
|
|
|
|
else
|
|
|
|
process_all(exp)
|
|
|
|
end
|
2019-10-05 13:49:45 +02:00
|
|
|
end
|
2020-02-09 15:33:34 +01:00
|
|
|
def on_ivar( expression)
|
|
|
|
puts "ivar #{expression}" if DEBUG
|
2019-10-07 19:14:40 +02:00
|
|
|
SlotMaker.new(expression.children.first)
|
2019-10-04 20:06:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def label(name)
|
|
|
|
SlotMachine::Label.new(name.to_s , name)
|
|
|
|
end
|
2019-10-05 13:49:45 +02:00
|
|
|
def goto(name , args)
|
|
|
|
# error handling would not hurt
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "goto #{name} , #{args}" if DEBUG
|
2019-10-05 13:49:45 +02:00
|
|
|
label = process(args.first)
|
|
|
|
SlotMachine::Jump.new( label )
|
|
|
|
end
|
|
|
|
def check(name , receiver , kids)
|
|
|
|
raise "Only ==, not #{name}" unless name == :==
|
|
|
|
raise "Familiy too large #{kids}" if kids.length > 1
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "Kids " + kids.to_s if DEBUG
|
2019-10-05 13:49:45 +02:00
|
|
|
right = process(kids.first)
|
|
|
|
CheckMaker.new(name , receiver , right)
|
|
|
|
end
|
2019-10-05 18:37:24 +02:00
|
|
|
def assign(receiver , name , kids)
|
|
|
|
name = name.to_s[0...-1].to_sym
|
|
|
|
receiver.add_slot_name(name)
|
2020-02-09 15:33:34 +01:00
|
|
|
right = process kids.shift
|
|
|
|
puts "Assign #{name} , #{receiver}" if DEBUG
|
2019-10-05 18:37:24 +02:00
|
|
|
LoadMaker.new(receiver,right)
|
|
|
|
end
|
2019-10-04 16:38:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
require_relative "named_slot"
|
|
|
|
require_relative "message_slot"
|
|
|
|
require_relative "slot_maker"
|
2019-10-05 18:37:24 +02:00
|
|
|
require_relative "load_maker"
|
2019-10-05 13:49:45 +02:00
|
|
|
require_relative "check_maker"
|
2019-10-06 18:49:53 +02:00
|
|
|
require_relative "macro_maker"
|