Skip to content
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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 89 additions & 16 deletions compiler/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 '{':
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Copy link
Member

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 like isIdentifier, instead of a lexer's function. We should change either its behavior or its name for the consistency.

Copy link
Member Author

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().

p := l.position
if p == 0 {
return true
}

p -= 1

Choose a reason for hiding this comment

The 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'
}
Expand Down
Loading