commit 49e03746f96a0ee887a57d61e608fabc6bc9aa5b
parent 09f67e20c4e9ecdbe9c81e6793263d04d1490f20
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date: Sat, 4 Apr 2026 11:38:32 +0200
lex (organization): move stuff to header
oops... i made lex.c self-contained! Now other files can use lex.h better, I guess.
I am sure I am missing something though...
Diffstat:
| M | lex.c | | | 64 | ---------------------------------------------------------------- |
| M | lex.h | | | 36 | ++++++++++++++++++++++++++++++------ |
2 files changed, 30 insertions(+), 70 deletions(-)
diff --git a/lex.c b/lex.c
@@ -8,70 +8,6 @@
#include "lex.h"
-enum toktype {
- TOK_EOF,
-
- TOK_CLASS,
- TOK_FC,
- TOK_USE,
- TOK_AS,
-
- TOK_F32,
- TOK_F64,
- TOK_I8,
- TOK_I16,
- TOK_I32,
- TOK_I64,
- TOK_U8,
- TOK_U16,
- TOK_U32,
- TOK_U64,
-
- TOK_IDENT,
-
- TOK_DOT,
- TOK_LBRACE,
- TOK_RBRACE,
- TOK_LPAREN,
- TOK_RPAREN,
- TOK_COMMA,
- TOK_SEMI,
-
- TOK_UNKNOWN
-};
-
-typedef enum toktype toktype_t;
-
-typedef struct tok {
- toktype_t type;
- char lexeme[64];
-} tok_t;
-
-typedef struct {
- const char *name;
- toktype_t type;
-} keyword_t;
-
-static const keyword_t kws[] = {
- {"use", TOK_USE},
- {"class", TOK_CLASS},
- {"fc", TOK_FC},
- {"as", TOK_AS},
-
- {"f32", TOK_F32},
- {"f64", TOK_F64},
-
- {"i8", TOK_I8},
- {"i16", TOK_I16},
- {"i32", TOK_I32},
- {"i64", TOK_I64},
-
- {"u8", TOK_U8},
- {"u16", TOK_U16},
- {"u32", TOK_U32},
- {"u64", TOK_U64},
-};
-
static const int nkeyws = sizeof(kws) / sizeof(kws[0]);
toktype_t
diff --git a/lex.h b/lex.h
@@ -7,7 +7,7 @@
#include <stddef.h>
-typedef enum toktype {
+enum toktype {
TOK_EOF,
TOK_CLASS,
@@ -37,17 +37,41 @@ typedef enum toktype {
TOK_SEMI,
TOK_UNKNOWN
-} toktype_t;
+};
-typedef struct tok {
+struct tok {
toktype_t type;
char lexeme[64];
-} tok_t;
+};
-typedef struct {
+struct keyword {
const char *name;
toktype_t type;
-} keyword_t;
+};
+
+typedef enum toktype toktype_t;
+typedef struct tok tok_t;
+typedef struct keyword keyword_t;
+
+static const keyword_t kws[] = {
+ {"use", TOK_USE},
+ {"class", TOK_CLASS},
+ {"fc", TOK_FC},
+ {"as", TOK_AS},
+
+ {"f32", TOK_F32},
+ {"f64", TOK_F64},
+
+ {"i8", TOK_I8},
+ {"i16", TOK_I16},
+ {"i32", TOK_I32},
+ {"i64", TOK_I64},
+
+ {"u8", TOK_U8},
+ {"u16", TOK_U16},
+ {"u32", TOK_U32},
+ {"u64", TOK_U64},
+};
/* state defined in lex.c */
extern const char *src;