rubyx/lib/vm/tree/to_code.rb

154 lines
3.7 KiB
Ruby
Raw Normal View History

2017-01-14 18:28:44 +01:00
module Vm
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
def on_parameters statement
params = {}
statement.children.each do |param , type , name|
type , name = *param
params[name] = type
end
params
end
def on_while_statement statement
branch_type , condition , statements = *statement
2016-12-10 02:43:44 +01:00
w = Tree::WhileStatement.new()
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()
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()
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()
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()
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 , arguments , receiver = *statement
2016-12-09 11:22:37 +01:00
w = Tree::CallSite.new()
w.name = name
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)
end
2016-12-17 19:09:17 +01:00
def on_true _expression
2016-12-09 11:13:33 +01:00
Tree::TrueExpression.new
end
2016-12-17 19:09:17 +01:00
def on_false _expression
2016-12-09 11:13:33 +01:00
Tree::FalseExpression.new
end
2016-12-17 19:09:17 +01:00
def on_nil _expression
2016-12-09 11:13:33 +01:00
Tree::NilExpression.new
end
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)
end
2017-01-15 19:04:52 +01:00
def on_string expression
2016-12-09 11:13:33 +01:00
Tree::StringExpression.new(expression.children.first)
end
def on_class_name expression
2016-12-10 02:43:44 +01:00
Tree::ClassExpression.new(expression.children.first)
end
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)
name , value = *statement
p_name = process name
p_value = process(value)
clazz.new(p_name , p_value)
end
end
end