Skip to content

Commit

Permalink
feat: allow words to start with numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
nawetimebomb committed Sep 28, 2024
1 parent 5bccbf9 commit 59a6657
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 3 additions & 4 deletions compiler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,8 @@ func addBind(token Token) {

func addExtern(token Token) {
var value Extern
var code Code
code.op = OP_EXTERN
code.loc = token.loc

code := Code{op: OP_EXTERN, loc: token.loc}

for !check(TOKEN_RIGHT_ARROW) && !check(TOKEN_PAREN_OPEN) && !check(TOKEN_EOF) {
advance()
Expand Down Expand Up @@ -550,8 +549,8 @@ func addExtern(token Token) {
}
}

code.value = value
consume(TOKEN_PAREN_CLOSE, MsgParseExternMissingCloseStmt)
code.value = value
emit(code)
}

Expand Down
18 changes: 16 additions & 2 deletions compiler/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,27 @@ func makeToken(t TokenType, value ...any) {
scanner.tokens = append(scanner.tokens, token)
}

// TODO: Find a better name for this function. Since I want to allow things like "2dup",
// I need this function to be able to post a WORD token if the number is followed by
// alpha characters.
func makeNumber(c byte, line string, index *int) {
result := string(c)

for Advance(&c, line, index) && IsDigit(c) {
result += string(c)
}

if !IsSpace(c) {
result += string(c)

for Advance(&c, line, index) && !IsSpace(c) {
result += string(c)
}

makeToken(TOKEN_WORD, result)
return
}

value, _ := strconv.Atoi(result)
makeToken(TOKEN_INT, value)
}
Expand Down Expand Up @@ -226,7 +240,7 @@ func makeChar(c byte, line string, index *int) {
func makeWord(c byte, line string, index *int) {
word := string(c)

for Advance(&c, line, index) && c != ' ' && c != '\t' {
for Advance(&c, line, index) && !IsSpace(c) {
word += string(c)
}

Expand Down Expand Up @@ -256,7 +270,7 @@ func TokenizeFile(f string, s string) []Token {
for index := 0; index < len(line); index++ {
c := line[index]
scanner.column = index
if (c == ' ' || c == '\t') {
if (IsSpace(c)) {
continue
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"strconv"
)

func IsSpace(c byte) bool {
return c == ' ' || c == '\t'
}

func IsDigit(c byte) bool {
if _, err := strconv.Atoi(string(c)); err == nil {
return true;
Expand Down

0 comments on commit 59a6657

Please sign in to comment.