From: momoyon Date: Sat, 17 May 2025 06:44:30 +0000 (+0500) Subject: [main.c] Introduce TK_TYPE and lex them... X-Git-Url: https://www.git.momoyon.org/?a=commitdiff_plain;h=9790a69a9aa3014b1de9b44bc0b9571e4cb5f6d3;p=lang.git [main.c] Introduce TK_TYPE and lex them... - [tests] New test types.momo and update keywords.momo. --- diff --git a/main.c b/main.c index 7c4a693..92600b1 100644 --- a/main.c +++ b/main.c @@ -130,6 +130,7 @@ void print_loc(FILE *f, Location loc); typedef enum { TK_IDENT, TK_KEYWORD, + TK_TYPE, TK_COMMENT, TK_MULTILINE_COMMENT, @@ -631,6 +632,7 @@ const char *token_type_as_str(Token_type t) { switch (t) { case TK_IDENT: return "IDENT"; case TK_KEYWORD: return "KEYWORD"; + case TK_TYPE: return "TYPE"; case TK_COMMENT: return "COMMENT"; case TK_MULTILINE_COMMENT: return "MULTILINE_COMMENT"; case TK_STRING: return "STRING"; @@ -691,7 +693,15 @@ const char *token_type_as_str(Token_type t) { } } -const char *keywords[] = { +typedef struct { + const char **items; + size_t count; + size_t capacity; +} Types; + +static Types types = {0}; + +const char *predefined_types[] = { "int", "int8", "int16", @@ -707,9 +717,13 @@ const char *keywords[] = { "float", "float32", "float64", + "char", "string", "bool", +}; + +const char *keywords[] = { "if", "else", @@ -741,6 +755,13 @@ bool is_keyword(String_view ident) { return false; } +bool is_type(String_view ident) { + for (size_t i = 0; i < types.count; ++i) { + if (sv_equals(ident, SV(types.items[i]))) return true; + } + return false; +} + // TODO: Should be differentiate hex, octal and binary here too? Token_type number_token_type(String_view number) { if (sv_contains_char(number, '.')) { @@ -1522,7 +1543,7 @@ bool next_token(Lexer *l, Token *t_out) { t_out->lexeme = ident_sv; t_out->loc = ident_loc; - t_out->type = (is_keyword(ident_sv) ? TK_KEYWORD : TK_IDENT); + t_out->type = (is_keyword(ident_sv) ? TK_KEYWORD : is_type(ident_sv) ? TK_TYPE : TK_IDENT); if (sv_equals(ident_sv, SV("true")) || sv_equals(ident_sv, SV("false"))) { t_out->type = TK_BOOL; } else if (sv_equals(ident_sv, SV("null"))) { @@ -1837,6 +1858,18 @@ typedef struct { size_t capacity; } Flags; +void init(void) { + + // Append predefined types to global types array + for (size_t i = 0; i < ARRAY_LEN(predefined_types); ++i) { + da_append(types, predefined_types[i]); + } + + // for (size_t i = 0; i < types.count; ++i) { + // log_debug("Type: %s", types.items[i]); + // } +} + int main(int argc, char **argv) { const char *program = shift_args(argv, argc); @@ -1890,6 +1923,8 @@ int main(int argc, char **argv) { return 1; } + init(); + Lexer l = make_lexer(filename); if (l.src.count == 0) { diff --git a/tests/.keywords.build.out.expected b/tests/.keywords.build.out.expected index 0d456a3..5f5eb55 100644 --- a/tests/.keywords.build.out.expected +++ b/tests/.keywords.build.out.expected @@ -1,31 +1,16 @@ -keywords.momo:1:0 [KEYWORD] 'int8' -keywords.momo:2:0 [KEYWORD] 'int16' -keywords.momo:3:0 [KEYWORD] 'int32' -keywords.momo:4:0 [KEYWORD] 'int64' -keywords.momo:6:0 [KEYWORD] 'uint' -keywords.momo:7:0 [KEYWORD] 'uint8' -keywords.momo:8:0 [KEYWORD] 'uint16' -keywords.momo:9:0 [KEYWORD] 'uint32' -keywords.momo:10:0 [KEYWORD] 'uint64' -keywords.momo:12:0 [KEYWORD] 'float' -keywords.momo:13:0 [KEYWORD] 'float32' -keywords.momo:14:0 [KEYWORD] 'float64' -keywords.momo:15:0 [KEYWORD] 'char' -keywords.momo:16:0 [KEYWORD] 'string' -keywords.momo:17:0 [KEYWORD] 'bool' -keywords.momo:19:0 [KEYWORD] 'if' -keywords.momo:20:0 [KEYWORD] 'else' -keywords.momo:22:0 [KEYWORD] 'for' -keywords.momo:23:0 [KEYWORD] 'while' -keywords.momo:25:0 [KEYWORD] 'fun' -keywords.momo:27:0 [KEYWORD] 'enum' -keywords.momo:28:0 [KEYWORD] 'struct' -keywords.momo:29:0 [KEYWORD] 'union' -keywords.momo:31:0 [KEYWORD] 'include' -keywords.momo:33:0 [KEYWORD] 'return' -keywords.momo:34:0 [KEYWORD] 'continue' -keywords.momo:35:0 [KEYWORD] 'switch' -keywords.momo:36:0 [KEYWORD] 'break' -keywords.momo:37:0 [KEYWORD] 'case' -keywords.momo:38:0 [KEYWORD] 'default' -keywords.momo:38:7 [EOF] 'EOF' +keywords.momo:1:0 [KEYWORD] 'if' +keywords.momo:2:0 [KEYWORD] 'else' +keywords.momo:4:0 [KEYWORD] 'for' +keywords.momo:5:0 [KEYWORD] 'while' +keywords.momo:7:0 [KEYWORD] 'fun' +keywords.momo:9:0 [KEYWORD] 'enum' +keywords.momo:10:0 [KEYWORD] 'struct' +keywords.momo:11:0 [KEYWORD] 'union' +keywords.momo:13:0 [KEYWORD] 'include' +keywords.momo:15:0 [KEYWORD] 'return' +keywords.momo:16:0 [KEYWORD] 'continue' +keywords.momo:17:0 [KEYWORD] 'switch' +keywords.momo:18:0 [KEYWORD] 'break' +keywords.momo:19:0 [KEYWORD] 'case' +keywords.momo:20:0 [KEYWORD] 'default' +keywords.momo:20:7 [EOF] 'EOF' diff --git a/tests/.types.build.code.expected b/tests/.types.build.code.expected new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/.types.build.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/.types.build.err.expected b/tests/.types.build.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.types.build.in.expected b/tests/.types.build.in.expected new file mode 100644 index 0000000..8aeb596 --- /dev/null +++ b/tests/.types.build.in.expected @@ -0,0 +1 @@ +dump_tokens \ No newline at end of file diff --git a/tests/.types.build.out.expected b/tests/.types.build.out.expected new file mode 100644 index 0000000..2a5be4a --- /dev/null +++ b/tests/.types.build.out.expected @@ -0,0 +1,33 @@ +types.momo:1:0 [TYPE] 'int' +types.momo:1:3 [;] ';' +types.momo:2:0 [TYPE] 'int8' +types.momo:2:4 [;] ';' +types.momo:3:0 [TYPE] 'int16' +types.momo:3:5 [;] ';' +types.momo:4:0 [TYPE] 'int32' +types.momo:4:5 [;] ';' +types.momo:5:0 [TYPE] 'int64' +types.momo:5:5 [;] ';' +types.momo:7:0 [TYPE] 'uint' +types.momo:7:4 [;] ';' +types.momo:8:0 [TYPE] 'uint8' +types.momo:8:5 [;] ';' +types.momo:9:0 [TYPE] 'uint16' +types.momo:9:6 [;] ';' +types.momo:10:0 [TYPE] 'uint32' +types.momo:10:6 [;] ';' +types.momo:11:0 [TYPE] 'uint64' +types.momo:11:6 [;] ';' +types.momo:13:0 [TYPE] 'float' +types.momo:13:5 [;] ';' +types.momo:14:0 [TYPE] 'float32' +types.momo:14:7 [;] ';' +types.momo:15:0 [TYPE] 'float64' +types.momo:15:7 [;] ';' +types.momo:17:0 [TYPE] 'char' +types.momo:17:4 [;] ';' +types.momo:18:0 [TYPE] 'string' +types.momo:18:6 [;] ';' +types.momo:19:0 [TYPE] 'bool' +types.momo:19:4 [;] ';' +types.momo:19:6 [EOF] 'EOF' diff --git a/tests/.types.code.expected b/tests/.types.code.expected new file mode 100644 index 0000000..d7d17fc --- /dev/null +++ b/tests/.types.code.expected @@ -0,0 +1 @@ +-1 \ No newline at end of file diff --git a/tests/.types.err.expected b/tests/.types.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.types.in.expected b/tests/.types.in.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.types.out.expected b/tests/.types.out.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/keywords.momo b/tests/keywords.momo index 3a3cf28..b9b0db4 100644 --- a/tests/keywords.momo +++ b/tests/keywords.momo @@ -1,21 +1,3 @@ -int8 -int16 -int32 -int64 - -uint -uint8 -uint16 -uint32 -uint64 - -float -float32 -float64 -char -string -bool - if else diff --git a/tests/types.momo b/tests/types.momo new file mode 100644 index 0000000..25a1764 --- /dev/null +++ b/tests/types.momo @@ -0,0 +1,19 @@ +int; +int8; +int16; +int32; +int64; + +uint; +uint8; +uint16; +uint32; +uint64; + +float; +float32; +float64; + +char; +string; +bool;