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