Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 7 additions & 36 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lexer
import (
"fmt"
"strings"
"unicode"

"github.com/skx/monkey/token"
)
Expand Down Expand Up @@ -604,49 +605,19 @@ func (l *Lexer) peekChar() rune {

// determinate ch is identifier or not
func isIdentifier(ch rune) bool {
return !isDigit(ch) && !isWhitespace(ch) && !isBrace(ch) && !isOperator(ch) && !isComparison(ch) && !isCompound(ch) && !isBrace(ch) && !isParen(ch) && !isBracket(ch) && !isEmpty(ch)

if unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '.' || ch == '?' || ch == '_' {
return true
}

return false
}

// is white space
func isWhitespace(ch rune) bool {
return ch == rune(' ') || ch == rune('\t') || ch == rune('\n') || ch == rune('\r')
}

// is operators
func isOperator(ch rune) bool {
return ch == rune('+') || ch == rune('%') || ch == rune('-') || ch == rune('/') || ch == rune('*')
}

// is comparison
func isComparison(ch rune) bool {
return ch == rune('=') || ch == rune('!') || ch == rune('>') || ch == rune('<')
}

// is compound
func isCompound(ch rune) bool {
return ch == rune(',') || ch == rune(':') || ch == rune('"') || ch == rune(';')
}

// is brace
func isBrace(ch rune) bool {
return ch == rune('{') || ch == rune('}')
}

// is bracket
func isBracket(ch rune) bool {
return ch == rune('[') || ch == rune(']')
}

// is parenthesis
func isParen(ch rune) bool {
return ch == rune('(') || ch == rune(')')
}

// is empty
func isEmpty(ch rune) bool {
return rune(0) == ch
}

// is Digit
func isDigit(ch rune) bool {
return rune('0') <= ch && ch <= rune('9')
Expand Down
8 changes: 3 additions & 5 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,13 @@ let a = "steve\t";
let a = "steve\r";
let a = "steve\\";
let a = "steve\"";
let c = 3.113£;
.;
`
input += "`/bin/ls`"
input += "/*\n"
let c = 3.113;
.;`

l := New(input)
tok := l.NextToken()
for tok.Type != token.EOF {

tok = l.NextToken()
}
}
Expand Down