add diff command variant

This commit is contained in:
2026-04-13 14:57:03 +03:00
parent d8a24a1349
commit 286e79f21a
2 changed files with 78 additions and 38 deletions

View File

@@ -19,8 +19,10 @@ module RubyAlgebra
def to_s def to_s
result = "Assignment command: " result = "Assignment command: "
result += @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s result += @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
result += ' := ' if @operation == :diff
result += @operand1.to_s result += ' := Diff(' + @operand1.to_s + @operand2.map { |v| ", #{v}"}.join + ')'
else
result += ' := ' + @operand1.to_s
case @operation case @operation
when :add when :add
result += ' + ' result += ' + '
@@ -32,6 +34,7 @@ module RubyAlgebra
result += ' / ' result += ' / '
end end
result += @operand2.to_s if @operation != :assign result += @operand2.to_s if @operation != :assign
end
result result
end end
end end

View File

@@ -141,7 +141,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
@@ -159,6 +159,8 @@ module RubyAlgebra
token = ParenthesisToken.new(false) token = ParenthesisToken.new(false)
when ')' when ')'
token = ParenthesisToken.new(true) token = ParenthesisToken.new(true)
when ','
token = CommaToken.new
end end
@i += 1 @i += 1
if c == ':' && @expr[@i] == '=' if c == ':' && @expr[@i] == '='
@@ -200,9 +202,40 @@ module RubyAlgebra
if tokenizer.lookahead.type == :assign if tokenizer.lookahead.type == :assign
n = tokenizer.next_token n = tokenizer.next_token
operand1 = tokenizer.lookahead operand1 = tokenizer.lookahead
unless operand1.type == :id && !operand1.variable? if operand1.type == :id && !operand1.variable?
return AssignmentCommand.new(left_hand_side, :assign, parse_polynomial(tokenizer), nil) if operand1.symbol == 'Diff'
tokenizer.next_token
n = tokenizer.next_token
unless n.type == :paren && n.closing == false
raise StandardError, "unexpected token #{n.type}, expected ("
end end
n = tokenizer.lookahead
if n.type == :id && !n.variable?
tokenizer.next_token
target = n.symbol
else
target = parse_polynomial(tokenizer)
end
diff_variables = []
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
diff_variables << n.symbol
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, :diff, target, diff_variables)
elsif operand1.symbol == 'Subs'
raise NotImplementedError
else
tokenizer.next_token tokenizer.next_token
operator = tokenizer.next_token operator = tokenizer.next_token
raise StandardError, "unexpected token #{operator.type}, expected +, -, * or /" unless [:plus, :minus, :mult, :div].include?(operator.type) raise StandardError, "unexpected token #{operator.type}, expected +, -, * or /" unless [:plus, :minus, :mult, :div].include?(operator.type)
@@ -228,6 +261,10 @@ module RubyAlgebra
return AssignmentCommand.new(left_hand_side, :mult, operand1.symbol, operand2.symbol) return AssignmentCommand.new(left_hand_side, :mult, operand1.symbol, operand2.symbol)
end end
end end
end
else
return AssignmentCommand.new(left_hand_side, :assign, parse_polynomial(tokenizer), nil)
end
elsif tokenizer.lookahead.type == :comma elsif tokenizer.lookahead.type == :comma
n = tokenizer.next_token n = tokenizer.next_token
second_lhs = tokenizer.next_token second_lhs = tokenizer.next_token