2017-01-14 18:28:44 +01:00
|
|
|
module Vm
|
2016-12-08 18:38:50 +01:00
|
|
|
|
|
|
|
def self.ast_to_code statement
|
|
|
|
compiler = ToCode.new
|
|
|
|
compiler.process statement
|
|
|
|
end
|
|
|
|
|
2017-01-18 11:24:15 +01:00
|
|
|
# ToCode converts an ast (from the ast gem) into the vm code expressions
|
|
|
|
# Code is the base class of the tree that is transformed to and
|
|
|
|
# Expression and Statement the next two subclasses.
|
|
|
|
# While it is an ast, it is NOT a ruby parser generated ast. Instead the ast is generated
|
|
|
|
# with s-expressions (also from the ast gem), mostly in tests, but also a little in
|
|
|
|
# the generation of functions (Builtin)
|
|
|
|
#
|
2016-12-08 18:38:50 +01:00
|
|
|
class ToCode < AST::Processor
|
|
|
|
|
|
|
|
def handler_missing node
|
|
|
|
raise "No handler on_#{node.type}(node)"
|
|
|
|
end
|
|
|
|
|
2016-12-13 17:49:45 +01:00
|
|
|
def on_parameters statement
|
|
|
|
params = {}
|
|
|
|
statement.children.each do |param , type , name|
|
|
|
|
type , name = *param
|
|
|
|
params[name] = type
|
|
|
|
end
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2016-12-08 18:38:50 +01:00
|
|
|
def on_while_statement statement
|
|
|
|
branch_type , condition , statements = *statement
|
2017-01-18 11:24:15 +01:00
|
|
|
whil = Tree::WhileStatement.new()
|
|
|
|
whil.branch_type = branch_type
|
|
|
|
whil.condition = process(condition)
|
|
|
|
whil.statements = process(statements)
|
|
|
|
whil
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_if_statement statement
|
|
|
|
branch_type , condition , if_true , if_false = *statement
|
2017-01-18 11:24:15 +01:00
|
|
|
iff = Tree::IfStatement.new()
|
|
|
|
iff.branch_type = branch_type
|
|
|
|
iff.condition = process(condition)
|
|
|
|
iff.if_true = process(if_true)
|
|
|
|
iff.if_false = process(if_false)
|
|
|
|
iff
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_first code
|
|
|
|
raise "Too many children #{code.inspect}" if code.children.length != 1
|
|
|
|
process code.children.first
|
|
|
|
end
|
|
|
|
alias :on_conditional :process_first
|
|
|
|
alias :on_condition :process_first
|
|
|
|
alias :on_field :process_first
|
|
|
|
|
2017-01-18 11:24:15 +01:00
|
|
|
def on_statements( statement )
|
|
|
|
list = Statements.new()
|
|
|
|
kids = statement.children
|
|
|
|
return list unless kids
|
|
|
|
return list unless kids.first
|
|
|
|
list.statements = process_all(kids)
|
|
|
|
list
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
2017-01-18 11:24:15 +01:00
|
|
|
|
2016-12-08 18:38:50 +01:00
|
|
|
alias :on_true_statements :on_statements
|
|
|
|
alias :on_false_statements :on_statements
|
|
|
|
|
|
|
|
def on_return statement
|
2017-01-18 11:24:15 +01:00
|
|
|
ret = Tree::ReturnStatement.new()
|
|
|
|
ret.return_value = process(statement.children.first)
|
|
|
|
ret
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_operator_value statement
|
|
|
|
operator , left_e , right_e = *statement
|
2017-01-18 11:24:15 +01:00
|
|
|
op = Tree::OperatorExpression.new()
|
|
|
|
op.operator = operator
|
|
|
|
op.left_expression = process(left_e)
|
|
|
|
op.right_expression = process(right_e)
|
|
|
|
op
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_field_access statement
|
|
|
|
receiver_ast , field_ast = *statement
|
2017-01-18 11:24:15 +01:00
|
|
|
field = Tree::FieldAccess.new()
|
|
|
|
field.receiver = process(receiver_ast)
|
|
|
|
field.field = process(field_ast)
|
|
|
|
field
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_receiver expression
|
|
|
|
process expression.children.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_call statement
|
2017-01-16 08:33:49 +01:00
|
|
|
name , arguments , receiver = *statement
|
2017-01-18 11:24:15 +01:00
|
|
|
call = Tree::CallSite.new()
|
|
|
|
call.name = name
|
|
|
|
call.arguments = process_all(arguments)
|
|
|
|
call.receiver = process(receiver)
|
|
|
|
call
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_int expression
|
2016-12-09 11:13:33 +01:00
|
|
|
Tree::IntegerExpression.new(expression.children.first)
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
2016-12-17 19:09:17 +01:00
|
|
|
def on_true _expression
|
2016-12-09 11:13:33 +01:00
|
|
|
Tree::TrueExpression.new
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
2016-12-17 19:09:17 +01:00
|
|
|
def on_false _expression
|
2016-12-09 11:13:33 +01:00
|
|
|
Tree::FalseExpression.new
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
2016-12-17 19:09:17 +01:00
|
|
|
def on_nil _expression
|
2016-12-09 11:13:33 +01:00
|
|
|
Tree::NilExpression.new
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
2017-01-16 08:33:49 +01:00
|
|
|
def on_arg statement
|
|
|
|
Tree::ArgumentName.new(statement.children.first)
|
|
|
|
end
|
|
|
|
def on_local statement
|
|
|
|
Tree::LocalName.new(statement.children.first)
|
|
|
|
end
|
|
|
|
def on_ivar statement
|
|
|
|
Tree::InstanceName.new(statement.children.first)
|
|
|
|
end
|
|
|
|
def on_known statement
|
|
|
|
Tree::KnownName.new(statement.children.first)
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
2017-01-15 19:04:52 +01:00
|
|
|
|
2016-12-08 18:38:50 +01:00
|
|
|
def on_string expression
|
2016-12-09 11:13:33 +01:00
|
|
|
Tree::StringExpression.new(expression.children.first)
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_class_name expression
|
2016-12-10 02:43:44 +01:00
|
|
|
Tree::ClassExpression.new(expression.children.first)
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
2017-01-15 12:01:28 +01:00
|
|
|
def on_i_assignment statement
|
|
|
|
assignment_for( statement, Vm::Tree::IvarAssignment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_a_assignment statement
|
|
|
|
assignment_for( statement, Vm::Tree::ArgAssignment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_l_assignment( statement )
|
|
|
|
assignment_for( statement, Vm::Tree::LocalAssignment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assignment_for( statement , clazz)
|
2016-12-08 18:38:50 +01:00
|
|
|
name , value = *statement
|
2017-01-15 12:01:28 +01:00
|
|
|
p_name = process name
|
|
|
|
p_value = process(value)
|
|
|
|
clazz.new(p_name , p_value)
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|