2017-04-08 11:09:25 +02:00
|
|
|
require_relative "statement"
|
2017-04-06 18:11:11 +02:00
|
|
|
|
2017-04-01 14:57:39 +02:00
|
|
|
module Vool
|
2017-04-06 14:36:41 +02:00
|
|
|
# This RubyCompiler compiles incoming ruby (string) into vools internal representation
|
|
|
|
# with the help of the parser gem. The parser outputs an abstract ast (nodes)
|
|
|
|
# that get transformed into concrete, specific classes.
|
|
|
|
#
|
|
|
|
# As a second step, it extracts classes, methods, ivars and locals.
|
|
|
|
#
|
|
|
|
# The next step is then to normalize the code and then finally to compile
|
|
|
|
# to the next level down, MOM (Minimal Object Machine)
|
|
|
|
class RubyCompiler < AST::Processor
|
2017-04-01 14:57:39 +02:00
|
|
|
|
|
|
|
def self.compile(input)
|
|
|
|
ast = Parser::Ruby22.parse( input )
|
|
|
|
self.new.process(ast)
|
|
|
|
end
|
|
|
|
|
2017-04-02 09:43:22 +02:00
|
|
|
# default to error, so non implemented stuff shows early
|
|
|
|
def handler_missing(node)
|
2017-04-02 12:24:09 +02:00
|
|
|
raise "Not implemented #{node.type} #{node}"
|
2017-04-02 09:43:22 +02:00
|
|
|
end
|
|
|
|
|
2017-04-01 14:57:39 +02:00
|
|
|
def on_class( statement )
|
|
|
|
name , sup , body = *statement
|
2017-04-08 11:09:25 +02:00
|
|
|
ClassStatement.new( get_name(name) , get_name(sup) , process(body) )
|
2017-04-01 14:57:39 +02:00
|
|
|
end
|
|
|
|
|
2017-04-01 15:27:32 +02:00
|
|
|
def on_def( statement )
|
|
|
|
name , args , body = *statement
|
|
|
|
arg_array = process_all( args )
|
2017-04-06 13:02:18 +02:00
|
|
|
MethodStatement.new( name , arg_array , process(body) )
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_arg( arg )
|
|
|
|
arg.first
|
|
|
|
end
|
|
|
|
|
2017-04-02 09:43:22 +02:00
|
|
|
#basic Values
|
2017-04-02 11:57:05 +02:00
|
|
|
def on_self exp
|
|
|
|
SelfStatement.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_nil expression
|
|
|
|
NilStatement.new
|
|
|
|
end
|
|
|
|
|
2017-04-02 08:44:56 +02:00
|
|
|
def on_int expression
|
|
|
|
IntegerStatement.new(expression.children.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_float expression
|
|
|
|
FloatStatement.new(expression.children.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_true expression
|
|
|
|
TrueStatement.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_false expression
|
|
|
|
FalseStatement.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_str expression
|
|
|
|
StringStatement.new(expression.children.first)
|
|
|
|
end
|
|
|
|
alias :on_string :on_str
|
|
|
|
|
2017-04-02 09:43:22 +02:00
|
|
|
def on_dstr expression
|
|
|
|
raise "Not implemented dynamic strings (with interpolation)"
|
|
|
|
end
|
|
|
|
alias :on_xstr :on_dstr
|
|
|
|
|
|
|
|
def on_sym expression
|
|
|
|
SymbolStatement.new(expression.children.first)
|
|
|
|
end
|
|
|
|
alias :on_string :on_str
|
|
|
|
|
|
|
|
def on_dsym
|
|
|
|
raise "Not implemented dynamix symbols (with interpolation)"
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
2017-04-02 17:25:30 +02:00
|
|
|
def on_kwbegin statement
|
|
|
|
ScopeStatement.new process_all( statement.children )
|
2017-04-02 12:24:09 +02:00
|
|
|
end
|
2017-04-02 17:25:30 +02:00
|
|
|
alias :on_begin :on_kwbegin
|
2017-04-02 09:43:22 +02:00
|
|
|
|
|
|
|
# Array + Hashes
|
|
|
|
def on_array expression
|
|
|
|
ArrayStatement.new expression.children.collect{ |elem| process(elem) }
|
|
|
|
end
|
|
|
|
|
2017-04-02 09:57:39 +02:00
|
|
|
def on_hash expression
|
|
|
|
hash = HashStatement.new
|
|
|
|
expression.children.each do |elem|
|
|
|
|
raise "Hash error, hash contains non pair: #{elem.type}" if elem.type != :pair
|
|
|
|
hash.add( process(elem.children[0]) , process(elem.children[1]) )
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2017-04-02 11:57:05 +02:00
|
|
|
#Variables
|
2017-04-02 17:25:30 +02:00
|
|
|
def on_lvar expression
|
|
|
|
LocalVariable.new(expression.children.first)
|
|
|
|
end
|
2017-04-04 13:04:35 +02:00
|
|
|
|
|
|
|
def on_ivar expression
|
2017-04-04 17:10:28 +02:00
|
|
|
InstanceVariable.new(instance_name(expression.children.first))
|
2017-04-04 13:04:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_cvar expression
|
|
|
|
ClassVariable.new(expression.children.first.to_s[2 .. -1].to_sym)
|
|
|
|
end
|
|
|
|
|
2017-04-04 17:00:21 +02:00
|
|
|
def on_const expression
|
|
|
|
scope = expression.children.first
|
|
|
|
if scope
|
|
|
|
raise "Only unscoped Names implemented #{scope}" unless scope.type == :cbase
|
|
|
|
end
|
|
|
|
ModuleName.new(expression.children[1])
|
|
|
|
end
|
|
|
|
|
2017-04-04 17:10:28 +02:00
|
|
|
# Assignements
|
2017-04-02 11:57:05 +02:00
|
|
|
def on_lvasgn expression
|
|
|
|
name = expression.children[0]
|
|
|
|
value = process(expression.children[1])
|
|
|
|
LocalAssignment.new(name,value)
|
|
|
|
end
|
|
|
|
|
2017-04-04 17:10:28 +02:00
|
|
|
def on_ivasgn expression
|
|
|
|
name = expression.children[0]
|
|
|
|
value = process(expression.children[1])
|
2017-04-12 19:29:45 +02:00
|
|
|
IvarAssignment.new(instance_name(name),value)
|
2017-04-04 17:10:28 +02:00
|
|
|
end
|
|
|
|
|
2017-04-02 08:44:56 +02:00
|
|
|
def on_return statement
|
2017-04-04 09:42:20 +02:00
|
|
|
return_value = process(statement.children.first)
|
|
|
|
ReturnStatement.new( return_value )
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
|
|
|
|
2017-04-03 10:49:21 +02:00
|
|
|
def on_while statement
|
|
|
|
condition , statements = *statement
|
2017-04-06 15:06:51 +02:00
|
|
|
WhileStatement.new( process(condition) , process(statements))
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
|
2017-04-02 18:12:42 +02:00
|
|
|
def on_if statement
|
|
|
|
condition , if_true , if_false = *statement
|
2017-04-06 15:06:51 +02:00
|
|
|
if_true = process(if_true)
|
|
|
|
if_false = process(if_false)
|
|
|
|
IfStatement.new( process(condition) , if_true , if_false )
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
|
2017-04-02 11:57:05 +02:00
|
|
|
def on_send statement
|
|
|
|
kids = statement.children.dup
|
2017-04-06 15:06:51 +02:00
|
|
|
receiver = process(kids.shift) || SelfStatement.new
|
2017-04-02 11:57:05 +02:00
|
|
|
name = kids.shift
|
2017-04-06 15:06:51 +02:00
|
|
|
arguments = process_all(kids)
|
|
|
|
SendStatement.new( name , receiver , arguments )
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
|
2017-04-04 17:35:15 +02:00
|
|
|
def on_and expression
|
|
|
|
name = expression.type
|
|
|
|
left = process(expression.children[0])
|
|
|
|
right = process( expression.children[1] )
|
|
|
|
LogicalStatement.new( name , left , right)
|
|
|
|
end
|
|
|
|
alias :on_or :on_and
|
|
|
|
|
2017-04-02 17:42:52 +02:00
|
|
|
# this is a call to super without args (z = zero arity)
|
|
|
|
def on_zsuper exp
|
2017-04-15 19:58:39 +02:00
|
|
|
SendStatement.new( nil , SuperStatement.new , nil)
|
2017-04-02 17:42:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# this is a call to super with args and
|
|
|
|
# same name as current method, which is set later
|
|
|
|
def on_super( statement )
|
2017-04-06 15:06:51 +02:00
|
|
|
arguments = process_all(statement.children)
|
|
|
|
SendStatement.new( nil , SuperStatement.new , arguments)
|
2017-04-02 17:42:52 +02:00
|
|
|
end
|
|
|
|
|
2017-04-01 20:28:57 +02:00
|
|
|
def on_assignment statement
|
|
|
|
name , value = *statement
|
|
|
|
w = Assignment.new()
|
|
|
|
w.name = process name
|
|
|
|
w.value = process(value)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
2017-08-30 21:27:12 +02:00
|
|
|
def handler_missing(node)
|
|
|
|
raise "Handler missing #{node}"
|
|
|
|
end
|
|
|
|
|
2017-04-01 20:28:57 +02:00
|
|
|
private
|
|
|
|
|
2017-04-04 17:10:28 +02:00
|
|
|
def instance_name sym
|
|
|
|
sym.to_s[1 .. -1].to_sym
|
|
|
|
end
|
|
|
|
|
2017-04-01 14:57:39 +02:00
|
|
|
def get_name( statement )
|
|
|
|
return nil unless statement
|
|
|
|
raise "Not const #{statement}" unless statement.type == :const
|
|
|
|
name = statement.children[1]
|
|
|
|
raise "Not symbol #{name}" unless name.is_a? Symbol
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|