Class: Semdiff::AlgebraCompiler
- Inherits:
-
Prism::MutationCompiler
- Object
- Prism::MutationCompiler
- Semdiff::AlgebraCompiler
- Includes:
- Prism::DSL, CompilerUtils
- Defined in:
- lib/semdiff/algebra_compiler.rb
Overview
AlgebraCompiler is a compiler that simplifies expressions with algebraic relations based on type information.
Expression Canonical Form
b + a a + b # commutativity c * a a * c # commutativity ( a + b ) + c a + b + c # associativity ( x * b ) + ( x * c ) x * ( b + c ) # distributivity x - ( - y ) x + y # negation ( x ** m ) * ( x ** n ) x *( m + n ) # exponentials ( a - b ) - b a - 2 b # optimization
</code>
Constant Summary collapse
- COMMUTATIVE_OPS =
%i[+ *].freeze
- ASSOCIATIVE_OPS =
%i[+ *].freeze
Instance Method Summary collapse
-
#initialize(node_types) ⇒ AlgebraCompiler
constructor
A new instance of AlgebraCompiler.
- #visit_call_node(node) ⇒ Object
- #visit_parentheses_node(node) ⇒ Object
Methods included from CompilerUtils
Constructor Details
#initialize(node_types) ⇒ AlgebraCompiler
Returns a new instance of AlgebraCompiler.
25 26 27 28 |
# File 'lib/semdiff/algebra_compiler.rb', line 25 def initialize(node_types) @node_types = node_types super() end |
Instance Method Details
#visit_call_node(node) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/semdiff/algebra_compiler.rb', line 30 def visit_call_node(node) receiver = visit(node.receiver) arguments = visit(node.arguments) block = visit(node.block) result = nil if COMMUTATIVE_OPS.include?(node.name) && receiver && arguments&.arguments&.size == 1 && block.nil? if ASSOCIATIVE_OPS.include?(node.name) operands = flatten_associative_operation(node.name, receiver, arguments.arguments.first) if operands.all? { |op| numeric?(op) } sorted_operands = operands.sort_by { |op| sort_key(op) } result = rebuild_associative_operation(node, node.name, sorted_operands) end else lhs = receiver rhs = arguments.arguments.first if numeric?(lhs) && numeric?(rhs) && should_swap?(lhs, rhs) result = node.copy( receiver: rhs, arguments: arguments_node( node_id: arguments.node_id, source: arguments.send(:source), location: arguments.location, flags: arguments.send(:flags), arguments: [lhs] ) ) end end end if result CompilerUtils.inherit_newline(node, result) else node.copy(receiver: receiver, arguments: arguments, block: block) end end |
#visit_parentheses_node(node) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/semdiff/algebra_compiler.rb', line 71 def visit_parentheses_node(node) if node.multiple_statements? statements = visit_all(node.body.body) body = node.body.copy(body: statements) node.copy(body: body) else inner = visit(node.body.body.first) if inner.type == :call_node && ASSOCIATIVE_OPS.include?(inner.name) && inner.arguments&.arguments&.size == 1 && numeric?(inner.receiver) && numeric?(inner.arguments.arguments.first) inner else body = node.body.copy(body: [inner]) node.copy(body: body) end end end |