From 59a6657575cf9d9f2ece599dd2e43758cf03210e Mon Sep 17 00:00:00 2001 From: "Nahuel J. Sacchetti" Date: Sat, 28 Sep 2024 14:30:14 -0500 Subject: [PATCH] feat: allow words to start with numbers --- compiler/frontend.go | 7 +++---- compiler/scanner.go | 18 ++++++++++++++++-- compiler/utils.go | 4 ++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/compiler/frontend.go b/compiler/frontend.go index 5242f0b..a8fcd4d 100644 --- a/compiler/frontend.go +++ b/compiler/frontend.go @@ -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() @@ -550,8 +549,8 @@ func addExtern(token Token) { } } - code.value = value consume(TOKEN_PAREN_CLOSE, MsgParseExternMissingCloseStmt) + code.value = value emit(code) } diff --git a/compiler/scanner.go b/compiler/scanner.go index f12ae08..1685035 100644 --- a/compiler/scanner.go +++ b/compiler/scanner.go @@ -169,6 +169,9 @@ 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) @@ -176,6 +179,17 @@ func makeNumber(c byte, line string, index *int) { 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) } @@ -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) } @@ -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 } diff --git a/compiler/utils.go b/compiler/utils.go index fefb986..4237c6c 100644 --- a/compiler/utils.go +++ b/compiler/utils.go @@ -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;