add subs command variant
This commit is contained in:
@@ -21,6 +21,8 @@ module RubyAlgebra
|
|||||||
result += @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
|
result += @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
|
||||||
if @operation == :diff
|
if @operation == :diff
|
||||||
result += ' := Diff(' + @operand1.to_s + @operand2.map { |v| ", #{v}"}.join + ')'
|
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
|
else
|
||||||
result += ' := ' + @operand1.to_s
|
result += ' := ' + @operand1.to_s
|
||||||
case @operation
|
case @operation
|
||||||
|
|||||||
@@ -68,6 +68,14 @@ module RubyAlgebra
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Знак равенства =
|
||||||
|
class EqualsToken < Token
|
||||||
|
def initialize
|
||||||
|
super()
|
||||||
|
@type = :equals
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Запятая
|
# Запятая
|
||||||
class CommaToken < Token
|
class CommaToken < Token
|
||||||
def initialize
|
def initialize
|
||||||
@@ -141,7 +149,7 @@ module RubyAlgebra
|
|||||||
token = IdentifierToken.new(@expr[@i...j])
|
token = IdentifierToken.new(@expr[@i...j])
|
||||||
@i = j
|
@i = j
|
||||||
return token
|
return token
|
||||||
elsif c.match(%r{[+\-*/^():,]})
|
elsif c.match(%r{[+\-*/^():,=]})
|
||||||
puts 'operator'
|
puts 'operator'
|
||||||
token = nil
|
token = nil
|
||||||
case c
|
case c
|
||||||
@@ -161,6 +169,8 @@ module RubyAlgebra
|
|||||||
token = ParenthesisToken.new(true)
|
token = ParenthesisToken.new(true)
|
||||||
when ','
|
when ','
|
||||||
token = CommaToken.new
|
token = CommaToken.new
|
||||||
|
when '='
|
||||||
|
token = EqualsToken.new
|
||||||
end
|
end
|
||||||
@i += 1
|
@i += 1
|
||||||
if c == ':' && @expr[@i] == '='
|
if c == ':' && @expr[@i] == '='
|
||||||
@@ -234,7 +244,43 @@ module RubyAlgebra
|
|||||||
end
|
end
|
||||||
return AssignmentCommand.new(left_hand_side, :diff, target, diff_variables)
|
return AssignmentCommand.new(left_hand_side, :diff, target, diff_variables)
|
||||||
elsif operand1.symbol == 'Subs'
|
elsif operand1.symbol == 'Subs'
|
||||||
raise NotImplementedError
|
tokenizer.next_token
|
||||||
|
n = tokenizer.next_token
|
||||||
|
unless n.type == :paren && n.closing == false
|
||||||
|
raise StandardError, "unexpected token #{n.type}, expected ("
|
||||||
|
end
|
||||||
|
n = tokenizer.lookahead
|
||||||
|
if n.type == :id && !n.variable?
|
||||||
|
tokenizer.next_token
|
||||||
|
target = n.symbol
|
||||||
|
else
|
||||||
|
target = parse_polynomial(tokenizer)
|
||||||
|
end
|
||||||
|
substitutions = {}
|
||||||
|
while tokenizer.lookahead.type == :comma
|
||||||
|
tokenizer.next_token
|
||||||
|
n = tokenizer.next_token
|
||||||
|
unless n.type == :id && n.variable?
|
||||||
|
raise StandardError, "unexpected token #{n.type}, expected variable"
|
||||||
|
end
|
||||||
|
variable = n.symbol
|
||||||
|
unless tokenizer.next_token.type == :equals
|
||||||
|
raise StandardError, "unexpected token #{n.type}, expected ="
|
||||||
|
end
|
||||||
|
n = tokenizer.next_token
|
||||||
|
unless n.type == :num
|
||||||
|
raise StandardError, "unexpected token #{n.type}, expected number"
|
||||||
|
end
|
||||||
|
substitutions[variable] = n.value
|
||||||
|
end
|
||||||
|
n = tokenizer.next_token
|
||||||
|
unless n.type == :paren && n.closing == true
|
||||||
|
raise StandardError, "unexpected token #{n.type}, expected )"
|
||||||
|
end
|
||||||
|
unless tokenizer.next_token.type == :end
|
||||||
|
raise StandardError, "unexpected token at the end"
|
||||||
|
end
|
||||||
|
return AssignmentCommand.new(left_hand_side, :subs, target, substitutions)
|
||||||
else
|
else
|
||||||
tokenizer.next_token
|
tokenizer.next_token
|
||||||
operator = tokenizer.next_token
|
operator = tokenizer.next_token
|
||||||
|
|||||||
Reference in New Issue
Block a user