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
|
|
|
|
2020-02-13 06:53:41 +01:00
|
|
|
# conditionals supported, currently only equal
|
|
|
|
def self.checks
|
|
|
|
[:==]
|
|
|
|
end
|
|
|
|
|
2019-10-04 16:38:51 +02:00
|
|
|
def self.compile(input)
|
|
|
|
ast = Parser::CurrentRuby.parse( input )
|
|
|
|
self.new.process(ast)
|
|
|
|
end
|
2020-02-13 06:53:41 +01:00
|
|
|
attr_reader :labels
|
2020-02-13 05:59:00 +01:00
|
|
|
def initialize
|
|
|
|
@labels = {}
|
|
|
|
end
|
|
|
|
|
2019-10-04 16:38:51 +02:00
|
|
|
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
|
2020-02-13 13:09:00 +01:00
|
|
|
receiver = kids.shift
|
2019-10-04 16:38:51 +02:00
|
|
|
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)
|
2020-02-13 06:53:41 +01:00
|
|
|
return check(name,receiver, kids) if SlotCompiler.checks.include?(name)
|
2019-10-05 18:37:24 +02:00
|
|
|
return assign(receiver, name , kids) if(name.to_s.end_with?("="))
|
2020-02-13 13:09:00 +01:00
|
|
|
puts "Send: #{statement} " if DEBUG
|
|
|
|
var = Variable.new( name )
|
|
|
|
if(receiver)
|
|
|
|
puts "receiver at #{name} #{receiver}" if DEBUG
|
|
|
|
process(receiver).chained(var)
|
|
|
|
else
|
|
|
|
var
|
|
|
|
end
|
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
|
2020-02-13 07:10:04 +01:00
|
|
|
Variable.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-13 13:09:00 +01:00
|
|
|
puts "i/lvasgn #{expression}" if DEBUG
|
|
|
|
var = var_for(expression.children[0])
|
2019-10-04 20:06:22 +02:00
|
|
|
value = process(expression.children[1])
|
2020-02-13 13:09:00 +01:00
|
|
|
Assignment.new(var , 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-13 13:09:00 +01:00
|
|
|
def on_ivar(expression)
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "ivar #{expression}" if DEBUG
|
2020-02-13 13:09:00 +01:00
|
|
|
var_for(expression.children.first)
|
2019-10-04 20:06:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2020-02-13 13:09:00 +01:00
|
|
|
|
|
|
|
def var_for( name )
|
|
|
|
name = name.to_s
|
|
|
|
if(name[0] == "@")
|
|
|
|
MessageVariable.new(name.to_s[1 .. -1].to_sym)
|
|
|
|
else
|
|
|
|
Variable.new(name.to_sym)
|
|
|
|
end
|
|
|
|
end
|
2019-10-04 20:06:22 +02:00
|
|
|
def label(name)
|
2020-02-13 05:59:00 +01:00
|
|
|
raise "no label #{name}" unless(name.to_s.end_with?("_label"))
|
|
|
|
if @labels.has_key?(name)
|
|
|
|
return @labels[name]
|
|
|
|
else
|
|
|
|
@labels[name] = SlotMachine::Label.new(name.to_s , name)
|
|
|
|
end
|
2019-10-04 20:06:22 +02:00
|
|
|
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)
|
2020-02-13 05:59:00 +01:00
|
|
|
Goto.new( label )
|
2019-10-05 13:49:45 +02:00
|
|
|
end
|
|
|
|
def check(name , receiver , kids)
|
|
|
|
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)
|
2020-02-13 06:53:41 +01:00
|
|
|
case name
|
|
|
|
when :==
|
2020-02-13 13:09:00 +01:00
|
|
|
return EqualGoto.new(process(receiver) , right)
|
2020-02-13 06:53:41 +01:00
|
|
|
else
|
|
|
|
raise "Only ==, not #{name}" unless name == :==
|
|
|
|
end
|
2019-10-05 13:49:45 +02:00
|
|
|
end
|
2020-02-13 06:53:41 +01:00
|
|
|
|
2019-10-05 18:37:24 +02:00
|
|
|
def assign(receiver , name , kids)
|
2020-02-13 13:45:47 +01:00
|
|
|
receiver = process(receiver)
|
2020-02-09 15:33:34 +01:00
|
|
|
puts "Assign #{name} , #{receiver}" if DEBUG
|
2020-02-13 13:45:47 +01:00
|
|
|
raise "Only one arg #{kids}" unless kids.length == 1
|
|
|
|
right = process kids.shift
|
|
|
|
name = name.to_s[0...-1].to_sym
|
|
|
|
receiver.chained(Variable.new(name))
|
2020-02-13 07:02:23 +01:00
|
|
|
Assignment.new(receiver,right)
|
2019-10-05 18:37:24 +02:00
|
|
|
end
|
2019-10-04 16:38:51 +02:00
|
|
|
end
|
|
|
|
end
|
2020-02-13 07:10:04 +01:00
|
|
|
require_relative "variable"
|
2020-02-13 07:02:23 +01:00
|
|
|
require_relative "assignment"
|
2019-10-06 18:49:53 +02:00
|
|
|
require_relative "macro_maker"
|
2020-02-13 05:59:00 +01:00
|
|
|
require_relative "goto"
|
2020-02-13 06:53:41 +01:00
|
|
|
require_relative "equal_goto"
|