59 lines
1.4 KiB
Ruby
59 lines
1.4 KiB
Ruby
# 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 = "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
|
|
end
|
|
|
|
# Команда вывода на экран
|
|
class DisplayCommand < Command
|
|
attr_reader :polynomial
|
|
|
|
def initialize(polynomial)
|
|
@polynomial = polynomial
|
|
end
|
|
|
|
def to_s
|
|
result = "Display command: "
|
|
result += @polynomial.to_s
|
|
result
|
|
end
|
|
end
|
|
end
|