optimize unary minus for constants

This commit is contained in:
2026-03-23 10:14:06 +03:00
parent daa2e4e747
commit 5cb52f7bb7

View File

@@ -183,9 +183,13 @@ module RubyAlgebra
when :positive when :positive
out_stack.push a out_stack.push a
when :negative when :negative
if a.type == :constant
out_stack.push Constant.new(-a.value)
else
out_stack.push Multiplication.new(Constant.new(-1), a) out_stack.push Multiplication.new(Constant.new(-1), a)
end end
end end
end
def self._parse_op_prio(op_token) def self._parse_op_prio(op_token)
case op_token.op case op_token.op