-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Fix minus literal #557
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,8 +42,9 @@ func (l *Lexer) NextToken() token.Token { | |
|
||
var tok token.Token | ||
l.resetNosymbol() | ||
|
||
l.skipWhitespace() | ||
l.reducePositiveSigns() | ||
|
||
switch l.ch { | ||
case '"', '\'': | ||
tok.Literal = l.readString(l.ch) | ||
|
@@ -63,13 +64,13 @@ func (l *Lexer) NextToken() token.Token { | |
tok = newToken(token.Assign, l.ch, l.line) | ||
} | ||
case '-': | ||
if l.peekChar() == '-' { | ||
tok.Literal = "--" | ||
tok.Line = l.line | ||
tok.Type = token.Decr | ||
l.readChar() | ||
l.readChar() | ||
return tok | ||
if isDigit(l.peekChar()) { | ||
if l.isUnary() { | ||
tok.Literal = string(l.readSignedNumber()) | ||
tok.Type = token.Int | ||
tok.Line = l.line | ||
return tok | ||
} | ||
} else if l.peekChar() == '=' { | ||
tok.Literal = "-=" | ||
tok.Line = l.line | ||
|
@@ -124,20 +125,20 @@ func (l *Lexer) NextToken() token.Token { | |
case ',': | ||
tok = newToken(token.Comma, l.ch, l.line) | ||
case '+': | ||
if l.peekChar() == '+' { | ||
tok.Literal = "++" | ||
tok.Line = l.line | ||
tok.Type = token.Incr | ||
l.readChar() | ||
l.readChar() | ||
return tok | ||
} else if l.peekChar() == '=' { | ||
if l.peekChar() == '=' { | ||
tok.Literal = "+=" | ||
tok.Line = l.line | ||
tok.Type = token.PlusEq | ||
l.readChar() | ||
l.readChar() | ||
return tok | ||
} else if l.isUnary() { | ||
if isDigit(l.peekChar()) { | ||
tok.Literal = string(l.readSignedNumber()) | ||
tok.Type = token.Int | ||
tok.Line = l.line | ||
return tok | ||
} | ||
} | ||
tok = newToken(token.Plus, l.ch, l.line) | ||
case '{': | ||
|
@@ -286,6 +287,15 @@ func (l *Lexer) readNumber() []rune { | |
return l.input[position:l.position] | ||
} | ||
|
||
func (l *Lexer) readSignedNumber() []rune { | ||
position := l.position | ||
l.readChar() | ||
for isDigit(l.ch) { | ||
l.readChar() | ||
} | ||
return l.input[position:l.position] | ||
} | ||
|
||
func (l *Lexer) readIdentifier() []rune { | ||
position := l.position | ||
for isLetter(l.ch) || isDigit(l.ch) { | ||
|
@@ -389,6 +399,69 @@ func (l *Lexer) peekChar() rune { | |
// Peek shouldn't increment positions. | ||
} | ||
|
||
func (l *Lexer) reducePositiveSigns() { | ||
if isPositiveSign(l.ch) { | ||
if l.isUnary() { | ||
if isPositiveSign(l.peekChar()) { | ||
l.readChar() | ||
for isPositiveSign(l.peekChar()) || isIdentifier(l.peekChar()) || isNegativeSign(l.peekChar()) { | ||
l.readChar() | ||
} | ||
} else if isIdentifier(l.peekChar()) { | ||
l.readChar() | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (l *Lexer) skipUnary() { | ||
if l.ch == '+' && l.isUnary() { | ||
l.readChar() | ||
} | ||
} | ||
|
||
func (l *Lexer) isUnary() bool { | ||
p := l.position | ||
if p == 0 { | ||
return true | ||
} | ||
|
||
p -= 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should replace p -= 1 with p-- |
||
switch l.input[p] { | ||
case '(', ':', ',', '[', '{', '=', '>', '<', '*', '/', '%', '+', '-', 0: | ||
return true | ||
case ' ': | ||
for l.input[p] == ' ' || p > 0 { | ||
p-- | ||
switch l.input[p] { | ||
case '(', ':', ',', '[', '{', '=', '>', '<', '*', '/', '%', '+', '-', 0: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func isIdentifier(ch rune) bool { | ||
switch ch { | ||
case '@', '(', ':', '[', '{': | ||
//case '@', '(', ':', '[', '{', '-', 0: | ||
return true | ||
default: | ||
return isLetter(ch) || isInstanceVariable(ch) | ||
} | ||
} | ||
|
||
func isPositiveSign(ch rune) bool { | ||
return ch == '+' | ||
} | ||
|
||
func isNegativeSign(ch rune) bool { | ||
return ch == '-' | ||
} | ||
|
||
func isDigit(ch rune) bool { | ||
return '0' <= ch && ch <= '9' | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a helper function that receives an
rune
argument just likeisIdentifier
, instead of alexer
's function. We should change either its behavior or its name for the consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First I thought as that. But this helper function goes backward the lexer so
*Lexer
is needed.Passing
*Lexer
via argument might be ineffective.I will change the function name to
checkUnary()
.