From f0c0128b38f9ee494b9bec774475231430015767 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 18 Jan 2017 12:24:15 +0200 Subject: [PATCH] improve to_code a bit variable names mainly, some description --- lib/vm/tree/to_code.rb | 77 +++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/vm/tree/to_code.rb b/lib/vm/tree/to_code.rb index eb120b59..575bedbc 100644 --- a/lib/vm/tree/to_code.rb +++ b/lib/vm/tree/to_code.rb @@ -5,6 +5,13 @@ module Vm compiler.process statement end + # 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) + # class ToCode < AST::Processor def handler_missing node @@ -22,21 +29,21 @@ module Vm def on_while_statement statement branch_type , condition , statements = *statement - w = Tree::WhileStatement.new() - w.branch_type = branch_type - w.condition = process(condition) - w.statements = process(statements) - w + whil = Tree::WhileStatement.new() + whil.branch_type = branch_type + whil.condition = process(condition) + whil.statements = process(statements) + whil end def on_if_statement statement branch_type , condition , if_true , if_false = *statement - 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 + 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 end def process_first code @@ -47,37 +54,39 @@ module Vm 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 + 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 end + alias :on_true_statements :on_statements alias :on_false_statements :on_statements def on_return statement - w = Tree::ReturnStatement.new() - w.return_value = process(statement.children.first) - w + ret = Tree::ReturnStatement.new() + ret.return_value = process(statement.children.first) + ret end def on_operator_value statement operator , left_e , right_e = *statement - w = Tree::OperatorExpression.new() - w.operator = operator - w.left_expression = process(left_e) - w.right_expression = process(right_e) - w + op = Tree::OperatorExpression.new() + op.operator = operator + op.left_expression = process(left_e) + op.right_expression = process(right_e) + op end def on_field_access statement receiver_ast , field_ast = *statement - w = Tree::FieldAccess.new() - w.receiver = process(receiver_ast) - w.field = process(field_ast) - w + field = Tree::FieldAccess.new() + field.receiver = process(receiver_ast) + field.field = process(field_ast) + field end def on_receiver expression @@ -86,11 +95,11 @@ module Vm def on_call statement name , arguments , receiver = *statement - w = Tree::CallSite.new() - w.name = name - w.arguments = process_all(arguments) - w.receiver = process(receiver) - w + call = Tree::CallSite.new() + call.name = name + call.arguments = process_all(arguments) + call.receiver = process(receiver) + call end def on_int expression