Files
RubyAlgebra/lib/ruby_algebra/command.rb
Slavasil 1fff529e83 implement interpreter and demo + bug fixes
- implement interpreter and demo
- replace StandardError with appropriate subclasses
- implement division
- rewrite Polynomial.simplify()
2026-04-13 19:33:50 +03:00

73 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module RubyAlgebra
# Команда интерпретатора
class Command
attr_reader :type
def type
raise NotImplementedError
end
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 = "Assignment command: "
result += @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
if @operation == :diff
result += ' := Diff(' + @operand1.to_s + @operand2.map { |v| ", #{v}"}.join + ')'
elsif @operation == :subs
result += ' := Subs(' + @operand1.to_s + @operand2.map { |variable, value| ", #{variable}=#{value}"}.join + ')'
else
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
end
result
end
def type
:assignment
end
end
# Команда вывода на экран
class DisplayCommand < Command
attr_reader :item
def initialize(item)
@type = :display
@item = item
end
def to_s
result = "Display command: "
result += @item.to_s
result
end
def type
:display
end
end
end