Class: SbuilderSexp::AbstactExpression

Inherits:
Root show all
Defined in:
lib/parser/parser_nodes.rb

Overview

Expression as a binary tree

Instance Method Summary collapse

Methods inherited from Treetop::Runtime::SyntaxNode

#node_type, #recursive_inject, #recursive_inject_parent, #recursive_select, #value

Instance Method Details

#expression_postfixString

Returns non nil value to yield in post-order.

Returns:

  • (String)

    non nil value to yield in post-order



141
142
143
# File 'lib/parser/parser_nodes.rb', line 141

def expression_postfix
  nil
end

#expression_prefixString

override in sub classes to make ‘traverse` method to yield `node_value` to block. Block may use `node_value`, or use `node_type` to determine, how to access AST properties on `node`

Returns:

  • (String)

    non nil value to yield in pre-order



131
132
133
# File 'lib/parser/parser_nodes.rb', line 131

def expression_prefix
  nil
end

#expression_valString

Returns non nil value to yield in in-order.

Returns:

  • (String)

    non nil value to yield in in-order



136
137
138
# File 'lib/parser/parser_nodes.rb', line 136

def expression_val
  nil
end

#expressionsAbstractExpression

Returns sub expressions in a binary tree.

Returns:

  • (AbstractExpression)

    sub expressions in a binary tree



107
108
109
110
111
112
# File 'lib/parser/parser_nodes.rb', line 107

def expressions
  # expressions = recursive_select( SbuilderSexp::AbstactExpression )
  expressions = elements && elements.select { |e| e.is_a?( SbuilderSexp::AbstactExpression ) }
  # raise "Parser error - compound expression should build binary tree" if expressions && expressios.length >2
        
end

#has_rhsObject



114
115
116
# File 'lib/parser/parser_nodes.rb', line 114

def has_rhs
  expressions && expressions.length > 1
end

#lhs_nodeObject



118
119
120
# File 'lib/parser/parser_nodes.rb', line 118

def lhs_node
  expressions && expressions[0]
end

#node_valueString|Integer|etc

Returns from sub-classes with applicable value.

Returns:

  • (String|Integer|etc)

    from sub-classes with applicable value



102
103
104
# File 'lib/parser/parser_nodes.rb', line 102

def node_value
  expression_val
end

#rhs_nodeObject



122
123
124
# File 'lib/parser/parser_nodes.rb', line 122

def rhs_node
  expressions[1]
end

#traverse(memo = "", &blk) ⇒ Object

walk down binary tree for the expresssion, and yield ‘expression_prefix’, ‘expxression_val’, and ‘expression_postfix’

Parameters:

  • variable (String)

    contructed in ‘blk` during `traversal`



150
151
152
153
154
155
156
157
158
159
# File 'lib/parser/parser_nodes.rb', line 150

def traverse( memo="",  &blk )

  memo = yield memo, node_type, self, expression_prefix   if blk && expression_prefix
  memo = lhs_node.traverse( memo, &blk ) if lhs_node
  memo = yield memo, node_type, self, expression_val  if blk && expression_val
  memo = rhs_node.traverse( memo,  &blk ) if has_rhs
  memo = yield memo, node_type, self, expression_postfix   if blk && expression_postfix
  memo
  
end