Module: Rubasteme::AST::Misc
- Included in:
- Rbsiev::Evaluator
- Defined in:
- lib/rubasteme/ast/misc.rb
Overview
Miscellaneous functions to operate AST nodes.
Instance Method Summary collapse
- #bindings_specs(ast_node) ⇒ Object
-
#cond_to_if(ast_node) ⇒ Object
Converter function.
- #do_to_named_let(ast_node, loop_identifier) ⇒ Object
-
#empty_node?(obj) ⇒ Boolean
For AST::ListNode.
- #first_binding(ast_node) ⇒ Object
- #first_node(obj) ⇒ Object
-
#identifier(ast_node) ⇒ Object
For AST::IdentifierNode.
-
#last_binding?(ast_node) ⇒ Boolean
For AST::BindingsNode.
- #last_node?(obj) ⇒ Boolean
- #let_star_to_nested_lets(bindings, body) ⇒ Object
- #let_to_combination(ast_node) ⇒ Object
- #list_elements(obj) ⇒ Object
- #make_begin(nodes) ⇒ Object
- #make_bind_spec(identifier, expression) ⇒ Object
- #make_bindings(bind_spec_nodes) ⇒ Object
- #make_body(definitions, sequence) ⇒ Object
- #make_combination(operator, operands) ⇒ Object
- #make_formals(identifiers) ⇒ Object
-
#make_identifier(literal) ⇒ Object
Construcing functions.
- #make_if(predicate, consequent, alternative) ⇒ Object
- #make_lambda(formals, body) ⇒ Object
- #make_let(bindings, body) ⇒ Object
- #make_sequence(nodes) ⇒ Object
-
#parameters(ast_node) ⇒ Object
for AST::FormalsNode.
- #rest_bindings(ast_node) ⇒ Object
- #rest_nodes(obj) ⇒ Object
- #sequence_to_node(ast_node) ⇒ Object
Instance Method Details
#bindings_specs(ast_node) ⇒ Object
75 76 77 78 |
# File 'lib/rubasteme/ast/misc.rb', line 75 def bindings_specs(ast_node) check_type(:ast_bindings, ast_node) ast_node.elements end |
#cond_to_if(ast_node) ⇒ Object
Converter function.
82 83 84 85 |
# File 'lib/rubasteme/ast/misc.rb', line 82 def cond_to_if(ast_node) check_type(:ast_cond, ast_node) (ast_node.cond_clauses) end |
#do_to_named_let(ast_node, loop_identifier) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rubasteme/ast/misc.rb', line 126 def do_to_named_let(ast_node, loop_identifier) bind_specs = [] bind_specs_with_step = [] ast_node.iteration_bindings.each { |iter_spec| spec = make_bind_spec(iter_spec.identifier, iter_spec.init) if iter_spec.step bind_specs_with_step << [spec, iter_spec.step] else bind_specs << spec end } let_bindings = make_bindings(bind_specs_with_step.map{|e| e[0]}) test_and_do_result = ast_node.test_and_do_result if_predicate = test_and_do_result.test if_consequent = make_begin(rest_nodes(test_and_do_result)) sequence = [] sequence += ast_node.commands sequence << make_combination(make_identifier(loop_identifier), bind_specs_with_step.map{|e| e[1]}) if_alternative = make_begin(sequence) if_node = make_if(if_predicate, if_consequent, if_alternative) let_body = make_body(nil, make_sequence([if_node])) let_node = make_let(let_bindings, let_body) let_node.identifier = make_identifier(loop_identifier) if bind_specs.empty? let_node else let_body = make_body(nil, make_sequence([let_node])) make_let(make_bindings(bind_specs), let_body) end end |
#empty_node?(obj) ⇒ Boolean
For AST::ListNode. They also accept an Array object as their arugment.
23 24 25 26 |
# File 'lib/rubasteme/ast/misc.rb', line 23 def empty_node?(obj) check_list_type(obj) obj.empty? end |
#first_binding(ast_node) ⇒ Object
67 68 69 |
# File 'lib/rubasteme/ast/misc.rb', line 67 def first_binding(ast_node) first_node(ast_node) end |
#first_node(obj) ⇒ Object
33 34 35 36 |
# File 'lib/rubasteme/ast/misc.rb', line 33 def first_node(obj) check_list_type(obj) obj[0] end |
#identifier(ast_node) ⇒ Object
For AST::IdentifierNode.
12 13 14 15 16 17 18 |
# File 'lib/rubasteme/ast/misc.rb', line 12 def identifier(ast_node) if ast_node.respond_to?(:literal) ast_node.literal else raise Error, "wrong type node: got=%s" % ast_node.to_a.to_s end end |
#last_binding?(ast_node) ⇒ Boolean
For AST::BindingsNode
63 64 65 |
# File 'lib/rubasteme/ast/misc.rb', line 63 def last_binding?(ast_node) last_node?(list_elements(ast_node)) end |
#last_node?(obj) ⇒ Boolean
28 29 30 31 |
# File 'lib/rubasteme/ast/misc.rb', line 28 def last_node?(obj) check_list_type(obj) obj.size == 1 end |
#let_star_to_nested_lets(bindings, body) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rubasteme/ast/misc.rb', line 115 def let_star_to_nested_lets(bindings, body) single_bindings = make_bindings([first_binding(bindings)]) if last_binding?(bindings) make_let(single_bindings, body) else seq = make_sequence([let_star_to_nested_lets(rest_bindings(bindings), body)]) new_body = make_body(nil, seq) make_let(single_bindings, new_body) end end |
#let_to_combination(ast_node) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rubasteme/ast/misc.rb', line 98 def let_to_combination(ast_node) check_types([:ast_let, :ast_letrec, :ast_letrec_star], ast_node) identifiers = [] operands = [] ast_node.bindings.each { |bind_spec| identifiers << bind_spec.identifier operands << bind_spec.expression } formals = make_formals(identifiers) operator = make_lambda(formals, ast_node.body) make_combination(operator, operands) end |
#list_elements(obj) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rubasteme/ast/misc.rb', line 43 def list_elements(obj) case obj when ListNode obj.elements when Array obj else raise Error, "wrong type node: expected=(Array or ListNode), got=%s" % obj.to_a.to_s end end |
#make_begin(nodes) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rubasteme/ast/misc.rb', line 178 def make_begin(nodes) begin_node = Rubasteme::AST.instantiate(:ast_begin, nil) case nodes when Rubasteme::AST::SequenceNode begin_node.sequence = nodes when Array begin_node.sequence = make_sequence(nodes) else raise Error, "wrong type argument: expected= Array or Rubasteme::AST::SequenceNode, got=%s" % nodes.class end begin_node end |
#make_bind_spec(identifier, expression) ⇒ Object
249 250 251 252 253 254 |
# File 'lib/rubasteme/ast/misc.rb', line 249 def make_bind_spec(identifier, expression) spec_node = Rubasteme::AST.instantiate(:ast_bind_spec, nil) spec_node.identifier = identifier spec_node.expression = expression spec_node end |
#make_bindings(bind_spec_nodes) ⇒ Object
256 257 258 259 260 261 262 263 |
# File 'lib/rubasteme/ast/misc.rb', line 256 def make_bindings(bind_spec_nodes) bindings_node = Rubasteme::AST.instantiate(:ast_bindings, nil) bind_spec_nodes.each { |spec| check_type(:ast_bind_spec, spec) bindings_node.add_bind_spec(spec) } bindings_node end |
#make_body(definitions, sequence) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rubasteme/ast/misc.rb', line 191 def make_body(definitions, sequence) body_node = Rubasteme::AST.instantiate(:ast_body, nil) if definitions.nil? definitions = Rubasteme::AST.instantiate(:ast_internal_definitions, nil) end if sequence.nil? sequence = Rubasteme::ASt.instantiate(:ast_sequence, nil) end body_node.definitions = definitions body_node.sequence = sequence body_node end |
#make_combination(operator, operands) ⇒ Object
231 232 233 234 235 236 237 238 |
# File 'lib/rubasteme/ast/misc.rb', line 231 def make_combination(operator, operands) proc_call_node = Rubasteme::AST.instantiate(:ast_procedure_call, nil) proc_call_node.operator = operator operands.each { |node| proc_call_node.add_operand(node) } proc_call_node end |
#make_formals(identifiers) ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/rubasteme/ast/misc.rb', line 222 def make_formals(identifiers) formals_node = Rubasteme::AST.instantiate(:ast_formals, nil) identifiers.each { |identifier_node| check_type(:ast_identifier, identifier_node) formals_node.add_identifier(identifier_node) } formals_node end |
#make_identifier(literal) ⇒ Object
Construcing functions.
166 167 168 |
# File 'lib/rubasteme/ast/misc.rb', line 166 def make_identifier(literal) Rubasteme::AST::instantiate(:ast_identifier, literal) end |
#make_if(predicate, consequent, alternative) ⇒ Object
214 215 216 217 218 219 220 |
# File 'lib/rubasteme/ast/misc.rb', line 214 def make_if(predicate, consequent, alternative) if_node = Rubasteme::AST.instantiate(:ast_conditional, nil) if_node.test = predicate if_node.consequent = consequent if_node.alternate = alternative if alternative if_node end |
#make_lambda(formals, body) ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/rubasteme/ast/misc.rb', line 170 def make_lambda(formals, body) check_type(:ast_body, body) lambda_node = Rubasteme::AST.instantiate(:ast_lambda_expression, nil) lambda_node.formals = formals lambda_node.body = body lambda_node end |
#make_let(bindings, body) ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/rubasteme/ast/misc.rb', line 240 def make_let(bindings, body) check_type(:ast_bindings, bindings) check_type(:ast_body, body) let_node = Rubasteme::AST.instantiate(:ast_let, nil) let_node.bindings = bindings let_node.body = body let_node end |
#make_sequence(nodes) ⇒ Object
207 208 209 210 211 212 |
# File 'lib/rubasteme/ast/misc.rb', line 207 def make_sequence(nodes) check_list_type(nodes) seq_node = Rubasteme::AST.instantiate(:ast_sequence, nil) nodes.each{|exp| seq_node.add_expression(exp)} seq_node end |
#parameters(ast_node) ⇒ Object
for AST::FormalsNode
56 57 58 59 |
# File 'lib/rubasteme/ast/misc.rb', line 56 def parameters(ast_node) check_type(:ast_formals, ast_node) ast_node.map{|e| identifier(e)} end |
#rest_bindings(ast_node) ⇒ Object
71 72 73 |
# File 'lib/rubasteme/ast/misc.rb', line 71 def rest_bindings(ast_node) make_bindings(rest_nodes(ast_node)) end |
#rest_nodes(obj) ⇒ Object
38 39 40 41 |
# File 'lib/rubasteme/ast/misc.rb', line 38 def rest_nodes(obj) check_list_type(obj) obj[1..-1] end |
#sequence_to_node(ast_node) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rubasteme/ast/misc.rb', line 87 def sequence_to_node(ast_node) check_type(:ast_sequence, ast_node) if ast_node.empty? EMPTY_LIST elsif last_node?(ast_node) first_node(ast_node) else make_begin(ast_node) end end |