slash

slash is a simple type-oriented programming language
Log | Files | Refs | README | LICENSE

commit b5bd5389506726f10fac668fe4a79c6003de12eb
parent bf50a1a701cd18b1ef3ad09847171ed8312ef7e1
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date:   Sun,  5 Apr 2026 11:58:14 +0200

lex: fixes

Diffstat:
Mlex.c | 123+++++++++++++++++++++++++++++++++++++------------------------------------------
Mlex.h | 11+++++------
2 files changed, 62 insertions(+), 72 deletions(-)

diff --git a/lex.c b/lex.c @@ -14,12 +14,9 @@ static const int nkeyws = sizeof(kws) / sizeof(kws[0]); toktype_t lu_kw(const char *str) { - int i = 0; - - while (i < nkeyws) { + for (int i = 0; i < nkeyws; i++) { if (strcmp(str, kws[i].name) == 0) return kws[i].type; - i++; } return TOK_IDENT; } @@ -34,109 +31,101 @@ lex_init(void) next = fgetc(stat.i_fstream); } -char +int peek(void) { - return (char)cur; + return cur; } -char +int peek2(void) { - return (char)next; + return next; } -char +int consume(void) { int c = cur; cur = next; next = fgetc(stat.i_fstream); - return (char)c; -} - -void -s_ws(void) -{ - while (isspace(peek())) - consume(); + return c; } -void -s_lc(void) +static void +skip_ws_c(void) { - while (peek() && peek() != '\n') - consume(); -} + while (1) { + /* whitespace */ + while (isspace(peek())) + consume(); -void -s_bc(void) -{ - /* we already consumed / and * before calling this */ + /* line comment */ + if (peek() == '/' && peek2() == '/') { + consume(); /* / */ + consume(); /* / */ + while (peek() != '\n' && peek() != EOF) + consume(); + continue; + } - while (peek() != EOF) { - if (peek() == '*' && peek2() == '/') { - consume(); /* '*' */ - consume(); /* '/' */ - break; + /* block comment */ + if (peek() == '/' && peek2() == '*') { + consume(); /* / */ + consume(); /* * */ + + while (peek() != EOF) { + if (peek() == '*' && peek2() == '/') { + consume(); /* * */ + consume(); /* / */ + break; + } + consume(); + } + continue; } - consume(); + + break; } } tok_t id(void) { - tok_t t; + tok_t t = {0}; int len = 0; + int c; - while (isalnum(peek()) || peek() == '_') { - if (len < 63) - t.lexeme[len++] = consume(); + while ((c = peek()) != EOF && (isalnum(c) || c == '_')) { + if (len < (int)sizeof(t.lexeme) - 1) + t.lexeme[len++] = (char)consume(); else consume(); } t.lexeme[len] = '\0'; t.type = lu_kw(t.lexeme); + return t; } tok_t nexttok(void) { - tok_t t; - char c; + tok_t t = {0}; + int c; - s_ws(); + skip_ws_c(); - t.lexeme[0] = '\0'; c = peek(); - if (c == '\0' || c == EOF) { + /* EOF */ + if (c == EOF) { t.type = TOK_EOF; return t; } - if (c == '/') { - consume(); - - if (peek() == '/') { - consume(); - s_lc(); - return nexttok(); - } - - if (peek() == '*') { - consume(); - s_bc(); - return nexttok(); - } - - t.type = TOK_UNKNOWN; - return t; - } - + /* identifier or keyword */ if (isalpha(c) || c == '_') return id(); @@ -158,14 +147,14 @@ nexttok(void) case ',': t.type = TOK_COMMA; break; - case ';': - t.type = TOK_SEMI; - break; case '.': - t.type = TOK_DOT; + t.type = TOK_PERIOD; break; + default: t.type = TOK_UNKNOWN; + t.lexeme[0] = (char)c; + t.lexeme[1] = '\0'; break; } @@ -190,14 +179,16 @@ tokname(toktype_t t) case TOK_U64: return "U64"; case TOK_IDENT: return "IDENT"; - case TOK_DOT: return "DOT"; + + case TOK_PERIOD: return "PERIOD"; case TOK_LBRACE: return "LBRACE"; case TOK_RBRACE: return "RBRACE"; case TOK_LPAREN: return "LPAREN"; case TOK_RPAREN: return "RPAREN"; case TOK_COMMA: return "COMMA"; - case TOK_SEMI: return "SEMI"; + case TOK_EOF: return "EOF"; + default: return "UNKNOWN"; } } diff --git a/lex.h b/lex.h @@ -16,7 +16,7 @@ enum toktype { TOK_CLASS, TOK_FC, - TOK_USE, + TOK_IMPORT, TOK_AS, TOK_F32, @@ -32,13 +32,12 @@ enum toktype { TOK_IDENT, - TOK_DOT, TOK_LBRACE, TOK_RBRACE, TOK_LPAREN, TOK_RPAREN, TOK_COMMA, - TOK_SEMI, + TOK_PERIOD, TOK_UNKNOWN }; @@ -58,7 +57,7 @@ typedef struct tok tok_t; typedef struct keyword keyword_t; static const keyword_t kws[] = { - {"use", TOK_USE}, + {"import", TOK_IMPORT}, {"class", TOK_CLASS}, {"fc", TOK_FC}, {"as", TOK_AS}, @@ -83,8 +82,8 @@ extern size_t i; toktype_t lu_kw(const char *str); -char peek(void); -char consume(void); +int peek(void); +int consume(void); void s_ws(void); void s_lc(void);