improve to_code a bit
variable names mainly, some description
This commit is contained in:
parent
a39fc76a12
commit
f0c0128b38
@ -5,6 +5,13 @@ module Vm
|
|||||||
compiler.process statement
|
compiler.process statement
|
||||||
end
|
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
|
class ToCode < AST::Processor
|
||||||
|
|
||||||
def handler_missing node
|
def handler_missing node
|
||||||
@ -22,21 +29,21 @@ module Vm
|
|||||||
|
|
||||||
def on_while_statement statement
|
def on_while_statement statement
|
||||||
branch_type , condition , statements = *statement
|
branch_type , condition , statements = *statement
|
||||||
w = Tree::WhileStatement.new()
|
whil = Tree::WhileStatement.new()
|
||||||
w.branch_type = branch_type
|
whil.branch_type = branch_type
|
||||||
w.condition = process(condition)
|
whil.condition = process(condition)
|
||||||
w.statements = process(statements)
|
whil.statements = process(statements)
|
||||||
w
|
whil
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_if_statement statement
|
def on_if_statement statement
|
||||||
branch_type , condition , if_true , if_false = *statement
|
branch_type , condition , if_true , if_false = *statement
|
||||||
w = Tree::IfStatement.new()
|
iff = Tree::IfStatement.new()
|
||||||
w.branch_type = branch_type
|
iff.branch_type = branch_type
|
||||||
w.condition = process(condition)
|
iff.condition = process(condition)
|
||||||
w.if_true = process(if_true)
|
iff.if_true = process(if_true)
|
||||||
w.if_false = process(if_false)
|
iff.if_false = process(if_false)
|
||||||
w
|
iff
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_first code
|
def process_first code
|
||||||
@ -47,37 +54,39 @@ module Vm
|
|||||||
alias :on_condition :process_first
|
alias :on_condition :process_first
|
||||||
alias :on_field :process_first
|
alias :on_field :process_first
|
||||||
|
|
||||||
def on_statements statement
|
def on_statements( statement )
|
||||||
w = Statements.new()
|
list = Statements.new()
|
||||||
return w unless statement.children
|
kids = statement.children
|
||||||
return w unless statement.children.first
|
return list unless kids
|
||||||
w.statements = process_all(statement.children)
|
return list unless kids.first
|
||||||
w
|
list.statements = process_all(kids)
|
||||||
|
list
|
||||||
end
|
end
|
||||||
|
|
||||||
alias :on_true_statements :on_statements
|
alias :on_true_statements :on_statements
|
||||||
alias :on_false_statements :on_statements
|
alias :on_false_statements :on_statements
|
||||||
|
|
||||||
def on_return statement
|
def on_return statement
|
||||||
w = Tree::ReturnStatement.new()
|
ret = Tree::ReturnStatement.new()
|
||||||
w.return_value = process(statement.children.first)
|
ret.return_value = process(statement.children.first)
|
||||||
w
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_operator_value statement
|
def on_operator_value statement
|
||||||
operator , left_e , right_e = *statement
|
operator , left_e , right_e = *statement
|
||||||
w = Tree::OperatorExpression.new()
|
op = Tree::OperatorExpression.new()
|
||||||
w.operator = operator
|
op.operator = operator
|
||||||
w.left_expression = process(left_e)
|
op.left_expression = process(left_e)
|
||||||
w.right_expression = process(right_e)
|
op.right_expression = process(right_e)
|
||||||
w
|
op
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_field_access statement
|
def on_field_access statement
|
||||||
receiver_ast , field_ast = *statement
|
receiver_ast , field_ast = *statement
|
||||||
w = Tree::FieldAccess.new()
|
field = Tree::FieldAccess.new()
|
||||||
w.receiver = process(receiver_ast)
|
field.receiver = process(receiver_ast)
|
||||||
w.field = process(field_ast)
|
field.field = process(field_ast)
|
||||||
w
|
field
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_receiver expression
|
def on_receiver expression
|
||||||
@ -86,11 +95,11 @@ module Vm
|
|||||||
|
|
||||||
def on_call statement
|
def on_call statement
|
||||||
name , arguments , receiver = *statement
|
name , arguments , receiver = *statement
|
||||||
w = Tree::CallSite.new()
|
call = Tree::CallSite.new()
|
||||||
w.name = name
|
call.name = name
|
||||||
w.arguments = process_all(arguments)
|
call.arguments = process_all(arguments)
|
||||||
w.receiver = process(receiver)
|
call.receiver = process(receiver)
|
||||||
w
|
call
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_int expression
|
def on_int expression
|
||||||
|
Loading…
Reference in New Issue
Block a user