fix display command parser
This commit is contained in:
@@ -17,7 +17,8 @@ module RubyAlgebra
|
||||
end
|
||||
|
||||
def to_s
|
||||
result = @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
|
||||
result = "Assignment command: "
|
||||
result += @lhs.is_a?(Array) ? @lhs.join(', ') : @lhs.to_s
|
||||
result += ' := '
|
||||
result += @operand1.to_s
|
||||
case @operation
|
||||
@@ -42,5 +43,11 @@ module RubyAlgebra
|
||||
def initialize(polynomial)
|
||||
@polynomial = polynomial
|
||||
end
|
||||
|
||||
def to_s
|
||||
result = "Display command: "
|
||||
result += @polynomial.to_s
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -189,21 +189,19 @@ module RubyAlgebra
|
||||
|
||||
def self.parse_command(expr)
|
||||
tokenizer = Tokenizer.new(expr)
|
||||
|
||||
n = tokenizer.lookahead
|
||||
if n.type == :id && !n.variable?
|
||||
parse_assignment_command(tokenizer)
|
||||
left_hand_side = tokenizer.next_token.symbol
|
||||
else
|
||||
parse_display_command(tokenizer)
|
||||
return DisplayCommand.new(parse_polynomial(tokenizer))
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_assignment_command(tokenizer)
|
||||
left_hand_side = tokenizer.next_token
|
||||
n = tokenizer.next_token
|
||||
if n.type == :assign
|
||||
if tokenizer.lookahead.type == :assign
|
||||
n = tokenizer.next_token
|
||||
operand1 = tokenizer.lookahead
|
||||
unless operand1.type == :id && !operand1.variable?
|
||||
return AssignmentCommand.new(left_hand_side.symbol, :assign, parse_polynomial(tokenizer), nil)
|
||||
return AssignmentCommand.new(left_hand_side, :assign, parse_polynomial(tokenizer), nil)
|
||||
end
|
||||
tokenizer.next_token
|
||||
operator = tokenizer.next_token
|
||||
@@ -216,21 +214,22 @@ module RubyAlgebra
|
||||
end
|
||||
if operand2.type == :num
|
||||
if operator.type == :mult
|
||||
return AssignmentCommand.new(left_hand_side.symbol, :scale, operand1.symbol, operand2.value)
|
||||
return AssignmentCommand.new(left_hand_side, :scale, operand1.symbol, operand2.value)
|
||||
else
|
||||
return AssignmentCommand.new(left_hand_side.symbol, :scale, operand1.symbol, 1.0 / operand2.value)
|
||||
return AssignmentCommand.new(left_hand_side, :scale, operand1.symbol, 1.0 / operand2.value)
|
||||
end
|
||||
elsif operand2.type == :id
|
||||
case operator.type
|
||||
when :plus
|
||||
return AssignmentCommand.new(left_hand_side.symbol, :add, operand1.symbol, operand2.symbol)
|
||||
return AssignmentCommand.new(left_hand_side, :add, operand1.symbol, operand2.symbol)
|
||||
when :minus
|
||||
return AssignmentCommand.new(left_hand_side.symbol, :sub, operand1.symbol, operand2.symbol)
|
||||
return AssignmentCommand.new(left_hand_side, :sub, operand1.symbol, operand2.symbol)
|
||||
when :mult
|
||||
return AssignmentCommand.new(left_hand_side.symbol, :mult, operand1.symbol, operand2.symbol)
|
||||
return AssignmentCommand.new(left_hand_side, :mult, operand1.symbol, operand2.symbol)
|
||||
end
|
||||
end
|
||||
elsif n.type == :comma
|
||||
elsif tokenizer.lookahead.type == :comma
|
||||
n = tokenizer.next_token
|
||||
second_lhs = tokenizer.next_token
|
||||
unless second_lhs.type == :id && !second_lhs.variable?
|
||||
raise StandardError, "unexpected token #{second_lhs.type}, expected polynomial variable"
|
||||
@@ -249,16 +248,14 @@ module RubyAlgebra
|
||||
unless operand2.type == :id && !operand2.variable?
|
||||
raise StandardError, "unexpected token #{operand2.type}, expected polynomial variable"
|
||||
end
|
||||
return AssignmentCommand.new([left_hand_side.symbol, second_lhs.symbol], :div, operand1.symbol)
|
||||
return AssignmentCommand.new([left_hand_side, second_lhs.symbol], :div, operand1.symbol)
|
||||
elsif tokenizer.lookahead.type == :end
|
||||
return DisplayCommand.new(left_hand_side)
|
||||
else
|
||||
raise StandardError, "unexpected token #{n.type}, expected ':=' or ','"
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_display_command(tokenizer)
|
||||
DisplayCommand.new(parse_polynomial(tokenizer))
|
||||
end
|
||||
|
||||
def self.parse_polynomial(tokenizer)
|
||||
puts "parse_polynomial <lookahead #{tokenizer.lookahead.type}>"
|
||||
terms = [parse_term(tokenizer)]
|
||||
@@ -305,7 +302,6 @@ module RubyAlgebra
|
||||
n = tokenizer.lookahead
|
||||
tokenizer.next_token
|
||||
puts 'eat variable'
|
||||
#n = tokenizer.next_token if n.type == :mult
|
||||
if n.type == :mult
|
||||
n = tokenizer.next_token
|
||||
puts 'eat *'
|
||||
|
||||
Reference in New Issue
Block a user