replace expressions with polynomials, add interpreter command class, rewrite parser

This commit is contained in:
2026-04-13 13:23:12 +03:00
parent afb81b475a
commit a420ba3ce2
5 changed files with 343 additions and 532 deletions

View File

@@ -0,0 +1,46 @@
# frozen_string_literal: true
module RubyAlgebra
# Команда интерпретатора
class Command
end
# Команда присвоения
class AssignmentCommand < Command
attr_reader :lhs, :operation, :operand1, :operand2
def initialize(lhs, operation, operand1, operand2)
@lhs = lhs
@operation = operation
@operand1 = operand1
@operand2 = operand2
end
def to_s
result = @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
result += ' := '
result += @operand1.to_s
case @operation
when :add
result += ' + '
when :sub
result += ' - '
when :mult, :scale
result += ' * '
when :div
result += ' / '
end
result += @operand2.to_s if @operation != :assign
result
end
end
# Команда вывода на экран
class DisplayCommand < Command
attr_reader :polynomial
def initialize(polynomial)
@polynomial = polynomial
end
end
end