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
|
|
|
|
|
|
|
|
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
|
2016-12-10 02:43:44 +01:00
|
|
|
w = Tree::WhileStatement.new()
|
2016-12-08 18:38:50 +01:00
|
|
|
w.branch_type = branch_type
|
|
|
|
w.condition = process(condition)
|
|
|
|
w.statements = process(statements)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_if_statement statement
|
|
|
|
branch_type , condition , if_true , if_false = *statement
|
2016-12-09 12:38:07 +01:00
|
|
|
w = Tree::IfStatement.new()
|
2016-12-08 18:38:50 +01:00
|
|
|
w.branch_type = branch_type
|
|
|
|
w.condition = process(condition)
|
|
|
|
w.if_true = process(if_true)
|
|
|
|
w.if_false = process(if_false)
|
|
|
|
w
|
|
|
|
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
|
|
|
|
|
|
|
|
def on_statements statement
|
|
|
|
w = Statements.new()
|
|
|
|
return w unless statement.children
|
|
|
|
return w unless statement.children.first
|
|
|
|
w.statements = process_all(statement.children)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
alias :on_true_statements :on_statements
|
|
|
|
alias :on_false_statements :on_statements
|
|
|
|
|
|
|
|
def on_return statement
|
2016-12-09 13:19:22 +01:00
|
|
|
w = Tree::ReturnStatement.new()
|
2016-12-08 18:38:50 +01:00
|
|
|
w.return_value = process(statement.children.first)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_operator_value statement
|
|
|
|
operator , left_e , right_e = *statement
|
2016-12-09 13:17:01 +01:00
|
|
|
w = Tree::OperatorExpression.new()
|
2016-12-08 18:38:50 +01:00
|
|
|
w.operator = operator
|
|
|
|
w.left_expression = process(left_e)
|
|
|
|
w.right_expression = process(right_e)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_field_access statement
|
|
|
|
receiver_ast , field_ast = *statement
|
2016-12-10 02:43:44 +01:00
|
|
|
w = Tree::FieldAccess.new()
|
2016-12-08 18:38:50 +01:00
|
|
|
w.receiver = process(receiver_ast)
|
|
|
|
w.field = process(field_ast)
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_receiver expression
|
|
|
|
process expression.children.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_call statement
|
|
|
|
name_s , arguments , receiver = *statement
|
2016-12-09 11:22:37 +01:00
|
|
|
w = Tree::CallSite.new()
|
2016-12-08 18:38:50 +01:00
|
|
|
w.name = name_s.children.first
|
|
|
|
w.arguments = process_all(arguments)
|
|
|
|
w.receiver = process(receiver)
|
|
|
|
w
|
|
|
|
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
|
|
|
|
|
|
|
|
def on_name statement
|
2016-12-09 11:13:33 +01:00
|
|
|
Tree::NameExpression.new(statement.children.first)
|
2016-12-08 18:38:50 +01:00
|
|
|
end
|
|
|
|
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
|