Skip to content

Commit

Permalink
Fixed while loop bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AvicennaJr committed Dec 16, 2022
1 parent f08bb63 commit ee36e9d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func evalWhileExpression(we *ast.WhileExpression, env *object.Environment) objec
if isError(evaluated) {
return evaluated
}
if evaluated.Type() == object.BREAK_OBJ {
if evaluated != nil && evaluated.Type() == object.BREAK_OBJ {
return evaluated
}
evalWhileExpression(we, env)
Expand Down
44 changes: 39 additions & 5 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,33 @@ func (l *Lexer) skipMultiLineComment() {
}

func (l *Lexer) readString() string {
position := l.position + 1
var str string
for {
l.readChar()
if l.ch == '"' || l.ch == 0 {
break
} else if l.ch == '\\' {
switch l.peekChar() {
case 'n':
l.readChar()
l.ch = '\n'
case 'r':
l.readChar()
l.ch = '\r'
case 't':
l.readChar()
l.ch = '\t'
case '"':
l.readChar()
l.ch = '"'
case '\\':
l.readChar()
l.ch = '\\'
}
}
str += string(l.ch)
}

return l.input[position:l.position]
return str
}

func (l *Lexer) readSingleQuoteString() string {
Expand All @@ -240,8 +258,24 @@ func (l *Lexer) readSingleQuoteString() string {
l.readChar()
if l.ch == '\'' || l.ch == 0 {
break
} else if l.ch == '\\' && l.peekChar() == '\'' {
l.readChar()
} else if l.ch == '\\' {
switch l.peekChar() {
case 'n':
l.readChar()
l.ch = '\n'
case 'r':
l.readChar()
l.ch = '\r'
case 't':
l.readChar()
l.ch = '\t'
case '"':
l.readChar()
l.ch = '"'
case '\\':
l.readChar()
l.ch = '\\'
}
}
str += string(l.ch)
}
Expand Down

0 comments on commit ee36e9d

Please sign in to comment.