replace expressions with polynomials, add interpreter command class, rewrite parser
This commit is contained in:
46
lib/ruby_algebra/command.rb
Normal file
46
lib/ruby_algebra/command.rb
Normal 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
|
||||
Reference in New Issue
Block a user