2018-07-19 13:46:51 +02:00
|
|
|
module Ruby
|
2019-02-10 20:02:16 +01:00
|
|
|
class ProcessError < Exception
|
|
|
|
attr_reader :node
|
|
|
|
|
|
|
|
def initialize(msg , node)
|
|
|
|
super(msg)
|
|
|
|
@node = node
|
|
|
|
end
|
|
|
|
def message
|
|
|
|
super + node_tos
|
|
|
|
end
|
|
|
|
def node_tos
|
|
|
|
return "" unless @node
|
|
|
|
@node.to_s[0 ... 200]
|
|
|
|
end
|
|
|
|
end
|
2019-02-27 08:48:21 +01:00
|
|
|
|
2018-07-19 13:46:51 +02:00
|
|
|
# This RubyCompiler compiles incoming ruby (string) into a typed
|
2019-03-05 19:30:24 +01:00
|
|
|
# version of the ast, with the help of the parser gem.
|
2018-07-19 13:46:51 +02:00
|
|
|
# The parser outputs an abstract ast (nodes)
|
2017-04-06 14:36:41 +02:00
|
|
|
# that get transformed into concrete, specific classes.
|
|
|
|
#
|
|
|
|
# As a second step, it extracts classes, methods, ivars and locals.
|
|
|
|
#
|
2018-07-19 13:46:51 +02:00
|
|
|
# The next step is then to go to the vool level, which is
|
|
|
|
# simpler, and then finally to compile
|
2017-04-06 14:36:41 +02:00
|
|
|
# to the next level down, MOM (Minimal Object Machine)
|
|
|
|
class RubyCompiler < AST::Processor
|
2019-02-10 20:02:16 +01:00
|
|
|
include AST::Sexp
|
2017-04-01 14:57:39 +02:00
|
|
|
|
|
|
|
def self.compile(input)
|
2019-03-04 18:00:47 +01:00
|
|
|
begin
|
|
|
|
ast = Parser::CurrentRuby.parse( input )
|
|
|
|
rescue => e
|
|
|
|
puts "Error parsing #{input}"
|
2019-08-25 13:40:59 +02:00
|
|
|
raise e
|
2019-03-04 18:00:47 +01:00
|
|
|
end
|
|
|
|
begin
|
|
|
|
self.new.process(ast)
|
|
|
|
rescue => e
|
|
|
|
puts "Error processing #{ast}"
|
|
|
|
raise e
|
|
|
|
end
|
2017-04-01 14:57:39 +02:00
|
|
|
end
|
|
|
|
|
2019-02-10 20:02:16 +01:00
|
|
|
# raise a ProcessError. This means ruby-x doesn't know how to handle it.
|
|
|
|
# Parser itself throws SyntaxError
|
|
|
|
def not_implemented(node)
|
|
|
|
raise ProcessError.new("Not implemented #{node.type}", node)
|
|
|
|
end
|
|
|
|
|
2017-04-02 09:43:22 +02:00
|
|
|
# default to error, so non implemented stuff shows early
|
|
|
|
def handler_missing(node)
|
2019-02-10 20:02:16 +01:00
|
|
|
not_implemented(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
|
2018-07-19 13:46:51 +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 )
|
2018-07-19 13:46:51 +02:00
|
|
|
MethodStatement.new( name , arg_array , process(body) )
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
|
2019-02-12 21:36:37 +01:00
|
|
|
def on_defs( statement )
|
|
|
|
me , name , args , body = *statement
|
|
|
|
raise "only class method implemented, not #{me.type}" unless me.type == :self
|
|
|
|
arg_array = process_all( args )
|
|
|
|
ClassMethodStatement.new( name , arg_array , process(body) )
|
|
|
|
end
|
|
|
|
|
2017-04-01 15:27:32 +02:00
|
|
|
def on_arg( arg )
|
|
|
|
arg.first
|
|
|
|
end
|
2019-08-19 17:48:13 +02:00
|
|
|
def on_optarg(arg)
|
|
|
|
arg.first
|
|
|
|
end
|
2017-04-01 15:27:32 +02:00
|
|
|
|
2018-06-26 19:28:27 +02:00
|
|
|
def on_block(block_node)
|
|
|
|
sendd = process(block_node.children[0])
|
|
|
|
args = process(block_node.children[1])
|
|
|
|
body = process(block_node.children[2])
|
2019-08-19 09:31:11 +02:00
|
|
|
RubyBlockStatement.new(sendd , args , body)
|
2018-06-26 19:28:27 +02:00
|
|
|
end
|
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
def on_yield(node)
|
|
|
|
args = process_all(node.children)
|
2018-07-19 13:46:51 +02:00
|
|
|
YieldStatement.new(args)
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
2018-06-26 19:28:27 +02:00
|
|
|
def on_args(args)
|
|
|
|
args.children.collect{|a| process(a)}
|
|
|
|
end
|
|
|
|
|
2017-04-02 09:43:22 +02:00
|
|
|
#basic Values
|
2017-04-02 11:57:05 +02:00
|
|
|
def on_self exp
|
2018-07-19 13:46:51 +02:00
|
|
|
SelfExpression.new
|
2017-04-02 11:57:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_nil expression
|
2018-07-19 13:46:51 +02:00
|
|
|
NilConstant.new
|
2017-04-02 11:57:05 +02:00
|
|
|
end
|
|
|
|
|
2017-04-02 08:44:56 +02:00
|
|
|
def on_int expression
|
2018-07-19 13:46:51 +02:00
|
|
|
IntegerConstant.new(expression.children.first)
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_float expression
|
2018-07-19 13:46:51 +02:00
|
|
|
FloatConstant.new(expression.children.first)
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_true expression
|
2018-07-19 13:46:51 +02:00
|
|
|
TrueConstant.new
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_false expression
|
2018-07-19 13:46:51 +02:00
|
|
|
FalseConstant.new
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_str expression
|
2018-07-19 13:46:51 +02:00
|
|
|
StringConstant.new(expression.children.first)
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
|
|
|
alias :on_string :on_str
|
|
|
|
|
2019-02-10 20:02:16 +01:00
|
|
|
def on_dstr( expression )
|
|
|
|
not_implemented(expression)
|
2017-04-02 09:43:22 +02:00
|
|
|
end
|
|
|
|
alias :on_xstr :on_dstr
|
|
|
|
|
|
|
|
def on_sym expression
|
2018-07-19 13:46:51 +02:00
|
|
|
SymbolConstant.new(expression.children.first)
|
2017-04-02 09:43:22 +02:00
|
|
|
end
|
|
|
|
alias :on_string :on_str
|
|
|
|
|
2019-02-10 20:02:16 +01:00
|
|
|
def on_dsym(expression)
|
|
|
|
not_implemented(expression)
|
2017-04-02 08:44:56 +02:00
|
|
|
end
|
2017-04-02 17:25:30 +02:00
|
|
|
def on_kwbegin statement
|
2018-07-19 13:46:51 +02:00
|
|
|
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
|
2018-07-19 13:46:51 +02:00
|
|
|
ArrayStatement.new expression.children.collect{ |elem| process(elem) }
|
2017-04-02 09:43:22 +02:00
|
|
|
end
|
|
|
|
|
2017-04-02 09:57:39 +02:00
|
|
|
def on_hash expression
|
2018-07-19 13:46:51 +02:00
|
|
|
hash = HashStatement.new
|
2017-04-02 09:57:39 +02:00
|
|
|
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
|
2018-07-19 13:46:51 +02:00
|
|
|
LocalVariable.new(expression.children.first)
|
2017-04-02 17:25:30 +02:00
|
|
|
end
|
2017-04-04 13:04:35 +02:00
|
|
|
|
|
|
|
def on_ivar expression
|
2018-07-19 13:46:51 +02:00
|
|
|
InstanceVariable.new(instance_name(expression.children.first))
|
2017-04-04 13:04:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_cvar expression
|
2018-07-19 13:46:51 +02:00
|
|
|
ClassVariable.new(expression.children.first.to_s[2 .. -1].to_sym)
|
2017-04-04 13:04:35 +02:00
|
|
|
end
|
|
|
|
|
2019-09-06 12:59:33 +02:00
|
|
|
# remove global parfait module scopes from consts
|
|
|
|
# Other modules _scopes_ not implemented
|
2017-04-04 17:00:21 +02:00
|
|
|
def on_const expression
|
|
|
|
scope = expression.children.first
|
|
|
|
if scope
|
2019-09-06 12:59:33 +02:00
|
|
|
unless(scope.type == :const and
|
|
|
|
scope.children.first and
|
|
|
|
scope.children.first.type == :cbase and
|
|
|
|
scope.children[1] == :Parfait)
|
|
|
|
not_implemented(expression) unless scope.type == :cbase
|
|
|
|
end
|
2017-04-04 17:00:21 +02:00
|
|
|
end
|
2018-07-19 13:46:51 +02:00
|
|
|
ModuleName.new(expression.children[1])
|
2017-04-04 17:00:21 +02:00
|
|
|
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])
|
2018-07-19 13:46:51 +02:00
|
|
|
LocalAssignment.new(name,value)
|
2017-04-02 11:57:05 +02:00
|
|
|
end
|
|
|
|
|
2017-04-04 17:10:28 +02:00
|
|
|
def on_ivasgn expression
|
|
|
|
name = expression.children[0]
|
|
|
|
value = process(expression.children[1])
|
2018-07-19 13:46:51 +02:00
|
|
|
IvarAssignment.new(instance_name(name),value)
|
2017-04-04 17:10:28 +02:00
|
|
|
end
|
|
|
|
|
2018-06-25 15:32:20 +02:00
|
|
|
def on_op_asgn(expression)
|
|
|
|
ass , op , exp = *expression
|
|
|
|
name = ass.children[0]
|
|
|
|
a_type = ass.type.to_s[0,3]
|
|
|
|
rewrite = s( a_type + "sgn" ,
|
|
|
|
name ,
|
|
|
|
s(:send , s( a_type + "r" , name ) , op , exp ) )
|
|
|
|
process(rewrite)
|
|
|
|
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)
|
2018-07-19 13:46:51 +02:00
|
|
|
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
|
2018-07-19 13:46: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)
|
2018-07-19 13:46:51 +02:00
|
|
|
IfStatement.new( process(condition) , if_true , if_false )
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
|
2018-07-19 15:22:44 +02:00
|
|
|
def on_send( statement )
|
2017-04-02 11:57:05 +02:00
|
|
|
kids = statement.children.dup
|
2018-07-19 13:46:51 +02:00
|
|
|
receiver = process(kids.shift) || SelfExpression.new
|
2017-04-02 11:57:05 +02:00
|
|
|
name = kids.shift
|
2017-04-06 15:06:51 +02:00
|
|
|
arguments = process_all(kids)
|
2018-07-19 13:46:51 +02:00
|
|
|
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] )
|
2018-07-19 13:46:51 +02:00
|
|
|
LogicalStatement.new( name , left , right)
|
2017-04-04 17:35:15 +02:00
|
|
|
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
|
2019-08-19 17:48:13 +02:00
|
|
|
SuperStatement.new([])
|
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)
|
2019-08-19 17:48:13 +02:00
|
|
|
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
|
2018-07-19 13:46:51 +02:00
|
|
|
w = Assignment.new()
|
2017-04-01 20:28:57 +02:00
|
|
|
w.name = process name
|
|
|
|
w.value = process(value)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
2019-09-06 12:59:33 +02:00
|
|
|
# Unscope stuff out of the parfait module
|
|
|
|
# less magic in requires
|
|
|
|
# Other modules still not implemented
|
|
|
|
def on_module(statement)
|
|
|
|
kids = statement.children.dup
|
|
|
|
name = kids.shift
|
|
|
|
if(name.type == :const and
|
|
|
|
name.children[1] == :Parfait)
|
|
|
|
process_all(kids)
|
|
|
|
else
|
|
|
|
not_implemented(statement)
|
|
|
|
end
|
|
|
|
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
|