rubyx/lib/slot_language/slot_compiler.rb

128 lines
3.4 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
2020-02-09 15:33:34 +01:00
DEBUG = false
2019-10-04 16:38:51 +02: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
attr_reader :labels
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)
return check(name,receiver, kids) if SlotCompiler.checks.include?(name)
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
def on_lvar(lvar)
2020-02-09 15:33:34 +01:00
puts "lvar #{lvar}" if DEBUG
Variable.new(lvar.children.first )
end
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
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)
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)
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)
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)
case name
when :==
2020-02-13 13:09:00 +01:00
return EqualGoto.new(process(receiver) , right)
else
raise "Only ==, not #{name}" unless name == :==
end
2019-10-05 13:49:45 +02:00
end
def assign(receiver , name , kids)
receiver = process(receiver)
2020-02-09 15:33:34 +01:00
puts "Assign #{name} , #{receiver}" if DEBUG
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)
end
2019-10-04 16:38:51 +02:00
end
end
require_relative "variable"
2020-02-13 07:02:23 +01:00
require_relative "assignment"
require_relative "macro_maker"
require_relative "goto"
require_relative "equal_goto"