From 749da24f2830ec53fb2a8ecf973abbb46a861cdc Mon Sep 17 00:00:00 2001 From: ctxcode Date: Mon, 29 Jan 2024 23:37:04 +0100 Subject: [PATCH 01/16] lexer update --- src/build/lexer.c | 23 ++++ src/build/stage-1-lex.c | 215 ++++++++++++++++++++++++++++++++++++++ src/build/stage-1-parse.c | 3 + src/headers/enums.h | 18 ++++ src/headers/functions.h | 3 + src/headers/string.h | 1 + src/string.c | 9 ++ 7 files changed, 272 insertions(+) create mode 100644 src/build/lexer.c create mode 100644 src/build/stage-1-lex.c diff --git a/src/build/lexer.c b/src/build/lexer.c new file mode 100644 index 00000000..a960edbd --- /dev/null +++ b/src/build/lexer.c @@ -0,0 +1,23 @@ + +#include "../all.h" + +char convert_backslash_char(char ch) { + if (ch == 'n') { + ch = '\n'; + } else if (ch == 'r') { + ch = '\r'; + } else if (ch == 't') { + ch = '\t'; + } else if (ch == 'f') { + ch = '\f'; + } else if (ch == 'b') { + ch = '\b'; + } else if (ch == 'v') { + ch = '\v'; + } else if (ch == 'f') { + ch = '\f'; + } else if (ch == 'a') { + ch = '\a'; + } + return ch; +} \ No newline at end of file diff --git a/src/build/stage-1-lex.c b/src/build/stage-1-lex.c new file mode 100644 index 00000000..fdde92cf --- /dev/null +++ b/src/build/stage-1-lex.c @@ -0,0 +1,215 @@ + +#include "../all.h" + +void stage_1_lex(Fc *fc) { + // + Chunk *chunk = fc->chunk; + char* content = chunk->content; + int length = chunk->length; + + int i = 0; + int o = 0; + int depth = 0; + char closer_chars[256]; + int closer_indexes[256]; + char bracket_table[128]; + bracket_table['('] = ')'; + bracket_table['['] = ']'; + bracket_table['{'] = '}'; + + + Str* tokens_str = str_make(fc->alc, length * 2 + 512); + char* tokens = tokens_str->data; + + while(true) { + const char ch = content[i]; + if(ch == '\0') + break; + i++; + // Make sure we have enough memory + if(tokens_str->mem_size - o < 512) { + tokens_str->length = o; + str_increase_memsize(tokens_str, tokens_str->mem_size * 2); + tokens = tokens_str->data; + } + if(ch == ' ' || ch == '\t') { + tokens[o++] = tok_space; + tokens[o++] = 0; + char ch = content[i]; + while(ch == ' ' || ch == '\t') { + ch = content[++i]; + } + continue; + } + if(ch == '\n') { + tokens[o++] = tok_newline; + tokens[o++] = 0; + char ch = content[i]; + while(ch <= 32) { + ch = content[++i]; + } + continue; + } + if(ch == '\r') { + continue; + } + // ID: a-zA-Z_ + if((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || ch == 95 || ch == '@') { + tokens[o++] = ch == '@' ? tok_at_word : tok_id; + tokens[o++] = ch; + // a-zA-Z0-9_ + char ch = content[i]; + while ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch == 95) { + tokens[o++] = ch; + ch = content[++i]; + } + tokens[o++] = '\0'; + continue; + } + // Number + if (ch >= 48 && ch <= 57) { + tokens[o++] = tok_number; + tokens[o++] = ch; + // a-zA-Z0-9_ + char ch = content[i]; + while (ch >= 48 && ch <= 57) { + tokens[o++] = ch; + ch = content[++i]; + } + tokens[o++] = '\0'; + continue; + } + // Comments + if(ch == '/' && content[i] == '/') { + i++; + char ch = content[i]; + while (ch != '\n' && ch != 0) { + ch = content[++i]; + } + if(ch == '\n') + i++; + continue; + } + // Strings + if(ch == '"') { + tokens[o++] = tok_string; + char ch = content[i]; + while(ch != '"') { + tokens[o++] = ch; + if(ch == '\\') { + ch = content[++i]; + tokens[o++] = ch; + } + if(ch == 0){ + sprintf(fc->sbuf, "Missing string closing tag '\"', compiler reached end of file"); + fc_error(fc); + } + ch = content[++i]; + } + tokens[o++] = '\0'; + i++; + continue; + } + if(ch == '\'') { + char ch = content[i++]; + if(ch == '\\') { + ch = content[i++]; + ch = convert_backslash_char(ch); + } + if(content[i++] != '\'') { + sprintf(fc->sbuf, "Missing character closing tag ('), found '%c'", content[i - 1]); + fc_error(fc); + } + tokens[o++] = tok_char_string; + tokens[o++] = ch; + tokens[o++] = '\0'; + continue; + } + // Scopes + if(ch == '(' || ch == '[' || ch == '{') { + tokens[o++] = tok_scope_open; + int index = o; + o += sizeof(int); + tokens[o++] = ch; + tokens[o++] = '\0'; + closer_chars[depth] = bracket_table[ch]; + closer_indexes[depth] = index; + depth++; + continue; + } + if(ch == ')' || ch == ']' || ch == '}') { + depth--; + if(depth < 0) { + sprintf(fc->sbuf, "Unexpected closing tag '%c'", ch); + fc_error(fc); + } + if(closer_chars[depth] != ch) { + sprintf(fc->sbuf, "Unexpected closing tag '%c', expected '%c'", ch, closer_chars[depth]); + fc_error(fc); + } + tokens[o++] = tok_scope_close; + tokens[o++] = ch; + tokens[o++] = '\0'; + int offset = closer_indexes[depth]; + *(int*)((intptr_t)tokens + offset) = o; + continue; + } + // Operators + bool op2 = false; + if (ch == '=' || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '!' || ch == '<' || ch == '>') { + const char ch2 = content[i]; + if(ch2 == '=') + op2 = true; + else if((ch == '+' && ch2 == '+') || (ch == '-' && ch2 == '-') || (ch == '!' && ch2 == '!') || (ch == '!' && ch2 == '?') || (ch == '>' && ch2 == '>') || (ch == '<' && ch2 == '<')) { + if(ch == '-' && content[i + 1] == '-') { + tokens[o++] = tok_op3; + tokens[o++] = ch; + tokens[o++] = ch; + tokens[o++] = ch; + tokens[o++] = '\0'; + i += 2; + continue; + } + op2 = true; + } + } else if(ch == '?') { + const char ch2 = content[i]; + if(ch2 == '?') + op2 = true; + else if(ch2 == '!') + op2 = true; + } else if(ch == '&' && content[i] == '&') { + op2 = true; + } else if(ch == '|' && content[i] == '|') { + op2 = true; + } + if(op2) { + tokens[o++] = tok_op2; + tokens[o++] = ch; + tokens[o++] = content[i]; + tokens[o++] = '\0'; + i++; + continue; + } + if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '<' || ch == '>' || ch == '%' || ch == '^' || ch == '|') { + tokens[o++] = tok_op1; + tokens[o++] = ch; + tokens[o++] = '\0'; + continue; + } + if (ch == '!' || ch == '=' || ch == '&' || ch == ':' || ch == '?' || ch == '.' || ch == '~' || ch == '#' || ch == ';' || ch == ',') { + tokens[o++] = tok_char; + tokens[o++] = ch; + tokens[o++] = '\0'; + continue; + } + + sprintf(fc->sbuf, "Unexpected token '%c'", ch); + fc_error(fc); + } + + if(depth > 0) { + sprintf(fc->sbuf, "Missing closing tag '%c'", closer_chars[depth - 1]); + fc_error(fc); + } +} \ No newline at end of file diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index cf0c66f6..c7226702 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -1,6 +1,7 @@ #include "../all.h" +void stage_1_lex(Fc *fc); void stage_1_func(Fc *fc, bool is_private); void stage_1_class(Fc *fc, bool is_struct, bool is_private); void stage_1_trait(Fc *fc, bool is_private); @@ -22,6 +23,8 @@ void stage_1(Fc *fc) { printf("# Stage 1 : Parse : %s\n", fc->path_ki); } + stage_1_lex(fc); + char *token = fc->token; while (true) { diff --git a/src/headers/enums.h b/src/headers/enums.h index ed227391..a4c68134 100644 --- a/src/headers/enums.h +++ b/src/headers/enums.h @@ -1,4 +1,22 @@ +enum TOKENS { + tok_eof, + tok_none, + tok_space, + tok_newline, + tok_id, + tok_at_word, + tok_number, + tok_string, + tok_char_string, + tok_op3, + tok_op2, + tok_op1, + tok_char, + tok_scope_open, + tok_scope_close, +}; + enum BUILD_TYPES { build_t_exe, build_t_shared_lib, diff --git a/src/headers/functions.h b/src/headers/functions.h index 6caf653a..ac03d1e0 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -176,6 +176,9 @@ void stage_3_class(Fc *fc, Class *class); // void stage_2_3_circular(Build *b, Class *class); // void stage_2_3_shared_circular_refs(Build *b, Class *class); +// Lexer +char convert_backslash_char(char ch); + // Read Chunk *chunk_init(Allocator *alc, Fc *fc); Chunk *chunk_clone(Allocator *alc, Chunk *chunk); diff --git a/src/headers/string.h b/src/headers/string.h index d52233b4..84da85cb 100644 --- a/src/headers/string.h +++ b/src/headers/string.h @@ -16,5 +16,6 @@ void str_append_chars(Str *, char *); void str_append_from_ptr(Str *str, void *ptr, int len); char *str_to_chars(Allocator *alc, Str *); void str_clear(Str *str); +void str_increase_memsize(Str *str, int new_memsize); #endif \ No newline at end of file diff --git a/src/string.c b/src/string.c index 58441021..5a87e7bc 100644 --- a/src/string.c +++ b/src/string.c @@ -51,6 +51,15 @@ void str_append_chars(Str *str, char *add) { int add_len = strlen(add); str_append_from_ptr(str, add, add_len); } + +void str_increase_memsize(Str *str, int new_memsize) { + // + void* data = al(str->alc, new_memsize); + memcpy(str->data, data, str->length); + str->data = data; + str->mem_size = new_memsize; +} + void str_append_from_ptr(Str *str, void *ptr, int len) { // if (len == 0) { From fd9c239fab895449b6cd5dd6bd4912c6774cb6d6 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 00:16:47 +0100 Subject: [PATCH 02/16] lex update --- src/build/lexer.c | 241 ++++++++++++++++++++++++++++++++++++++ src/build/read.c | 1 + src/build/stage-1-lex.c | 213 --------------------------------- src/build/stage-1-parse.c | 5 +- src/build/stage-4-1-ast.c | 8 +- src/build/value.c | 1 + src/headers/functions.h | 1 + src/headers/structs.h | 1 + 8 files changed, 252 insertions(+), 219 deletions(-) diff --git a/src/build/lexer.c b/src/build/lexer.c index a960edbd..148e7466 100644 --- a/src/build/lexer.c +++ b/src/build/lexer.c @@ -1,6 +1,247 @@ #include "../all.h" +void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { + // + char* content = chunk->content; + int length = chunk->length; + + int i = 0; + int o = 0; + int depth = 0; + char closer_chars[256]; + int closer_indexes[256]; + char bracket_table[128]; + bracket_table['('] = ')'; + bracket_table['['] = ']'; + bracket_table['{'] = '}'; + + int line = 0; + int col = 0; + int i_last = 0; + + Str* tokens_str = str_make(fc->alc, length * 2 + 512); + char* tokens = tokens_str->data; + + while(true) { + const char ch = content[i]; + if(err_i > -1 && i >= err_i) { + // Throw err + } + if(ch == '\0') + break; + i++; + col += i - i_last; + i_last = i; + // Make sure we have enough memory + if(tokens_str->mem_size - o < 512) { + tokens_str->length = o; + str_increase_memsize(tokens_str, tokens_str->mem_size * 2); + tokens = tokens_str->data; + } + if(ch == ' ' || ch == '\t') { + tokens[o++] = tok_space; + tokens[o++] = 0; + char ch = content[i]; + while(ch == ' ' || ch == '\t') { + ch = content[++i]; + } + continue; + } + if(ch == '\n') { + tokens[o++] = tok_newline; + tokens[o++] = 0; + i_last = i; + col = 0; + line++; + char ch = content[i]; + while(ch <= 32) { + if(ch == 0) + break; + ch = content[++i]; + } + continue; + } + if(ch == '\r') { + continue; + } + // ID: a-zA-Z_ + if((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || ch == 95 || ch == '@') { + tokens[o++] = ch == '@' ? tok_at_word : tok_id; + tokens[o++] = ch; + // a-zA-Z0-9_ + char ch = content[i]; + while ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch == 95) { + tokens[o++] = ch; + ch = content[++i]; + } + tokens[o++] = '\0'; + continue; + } + // Number + if (ch >= 48 && ch <= 57) { + tokens[o++] = tok_number; + tokens[o++] = ch; + // a-zA-Z0-9_ + char ch = content[i]; + while (ch >= 48 && ch <= 57) { + tokens[o++] = ch; + ch = content[++i]; + } + tokens[o++] = '\0'; + continue; + } + // Comments + if(ch == '/' && content[i] == '/') { + i++; + char ch = content[i]; + while (ch != '\n' && ch != 0) { + ch = content[++i]; + } + if(ch == '\n') + i++; + continue; + } + // Strings + if(ch == '"') { + tokens[o++] = tok_string; + char ch = content[i]; + while(ch != '"') { + tokens[o++] = ch; + if(ch == '\\') { + ch = content[++i]; + tokens[o++] = ch; + } + if(ch == 0){ + sprintf(fc->sbuf, "Missing string closing tag '\"', compiler reached end of file"); + fc_error(fc); + } + ch = content[++i]; + // Extend memory if needed + if ((o % 200 == 0) && tokens_str->mem_size - o < 512) { + tokens_str->length = o; + str_increase_memsize(tokens_str, tokens_str->mem_size * 2); + tokens = tokens_str->data; + } + } + tokens[o++] = '\0'; + i++; + continue; + } + if(ch == '\'') { + char ch = content[i++]; + if(ch == '\\') { + ch = content[i++]; + ch = convert_backslash_char(ch); + } + if(content[i++] != '\'') { + sprintf(fc->sbuf, "Missing character closing tag ('), found '%c'", content[i - 1]); + fc_error(fc); + } + tokens[o++] = tok_char_string; + tokens[o++] = ch; + tokens[o++] = '\0'; + continue; + } + // Scopes + if(ch == '(' || ch == '[' || ch == '{') { + tokens[o++] = tok_scope_open; + int index = o; + o += sizeof(int); + tokens[o++] = ch; + tokens[o++] = '\0'; + closer_chars[depth] = bracket_table[ch]; + closer_indexes[depth] = index; + depth++; + continue; + } + if(ch == ')' || ch == ']' || ch == '}') { + depth--; + if(depth < 0) { + chunk->i = i; + sprintf(fc->sbuf, "Unexpected closing tag '%c'", ch); + fc_error(fc); + } + if(closer_chars[depth] != ch) { + chunk->i = i; + sprintf(fc->sbuf, "Unexpected closing tag '%c', expected '%c'", ch, closer_chars[depth]); + fc_error(fc); + } + tokens[o++] = tok_scope_close; + tokens[o++] = ch; + tokens[o++] = '\0'; + int offset = closer_indexes[depth]; + *(int*)((intptr_t)tokens + offset) = o; + continue; + } + // Operators + bool op2 = false; + if (ch == '=' || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '!' || ch == '<' || ch == '>') { + const char ch2 = content[i]; + if(ch2 == '=') + op2 = true; + else if((ch == '+' && ch2 == '+') || (ch == '-' && ch2 == '-') || (ch == '!' && ch2 == '!') || (ch == '!' && ch2 == '?') || (ch == '>' && ch2 == '>') || (ch == '<' && ch2 == '<')) { + if(ch == '-' && content[i + 1] == '-') { + tokens[o++] = tok_op3; + tokens[o++] = ch; + tokens[o++] = ch; + tokens[o++] = ch; + tokens[o++] = '\0'; + i += 2; + continue; + } + op2 = true; + } + } else if(ch == '?') { + const char ch2 = content[i]; + if(ch2 == '?') + op2 = true; + else if(ch2 == '!') + op2 = true; + } else if(ch == '&' && content[i] == '&') { + op2 = true; + } else if(ch == '|' && content[i] == '|') { + op2 = true; + } + if(op2) { + tokens[o++] = tok_op2; + tokens[o++] = ch; + tokens[o++] = content[i]; + tokens[o++] = '\0'; + i++; + continue; + } + if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '<' || ch == '>' || ch == '%' || ch == '^' || ch == '|') { + tokens[o++] = tok_op1; + tokens[o++] = ch; + tokens[o++] = '\0'; + continue; + } + if (ch == '!' || ch == '=' || ch == '&' || ch == ':' || ch == '?' || ch == '.' || ch == '~' || ch == '#' || ch == ';' || ch == ',') { + tokens[o++] = tok_char; + tokens[o++] = ch; + tokens[o++] = '\0'; + continue; + } + + chunk->i = i; + printf("i: %d\n", i); + printf("len: %d\n", chunk->length); + sprintf(fc->sbuf, "Unexpected token '%c'", ch); + fc_error(fc); + } + + if(depth > 0) { + sprintf(fc->sbuf, "Missing closing tag '%c'", closer_chars[depth - 1]); + fc_error(fc); + } + + tokens[o++] = tok_eof; + tokens[o++] = '\0'; + + chunk->tokens= tokens; +} + char convert_backslash_char(char ch) { if (ch == 'n') { ch = '\n'; diff --git a/src/build/read.c b/src/build/read.c index bd303140..cd171599 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -6,6 +6,7 @@ Chunk *chunk_init(Allocator *alc, Fc *fc) { ch->parent = NULL; ch->fc = fc; ch->content = NULL; + ch->tokens = NULL; ch->length = 0; ch->i = 0; ch->line = 1; diff --git a/src/build/stage-1-lex.c b/src/build/stage-1-lex.c index fdde92cf..ebb224ac 100644 --- a/src/build/stage-1-lex.c +++ b/src/build/stage-1-lex.c @@ -1,215 +1,2 @@ #include "../all.h" - -void stage_1_lex(Fc *fc) { - // - Chunk *chunk = fc->chunk; - char* content = chunk->content; - int length = chunk->length; - - int i = 0; - int o = 0; - int depth = 0; - char closer_chars[256]; - int closer_indexes[256]; - char bracket_table[128]; - bracket_table['('] = ')'; - bracket_table['['] = ']'; - bracket_table['{'] = '}'; - - - Str* tokens_str = str_make(fc->alc, length * 2 + 512); - char* tokens = tokens_str->data; - - while(true) { - const char ch = content[i]; - if(ch == '\0') - break; - i++; - // Make sure we have enough memory - if(tokens_str->mem_size - o < 512) { - tokens_str->length = o; - str_increase_memsize(tokens_str, tokens_str->mem_size * 2); - tokens = tokens_str->data; - } - if(ch == ' ' || ch == '\t') { - tokens[o++] = tok_space; - tokens[o++] = 0; - char ch = content[i]; - while(ch == ' ' || ch == '\t') { - ch = content[++i]; - } - continue; - } - if(ch == '\n') { - tokens[o++] = tok_newline; - tokens[o++] = 0; - char ch = content[i]; - while(ch <= 32) { - ch = content[++i]; - } - continue; - } - if(ch == '\r') { - continue; - } - // ID: a-zA-Z_ - if((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || ch == 95 || ch == '@') { - tokens[o++] = ch == '@' ? tok_at_word : tok_id; - tokens[o++] = ch; - // a-zA-Z0-9_ - char ch = content[i]; - while ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch == 95) { - tokens[o++] = ch; - ch = content[++i]; - } - tokens[o++] = '\0'; - continue; - } - // Number - if (ch >= 48 && ch <= 57) { - tokens[o++] = tok_number; - tokens[o++] = ch; - // a-zA-Z0-9_ - char ch = content[i]; - while (ch >= 48 && ch <= 57) { - tokens[o++] = ch; - ch = content[++i]; - } - tokens[o++] = '\0'; - continue; - } - // Comments - if(ch == '/' && content[i] == '/') { - i++; - char ch = content[i]; - while (ch != '\n' && ch != 0) { - ch = content[++i]; - } - if(ch == '\n') - i++; - continue; - } - // Strings - if(ch == '"') { - tokens[o++] = tok_string; - char ch = content[i]; - while(ch != '"') { - tokens[o++] = ch; - if(ch == '\\') { - ch = content[++i]; - tokens[o++] = ch; - } - if(ch == 0){ - sprintf(fc->sbuf, "Missing string closing tag '\"', compiler reached end of file"); - fc_error(fc); - } - ch = content[++i]; - } - tokens[o++] = '\0'; - i++; - continue; - } - if(ch == '\'') { - char ch = content[i++]; - if(ch == '\\') { - ch = content[i++]; - ch = convert_backslash_char(ch); - } - if(content[i++] != '\'') { - sprintf(fc->sbuf, "Missing character closing tag ('), found '%c'", content[i - 1]); - fc_error(fc); - } - tokens[o++] = tok_char_string; - tokens[o++] = ch; - tokens[o++] = '\0'; - continue; - } - // Scopes - if(ch == '(' || ch == '[' || ch == '{') { - tokens[o++] = tok_scope_open; - int index = o; - o += sizeof(int); - tokens[o++] = ch; - tokens[o++] = '\0'; - closer_chars[depth] = bracket_table[ch]; - closer_indexes[depth] = index; - depth++; - continue; - } - if(ch == ')' || ch == ']' || ch == '}') { - depth--; - if(depth < 0) { - sprintf(fc->sbuf, "Unexpected closing tag '%c'", ch); - fc_error(fc); - } - if(closer_chars[depth] != ch) { - sprintf(fc->sbuf, "Unexpected closing tag '%c', expected '%c'", ch, closer_chars[depth]); - fc_error(fc); - } - tokens[o++] = tok_scope_close; - tokens[o++] = ch; - tokens[o++] = '\0'; - int offset = closer_indexes[depth]; - *(int*)((intptr_t)tokens + offset) = o; - continue; - } - // Operators - bool op2 = false; - if (ch == '=' || ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '!' || ch == '<' || ch == '>') { - const char ch2 = content[i]; - if(ch2 == '=') - op2 = true; - else if((ch == '+' && ch2 == '+') || (ch == '-' && ch2 == '-') || (ch == '!' && ch2 == '!') || (ch == '!' && ch2 == '?') || (ch == '>' && ch2 == '>') || (ch == '<' && ch2 == '<')) { - if(ch == '-' && content[i + 1] == '-') { - tokens[o++] = tok_op3; - tokens[o++] = ch; - tokens[o++] = ch; - tokens[o++] = ch; - tokens[o++] = '\0'; - i += 2; - continue; - } - op2 = true; - } - } else if(ch == '?') { - const char ch2 = content[i]; - if(ch2 == '?') - op2 = true; - else if(ch2 == '!') - op2 = true; - } else if(ch == '&' && content[i] == '&') { - op2 = true; - } else if(ch == '|' && content[i] == '|') { - op2 = true; - } - if(op2) { - tokens[o++] = tok_op2; - tokens[o++] = ch; - tokens[o++] = content[i]; - tokens[o++] = '\0'; - i++; - continue; - } - if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '<' || ch == '>' || ch == '%' || ch == '^' || ch == '|') { - tokens[o++] = tok_op1; - tokens[o++] = ch; - tokens[o++] = '\0'; - continue; - } - if (ch == '!' || ch == '=' || ch == '&' || ch == ':' || ch == '?' || ch == '.' || ch == '~' || ch == '#' || ch == ';' || ch == ',') { - tokens[o++] = tok_char; - tokens[o++] = ch; - tokens[o++] = '\0'; - continue; - } - - sprintf(fc->sbuf, "Unexpected token '%c'", ch); - fc_error(fc); - } - - if(depth > 0) { - sprintf(fc->sbuf, "Missing closing tag '%c'", closer_chars[depth - 1]); - fc_error(fc); - } -} \ No newline at end of file diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index c7226702..0abdd347 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -1,7 +1,6 @@ #include "../all.h" -void stage_1_lex(Fc *fc); void stage_1_func(Fc *fc, bool is_private); void stage_1_class(Fc *fc, bool is_struct, bool is_private); void stage_1_trait(Fc *fc, bool is_private); @@ -23,7 +22,7 @@ void stage_1(Fc *fc) { printf("# Stage 1 : Parse : %s\n", fc->path_ki); } - stage_1_lex(fc); + chunk_lex(fc, fc->chunk, -1); char *token = fc->token; @@ -802,9 +801,9 @@ void stage_1_test(Fc *fc) { test->expects = NULL; Chunk *chunk = chunk_init(alc, fc); - chunk->fc = fc; chunk->content = "ki__test__expect_count: u32[1], ki__test__success_count: u32[1], ki__test__fail_count: u32[1]) void {"; chunk->length = strlen(chunk->content); + chunk_lex(fc, chunk, -1); func->chunk_args = chunk; func->chunk_body = chunk_clone(fc->alc, fc->chunk); diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index 6d2680af..be55b968 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -863,6 +863,7 @@ void stage_4_1_gen_main(Fc *fc) { bool main_has_arg = mfunc && mfunc->args->length > 0; Str *code = str_make(alc, 1000); + str_append_chars(code, "{\n"); str_append_chars(code, "let arr = Array[String].new();\n"); str_append_chars(code, "let i = 0;\n"); str_append_chars(code, "while i < argc {\n"); @@ -885,18 +886,18 @@ void stage_4_1_gen_main(Fc *fc) { str_append_chars(code, ");\n"); } if (!main_has_return) - str_append_chars(code, "return 0;"); + str_append_chars(code, "return 0;\n"); } str_append_chars(code, "}\n"); chunk->content = str_to_chars(alc, code); chunk->length = code->length; - chunk->i = 0; + chunk->i = 1; chunk->line = 1; chunk->col = 1; - fc->chunk = chunk; + chunk_lex(fc, chunk, -1); read_ast(fc, scope, false); } @@ -979,6 +980,7 @@ void stage_4_1_gen_test_main(Fc *fc) { Chunk *chunk = chunk_init(alc, fc); chunk->content = str_to_chars(alc, code); chunk->length = code->length; + chunk_lex(fc, chunk, -1); func->chunk_body = chunk; } diff --git a/src/build/value.c b/src/build/value.c index 1a206394..1e1c425c 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -1204,6 +1204,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { chunk->parent = fc->chunk; chunk->content = content; chunk->length = buf->length; + chunk_lex(fc, chunk, -1); // printf(">>>%s<<<", content); fc->chunk = chunk; diff --git a/src/headers/functions.h b/src/headers/functions.h index ac03d1e0..2af86181 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -184,6 +184,7 @@ Chunk *chunk_init(Allocator *alc, Fc *fc); Chunk *chunk_clone(Allocator *alc, Chunk *chunk); void chunk_move(Chunk *chunk, int pos); void chunk_update_col(Chunk *chunk); +void chunk_lex(Fc *fc, Chunk* chunk, int err_i); void tok(Fc *fc, char *token, bool sameline, bool allow_space); void rtok(Fc *fc); void tok_expect(Fc *fc, char *expect, bool sameline, bool allow_space); diff --git a/src/headers/structs.h b/src/headers/structs.h index 330f7111..f8f0a1d0 100644 --- a/src/headers/structs.h +++ b/src/headers/structs.h @@ -226,6 +226,7 @@ struct Chunk { Fc *fc; Chunk *parent; char *content; + char *tokens; int length; int i; int line; From 35a862516b89108cf650b19b232121b1340bf1ba Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 19:32:58 +0100 Subject: [PATCH 03/16] lexer updates --- lib/src/os/thread.ki | 4 +- lib/src/type/array.ki | 26 +- lib/src/type/map.ki | 26 +- src/build/build.c | 3 +- src/build/fc.c | 32 +- src/build/lexer.c | 86 ++++- src/build/macro.c | 13 +- src/build/read.c | 273 +++++----------- src/build/skip.c | 237 ++++---------- src/build/stage-1-parse.c | 94 +++--- src/build/stage-2-types.c | 638 -------------------------------------- src/build/stage-4-1-ast.c | 4 +- src/build/value.c | 2 +- src/headers/enums.h | 2 + src/headers/functions.h | 9 +- src/headers/structs.h | 2 + src/string.c | 2 +- 17 files changed, 313 insertions(+), 1140 deletions(-) delete mode 100644 src/build/stage-2-types.c diff --git a/lib/src/os/thread.ki b/lib/src/os/thread.ki index 9ddd51e4..25cf2521 100644 --- a/lib/src/os/thread.ki +++ b/lib/src/os/thread.ki @@ -34,7 +34,7 @@ class Thread[T] { rep thr = thr ?! throw fail;; let t = CLASS{ os_handle: thr }; #else - let thr = mem:alloc(sizeof_class(pt.pthread_t)) @as pt.pthread_t; + let thr = mem:alloc(@sizeof_class(pt.pthread_t)) @as pt.pthread_t; let err = pt.pthread_create(thr, null, CLASS.entry, start_func @as ptr); if err != 0 { throw fail; @@ -65,7 +65,7 @@ class Thread[T] { rep thr = thr ?! throw fail;; let t = CLASS{ os_handle: thr }; #else - let thr = mem:alloc(sizeof_class(pt.pthread_t)) @as pt.pthread_t; + let thr = mem:alloc(@sizeof_class(pt.pthread_t)) @as pt.pthread_t; let err = pt.pthread_create(thr, null, CLASS.entry, entry_data @as ptr); if err != 0 { throw fail; diff --git a/lib/src/type/array.ki b/lib/src/type/array.ki index 72b3ddde..23d1ebbf 100644 --- a/lib/src/type/array.ki +++ b/lib/src/type/array.ki @@ -1,19 +1,19 @@ use mem; -macro array { - input { - "[]" type; - "{}" values @repeat; - } - output { - "{{\n" - "let array = Array[%type].new();\n" - "array.push(%values);\n" - "return array;\n" - "}}" - } -} +// macro array { +// input { +// "[]" type; +// "{}" values @repeat; +// } +// output { +// "{{\n" +// "let array = Array[%type].new();\n" +// "array.push(%values);\n" +// "return array;\n" +// "}}" +// } +// } class Array[T] { ~size: uxx; diff --git a/lib/src/type/map.ki b/lib/src/type/map.ki index b0fb0b73..239af4de 100644 --- a/lib/src/type/map.ki +++ b/lib/src/type/map.ki @@ -1,17 +1,17 @@ -macro map { - input { - "[]" type; - "{}" values @repeat @replace("=>", ","); - } - output { - "{{\n" - "let map = Map[%type].new();\n" - "map.set(%values);\n" - "return map;\n" - "}}" - } -} +// macro map { +// input { +// "[]" type; +// "{}" values @repeat @replace("=>", ","); +// } +// output { +// "{{\n" +// "let map = Map[%type].new();\n" +// "map.set(%values);\n" +// "return map;\n" +// "}}" +// } +// } // TODO : Use hash maps class Map[T] { diff --git a/src/build/build.c b/src/build/build.c index 6d132ee6..93a66ac9 100644 --- a/src/build/build.c +++ b/src/build/build.c @@ -360,7 +360,7 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { } // Linker stage - stage_5(b); + // stage_5(b); #ifdef WIN32 QueryPerformanceCounter(&end); @@ -382,6 +382,7 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { #endif int i = 0; while (!file_exists(b->path_out)) { + break; sleep_ms(10); i++; if (i == 100) diff --git a/src/build/fc.c b/src/build/fc.c index c5fa8a97..ca1312e0 100644 --- a/src/build/fc.c +++ b/src/build/fc.c @@ -173,25 +173,14 @@ void fc_error(Fc *fc) { build_end(b, 1); } - if (is_newline(get_char(fc, 0))) { - chunk->i--; - } - - int line = chunk->line; - int i = chunk->i; + // if (is_newline(get_char(fc, 0))) { + // chunk->i--; + // } - int col = 0; - i = chunk->i; - while (i >= 0) { - char ch = content[i]; - if (is_newline(ch)) { - i++; - break; - } - col++; - i--; - } - int start = i; + int line = -1; + int col = -1; + int i; + chunk_lex(chunk, chunk->i, &i, &line, &col); printf("\n"); Chunk *parent = chunk->parent; @@ -208,10 +197,14 @@ void fc_error(Fc *fc) { if (fc->error_func_info) { printf("Function: %s\n", fc->error_func_info->dname); } - printf("At: line:%d | col:%d\n", chunk->line, chunk->col); + printf("At: line:%d | col:%d\n", line, col); printf("Error: %s\n", fc->sbuf); printf("\n"); + int start = i - col + 1; + if(start < 0) + start = 0; + // Line 1 int c = 40; while (c > 0) { @@ -221,7 +214,6 @@ void fc_error(Fc *fc) { printf("\n"); // Code - i = chunk->i; while (i < length) { char ch = content[i]; if (is_newline(ch)) { diff --git a/src/build/lexer.c b/src/build/lexer.c index 148e7466..db80c8d5 100644 --- a/src/build/lexer.c +++ b/src/build/lexer.c @@ -1,10 +1,15 @@ #include "../all.h" -void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { +void chunk_lex_start(Chunk *chunk) { + chunk_lex(chunk, -1, NULL, NULL, NULL); +} + +void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, int *err_col) { // char* content = chunk->content; int length = chunk->length; + Fc* fc = chunk->fc; int i = 0; int o = 0; @@ -16,17 +21,25 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { bracket_table['['] = ']'; bracket_table['{'] = '}'; + int cc_depth = 0; + + char token[256]; + int token_i = 0; + int line = 0; int col = 0; int i_last = 0; - Str* tokens_str = str_make(fc->alc, length * 2 + 512); + Str* tokens_str = str_make(fc->alc, length * 3 + 1024); char* tokens = tokens_str->data; while(true) { const char ch = content[i]; - if(err_i > -1 && i >= err_i) { - // Throw err + if(err_token_i > -1 && o >= err_token_i) { + *err_content_i = i; + *err_line = line; + *err_col = col; + return; } if(ch == '\0') break; @@ -40,8 +53,10 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { tokens = tokens_str->data; } if(ch == ' ' || ch == '\t') { - tokens[o++] = tok_space; - tokens[o++] = 0; + if (o < 2 || tokens[o - 2] != tok_newline) { + tokens[o++] = tok_space; + tokens[o++] = 0; + } char ch = content[i]; while(ch == ' ' || ch == '\t') { ch = content[++i]; @@ -49,8 +64,10 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { continue; } if(ch == '\n') { - tokens[o++] = tok_newline; - tokens[o++] = 0; + if (o < 2 || tokens[o - 2] != tok_newline) { + tokens[o++] = tok_newline; + tokens[o++] = 0; + } i_last = i; col = 0; line++; @@ -65,9 +82,52 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { if(ch == '\r') { continue; } + // Compile conditions + if(ch == '#' && (o < 2 || tokens[o - 2] == tok_newline)) { + int x = i; + char ch = content[x]; + while(ch >= 97 && ch <= 122) { + token[token_i++] = ch; + ch = content[++x]; + } + token[token_i] = '\0'; + token_i = 0; + if(strcmp(token, "if") == 0) { + tokens[o++] = tok_cc; + strcpy((char*)((intptr_t)tokens + o), token); + o += 3; + cc_depth++; + i = x; + continue; + } + if(strcmp(token, "elif") == 0 || strcmp(token, "else") == 0 || strcmp(token, "end") == 0) { + tokens[o++] = tok_cc; + strcpy((char*)((intptr_t)tokens + o), token); + o += (strcmp(token, "end") == 0) ? 4 : 5; + if (strcmp(token, "end") == 0) { + cc_depth--; + if (cc_depth < 0) { + fc->chunk->i = i; + sprintf(fc->sbuf, "Using #%s without an #if before it", token); + fc_error(fc); + } + } + i = x; + continue; + } + } // ID: a-zA-Z_ if((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || ch == 95 || ch == '@') { - tokens[o++] = ch == '@' ? tok_at_word : tok_id; + if(ch == '@') { + tokens[o++] = tok_at_word; + } else { + tokens[o++] = tok_pos; + *(int*)((intptr_t)tokens + o) = line; + o += 4; + *(int*)((intptr_t)tokens + o) = col; + o += 4; + tokens[o++] = tok_id; + } tokens[o++] = ch; // a-zA-Z0-9_ char ch = content[i]; @@ -98,8 +158,6 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { while (ch != '\n' && ch != 0) { ch = content[++i]; } - if(ch == '\n') - i++; continue; } // Strings @@ -171,7 +229,7 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { tokens[o++] = ch; tokens[o++] = '\0'; int offset = closer_indexes[depth]; - *(int*)((intptr_t)tokens + offset) = o; + *(int*)(&tokens[offset]) = o; continue; } // Operators @@ -225,8 +283,6 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { } chunk->i = i; - printf("i: %d\n", i); - printf("len: %d\n", chunk->length); sprintf(fc->sbuf, "Unexpected token '%c'", ch); fc_error(fc); } @@ -239,7 +295,7 @@ void chunk_lex(Fc *fc, Chunk* chunk, int err_i) { tokens[o++] = tok_eof; tokens[o++] = '\0'; - chunk->tokens= tokens; + chunk->tokens = tokens; } char convert_backslash_char(char ch) { diff --git a/src/build/macro.c b/src/build/macro.c index f0640f3e..069b0766 100644 --- a/src/build/macro.c +++ b/src/build/macro.c @@ -279,19 +279,18 @@ char *macro_get_var(MacroScope *mc, char *key) { return NULL; } -Str *macro_replace_str_vars(Allocator *alc, Fc *fc, Str *str) { +char *macro_replace_str_vars(Allocator *alc, Fc *fc, char *str) { // - int len = str->length; + int len = strlen(str); Str *result = str_make(alc, len + 1); - char *data = str->data; for (int i = 0; i < len; i++) { - char ch = data[i]; + char ch = str[i]; if (ch == '[') { i++; char var_name[128]; int vi = 0; - while (data[i] != ']') { - var_name[vi] = data[i]; + while (str[i] != ']') { + var_name[vi] = str[i]; vi++; i++; } @@ -311,5 +310,5 @@ Str *macro_replace_str_vars(Allocator *alc, Fc *fc, Str *str) { } str_append_char(result, ch); } - return result; + return str_to_chars(alc, result); } \ No newline at end of file diff --git a/src/build/read.c b/src/build/read.c index cd171599..28e3e73e 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -11,6 +11,8 @@ Chunk *chunk_init(Allocator *alc, Fc *fc) { ch->i = 0; ch->line = 1; ch->col = 1; + ch->token = 0; + ch->scope_end_i = 0; return ch; } @@ -21,34 +23,35 @@ Chunk *chunk_clone(Allocator *alc, Chunk *chunk) { return ch; } void chunk_move(Chunk *chunk, int pos) { + tok_next(chunk, false, true, true); // - int i = chunk->i; - while (pos > 0) { - pos--; - char ch = chunk->content[chunk->i]; - chunk->i++; - chunk->col++; - if (ch == '\n') { - chunk->line++; - chunk->col = 0; - } - } - while (pos < 0) { - pos++; - char ch = chunk->content[chunk->i]; - chunk->i--; - chunk->col--; - if (ch == '\n') { - chunk->line--; - int x = chunk->i; - int col = 0; - while (x > 0 && chunk->content[x] != '\n') { - x--; - col++; - } - chunk->col = col; - } - } + // int i = chunk->i; + // while (pos > 0) { + // pos--; + // char ch = chunk->content[chunk->i]; + // chunk->i++; + // chunk->col++; + // if (ch == '\n') { + // chunk->line++; + // chunk->col = 0; + // } + // } + // while (pos < 0) { + // pos++; + // char ch = chunk->content[chunk->i]; + // chunk->i--; + // chunk->col--; + // if (ch == '\n') { + // chunk->line--; + // int x = chunk->i; + // int col = 0; + // while (x > 0 && chunk->content[x] != '\n') { + // x--; + // col++; + // } + // chunk->col = col; + // } + // } } void chunk_update_col(Chunk *chunk) { // @@ -65,182 +68,63 @@ void chunk_update_col(Chunk *chunk) { chunk->col = col; } -void tok(Fc *fc, char *token, bool sameline, bool allow_space) { +char* tok(Fc *fc, char *token, bool sameline, bool allow_space) { // Chunk *chunk = fc->chunk; *fc->chunk_prev = *chunk; + char *res = tok_next(chunk, sameline, allow_space, true); + if(token) + strcpy(token, res); + return res; +} - int i = chunk->i; - int col = chunk->col; - const char *content = chunk->content; - char ch = content[i]; - - while (ch == '\0') { - if (chunk->parent) { - chunk = chunk->parent; - fc->chunk = chunk; - *fc->chunk_prev = *chunk; - i = chunk->i; - col = chunk->col; - content = chunk->content; - ch = content[i]; - continue; - } - token[0] = '\0'; - return; +char* tok_next(Chunk* chunk, bool sameline, bool allow_space, bool update) { + int x = chunk->i; + char* res = tok_read(chunk, &x); + char t = chunk->token; + if(t == tok_space) { + if(!allow_space) + return ""; + res = tok_read(chunk, &x); + t = chunk->token; } - - if (!allow_space && is_whitespace(ch)) { - token[0] = '\0'; - return; + if(t == tok_newline) { + if(!allow_space || sameline) + return ""; + res = tok_read(chunk, &x); } - - // Skip whitespace - while (true) { - while (is_whitespace(ch)) { - // - if (is_newline(ch)) { - chunk->line++; - col = 1; - if (sameline) { - token[0] = '\0'; - chunk->i = i; - chunk->col = col; - return; - } - } - // - i++; - col++; - ch = content[i]; - if (ch == '\0') { - if (chunk->parent) { - chunk->i = i; - chunk->col = col; - chunk = chunk->parent; - fc->chunk = chunk; - *fc->chunk_prev = *chunk; - i = chunk->i; - col = chunk->col; - content = chunk->content; - ch = content[i]; - if (ch != '\0') { - continue; - } - } - token[0] = '\0'; - chunk->i = i; - chunk->col = col; - return; - } - } - // Skip comment - if (!sameline && ch == '/' && content[i + 1] == '/') { - i += 2; - col += 2; - ch = content[i]; - while (!is_newline(ch)) { - if (ch == '\0') { - token[0] = '\0'; - chunk->i = i; - chunk->col = col; - return; - } - i++; - col++; - ch = content[i]; - } - i++; - col++; - ch = content[i]; - chunk->line++; - chunk->col = 1; - } else { - break; - } + if(update) { + chunk->i = x; } + return res; +} - int pos = 1; - token[0] = ch; - i++; - col++; - - if (is_number(ch)) { - // Read number - ch = content[i]; - while (is_number(ch)) { - token[pos] = ch; - i++; - col++; - pos++; - ch = content[i]; - } - } else if (is_valid_varname_char(ch) || ch == '@') { - // Read var name - ch = content[i]; - while (is_valid_varname_char(ch)) { - token[pos] = ch; - i++; - pos++; - col++; - ch = content[i]; - } - } else { - // Special character - char nch = content[i]; - if ((nch == '=' && (ch == '+' || ch == '-' || ch == '*' || ch == '/'))) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '&' && nch == '&') || (ch == '|' && nch == '|')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '=' && nch == '=') || (ch == '!' && nch == '=') || (ch == '<' && nch == '=') || (ch == '>' && nch == '=')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '<' && nch == '<') || (ch == '>' && nch == '>')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '+' && nch == '+') || (ch == '-' && nch == '-')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '-' && nch == '>')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '?' && nch == '?') || (ch == '?' && nch == '!')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '!' && nch == '!') || (ch == '!' && nch == '?')) { - i++; - col++; - token[pos] = nch; - pos++; - } else if ((ch == '{' && nch == '{') || (ch == '}' && nch == '}')) { - i++; - col++; - token[pos] = nch; - pos++; +char* tok_read(Chunk* chunk, int *i_ref) { + // + int i = i_ref ? *i_ref : chunk->i; + char* tokens = chunk->tokens; + char t = tokens[i++]; + if(t == tok_pos) { + int line = *(int*)(&tokens[i]); + i += sizeof(int); + int col = *(int*)(&tokens[i]); + i += sizeof(int); + chunk->line = line; + chunk->col = col; + t = tokens[i++]; + } + if(t == tok_scope_open) { + chunk->scope_end_i = *(int*)(&tokens[i]); + i += sizeof(int); + } + char* tchars = (char*)(&tokens[i]); + if(t != tok_eof && i_ref) { + while(tokens[i++] != 0) { } + *i_ref = i; } - - token[pos] = '\0'; - // printf("tok: '%s'\n", token); - - chunk->i = i; - chunk->col = col; + chunk->token = t; + return tchars; } void rtok(Fc *fc) { *fc->chunk = *fc->chunk_prev; } @@ -257,7 +141,8 @@ void tok_expect(Fc *fc, char *expect, bool sameline, bool allow_space) { char get_char(Fc *fc, int index) { // Chunk *chunk = fc->chunk; - return chunk->content[chunk->i + index]; + char *res = tok_next(chunk, true, false, false); + return res[0]; } void read_hex(Fc *fc, char *token) { diff --git a/src/build/skip.c b/src/build/skip.c index bdf69e32..d9db4451 100644 --- a/src/build/skip.c +++ b/src/build/skip.c @@ -2,46 +2,8 @@ void skip_body(Fc *fc, char until_ch) { // - int depth = 0; - if (until_ch == '}' || until_ch == ')' || until_ch == ']') - depth = 1; - char *token = fc->token; - while (true) { - // - tok(fc, token, false, true); - - if (token[0] == 0) - break; - - char ch = token[0]; - - if (ch == '"' || ch == '\'') { - skip_string(fc, ch); - continue; - } - - if (ch == '/' && get_char(fc, 0) == '/') { - skip_until_char(fc, "\n"); - continue; - } - - if (ch == '{' || ch == '(' || ch == '[') { - depth++; - continue; - } - if (ch == '}' || ch == ')' || ch == ']') { - depth--; - if (depth == 0 && until_ch == ch) - break; - if (depth < 0) - break; - continue; - } - } - if (depth != 0) { - sprintf(fc->sbuf, "Unexpected end of code, missing bracket somewhere"); - fc_error(fc); - } + Chunk* chunk = fc->chunk; + chunk->i = chunk->scope_end_i; } void skip_string(Fc *fc, char end_char) { @@ -84,125 +46,62 @@ void skip_string(Fc *fc, char end_char) { void skip_until_char(Fc *fc, char *find) { // - int depth = 0; - Chunk *chunk = fc->chunk; - int len = strlen(find); - char ch; - int i = chunk->i; - int col = chunk->col; - const char *content = chunk->content; - int chunk_len = chunk->length; - while (i < chunk_len) { - // - ch = chunk->content[i]; - i++; - col++; - - if (is_newline(ch)) { - chunk->line++; - col = 1; - } - - if (depth == 0) { - for (int o = 0; o < len; o++) { - char fch = find[o]; - if (ch == fch) { - chunk->i = i; - chunk->col = col; - return; - } - } - } - - if (ch == '{' || ch == '(' || ch == '[') { - depth++; - continue; - } - if (ch == '}' || ch == ')' || ch == ']') { - depth--; - continue; + char* token = ""; + Chunk* chunk = fc->chunk; + while(strcmp(token, find) != 0 || fc->chunk->token == tok_string) { + if(fc->chunk->token == tok_eof) { + sprintf(fc->sbuf, "End of file, missing '%s' token", find); + fc_error(fc); } + token = tok(fc, NULL, false, true); } - chunk->i = i; - chunk->col = col; } void skip_whitespace(Fc *fc) { // Chunk *chunk = fc->chunk; - char ch; - int i = chunk->i; - int col = chunk->col; - const char *content = chunk->content; - while (chunk->i < chunk->length) { - // - ch = chunk->content[i]; - if (!is_whitespace(ch)) { - break; - } - if (is_newline(ch)) { - chunk->line++; - col = 0; - } - i++; - col++; + int x = chunk->i; + tok_read(chunk, &x); + if(chunk->token == tok_space) { + chunk->i = x; + tok_read(chunk, &x); + } + if(chunk->token == tok_newline) { + chunk->i = x; } - chunk->i = i; - chunk->col = col; } void skip_macro_if(Fc *fc) { // Chunk *chunk = fc->chunk; - const int length = chunk->length; - const char *content = chunk->content; - char ch; - char *token = fc->token; int depth = 1; - while (chunk->i < length) { - // - char ch = content[chunk->i]; - chunk->i++; - - if (ch == '"' || ch == '\'') { - skip_string(fc, ch); - continue; - } - - if (!is_newline(ch)) { - continue; - } - - chunk->line++; - - tok(fc, token, false, true); - - if (strcmp(token, "#") != 0) { - continue; - } - - tok(fc, token, true, false); - if (strcmp(token, "if") == 0) { - depth++; - } else if (strcmp(token, "elif") == 0 || strcmp(token, "else") == 0) { - if (depth == 1) { + while (true) { + char* token = tok(fc, NULL, false, true); + if(chunk->token == tok_cc) { + if(strcmp(token, "if") == 0) { + depth++; + } else if(strcmp(token, "elif") == 0 || strcmp(token, "else")) { + if(depth == 1) { + depth--; + break; + } + } else if(strcmp(token, "end") == 0) { depth--; - chunk_move(chunk, -5); - break; - } - } else if (strcmp(token, "end") == 0) { - depth--; - if (depth == 0) { - chunk_move(chunk, -4); - break; + if(depth == 0) + break; } + } else if(chunk->token == tok_eof) { + break; } } if (depth != 0) { - sprintf(fc->sbuf, "End of file, missing #end macro"); + sprintf(fc->sbuf, "End of file, missing #end token"); fc_error(fc); } + + rtok(fc); + skip_whitespace(fc); } void skip_traits(Fc *fc) { @@ -264,58 +163,26 @@ void skip_value(Fc *fc) { } void skip_type(Fc *fc) { - - skip_whitespace(fc); - // Chunk *chunk = fc->chunk; - char ch; - int i = chunk->i; - int col = chunk->col; - const char *content = chunk->content; - while (chunk->i < chunk->length) { - // - ch = chunk->content[i]; - i++; - col++; - - if (ch == '?') { - continue; - } - if (ch == ':') { - continue; - } - if (ch == '.') { - continue; - } - if (ch == '!') { - continue; - } - if (ch == '(') { - chunk->i = i; - chunk->col = col; - skip_body(fc, ')'); - i = chunk->i; - continue; - } - if (ch == '[') { - chunk->i = i; - chunk->col = col; - skip_body(fc, ']'); - i = chunk->i; - continue; - } - if (is_valid_varname_char(ch)) { - continue; - } - i--; - col--; - break; + char * token = tok(fc, NULL, false, true); + if(strcmp(token, "raw") == 0 || strcmp(token, "weak") == 0){ + token = tok(fc, NULL, true, true); + } + while (strcmp(token, "?") == 0 || strcmp(token, ".") == 0 || strcmp(token, "&") == 0 || strcmp(token, "+") == 0) { + token = tok(fc, NULL, true, false); + } + if(chunk->token != tok_id && token[0] != ':') { + sprintf(fc->sbuf, "Expected a type here"); + fc_error(fc); + } + token = tok(fc, NULL, true, false); + if(token[0] == '[') { + skip_body(fc, ']'); + } else { + rtok(fc); } - - chunk->i = i; - chunk->col = col; } void skip_macro_input(Fc *fc, char *end) { diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index 0abdd347..894f905b 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -22,18 +22,22 @@ void stage_1(Fc *fc) { printf("# Stage 1 : Parse : %s\n", fc->path_ki); } - chunk_lex(fc, fc->chunk, -1); + Chunk *chunk = fc->chunk; + chunk_lex_start(chunk); - char *token = fc->token; + char *token; + char *t_ = &chunk->token; while (true) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (token[0] == 0) break; - if (strcmp(token, "#") == 0) { + if (*t_ == tok_cc) { + rtok(fc); + skip_whitespace(fc); read_macro(fc, fc->alc, fc->scope); continue; } @@ -123,20 +127,19 @@ void stage_1(Fc *fc) { b->LOC += fc->chunk->line; // - chain_add(b->stage_2_1, fc); + // chain_add(b->stage_2_1, fc); } void stage_1_func(Fc *fc, bool is_private) { // Build *b = fc->b; - char *token = fc->token; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); bool will_exit = false; if (strcmp(token, "!") == 0) { will_exit = true; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } if (!is_valid_varname(token)) { @@ -151,11 +154,11 @@ void stage_1_func(Fc *fc, bool is_private) { bool is_main = fc->nsc == b->nsc_main && strcmp(token, "main") == 0; if (is_main) { - strcpy(token, "ki__main__"); + token = "ki__main__"; b->main_func = func; } - char *name = dups(fc->alc, token); + char *name = token; char *gname = nsc_gname(fc, name); char *dname = nsc_dname(fc, name); @@ -182,7 +185,7 @@ void stage_1_func(Fc *fc, bool is_private) { map_set(fc->scope->identifiers, name, idf); } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, "(") == 0) { func->chunk_args = chunk_clone(fc->alc, fc->chunk); skip_body(fc, ')'); @@ -205,8 +208,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { // Build *b = fc->b; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char *token = fc->token; - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid class name '%s'", token); @@ -248,7 +250,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { class->generic_names = array_make(fc->alc, 4); class->generics = map_make(fc->alc); - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); while (true) { if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid name"); @@ -264,29 +266,33 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { char *name = dups(fc->alc, token); array_push(class->generic_names, name); - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, ",") == 0) - tok(fc, token, true, true); - if (strcmp(token, "]") == 0) + token = tok(fc, NULL, true, true); + else if (strcmp(token, "]") == 0) break; + else { + sprintf(fc->sbuf, "Unexpected token '%s', expected ',' or ']'", token); + fc_error(fc); + } } } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); bool track = false; while (strcmp(token, "{") != 0) { if (strcmp(token, "type") == 0) { class->is_rc = false; class->track_ownership = false; tok_expect(fc, ":", true, false); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); if (strcmp(token, "ptr") == 0) { class->type = ct_ptr; class->size = fc->b->ptr_size; } else if (strcmp(token, "int") == 0 || strcmp(token, "float") == 0) { class->type = strcmp(token, "int") == 0 ? ct_int : ct_float; tok_expect(fc, ":", true, false); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); int size = 0; if (strcmp(token, "*") == 0) { size = fc->b->ptr_size; @@ -303,7 +309,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { fc_error(fc); } tok_expect(fc, ":", true, false); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); if (strcmp(token, "true") != 0 && strcmp(token, "false") != 0) { sprintf(fc->sbuf, "Invalid value for is_signed, options: true,false, received: '%s'", token); fc_error(fc); @@ -337,7 +343,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { fc_error(fc); } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } if (track) @@ -350,9 +356,8 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { void stage_1_trait(Fc *fc, bool is_private) { // - char *token = fc->token; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - tok(fc, token, true, true); + char *token = tok(fc, NULL, true, true); if (fc->is_header) { sprintf(fc->sbuf, "You cannot use 'trait' inside a header file"); @@ -403,9 +408,8 @@ void stage_1_extend(Fc *fc) { void stage_1_enum(Fc *fc, bool is_private) { // - char *token = fc->token; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid enum name '%s'", token); @@ -430,20 +434,20 @@ void stage_1_enum(Fc *fc, bool is_private) { Map *values = map_make(fc->alc); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); while (strcmp(token, "}") != 0) { if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid enum property name '%s'", token); fc_error(fc); } char *name = dups(fc->alc, token); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ":") == 0) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); bool negative = false; if (strcmp(token, "-") == 0) { negative = true; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } if (is_valid_number(token)) { int value = 0; @@ -461,9 +465,9 @@ void stage_1_enum(Fc *fc, bool is_private) { map_set(values, name, (void *)(intptr_t)value); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ",") == 0) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); continue; } else if (strcmp(token, "}") == 0) { break; @@ -483,7 +487,7 @@ void stage_1_enum(Fc *fc, bool is_private) { val = autov++; } map_set(values, name, (void *)(intptr_t)val); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); continue; } else if (strcmp(token, "}") == 0) { break; @@ -506,12 +510,12 @@ void stage_1_enum(Fc *fc, bool is_private) { void stage_1_header(Fc *fc) { // - tok_expect(fc, "\"", true, true); - - Str *buf = read_string(fc); - Str *fnstr = macro_replace_str_vars(fc->alc, fc, buf); - char *fn = str_to_chars(fc->alc, fnstr); - Nsc *nsc_main = fc->b->nsc_main; + char* fn = tok(fc, NULL, true, true); + if(fc->chunk->token != tok_string) { + sprintf(fc->sbuf, "Expected a filepath here wrapped in dubbel-quotes, e.g. \"headers/mylib\""); + fc_error(fc); + } + fn = macro_replace_str_vars(fc->alc, fc, fn); Array *dirs = fc->config_pkc->header_dirs; bool found = false; @@ -568,11 +572,11 @@ void stage_1_header(Fc *fc) { void stage_1_link(Fc *fc, int link_type) { // - tok_expect(fc, "\"", true, true); - - Str *buf = read_string(fc); - Str *fnstr = macro_replace_str_vars(fc->alc, fc, buf); - char *fn = str_to_chars(fc->alc, fnstr); + char* fn = tok(fc, NULL, true, true); + if(fc->chunk->token != tok_string) { + sprintf(fc->sbuf, "Expected a library name here wrapped in dubbel-quotes, e.g. \"pthread\""); + fc_error(fc); + } if (link_type == link_default) { link_type = fc->b->link_static ? link_static : link_dynamic; @@ -803,7 +807,7 @@ void stage_1_test(Fc *fc) { Chunk *chunk = chunk_init(alc, fc); chunk->content = "ki__test__expect_count: u32[1], ki__test__success_count: u32[1], ki__test__fail_count: u32[1]) void {"; chunk->length = strlen(chunk->content); - chunk_lex(fc, chunk, -1); + chunk_lex_start(chunk); func->chunk_args = chunk; func->chunk_body = chunk_clone(fc->alc, fc->chunk); diff --git a/src/build/stage-2-types.c b/src/build/stage-2-types.c deleted file mode 100644 index 8a60d941..00000000 --- a/src/build/stage-2-types.c +++ /dev/null @@ -1,638 +0,0 @@ - -#include "../all.h" - -// void stage_2_class(Fc *fc, Class *class); -// void stage_2_class_props(Fc *fc, Class *class, bool is_trait, bool is_extend); -// void stage_2_func(Fc *fc, Func *func); -// void stage_2_class_defaults(Fc *fc, Class *class); -// void stage_2_class_type_checks(Fc *fc, Class *class); -// void stage_2_class_type_check(Fc *fc, Func *func, TypeCheck *args[], int argc_ex, TypeCheck *tc_rett, bool can_error); - -// void stage_2(Fc *fc) { -// // -// Build *b = fc->b; -// if (b->verbose > 2) { -// printf("# Stage 2 : Read types : %s\n", fc->path_ki); -// } - -// for (int i = 0; i < fc->aliasses->length; i++) { -// Alias *a = array_get_index(fc->aliasses, i); -// fc->chunk = a->chunk; - -// Idf *idf_a = NULL; -// if (a->type == alias_type) { -// Type *type = read_type(fc, fc->alc, fc->scope, true, true, rtc_default); -// idf_a = idf_init(fc->alc, idf_type); -// idf_a->item = type; -// } else { -// Id *id = read_id(fc, true, true, true); -// Idf *idf = idf_by_id(fc, fc->scope, id, true); -// idf_a = idf_init(fc->alc, idf->type); -// idf_a->item = idf->item; -// } -// map_set(fc->nsc->scope->identifiers, a->name, idf_a); -// if (fc->is_header) { -// map_set(fc->scope->identifiers, a->name, idf_a); -// } -// } - -// for (int i = 0; i < fc->classes->length; i++) { -// Class *class = array_get_index(fc->classes, i); -// if (class->is_generic_base) -// continue; -// if (b->verbose > 2) { -// printf("> Scan class properties: %s\n", class->dname); -// } -// stage_2_class(fc, class); -// } -// for (int i = 0; i < fc->classes->length; i++) { -// Class *class = array_get_index(fc->classes, i); -// if (class->is_generic_base) -// continue; -// if (b->verbose > 2) { -// printf("> Class generate defaults: %s\n", class->dname); -// } -// stage_2_class_defaults(fc, class); -// } -// for (int i = 0; i < fc->globals->length; i++) { -// Global *g = array_get_index(fc->globals, i); -// fc->chunk = g->type_chunk; -// Type *type = read_type(fc, fc->alc, fc->scope, true, true, rtc_default); -// if (type->ptr_depth > 0 && !type->nullable) { -// sprintf(fc->sbuf, "Global types must be prefixed with '?' because the default value of a global is 'null' for pointer types"); -// fc_error(fc); -// } -// g->type = type; -// } - -// // -// chain_add(b->stage_2_1, fc); -// } - -// void stage_2_class(Fc *fc, Class *class) { - -// // -// Class *prev_error_class_info = fc->error_class_info; -// fc->error_class_info = class; - -// fc->chunk = class->chunk_body; -// stage_2_class_props(fc, class, false, false); - -// // Generate __free / __deref / _RC -// if (class->type == ct_struct) { -// Build *b = fc->b; -// if (class->is_rc) { -// // Define _RC -// Type *type = type_gen(b, b->alc, "u32"); -// ClassProp *prop = class_prop_init(b->alc, class, type); -// prop->value = vgen_vint(b->alc, 1, type, false); -// prop->act = act_private; -// map_set(class->props, "_RC", prop); - -// Type *type_weak = type_gen(b, b->alc, "u32"); -// ClassProp *prop_weak = class_prop_init(b->alc, class, type_weak); -// prop_weak->value = vgen_vint(b->alc, 0, type_weak, false); -// prop_weak->act = act_private; -// map_set(class->props, "_RC_WEAK", prop_weak); -// } -// } - -// // -// if (class->type == ct_struct && class->props->keys->length == 0) { -// sprintf(fc->sbuf, "Class has no properties"); -// fc_error(fc); -// } - -// // -// if (!class_check_size(class)) { -// array_push(fc->class_size_checks, class); -// } - -// fc->error_class_info = prev_error_class_info; -// } - -// void stage_2_class_props(Fc *fc, Class *class, bool is_trait, bool is_extend) { -// // -// char *token = fc->token; -// Scope *scope = class->scope; - -// while (true) { - -// tok(fc, token, false, true); -// // printf("t:'%s'\n", token); - -// if (token[0] == 0) { -// sprintf(fc->sbuf, "Unexpected end of file"); -// fc_error(fc); -// } - -// if (strcmp(token, "}") == 0) { -// break; -// } - -// if (strcmp(token, "/") == 0 && get_char(fc, 0) == '/') { -// skip_body(fc, '\n'); -// continue; -// } - -// if (strcmp(token, "#") == 0) { -// read_macro(fc, fc->alc, scope); -// continue; -// } - -// if (strcmp(token, "trait") == 0) { - -// if (is_trait) { -// sprintf(fc->sbuf, "You cannot use traits inside traits"); -// fc_error(fc); -// } - -// Id *id = read_id(fc, true, true, true); -// Idf *idf = idf_by_id(fc, scope, id, true); - -// if (idf->type != idf_trait) { -// sprintf(fc->sbuf, "This is not a trait"); -// fc_error(fc); -// } - -// Trait *tr = idf->item; - -// Chunk *current = fc->chunk; -// fc->chunk = chunk_clone(fc->alc, tr->chunk); -// Scope *trait_scope = scope_init(fc->alc, sct_default, tr->fc->scope, false); -// map_set(trait_scope->identifiers, "CLASS", map_get(scope->identifiers, "CLASS")); -// class->scope = trait_scope; - -// stage_2_class_props(fc, class, true, false); - -// class->scope = scope; -// fc->chunk = current; - -// tok_expect(fc, ";", false, true); - -// continue; -// } - -// bool is_static = false; - -// int act = act_public; - -// if (strcmp(token, "-") == 0) { -// act = act_private; -// tok(fc, token, true, true); -// } else if (strcmp(token, "~") == 0) { -// act = act_readonly; -// tok(fc, token, true, true); -// } - -// Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - -// char next_token[KI_TOKEN_MAX]; -// tok(fc, next_token, true, true); - -// if (strcmp(token, "static") == 0 && strcmp(next_token, "fn") == 0) { -// is_static = true; -// strcpy(token, next_token); -// } else { -// rtok(fc); -// } - -// if (strcmp(token, "stores_references_to") == 0) { - -// Type *type = read_type(fc, fc->alc, scope, true, true, rtc_prop_type); -// tok_expect(fc, ";", false, true); - -// array_push(class->refers_to_types, type); -// array_push(class->refers_to_names, "*"); - -// } else if (strcmp(token, "fn") == 0) { -// // Function -// *def_chunk = *fc->chunk; -// tok(fc, token, true, true); - -// bool borrow = true; -// bool ref = false; -// bool will_exit = false; - -// if (strcmp(token, "!") == 0) { -// will_exit = true; -// *def_chunk = *fc->chunk; -// tok(fc, token, true, false); -// } - -// if (!is_static) { -// if (strcmp(token, ">") == 0) { -// borrow = false; -// *def_chunk = *fc->chunk; -// tok(fc, token, true, false); -// } -// } - -// if (!is_valid_varname(token)) { -// sprintf(fc->sbuf, "Invalid function name syntax: '%s'", token); -// fc_error(fc); -// } -// if (map_get(class->props, token) || map_get(class->funcs, token)) { -// sprintf(fc->sbuf, "Name already used (func name): '%s'", token); -// fc_error(fc); -// } - -// Func *func = class_define_func(fc, class, is_static, token, NULL, NULL, fc->chunk->line); -// func->act = act; -// func->will_exit = will_exit; -// func->def_chunk = def_chunk; - -// if (!is_static) { -// Arg *arg = array_get_index(func->args, 0); -// if (type_tracks_ownership(arg->type)) { -// arg->type->borrow = borrow; -// arg->type->shared_ref = ref; -// } -// } - -// if (strcmp(func->name, "__ref") == 0) { -// class->func_ref = func; -// } else if (strcmp(func->name, "__deref") == 0) { -// class->func_deref = func; -// } else if (strcmp(func->name, "__ref_weak") == 0) { -// class->func_ref_weak = func; -// } else if (strcmp(func->name, "__deref_weak") == 0) { -// class->func_deref_weak = func; -// } else if (strcmp(func->name, "__free") == 0) { -// class->func_free = func; -// } else if (strcmp(func->name, "__before_free") == 0) { -// class->func_before_free = func; -// } - -// tok(fc, token, true, true); -// if (strcmp(token, "(") == 0) { -// func->chunk_args = chunk_clone(fc->alc, fc->chunk); -// skip_body(fc, ')'); -// } else { -// rtok(fc); -// func->chunk_args = chunk_clone(fc->alc, fc->chunk); -// func->parse_args = false; -// } - -// skip_until_char(fc, "{"); -// func->chunk_body = chunk_clone(fc->alc, fc->chunk); -// skip_body(fc, '}'); - -// } else { -// // Property -// if (is_extend) { -// sprintf(fc->sbuf, "You cannot define new properties using 'extend'"); -// fc_error(fc); -// } -// if (is_static) { -// sprintf(fc->sbuf, "Static properties are not allowed, use globals instead"); -// fc_error(fc); -// } - -// if (!is_valid_varname(token)) { -// sprintf(fc->sbuf, "Invalid property name syntax: '%s'", token); -// fc_error(fc); -// } -// if (map_get(class->props, token) || map_get(class->funcs, token)) { -// sprintf(fc->sbuf, "Name already used (property name): '%s'", token); -// fc_error(fc); -// } - -// char *prop_name = dups(fc->alc, token); - -// ClassProp *prop = class_prop_init(fc->alc, class, NULL); -// prop->act = act; -// prop->def_chunk = def_chunk; - -// tok(fc, token, true, true); -// if (strcmp(token, ":") == 0) { -// Type *type = read_type(fc, fc->alc, scope, true, true, rtc_prop_type); -// if (type_is_void(type)) { -// sprintf(fc->sbuf, "Cannot use 'void' as a type for a class property"); -// fc_error(fc); -// } -// if (type->borrow) { -// class->has_borrows = true; -// } -// prop->type = type; - -// tok(fc, token, true, true); -// } - -// if (strcmp(token, "=") == 0) { -// prop->value_chunk = chunk_clone(fc->alc, fc->chunk); -// } else { -// rtok(fc); -// } - -// if (!prop->type && !prop->value_chunk) { -// sprintf(fc->sbuf, "The property must either have a type or default value defined. Both are missing."); -// fc_error(fc); -// } - -// map_set(class->props, prop_name, prop); -// array_push(class->refers_to_types, prop->type); -// array_push(class->refers_to_names, prop_name); - -// skip_until_char(fc, ";"); -// } -// } -// } - -// void stage_2_func(Fc *fc, Func *func) { -// // -// Func *prev_error_func_info = fc->error_func_info; -// fc->error_func_info = func; -// // -// char *token = fc->token; -// Allocator *alc = fc->alc; - -// // Args -// fc->chunk = func->chunk_args; -// if (func->parse_args) { -// tok(fc, token, true, true); -// while (strcmp(token, ")") != 0) { - -// if (!is_valid_varname(token)) { -// sprintf(fc->sbuf, "Invalid argument name: '%s'", token); -// fc_error(fc); -// } -// if (map_get(func->args_by_name, token)) { -// sprintf(fc->sbuf, "Argument name already used: '%s'", token); -// fc_error(fc); -// } - -// char *name = dups(alc, token); - -// tok_expect(fc, ":", true, true); - -// Chunk *val_chunk = NULL; -// Chunk *type_chunk = chunk_clone(alc, fc->chunk); - -// Type *type = read_type(fc, alc, func->scope->parent, true, true, rtc_func_arg); - -// tok(fc, token, true, true); -// if (strcmp(token, "=") == 0) { -// val_chunk = chunk_clone(alc, fc->chunk); -// skip_value(fc); -// } else { -// rtok(fc); -// } - -// tok(fc, token, false, true); -// if (strcmp(token, ",") == 0) { -// tok(fc, token, false, true); -// } else if (strcmp(token, ")") != 0) { -// sprintf(fc->sbuf, "Unexpected token '%s'", token); -// fc_error(fc); -// } - -// Arg *arg = arg_init(alc, name, type); -// arg->value_chunk = val_chunk; -// arg->type_chunk = type_chunk; - -// array_push(func->args, arg); -// map_set(func->args_by_name, name, arg); -// } -// } - -// // Return type -// tok(fc, token, false, true); -// if (strcmp(token, "!") != 0 && strcmp(token, "%") != 0 && strcmp(token, "{") != 0) { -// rtok(fc); -// func->rett = read_type(fc, alc, func->scope->parent, true, true, rtc_func_rett); -// tok(fc, token, false, true); -// } - -// if (func->will_exit && !type_is_void(func->rett)) { -// sprintf(fc->sbuf, "Using '!' before the function name tells the compiler this function will exit the program. Therefore the return type must be void."); -// fc_error(fc); -// } - -// Array *errors = NULL; - -// while (strcmp(token, "!") == 0) { -// if (!errors) { -// errors = array_make(alc, 4); -// func->can_error = true; -// } - -// tok(fc, token, true, false); -// if (!is_valid_varname(token)) { -// sprintf(fc->sbuf, "Invalid error name '%s'", token); -// fc_error(fc); -// } -// if (array_contains(errors, token, arr_find_str)) { -// sprintf(fc->sbuf, "Duplicate error name '%s'", token); -// fc_error(fc); -// } -// array_push(errors, dups(alc, token)); - -// tok(fc, token, false, true); -// } - -// func->errors = errors; - -// while (strcmp(token, "%") == 0) { -// tok(fc, token, false, false); -// if (strcmp(token, "hot") == 0) { -// func->opt_hot = true; -// } else if (strcmp(token, "inline") == 0) { -// func->opt_inline = true; -// } else { -// sprintf(fc->sbuf, "Unknown tag '%s'", token); -// fc_error(fc); -// } - -// tok(fc, token, false, true); -// } - -// // Define arguments in AST -// func_make_arg_decls(func); - -// rtok(fc); - -// Build *b = fc->b; -// if (func == b->main_func) { -// // Type check arguments (TODO) - -// // Type check return type -// Type *rett = func->rett; -// Class *class = rett->class; -// if ((!type_is_void(rett) && class != ki_get_class(b, "type", "i32")) || rett->ptr_depth > 0) { -// sprintf(fc->sbuf, "func 'main' should return 'void' or 'i32'"); -// fc_error(fc); -// } -// } - -// fc->error_func_info = prev_error_func_info; -// } - -// void stage_2_class_defaults(Fc *fc, Class *class) { - -// // Generate __free / __deref / _RC -// if (class->type == ct_struct) { -// Build *b = fc->b; -// if (class->is_rc) { -// // Define __deref_props -// // Only generate the function if there are properties that can be dereferenced -// Array *props = class->props->values; -// for (int i = 0; i < props->length; i++) { -// ClassProp *prop = array_get_index(props, i); -// Class *pclass = prop->type->class; -// if (pclass && pclass->is_rc) { -// class->func_deref_props = class_define_func(fc, class, false, "__deref_props", NULL, b->type_void, 0); -// class->func_deref_props->is_generated = true; -// break; -// } -// } -// } -// // Define __free -// if (!class->func_free) { -// class->func_free = class_define_func(fc, class, false, "__free", NULL, b->type_void, 0); -// class->func_free->is_generated = true; -// } -// } - -// if (class->is_rc) { -// if (class->type != ct_struct) { -// if (!class->func_ref) { -// sprintf(fc->sbuf, "Missing function '__ref' in type '%s'", class->dname); -// fc_error(fc); -// } -// if (!class->func_deref) { -// sprintf(fc->sbuf, "Missing function '__deref' in type '%s'", class->dname); -// fc_error(fc); -// } -// if (!class->func_ref_weak) { -// sprintf(fc->sbuf, "Missing function '__ref_weak' in type '%s'", class->dname); -// fc_error(fc); -// } -// if (!class->func_deref_weak) { -// sprintf(fc->sbuf, "Missing function '__deref_weak' in type '%s'", class->dname); -// fc_error(fc); -// } -// } -// if (!class->func_free) { -// sprintf(fc->sbuf, "Missing function '__free' in type '%s'", class->dname); -// fc_error(fc); -// } -// } -// } - -// void stage_2_class_type_checks(Fc *fc, Class *class) { -// // -// Func *func; -// Allocator *alc = fc->alc; - -// TypeCheck tc_this; -// tc_this.class = class; -// tc_this.borrow = true; -// tc_this.shared_ref = false; -// tc_this.array_of = NULL; -// tc_this.type = -1; -// tc_this.array_size = 0; - -// TypeCheck *checks[10]; -// checks[0] = &tc_this; - -// TypeCheck tc_void; -// tc_void.class = NULL; -// tc_void.borrow = false; -// tc_void.shared_ref = false; -// tc_void.array_of = NULL; -// tc_void.type = type_void; -// tc_void.array_size = 0; - -// func = map_get(class->funcs, "__ref"); -// if (func) -// stage_2_class_type_check(fc, func, checks, 1, &tc_void, false); -// func = map_get(class->funcs, "__deref"); -// if (func) -// stage_2_class_type_check(fc, func, checks, 1, &tc_void, false); -// func = map_get(class->funcs, "__ref_weak"); -// if (func) -// stage_2_class_type_check(fc, func, checks, 1, &tc_void, false); -// func = map_get(class->funcs, "__deref_weak"); -// if (func) -// stage_2_class_type_check(fc, func, checks, 1, &tc_void, false); -// func = map_get(class->funcs, "__free"); -// if (func) -// stage_2_class_type_check(fc, func, checks, 1, &tc_void, false); -// func = map_get(class->funcs, "__before_free"); -// if (func) -// stage_2_class_type_check(fc, func, checks, 1, &tc_void, false); - -// // Iter -// Func *func_init = map_get(class->funcs, "__each_init"); -// func = map_get(class->funcs, "__each"); -// if (func_init && func) { -// if (type_is_void(func_init->rett)) { -// fc->chunk = func_init->chunk_args; -// sprintf(fc->sbuf, "__each_init can have any return type except 'void'"); -// fc_error(fc); -// } -// stage_2_class_type_check(fc, func_init, checks, 1, NULL, false); - -// TypeCheck tc_key; -// tc_key.class = NULL; -// tc_key.borrow = type_is_rc(func_init->rett); -// tc_key.shared_ref = false; -// tc_key.array_of = type_gen_type_check(alc, func_init->rett); -// tc_key.type = -1; -// tc_key.array_size = 1; - -// checks[1] = &tc_key; - -// if (type_is_void(func->rett)) { -// fc->chunk = func->chunk_args; -// sprintf(fc->sbuf, "__each can have any return type except 'void'"); -// fc_error(fc); -// } - -// stage_2_class_type_check(fc, func, checks, 2, NULL, true); -// class->can_iter = true; -// } -// } -// void stage_2_class_type_check(Fc *fc, Func *func, TypeCheck *checks[], int argc_ex, TypeCheck *tc_rett, bool can_error) { -// // -// if (!func->chunk_args) { -// // Generated func -// return; -// } - -// Chunk *chunk = fc->chunk; -// int argc = func->args->length; -// if (argc > argc_ex) { -// fc->chunk = func->chunk_args; -// sprintf(fc->sbuf, "Too many arguments. Expected %d argument(s). But found: %d", argc_ex, argc); -// fc_error(fc); -// } -// if (argc < argc_ex) { -// fc->chunk = func->chunk_args; -// sprintf(fc->sbuf, "Missing arguments. Expected %d argument(s). But found: %d", argc_ex, argc); -// fc_error(fc); -// } - -// int i = 0; -// char buf[200]; -// while (i < argc) { -// TypeCheck *tc = checks[i]; -// Arg *arg = array_get_index(func->args, i); -// i++; - -// fc->chunk = arg->type_chunk; -// sprintf(buf, "Incorrect type for argument '%s'", arg->name); -// type_validate(fc, tc, arg->type, buf); -// } - -// if (tc_rett) { -// fc->chunk = func->chunk_args; -// type_validate(fc, tc_rett, func->rett, "Incorrect function return type"); -// } - -// if (func->can_error != can_error) { -// sprintf(fc->sbuf, can_error ? "This function should atleast have 1 error defined" : "This function should not be able to return errors"); -// fc_error(fc); -// } -// } diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index be55b968..71760f09 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -897,7 +897,7 @@ void stage_4_1_gen_main(Fc *fc) { chunk->line = 1; chunk->col = 1; fc->chunk = chunk; - chunk_lex(fc, chunk, -1); + chunk_lex_start(chunk); read_ast(fc, scope, false); } @@ -980,7 +980,7 @@ void stage_4_1_gen_test_main(Fc *fc) { Chunk *chunk = chunk_init(alc, fc); chunk->content = str_to_chars(alc, code); chunk->length = code->length; - chunk_lex(fc, chunk, -1); + chunk_lex_start(chunk); func->chunk_body = chunk; } diff --git a/src/build/value.c b/src/build/value.c index 1e1c425c..e50d1846 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -1204,7 +1204,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { chunk->parent = fc->chunk; chunk->content = content; chunk->length = buf->length; - chunk_lex(fc, chunk, -1); + chunk_lex_start(chunk); // printf(">>>%s<<<", content); fc->chunk = chunk; diff --git a/src/headers/enums.h b/src/headers/enums.h index a4c68134..57c29168 100644 --- a/src/headers/enums.h +++ b/src/headers/enums.h @@ -15,6 +15,8 @@ enum TOKENS { tok_char, tok_scope_open, tok_scope_close, + tok_pos, + tok_cc, }; enum BUILD_TYPES { diff --git a/src/headers/functions.h b/src/headers/functions.h index 2af86181..3ab09d4f 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -184,8 +184,11 @@ Chunk *chunk_init(Allocator *alc, Fc *fc); Chunk *chunk_clone(Allocator *alc, Chunk *chunk); void chunk_move(Chunk *chunk, int pos); void chunk_update_col(Chunk *chunk); -void chunk_lex(Fc *fc, Chunk* chunk, int err_i); -void tok(Fc *fc, char *token, bool sameline, bool allow_space); +void chunk_lex_start(Chunk *chunk); +void chunk_lex(Chunk* chunk, int err_token_i, int* err_content_i, int* err_line, int* err_col); +char* tok(Fc *fc, char *token, bool sameline, bool allow_space); +char* tok_next(Chunk* chunk, bool sameline, bool allow_space, bool update); +char* tok_read(Chunk* chunk, int *i_ref); void rtok(Fc *fc); void tok_expect(Fc *fc, char *expect, bool sameline, bool allow_space); char get_char(Fc *fc, int index); @@ -210,7 +213,7 @@ MacroScope *init_macro_scope(Allocator *alc); void read_macro(Fc *fc, Allocator *alc, Scope *scope); bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc); char *macro_get_var(MacroScope *mc, char *key); -Str *macro_replace_str_vars(Allocator *alc, Fc *fc, Str *str); +char *macro_replace_str_vars(Allocator *alc, Fc *fc, char *str); // Id Id *id_init(Allocator *alc); diff --git a/src/headers/structs.h b/src/headers/structs.h index f8f0a1d0..0cdd49ee 100644 --- a/src/headers/structs.h +++ b/src/headers/structs.h @@ -231,6 +231,8 @@ struct Chunk { int i; int line; int col; + int scope_end_i; + char token; }; struct Scope { int type; diff --git a/src/string.c b/src/string.c index 5a87e7bc..f09538ba 100644 --- a/src/string.c +++ b/src/string.c @@ -55,7 +55,7 @@ void str_append_chars(Str *str, char *add) { void str_increase_memsize(Str *str, int new_memsize) { // void* data = al(str->alc, new_memsize); - memcpy(str->data, data, str->length); + memcpy(data, str->data, str->length); str->data = data; str->mem_size = new_memsize; } From 206a23ec79bc9a900e14c3de1ca25ffa31473b39 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 19:45:44 +0100 Subject: [PATCH 04/16] tok update --- src/build/stage-1-parse.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index 894f905b..e48751bc 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -216,7 +216,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { } name_taken_check(fc, fc->nsc->scope, token); - char *name = dups(fc->alc, token); + char *name = token; char *gname = nsc_gname(fc, name); char *dname = nsc_dname(fc, name); @@ -263,7 +263,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { fc_error(fc); } - char *name = dups(fc->alc, token); + char *name = token; array_push(class->generic_names, name); token = tok(fc, NULL, true, true); @@ -369,7 +369,7 @@ void stage_1_trait(Fc *fc, bool is_private) { } name_taken_check(fc, fc->nsc->scope, token); - char *name = dups(fc->alc, token); + char *name = token; char *gname = nsc_gname(fc, name); char *dname = nsc_dname(fc, name); @@ -417,7 +417,7 @@ void stage_1_enum(Fc *fc, bool is_private) { } name_taken_check(fc, fc->nsc->scope, token); - char *name = dups(fc->alc, token); + char *name = token; char *gname = nsc_gname(fc, name); char *dname = nsc_dname(fc, name); @@ -440,7 +440,7 @@ void stage_1_enum(Fc *fc, bool is_private) { sprintf(fc->sbuf, "Invalid enum property name '%s'", token); fc_error(fc); } - char *name = dups(fc->alc, token); + char *name = token; token = tok(fc, NULL, false, true); if (strcmp(token, ":") == 0) { token = tok(fc, NULL, false, true); @@ -542,7 +542,7 @@ void stage_1_header(Fc *fc) { } name_taken_check(fc, fc->scope, token); - char *as = dups(fc->alc, token); + char *as = token; // Idf *idf = idf_init(fc->alc, idf_fc); @@ -624,10 +624,9 @@ void stage_1_use(Fc *fc) { } char *as = nsc_name; - char *token = fc->token; - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (strcmp(token, "as") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name syntax '%s'", token); fc_error(fc); @@ -648,9 +647,8 @@ void stage_1_use(Fc *fc) { void stage_1_global(Fc *fc, bool shared, bool is_private) { // - char *token = fc->token; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid global name syntax '%s'", token); @@ -700,8 +698,7 @@ void stage_1_alias(Fc *fc, int alias_type, bool is_private) { tok_expect(fc, "as", true, true); - char *token = fc->token; - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid alias name syntax '%s'", token); @@ -824,8 +821,7 @@ void stage_1_macro(Fc *fc) { Build *b = fc->b; Allocator *alc = fc->alc; - char *token = fc->token; - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid macro name syntax '%s'", token); @@ -833,7 +829,7 @@ void stage_1_macro(Fc *fc) { } name_taken_check(fc, fc->nsc->scope, token); - char *name = dups(fc->alc, token); + char *name = token; char *dname = nsc_dname(fc, name); Macro *mac = al(alc, sizeof(Macro)); @@ -862,7 +858,7 @@ void stage_1_macro(Fc *fc) { while (true) { // Read input groups - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, "\"") == 0) { // New group MacroVarGroup *mvg = al(alc, sizeof(MacroVarGroup)); @@ -912,7 +908,7 @@ void stage_1_macro(Fc *fc) { // sprintf(fc->sbuf, "Expected ':', found: '%s'", pattern); // fc_error(fc); // } - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid input name syntax '%s'", token); fc_error(fc); @@ -923,7 +919,7 @@ void stage_1_macro(Fc *fc) { } MacroVar *mv = al(alc, sizeof(MacroVar)); - mv->name = dups(alc, token); + mv->name = token; mv->replaces = array_make(alc, 4); mv->repeat = false; @@ -931,7 +927,7 @@ void stage_1_macro(Fc *fc) { array_push(mvg->vars, mv); while (true) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, "@repeat") == 0) { repeat = true; From 67d2917cbbe9bc57c24556ee4c65882f9186b9cc Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 20:39:23 +0100 Subject: [PATCH 05/16] lexer update --- src/build/lexer.c | 30 +++---------- src/build/read.c | 89 +++++++++++++------------------------ src/build/skip.c | 29 +++++------- src/build/stage-1-parse.c | 14 +++--- src/build/stage-2-2-props.c | 10 +---- src/build/stage-4-1-ast.c | 2 +- src/build/value.c | 48 +++++--------------- src/headers/functions.h | 9 ++-- src/syntax.c | 21 +++++++++ 9 files changed, 96 insertions(+), 156 deletions(-) diff --git a/src/build/lexer.c b/src/build/lexer.c index db80c8d5..e3c9bec9 100644 --- a/src/build/lexer.c +++ b/src/build/lexer.c @@ -26,7 +26,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, char token[256]; int token_i = 0; - int line = 0; + int line = 1; int col = 0; int i_last = 0; @@ -75,6 +75,11 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, while(ch <= 32) { if(ch == 0) break; + if(ch == '\n') { + i_last = i; + col = 0; + line++; + } ch = content[++i]; } continue; @@ -190,7 +195,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, char ch = content[i++]; if(ch == '\\') { ch = content[i++]; - ch = convert_backslash_char(ch); + ch = backslash_char(ch); } if(content[i++] != '\'') { sprintf(fc->sbuf, "Missing character closing tag ('), found '%c'", content[i - 1]); @@ -297,24 +302,3 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, chunk->tokens = tokens; } - -char convert_backslash_char(char ch) { - if (ch == 'n') { - ch = '\n'; - } else if (ch == 'r') { - ch = '\r'; - } else if (ch == 't') { - ch = '\t'; - } else if (ch == 'f') { - ch = '\f'; - } else if (ch == 'b') { - ch = '\b'; - } else if (ch == 'v') { - ch = '\v'; - } else if (ch == 'f') { - ch = '\f'; - } else if (ch == 'a') { - ch = '\a'; - } - return ch; -} \ No newline at end of file diff --git a/src/build/read.c b/src/build/read.c index 28e3e73e..f03d5efa 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -236,80 +236,55 @@ Str *read_string(Fc *fc) { return buf; } -Array *read_string_chunks(Allocator *alc, Fc *fc) { +Array *string_read_format_chunks(Allocator *alc, Fc* fc, char *body) { // Array *result = array_make(alc, 4); Str *buf = fc->b->str_buf; str_clear(buf); - Chunk *chunk = fc->chunk; - char *data = chunk->content; - int i = chunk->i; - int col = chunk->col; - int line = chunk->line; - int len = chunk->length; - while (i < len) { - char ch = *(data + i); - i++; - col++; - + int i = 0; + while (true) { + const char ch = body[i++]; + if(ch == 0) + break; if (ch == '\\') { - if (i == len) { - break; - } - char add = *(data + i); - if (add == 'n') { - add = '\n'; - } else if (add == 'r') { - add = '\r'; - } else if (add == 't') { - add = '\t'; - } else if (add == 'f') { - add = '\f'; - } else if (add == 'b') { - add = '\b'; - } else if (add == 'v') { - add = '\v'; - } else if (add == 'f') { - add = '\f'; - } else if (add == 'a') { - add = '\a'; - } - i++; - col++; - - str_append_char(buf, add); + char ch = body[i++]; + ch = backslash_char(ch); + str_append_char(buf, ch); continue; } - - if (ch == '"') { - array_push(result, str_to_chars(alc, buf)); - str_clear(buf); - break; - } - if (ch == '%') { array_push(result, str_to_chars(alc, buf)); str_clear(buf); continue; } - - if (is_newline(ch)) { - line++; - col = 1; - } str_append_char(buf, ch); } - if (i == len) { - sprintf(fc->sbuf, "Missing end of string"); - fc_error(fc); - } - - chunk->i = i; - chunk->col = col; - chunk->line = line; + return result; +} +char* string_replace_backslash_chars(Allocator* alc, char* body) { + // + int len = strlen(body); + if(len == 0) + return ""; + char* result = al(alc, len); + int i = 0; + int ri = 0; + while(true) { + const char ch = body[i++]; + if(ch == 0) + break; + if (ch == '\\') { + char ch = body[i++]; + ch = backslash_char(ch); + result[ri++] = ch; + continue; + } + result[ri++] = ch; + } + result[ri] = 0; return result; } diff --git a/src/build/skip.c b/src/build/skip.c index d9db4451..83db04c2 100644 --- a/src/build/skip.c +++ b/src/build/skip.c @@ -1,6 +1,6 @@ #include "../all.h" -void skip_body(Fc *fc, char until_ch) { +void skip_body(Fc *fc) { // Chunk* chunk = fc->chunk; chunk->i = chunk->scope_end_i; @@ -123,35 +123,28 @@ void skip_traits(Fc *fc) { void skip_value(Fc *fc) { - char *token = fc->token; + Chunk *chunk = fc->chunk; while (true) { - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); + char t = chunk->token; - if (strcmp(token, "\"") == 0) { - skip_string(fc, '"'); + if (t == tok_string) { continue; } - if (strcmp(token, "'") == 0) { - skip_string(fc, '\''); + if (t == tok_char_string) { continue; } - if (strcmp(token, "(") == 0) { - skip_until_char(fc, ")"); + if (t == tok_scope_open) { + skip_body(fc); continue; } - if (strcmp(token, "{") == 0) { - skip_until_char(fc, "}"); + if (t == tok_id) { continue; } - if (strcmp(token, "[") == 0) { - skip_until_char(fc, "]"); + if (t == tok_number) { continue; } - if (is_valid_varname_char(token[0])) - continue; - if (is_number(token[0])) - continue; if (strcmp(token, ":") == 0 || strcmp(token, ".") == 0 || strcmp(token, "<=") == 0 || strcmp(token, ">=") == 0 || strcmp(token, "==") == 0 || strcmp(token, "!=") == 0 || strcmp(token, "&&") == 0 || strcmp(token, "||") == 0 || strcmp(token, "+") == 0 || strcmp(token, "-") == 0 || strcmp(token, "/") == 0 || strcmp(token, "*") == 0 || strcmp(token, "%") == 0 || strcmp(token, "&") == 0 || strcmp(token, "|") == 0 || strcmp(token, "^") == 0 || strcmp(token, "++") == 0 || strcmp(token, "--") == 0 || strcmp(token, "->") == 0 || strcmp(token, "??") == 0 || strcmp(token, "?!") == 0 || strcmp(token, "!!") == 0 || strcmp(token, "?") == 0) { continue; @@ -179,7 +172,7 @@ void skip_type(Fc *fc) { } token = tok(fc, NULL, true, false); if(token[0] == '[') { - skip_body(fc, ']'); + skip_body(fc); } else { rtok(fc); } diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index e48751bc..a8dfc96f 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -127,7 +127,7 @@ void stage_1(Fc *fc) { b->LOC += fc->chunk->line; // - // chain_add(b->stage_2_1, fc); + chain_add(b->stage_2_1, fc); } void stage_1_func(Fc *fc, bool is_private) { @@ -188,7 +188,7 @@ void stage_1_func(Fc *fc, bool is_private) { token = tok(fc, NULL, true, true); if (strcmp(token, "(") == 0) { func->chunk_args = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, ')'); + skip_body(fc); } else { rtok(fc); func->parse_args = false; @@ -200,7 +200,7 @@ void stage_1_func(Fc *fc, bool is_private) { } else { skip_until_char(fc, "{"); func->chunk_body = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, '}'); + skip_body(fc); } } @@ -351,7 +351,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { class->chunk_body = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, '}'); + skip_body(fc); } void stage_1_trait(Fc *fc, bool is_private) { @@ -389,7 +389,7 @@ void stage_1_trait(Fc *fc, bool is_private) { tr->chunk = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, '}'); + skip_body(fc); } void stage_1_extend(Fc *fc) { @@ -401,7 +401,7 @@ void stage_1_extend(Fc *fc) { skip_type(fc); tok_expect(fc, "{", false, true); ex->chunk_body = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, '}'); + skip_body(fc); array_push(fc->extends, ex); } @@ -813,7 +813,7 @@ void stage_1_test(Fc *fc) { array_push(b->tests, test); } - skip_body(fc, '}'); + skip_body(fc); } void stage_1_macro(Fc *fc) { diff --git a/src/build/stage-2-2-props.c b/src/build/stage-2-2-props.c index 98c71c78..0b8273e8 100644 --- a/src/build/stage-2-2-props.c +++ b/src/build/stage-2-2-props.c @@ -36,7 +36,6 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext while (true) { tok(fc, token, false, true); - // printf("t:'%s'\n", token); if (token[0] == 0) { sprintf(fc->sbuf, "Unexpected end of file"); @@ -47,11 +46,6 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext break; } - if (strcmp(token, "/") == 0 && get_char(fc, 0) == '/') { - skip_body(fc, '\n'); - continue; - } - if (strcmp(token, "#") == 0) { read_macro(fc, fc->alc, scope); continue; @@ -184,7 +178,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext tok(fc, token, true, true); if (strcmp(token, "(") == 0) { func->chunk_args = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, ')'); + skip_body(fc); } else { rtok(fc); func->chunk_args = chunk_clone(fc->alc, fc->chunk); @@ -193,7 +187,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext skip_until_char(fc, "{"); func->chunk_body = chunk_clone(fc->alc, fc->chunk); - skip_body(fc, '}'); + skip_body(fc); } else { // Property diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index 71760f09..ebcad6b7 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -46,7 +46,7 @@ void stage_4_1(Fc *fc) { } // Write IR - stage_4_2(fc); + // stage_4_2(fc); } void stage_4_1_func(Fc *fc, Func *func) { diff --git a/src/build/value.c b/src/build/value.c index e50d1846..7d0d0084 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -21,8 +21,10 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, char *token = fc->token; Build *b = fc->b; Value *v = NULL; + Chunk* chunk = fc->chunk; - tok(fc, token, sameline, true); + char* tkn = tok(fc, token, sameline, true); + char t = chunk->token; bool skip_move = assignable; @@ -31,15 +33,11 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, tok_expect(fc, ")", false, true); skip_move = true; // - } else if (strcmp(token, "\"") == 0) { - Chunk before_str[1]; - *before_str = *fc->chunk; - Str *str = read_string(fc); - char *body = str_to_chars(alc, str); + } else if (t == tok_string) { + char *body = tkn; if (get_char(fc, 0) == '{') { // Format string - *fc->chunk = *before_str; - Array *parts = read_string_chunks(alc, fc); + Array *parts = string_read_format_chunks(alc, fc, body); tok_expect(fc, "{", true, false); @@ -59,46 +57,22 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, VFString *vfs = al(alc, sizeof(VFString)); vfs->parts = parts; vfs->values = values; - vfs->line = before_str->line; - vfs->col = before_str->col; + vfs->line = chunk->line; + vfs->col = chunk->col; Type *rett = type_gen(fc->b, alc, "String"); rett->strict_ownership = true; v = value_init(alc, v_fstring, vfs, rett); tok_expect(fc, "}", false, true); } else { + body = string_replace_backslash_chars(alc, body); Type *rett = type_gen(fc->b, alc, "String"); rett->strict_ownership = true; v = value_init(alc, v_string, body, rett); } // - } else if (strcmp(token, "'") == 0) { - char ch = get_char(fc, 0); - chunk_move(fc->chunk, 1); - if (ch == '\\') { - char nch = get_char(fc, 0); - chunk_move(fc->chunk, 1); - if (nch == '0') { - ch = '\0'; - } else if (nch == 'n') { - ch = '\n'; - } else if (nch == 'r') { - ch = '\r'; - } else if (nch == 't') { - ch = '\t'; - } else if (nch == 'v') { - ch = '\v'; - } else if (nch == 'f') { - ch = '\f'; - } else if (nch == 'b') { - ch = '\b'; - } else if (nch == 'a') { - ch = '\a'; - } - } - - tok_expect(fc, "'", true, false); - + } else if (t == tok_char_string) { + char ch = token[0]; v = vgen_vint(alc, ch, type_gen(b, alc, "u8"), true); // } else if (strcmp(token, "!") == 0) { diff --git a/src/headers/functions.h b/src/headers/functions.h index 3ab09d4f..19eeac4c 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -30,6 +30,7 @@ bool is_valid_hex_number(char *str); bool is_valid_macro_number(char *str); bool ends_with(const char *str, const char *suffix); bool starts_with(const char *a, const char *b); +char backslash_char(char ch); // Alloc Allocator *alc_make(); @@ -176,9 +177,6 @@ void stage_3_class(Fc *fc, Class *class); // void stage_2_3_circular(Build *b, Class *class); // void stage_2_3_shared_circular_refs(Build *b, Class *class); -// Lexer -char convert_backslash_char(char ch); - // Read Chunk *chunk_init(Allocator *alc, Fc *fc); Chunk *chunk_clone(Allocator *alc, Chunk *chunk); @@ -194,11 +192,12 @@ void tok_expect(Fc *fc, char *expect, bool sameline, bool allow_space); char get_char(Fc *fc, int index); void read_hex(Fc *fc, char *token); Str *read_string(Fc *fc); -Array *read_string_chunks(Allocator *alc, Fc *fc); +Array *string_read_format_chunks(Allocator *alc, Fc* fc, char *body); +char* string_replace_backslash_chars(Allocator* alc, char* body); char *read_part(Allocator *alc, Fc *fc, int i, int len); // Skips -void skip_body(Fc *fc, char until_ch); +void skip_body(Fc *fc); void skip_string(Fc *fc, char end_char); void skip_until_char(Fc *fc, char *find); void skip_whitespace(Fc *fc); diff --git a/src/syntax.c b/src/syntax.c index 1df05d1e..12bf949a 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -166,3 +166,24 @@ bool starts_with(const char *a, const char *b) { return 1; return 0; } + +char backslash_char(char ch) { + if (ch == 'n') { + ch = '\n'; + } else if (ch == 'r') { + ch = '\r'; + } else if (ch == 't') { + ch = '\t'; + } else if (ch == 'f') { + ch = '\f'; + } else if (ch == 'b') { + ch = '\b'; + } else if (ch == 'v') { + ch = '\v'; + } else if (ch == 'f') { + ch = '\f'; + } else if (ch == 'a') { + ch = '\a'; + } + return ch; +} From e20e2cd59cf22507dc1a2d1357a71132cbf465fe Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 23:01:05 +0100 Subject: [PATCH 06/16] lexer updates --- ROADMAP.md | 2 +- lib/src/fs/mime.ki | 4 ++-- lib/src/http/router.ki | 4 ++-- lib/src/net/socket-tcp.ki | 4 ++-- lib/src/os/thread.ki | 4 ++-- lib/src/type/array.ki | 4 ++-- lib/src/type/map.ki | 4 ++-- src/build/lexer.c | 17 ++++++++++++++++- src/build/read.c | 11 +++++++++-- src/build/skip.c | 2 +- src/build/stage-1-parse.c | 3 ++- src/build/stage-2-2-props.c | 8 +++++++- src/build/stage-4-1-ast.c | 21 ++++++++++++++------- src/build/value.c | 6 +++--- src/headers/enums.h | 2 +- 15 files changed, 66 insertions(+), 30 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index eada1554..81b8549f 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -65,7 +65,7 @@ Feature todo list in order: - format string - http server - __eq -- {{ ... }} (value-scopes) +- <{ ... } (value-scopes) - __before_free - strict ownership types - shared ownership types diff --git a/lib/src/fs/mime.ki b/lib/src/fs/mime.ki index 1eef5c3d..a4debf1c 100644 --- a/lib/src/fs/mime.ki +++ b/lib/src/fs/mime.ki @@ -2,7 +2,7 @@ global g_mimes : ?Map[String]; fn mime(extension_no_dot: &String) String { - let mimes = g_mimes ?? {{ + let mimes = g_mimes ?? <{ let mimes = Map[String]{}; mimes.set("aac", "audio/aac"); @@ -84,7 +84,7 @@ fn mime(extension_no_dot: &String) String { let ref = share(mimes); g_mimes = mimes; return ref; - }}; + }; let mime = mimes.get(extension_no_dot) !? "application/octet-stream"; //println("MI:"+mime); diff --git a/lib/src/http/router.ki b/lib/src/http/router.ki index df103fe6..980723c5 100644 --- a/lib/src/http/router.ki +++ b/lib/src/http/router.ki @@ -63,12 +63,12 @@ class Router[T] { throw invalid_route; } - let find = blocks.get(key) !? {{ + let find = blocks.get(key) !? <{ let v = RouteBlock[T]{ blocks: Map[RouteBlock[T]]{} }; let ref = share(v); blocks.set(key, v); return ref @as RouteBlock[T]; - }}; + }; block = find; } diff --git a/lib/src/net/socket-tcp.ki b/lib/src/net/socket-tcp.ki index 9f998f4e..6075ba94 100644 --- a/lib/src/net/socket-tcp.ki +++ b/lib/src/net/socket-tcp.ki @@ -73,7 +73,7 @@ class SocketTCP async { fn connect() +Connection !connection_failed { - let c = this.connection ?? {{ + let c = this.connection ?? <{ let c = Connection { fd: this.fd open: false, @@ -81,7 +81,7 @@ class SocketTCP async { let res = share(c); this.connection = c; return res; - }}; + }; if !c.open { #if OS == win diff --git a/lib/src/os/thread.ki b/lib/src/os/thread.ki index 25cf2521..9ddd51e4 100644 --- a/lib/src/os/thread.ki +++ b/lib/src/os/thread.ki @@ -34,7 +34,7 @@ class Thread[T] { rep thr = thr ?! throw fail;; let t = CLASS{ os_handle: thr }; #else - let thr = mem:alloc(@sizeof_class(pt.pthread_t)) @as pt.pthread_t; + let thr = mem:alloc(sizeof_class(pt.pthread_t)) @as pt.pthread_t; let err = pt.pthread_create(thr, null, CLASS.entry, start_func @as ptr); if err != 0 { throw fail; @@ -65,7 +65,7 @@ class Thread[T] { rep thr = thr ?! throw fail;; let t = CLASS{ os_handle: thr }; #else - let thr = mem:alloc(@sizeof_class(pt.pthread_t)) @as pt.pthread_t; + let thr = mem:alloc(sizeof_class(pt.pthread_t)) @as pt.pthread_t; let err = pt.pthread_create(thr, null, CLASS.entry, entry_data @as ptr); if err != 0 { throw fail; diff --git a/lib/src/type/array.ki b/lib/src/type/array.ki index 23d1ebbf..9bc31684 100644 --- a/lib/src/type/array.ki +++ b/lib/src/type/array.ki @@ -7,11 +7,11 @@ use mem; // "{}" values @repeat; // } // output { -// "{{\n" +// "<{\n" // "let array = Array[%type].new();\n" // "array.push(%values);\n" // "return array;\n" -// "}}" +// "}" // } // } diff --git a/lib/src/type/map.ki b/lib/src/type/map.ki index 239af4de..4152c616 100644 --- a/lib/src/type/map.ki +++ b/lib/src/type/map.ki @@ -5,11 +5,11 @@ // "{}" values @repeat @replace("=>", ","); // } // output { -// "{{\n" +// "<{\n" // "let map = Map[%type].new();\n" // "map.set(%values);\n" // "return map;\n" -// "}}" +// "}" // } // } diff --git a/src/build/lexer.c b/src/build/lexer.c index e3c9bec9..05d3af57 100644 --- a/src/build/lexer.c +++ b/src/build/lexer.c @@ -20,6 +20,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, bracket_table['('] = ')'; bracket_table['['] = ']'; bracket_table['{'] = '}'; + bracket_table['<'] = '}'; int cc_depth = 0; @@ -176,6 +177,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, tokens[o++] = ch; } if(ch == 0){ + chunk->i = i; sprintf(fc->sbuf, "Missing string closing tag '\"', compiler reached end of file"); fc_error(fc); } @@ -198,6 +200,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, ch = backslash_char(ch); } if(content[i++] != '\'') { + chunk->i = i; sprintf(fc->sbuf, "Missing character closing tag ('), found '%c'", content[i - 1]); fc_error(fc); } @@ -207,11 +210,14 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, continue; } // Scopes - if(ch == '(' || ch == '[' || ch == '{') { + if(ch == '(' || ch == '[' || ch == '{' || (ch == '<' && content[i] == '{')) { tokens[o++] = tok_scope_open; int index = o; o += sizeof(int); tokens[o++] = ch; + if(ch == '<') { + tokens[o++] = content[i++]; + } tokens[o++] = '\0'; closer_chars[depth] = bracket_table[ch]; closer_indexes[depth] = index; @@ -293,6 +299,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, } if(depth > 0) { + chunk->i = i; sprintf(fc->sbuf, "Missing closing tag '%c'", closer_chars[depth - 1]); fc_error(fc); } @@ -301,4 +308,12 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, tokens[o++] = '\0'; chunk->tokens = tokens; + + // Probably will never happen + // if (err_token_i > -1) { + // *err_content_i = i; + // *err_line = line; + // *err_col = col; + // return; + // } } diff --git a/src/build/read.c b/src/build/read.c index f03d5efa..d44d4e37 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -83,14 +83,18 @@ char* tok_next(Chunk* chunk, bool sameline, bool allow_space, bool update) { char* res = tok_read(chunk, &x); char t = chunk->token; if(t == tok_space) { - if(!allow_space) + if(!allow_space) { + chunk->token = tok_none; return ""; + } res = tok_read(chunk, &x); t = chunk->token; } if(t == tok_newline) { - if(!allow_space || sameline) + if(!allow_space || sameline){ + chunk->token = tok_none; return ""; + } res = tok_read(chunk, &x); } if(update) { @@ -261,6 +265,9 @@ Array *string_read_format_chunks(Allocator *alc, Fc* fc, char *body) { str_append_char(buf, ch); } + array_push(result, str_to_chars(alc, buf)); + str_clear(buf); + return result; } diff --git a/src/build/skip.c b/src/build/skip.c index 83db04c2..c7e4b540 100644 --- a/src/build/skip.c +++ b/src/build/skip.c @@ -80,7 +80,7 @@ void skip_macro_if(Fc *fc) { if(chunk->token == tok_cc) { if(strcmp(token, "if") == 0) { depth++; - } else if(strcmp(token, "elif") == 0 || strcmp(token, "else")) { + } else if(strcmp(token, "elif") == 0 || strcmp(token, "else") == 0) { if(depth == 1) { depth--; break; diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index a8dfc96f..160696d8 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -35,7 +35,8 @@ void stage_1(Fc *fc) { if (token[0] == 0) break; - if (*t_ == tok_cc) { + int t = *t_; + if (t == tok_cc) { rtok(fc); skip_whitespace(fc); read_macro(fc, fc->alc, fc->scope); diff --git a/src/build/stage-2-2-props.c b/src/build/stage-2-2-props.c index 0b8273e8..cd9b2393 100644 --- a/src/build/stage-2-2-props.c +++ b/src/build/stage-2-2-props.c @@ -33,6 +33,9 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext printf("> Read class properties: %s\n", class->dname); } + Chunk *chunk = fc->chunk; + char *t_ = &chunk->token; + while (true) { tok(fc, token, false, true); @@ -46,7 +49,10 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext break; } - if (strcmp(token, "#") == 0) { + int t = *t_; + if (t == tok_cc) { + rtok(fc); + skip_whitespace(fc); read_macro(fc, fc->alc, scope); continue; } diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index ebcad6b7..70673428 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -104,7 +104,10 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { Allocator *alc = fc->alc_ast; bool is_vscope = scope->vscope != NULL; - const char *end_char = is_vscope ? "}}" : "}"; + const char *end_char = is_vscope ? "}" : "}"; + + Chunk *chunk = fc->chunk; + char *t_ = &chunk->token; while (true) { // @@ -115,7 +118,11 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { fc_error(fc); } - if (strcmp(token, "#") == 0) { + + int t = *t_; + if (t == tok_cc) { + rtok(fc); + skip_whitespace(fc); read_macro(fc, alc, scope); continue; } @@ -460,7 +467,7 @@ void token_return(Allocator *alc, Fc *fc, Scope *scope) { Type *rett = vscope->vscope->rett; Value *val = read_value(fc, alc, scope, true, 0, false); if (type_is_void(val->rett)) { - sprintf(fc->sbuf, "You cannot return a void value inside a value scope '{{'"); + sprintf(fc->sbuf, "You cannot return a void value inside a value scope '<{'"); fc_error(fc); } if (rett) { @@ -631,7 +638,7 @@ void token_if(Allocator *alc, Fc *fc, Scope *scope) { read_ast(fc, sub, single); tok(fc, token, false, true); - if (strcmp(token, "else") == 0) { + if (strcmp(token, "else") == 0 && fc->chunk->token != tok_cc) { tok(fc, token, true, true); bool has_if = strcmp(token, "if") == 0; if (has_if) { @@ -893,11 +900,11 @@ void stage_4_1_gen_main(Fc *fc) { chunk->content = str_to_chars(alc, code); chunk->length = code->length; - chunk->i = 1; - chunk->line = 1; - chunk->col = 1; + chunk->i = 0; fc->chunk = chunk; chunk_lex_start(chunk); + // Skip first character + tok(fc, NULL, false, true); read_ast(fc, scope, false); } diff --git a/src/build/value.c b/src/build/value.c index 7d0d0084..b5775658 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -296,7 +296,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, } v = vgen_vint(alc, class->size, type_gen(b, alc, "i32"), false); // - } else if (strcmp(token, "{{") == 0) { + } else if (strcmp(token, "<{") == 0) { // value scope // tok_expect(fc, ":", false, true); // Type *rett = read_type(fc, alc, scope, false, true, rtc_func_rett); @@ -312,7 +312,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, read_ast(fc, sub, false); if (!sub->did_return || !sub->vscope->rett) { - sprintf(fc->sbuf, "The value scope '{{' did not return a value"); + sprintf(fc->sbuf, "The value scope '<{' did not return a value"); fc_error(fc); } @@ -413,7 +413,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Idf *idf = read_idf(fc, scope, sameline, true); v = value_handle_idf(fc, alc, scope, idf); } else { - sprintf(fc->sbuf, "Unknown value: '%s'", token); + sprintf(fc->sbuf, "Unknown value: '%s' (token_type: %d)", token, t); fc_error(fc); } diff --git a/src/headers/enums.h b/src/headers/enums.h index 57c29168..644bf69b 100644 --- a/src/headers/enums.h +++ b/src/headers/enums.h @@ -10,7 +10,7 @@ enum TOKENS { tok_string, tok_char_string, tok_op3, - tok_op2, + tok_op2, // 10 tok_op1, tok_char, tok_scope_open, From 54dea3994758e2c0eabfed282d24bdd9a43bbbea Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 23:23:09 +0100 Subject: [PATCH 07/16] change tok usage --- src/build/class.c | 3 +- src/build/id.c | 12 ++--- src/build/macro.c | 18 +++---- src/build/read.c | 3 +- src/build/skip.c | 6 +-- src/build/stage-1-parse.c | 11 ++--- src/build/stage-2-2-props.c | 24 +++++---- src/build/stage-2-6-read-types.c | 22 ++++----- src/build/stage-4-1-ast.c | 42 +++++++--------- src/build/type.c | 35 +++++++------ src/build/value.c | 84 ++++++++++++++++---------------- 11 files changed, 119 insertions(+), 141 deletions(-) diff --git a/src/build/class.c b/src/build/class.c index cb37f72b..4c66fd54 100644 --- a/src/build/class.c +++ b/src/build/class.c @@ -553,14 +553,13 @@ Class *class_get_generic_class(Class *class, Array *types) { Array *read_generic_types(Fc *fc, Scope *scope, Class *class) { // - char *token = fc->token; tok_expect(fc, "[", true, true); Array *types = array_make(fc->alc, class->generic_names->length + 1); while (true) { Type *type = read_type(fc, fc->alc, scope, true, true, rtc_default); array_push(types, type); - tok(fc, token, true, true); + char *token = tok(fc, NULL, true, true); if (strcmp(token, ",") != 0 && strcmp(token, "]") != 0) { sprintf(fc->sbuf, "Unexpected token '%s'", token); fc_error(fc); diff --git a/src/build/id.c b/src/build/id.c index 019ded1c..c799a770 100644 --- a/src/build/id.c +++ b/src/build/id.c @@ -24,11 +24,10 @@ Idf *idf_init_item(Allocator *alc, int type, void *item) { Id *read_id(Fc *fc, bool sameline, bool allow_space, bool crash) { // - char *token = fc->token; Id *id = fc->id_buf; id->has_nsc = false; - tok(fc, token, sameline, allow_space); + char* token = tok(fc, NULL, sameline, allow_space); // if (token[0] == ':') { // strcpy(token, "main"); @@ -46,7 +45,7 @@ Id *read_id(Fc *fc, bool sameline, bool allow_space, bool crash) { id->has_nsc = true; strcpy(id->nsc_name, token); chunk_move(fc->chunk, 1); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); if (!is_valid_varname(token)) { if (!crash) @@ -66,8 +65,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { bool lsp = fc->lsp_file && lsp_check(fc); Build *b = fc->b; - char *token = fc->token; - tok(fc, token, sameline, allow_space); + char* token = tok(fc, NULL, sameline, allow_space); Idf *idf = NULL; @@ -132,7 +130,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { build_end(b, 0); } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); id.has_nsc = false; id.name = token; @@ -152,7 +150,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { char buf[256]; strcpy(buf, token); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); idf = idf_get_from_header(rfc, token, 0); if (!idf) { diff --git a/src/build/macro.c b/src/build/macro.c index 069b0766..42e52d01 100644 --- a/src/build/macro.c +++ b/src/build/macro.c @@ -11,8 +11,7 @@ MacroScope *init_macro_scope(Allocator *alc) { void read_macro(Fc *fc, Allocator *alc, Scope *scope) { // - char *token = fc->token; - tok(fc, token, true, false); + char *token = tok(fc, NULL, true, false); if (strcmp(token, "if") == 0) { MacroScope *mc = fc->current_macro_scope; @@ -91,8 +90,7 @@ void read_macro(Fc *fc, Allocator *alc, Scope *scope) { bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { // - char *token = fc->token; - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); bool result = false; @@ -192,7 +190,7 @@ bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { fc_error(fc); } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); result = map_get(class->props, token) ? true : false; @@ -210,7 +208,7 @@ bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { fc_error(fc); } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); result = map_get(class->funcs, token) ? true : false; @@ -242,19 +240,19 @@ bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { } result = strcmp(value, "1") == 0; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, "==") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); result = strcmp(value, token) == 0; } else if (strcmp(token, "!=") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); result = strcmp(value, token) != 0; } else { rtok(fc); } } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, "&&") == 0) { result = result && macro_resolve_if_value(fc, scope, mc); } else if (strcmp(token, "||") == 0) { diff --git a/src/build/read.c b/src/build/read.c index d44d4e37..488c4712 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -134,8 +134,7 @@ char* tok_read(Chunk* chunk, int *i_ref) { void rtok(Fc *fc) { *fc->chunk = *fc->chunk_prev; } void tok_expect(Fc *fc, char *expect, bool sameline, bool allow_space) { - char token[KI_TOKEN_MAX]; - tok(fc, token, sameline, allow_space); + char* token = tok(fc, NULL, sameline, allow_space); if (strcmp(token, expect) != 0) { sprintf(fc->sbuf, "Expected: '%s', but found: '%s'", expect, token); fc_error(fc); diff --git a/src/build/skip.c b/src/build/skip.c index c7e4b540..0471ae5d 100644 --- a/src/build/skip.c +++ b/src/build/skip.c @@ -106,9 +106,8 @@ void skip_macro_if(Fc *fc) { void skip_traits(Fc *fc) { // - char *token = fc->token; while (true) { - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); if (is_valid_varname_char(token[0])) { rtok(fc); read_id(fc, false, true, true); @@ -180,10 +179,9 @@ void skip_type(Fc *fc) { void skip_macro_input(Fc *fc, char *end) { - char *token = fc->token; while (true) { - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); if (token[0] == '\0') { sprintf(fc->sbuf, "Your macro is missing a closing tag, unexpected end of file"); diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index 160696d8..5a3e2fcd 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -46,7 +46,7 @@ void stage_1(Fc *fc) { bool private = false; if (strcmp(token, "-") == 0) { private = true; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } // Indentifiers @@ -534,8 +534,7 @@ void stage_1_header(Fc *fc) { } else { tok_expect(fc, "as", true, true); - char *token = fc->token; - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (!is_valid_varname_char(token[0])) { sprintf(fc->sbuf, "Invalid variable name syntax: '%s'", token); @@ -890,7 +889,7 @@ void stage_1_macro(Fc *fc) { } // int type; - // tok(fc, token, false, true); + // token = tok(fc, NULL, false, true); // if (strcmp(token, "T") == 0) { // type = macro_part_type; // } else if (strcmp(token, "V") == 0) { @@ -900,10 +899,10 @@ void stage_1_macro(Fc *fc) { // fc_error(fc); // } - // tok(fc, token, true, false); + // token = tok(fc, NULL, true, false); // if (strcmp(token, "*") == 0) { // infinite = true; - // tok(fc, token, true, false); + // token = tok(fc, NULL, true, false); // } // if (strcmp(token, ":") != 0) { // sprintf(fc->sbuf, "Expected ':', found: '%s'", pattern); diff --git a/src/build/stage-2-2-props.c b/src/build/stage-2-2-props.c index cd9b2393..d7df92a7 100644 --- a/src/build/stage-2-2-props.c +++ b/src/build/stage-2-2-props.c @@ -23,7 +23,6 @@ void stage_2_2(Fc *fc) { void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_extend) { // - char *token = fc->token; Scope *scope = class->scope; Class *prev_error_class_info = fc->error_class_info; @@ -38,7 +37,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext while (true) { - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); if (token[0] == 0) { sprintf(fc->sbuf, "Unexpected end of file"); @@ -96,20 +95,19 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext if (strcmp(token, "-") == 0) { act = act_private; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } else if (strcmp(token, "~") == 0) { act = act_readonly; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char next_token[KI_TOKEN_MAX]; - tok(fc, next_token, true, true); + char* next_token = tok(fc, NULL, true, true); if (strcmp(token, "static") == 0 && strcmp(next_token, "fn") == 0) { is_static = true; - strcpy(token, next_token); + token = next_token; } else { rtok(fc); } @@ -125,7 +123,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext } else if (strcmp(token, "fn") == 0) { // Function *def_chunk = *fc->chunk; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); bool borrow = true; bool ref = false; @@ -134,14 +132,14 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext if (strcmp(token, "!") == 0) { will_exit = true; *def_chunk = *fc->chunk; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } if (!is_static) { if (strcmp(token, ">") == 0) { borrow = false; *def_chunk = *fc->chunk; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } } @@ -181,7 +179,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext class->func_before_free = func; } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, "(") == 0) { func->chunk_args = chunk_clone(fc->alc, fc->chunk); skip_body(fc); @@ -223,7 +221,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext tok_expect(fc, ":", true, true); - // tok(fc, token, true, true); + // token = tok(fc, NULL, true, true); // if (strcmp(token, ":") == 0) { Type *type = read_type(fc, fc->alc, scope, true, true, rtc_prop_type); if (type_is_void(type)) { @@ -235,7 +233,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext } prop->type = type; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); // } if (strcmp(token, "=") == 0) { diff --git a/src/build/stage-2-6-read-types.c b/src/build/stage-2-6-read-types.c index 9d4c387a..8a5fdd46 100644 --- a/src/build/stage-2-6-read-types.c +++ b/src/build/stage-2-6-read-types.c @@ -56,16 +56,16 @@ void stage_2_6_class_functions(Fc *fc, Class *class) { void stage_2_6_func(Fc *fc, Func *func) { // + char* token; Func *prev_error_func_info = fc->error_func_info; fc->error_func_info = func; // - char *token = fc->token; Allocator *alc = fc->alc; // Args fc->chunk = func->chunk_args; if (func->parse_args) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); while (strcmp(token, ")") != 0) { if (!is_valid_varname(token)) { @@ -86,7 +86,7 @@ void stage_2_6_func(Fc *fc, Func *func) { Type *type = read_type(fc, alc, func->scope->parent, true, true, rtc_func_arg); - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, "=") == 0) { val_chunk = chunk_clone(alc, fc->chunk); skip_value(fc); @@ -94,9 +94,9 @@ void stage_2_6_func(Fc *fc, Func *func) { rtok(fc); } - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ",") == 0) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } else if (strcmp(token, ")") != 0) { sprintf(fc->sbuf, "Unexpected token '%s'", token); fc_error(fc); @@ -113,11 +113,11 @@ void stage_2_6_func(Fc *fc, Func *func) { } // Return type - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, "!") != 0 && strcmp(token, "%") != 0 && strcmp(token, "{") != 0) { rtok(fc); func->rett = read_type(fc, alc, func->scope->parent, true, true, rtc_func_rett); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } if (func->will_exit && !type_is_void(func->rett)) { @@ -133,7 +133,7 @@ void stage_2_6_func(Fc *fc, Func *func) { func->can_error = true; } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid error name '%s'", token); fc_error(fc); @@ -144,13 +144,13 @@ void stage_2_6_func(Fc *fc, Func *func) { } array_push(errors, dups(alc, token)); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } func->errors = errors; while (strcmp(token, "%") == 0) { - tok(fc, token, false, false); + token = tok(fc, NULL, false, false); if (strcmp(token, "hot") == 0) { func->opt_hot = true; } else if (strcmp(token, "inline") == 0) { @@ -160,7 +160,7 @@ void stage_2_6_func(Fc *fc, Func *func) { fc_error(fc); } - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } // Define arguments in AST diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index 70673428..37a0bdb9 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -99,7 +99,7 @@ void stage_4_1_func(Fc *fc, Func *func) { void read_ast(Fc *fc, Scope *scope, bool single_line) { // - char *token = fc->token; + char *token; bool first_line = true; Allocator *alc = fc->alc_ast; @@ -111,7 +111,7 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { while (true) { // - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (token[0] == 0) { sprintf(fc->sbuf, "Unexpected end of file"); @@ -259,7 +259,7 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { // Assign if (value_is_assignable(left)) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); sprintf(fc->sbuf, ".%s.", token); if (strstr(".=.+=.-=.*=./=.", fc->sbuf)) { char *sign = dups(alc, token); @@ -388,9 +388,7 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { void token_declare(Allocator *alc, Fc *fc, Scope *scope, bool replace) { // - char *token = fc->token; - - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); if (is_valid_varname_char(token[0])) { sprintf(fc->sbuf, "Invalid variable name syntax '%s'", token); @@ -414,10 +412,10 @@ void token_declare(Allocator *alc, Fc *fc, Scope *scope, bool replace) { Type *type = NULL; - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ":") == 0) { type = read_type(fc, alc, scope, false, true, rtc_decl); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } if (strcmp(token, "=") != 0) { @@ -570,15 +568,13 @@ void token_throw(Allocator *alc, Fc *fc, Scope *scope) { fc_error(fc); } - char *token = fc->token; Array *errors = func->errors; - if (!func->errors) { sprintf(fc->sbuf, "Missing list of errors (compiler bug)"); fc_error(fc); } - tok(fc, token, true, true); + char* token = tok(fc, NULL, true, true); int index = array_find(errors, token, arr_find_str); if (index < 0) { sprintf(fc->sbuf, "The function has no error named '%s'", token); @@ -591,7 +587,7 @@ void token_throw(Allocator *alc, Fc *fc, Scope *scope) { throw->code = index + 1; throw->msg = NULL; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, ",") == 0) { Value *msg = read_value(fc, alc, scope, false, 0, false); if (msg->type != v_string) { @@ -612,15 +608,13 @@ void token_throw(Allocator *alc, Fc *fc, Scope *scope) { void token_if(Allocator *alc, Fc *fc, Scope *scope) { // - char *token = fc->token; - Value *cond = read_value(fc, alc, scope, true, 0, false); if (!type_is_bool(cond->rett, fc->b)) { sprintf(fc->sbuf, "Condition value must return a bool"); fc_error(fc); } - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); bool single = false; if (strcmp(token, "{") == 0) { } else if (strcmp(token, ":") == 0) { @@ -637,15 +631,15 @@ void token_if(Allocator *alc, Fc *fc, Scope *scope) { read_ast(fc, sub, single); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, "else") == 0 && fc->chunk->token != tok_cc) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); bool has_if = strcmp(token, "if") == 0; if (has_if) { token_if(alc, fc, else_scope); } else { rtok(fc); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); bool single = false; if (strcmp(token, "{") == 0) { } else if (strcmp(token, ":") == 0) { @@ -673,7 +667,6 @@ void token_if(Allocator *alc, Fc *fc, Scope *scope) { void token_while(Allocator *alc, Fc *fc, Scope *scope) { // - char *token = fc->token; Scope *sub = usage_scope_init(alc, scope, sct_loop); Value *cond = read_value(fc, alc, sub, true, 0, false); @@ -682,7 +675,7 @@ void token_while(Allocator *alc, Fc *fc, Scope *scope) { fc_error(fc); } - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); bool single = false; if (strcmp(token, "{") == 0) { } else if (strcmp(token, ":") == 0) { @@ -709,7 +702,6 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { int col = fc->chunk->col; int line = fc->chunk->line; - char *token = fc->token; Value *value = read_value(fc, alc, scope, true, 0, false); Value *on = vgen_ir_val(alc, value, value->rett); array_push(scope->ast, token_init(alc, tkn_ir_val, on->item)); @@ -731,7 +723,7 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { } tok_expect(fc, "as", false, true); - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); @@ -741,11 +733,11 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { char *key_name = NULL; char *value_name = dups(alc, token); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ",") == 0) { key_name = value_name; - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); @@ -757,7 +749,7 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { name_taken_check(fc, scope, token); value_name = dups(alc, token); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } // Declare next_key diff --git a/src/build/type.c b/src/build/type.c index a2e6e8d1..a938aca6 100644 --- a/src/build/type.c +++ b/src/build/type.c @@ -127,7 +127,6 @@ Type *type_gen_void(Allocator *alc) { Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_space, int context) { // - char *token = fc->token; bool nullable = false; bool t_inline = false; bool async = false; @@ -138,36 +137,36 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ bool weak_ptr = false; bool inline_ = false; - tok(fc, token, sameline, allow_space); + char* token = tok(fc, NULL, sameline, allow_space); if (strcmp(token, "async") == 0) { async = true; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } if (strcmp(token, ".") == 0) { inline_ = true; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } else if (strcmp(token, "raw") == 0) { raw_ptr = true; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } else if (strcmp(token, "weak") == 0) { if (context != rtc_prop_type) { sprintf(fc->sbuf, "'weak' types are only allowed for object properties"); fc_error(fc); } weak_ptr = true; - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } else if (strcmp(token, "&") == 0) { borrow = true; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } else if (strcmp(token, "+") == 0) { shared_ref = true; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } if (strcmp(token, "?") == 0) { nullable = true; - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } if (inline_ && (borrow || shared_ref || raw_ptr || weak_ptr || nullable)) { @@ -195,7 +194,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ Array *args = array_make(alc, 2); tok_expect(fc, "(", true, true); - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); while (strcmp(token, ")") != 0) { rtok(fc); @@ -204,9 +203,9 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ Arg *arg = arg_init(alc, "", type); array_push(args, arg); - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, ",") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); } } type->func_args = args; @@ -216,7 +215,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ type->func_rett = read_type(fc, alc, scope, true, true, rtc_func_rett); tok_expect(fc, ")", true, true); - // i = tok(fc, token, true, true); + // i = token = tok(fc, NULL, true, true); // if (strcmp(token, "or") == 0) { // // Read error codes @@ -225,7 +224,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ // type->func_error_codes = array_make(2); // while (true) { - // fc->i = tok(fc, token, true, true); + // fc->i = token = tok(fc, NULL, true, true); // if (!is_valid_varname(token)) { // fc_error(fc, "Invalid error code syntax: '%s'", token); // } @@ -235,7 +234,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ // array_push(type->func_error_codes, strdup(token)); - // i = tok(fc, token, true, true); + // i = token = tok(fc, NULL, true, true); // if (strcmp(token, ",") == 0) { // fc->i = i; // continue; @@ -284,9 +283,9 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ } } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); while (strcmp(token, "[") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); int count = -1; if (is_valid_number(token)) { count = atoi(token); @@ -300,7 +299,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ type = type_array_of(alc, fc->b, type, count); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } rtok(fc); // diff --git a/src/build/value.c b/src/build/value.c index b5775658..6a6ef544 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -18,12 +18,11 @@ Value *value_init(Allocator *alc, int type, void *item, Type *rett) { Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, bool assignable) { // - char *token = fc->token; Build *b = fc->b; Value *v = NULL; Chunk* chunk = fc->chunk; - char* tkn = tok(fc, token, sameline, true); + char* token = tok(fc, NULL, sameline, true); char t = chunk->token; bool skip_move = assignable; @@ -34,7 +33,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, skip_move = true; // } else if (t == tok_string) { - char *body = tkn; + char *body = token; if (get_char(fc, 0) == '{') { // Format string Array *parts = string_read_format_chunks(alc, fc, body); @@ -235,7 +234,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, fc_error(fc); } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); int op = op_add; if (strcmp(token, "ADD") == 0) { @@ -346,7 +345,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, bool is_negative = strcmp(token, "-") == 0; if (is_negative) { - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } bool is_float = false; @@ -367,7 +366,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, is_float = true; chunk_move(fc->chunk, 1); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); float_str = al(alc, strlen(num_str) + strlen(token) + 2); strcpy(float_str, num_str); @@ -422,7 +421,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, fc_error(fc); } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); while (strcmp(token, ".") == 0 || strcmp(token, "(") == 0 || strcmp(token, "++") == 0 || strcmp(token, "--") == 0 || strcmp(token, "[") == 0) { Type *rett = v->rett; @@ -481,7 +480,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, build_end(b, 0); } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); ClassProp *prop = map_get(class->props, token); if (prop) { @@ -567,11 +566,11 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_array_item(alc, scope, v, index); } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); } rtok(fc); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (prio == 0 || prio > 7) { while (strcmp(token, "@as") == 0) { @@ -583,7 +582,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Type *type = read_type(fc, alc, scope, false, true, rtc_ptrv); v = vgen_cast(alc, v, type); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -625,7 +624,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_this_or_that(alc, v, true_scope, left, false_scope, right, type); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -648,7 +647,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, if (strcmp(token, "?!") == 0) { // ?! - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); bool single_line = strcmp(token, "{") != 0; if (single_line) rtok(fc); @@ -676,7 +675,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_or_value(alc, v, right, usage_scope, else_scope, deref_scope); } - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -692,7 +691,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 10, false); v = value_op(fc, alc, scope, v, right, op); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -706,7 +705,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 20, false); v = value_op(fc, alc, scope, v, right, op); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -720,7 +719,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 25, false); v = value_op(fc, alc, scope, v, right, op); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -799,7 +798,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_compare(alc, b, left, right, op); } - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); sprintf(fc->sbuf, ".%s.", token); } } @@ -816,7 +815,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 35, false); v = value_op(fc, alc, scope, v, right, op); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -843,7 +842,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_and_or(alc, b, v, right, op); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } @@ -854,7 +853,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { // - char *token = fc->token; + char *token; if (idf->type == idf_decl_type_overwrite) { DeclOverwrite *dov = idf->item; @@ -940,7 +939,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { build_end(b, 0); } - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); Func *func = map_get(class->funcs, token); if (!func || !func->is_static) { sprintf(fc->sbuf, "Unknown static function: '%s'", token); @@ -957,11 +956,11 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { return vgen_fptr(alc, func, NULL); } - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (strcmp(token, "{") == 0) { // Class init Map *values = map_make(alc); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); while (strcmp(token, "}") != 0) { ClassProp *prop = map_get(class->props, token); if (!prop) { @@ -983,9 +982,9 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { map_set(values, name, value); // - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ",") == 0) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } } for (int i = 0; i < class->props->keys->length; i++) { @@ -1012,7 +1011,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { Enum *enu = idf->item; tok_expect(fc, ".", true, false); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); if (!map_contains(enu->values, token)) { sprintf(fc->sbuf, "Enum property does not exist '%s'", token); fc_error(fc); @@ -1026,7 +1025,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { Fc *rfc = idf->item; tok_expect(fc, ".", true, false); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); Idf *idf_ = idf_get_from_header(rfc, token, 0); if (!idf_) { @@ -1041,7 +1040,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { Decl *decl = idf->item; if (get_char(fc, 0) == '#') { chunk_move(fc->chunk, 1); - tok(fc, token, true, false); + token = tok(fc, NULL, true, false); Array *errors = decl->type->func_errors; int index = array_find(errors, token, arr_find_str); if (index < 0) { @@ -1078,7 +1077,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { } int count = 0; - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, mvg->end) == 0) { } else { rtok(fc); @@ -1105,7 +1104,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { } count++; - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, mvg->end) == 0) { break; } else if (strcmp(token, ",") == 0) { @@ -1468,7 +1467,6 @@ Value *try_convert(Fc *fc, Allocator *alc, Value *val, Type *to_type) { Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { // - char *token = fc->token; Type *ont = on->rett; int ontt = ont->type; @@ -1524,7 +1522,7 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { build_end(fc->b, 0); } - tok(fc, token, false, true); + char* token = tok(fc, NULL, false, true); bool named_args = strcmp(token, "{") == 0; if (named_args) { @@ -1554,10 +1552,10 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { type_check(fc, arg->type, val->rett); array_push(values, val); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ",") == 0) { if (fc->lsp_file) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); rtok(fc); } continue; @@ -1590,7 +1588,7 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { if (can_error) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, "!") == 0) { // ! if (!type_is_void(rett)) { @@ -1616,10 +1614,10 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { if (strcmp(token, "!!") == 0) { // !! - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, "|") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); @@ -1628,19 +1626,19 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { char *msg_name = NULL; // tok_expect(fc, "|", true, true); - // tok(fc, token, false, true); - tok(fc, token, false, true); + // token = tok(fc, NULL, false, true); + token = tok(fc, NULL, false, true); if (strcmp(token, ",") == 0) { - tok(fc, token, true, true); + token = tok(fc, NULL, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); } msg_name = dups(alc, token); tok_expect(fc, "|", true, true); - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } else if (strcmp(token, "|") == 0) { - tok(fc, token, false, true); + token = tok(fc, NULL, false, true); } else { sprintf(fc->sbuf, "Expected '|' or ',' but found: '%s'", token); fc_error(fc); From 4c22a7c650d9a398a4cbf384bfe938474826c418 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 23:26:12 +0100 Subject: [PATCH 08/16] change tok args --- src/build/class.c | 2 +- src/build/id.c | 10 ++-- src/build/macro.c | 16 +++---- src/build/read.c | 9 ++-- src/build/skip.c | 18 ++++---- src/build/stage-1-parse.c | 74 +++++++++++++++--------------- src/build/stage-2-2-props.c | 20 ++++---- src/build/stage-2-6-read-types.c | 20 ++++---- src/build/stage-4-1-ast.c | 34 +++++++------- src/build/type.c | 55 ++++++---------------- src/build/value.c | 78 ++++++++++++++++---------------- src/headers/functions.h | 2 +- 12 files changed, 153 insertions(+), 185 deletions(-) diff --git a/src/build/class.c b/src/build/class.c index 4c66fd54..54f8badf 100644 --- a/src/build/class.c +++ b/src/build/class.c @@ -559,7 +559,7 @@ Array *read_generic_types(Fc *fc, Scope *scope, Class *class) { Type *type = read_type(fc, fc->alc, scope, true, true, rtc_default); array_push(types, type); - char *token = tok(fc, NULL, true, true); + char *token = tok(fc, true, true); if (strcmp(token, ",") != 0 && strcmp(token, "]") != 0) { sprintf(fc->sbuf, "Unexpected token '%s'", token); fc_error(fc); diff --git a/src/build/id.c b/src/build/id.c index c799a770..e73cf33d 100644 --- a/src/build/id.c +++ b/src/build/id.c @@ -27,7 +27,7 @@ Id *read_id(Fc *fc, bool sameline, bool allow_space, bool crash) { Id *id = fc->id_buf; id->has_nsc = false; - char* token = tok(fc, NULL, sameline, allow_space); + char* token = tok(fc, sameline, allow_space); // if (token[0] == ':') { // strcpy(token, "main"); @@ -45,7 +45,7 @@ Id *read_id(Fc *fc, bool sameline, bool allow_space, bool crash) { id->has_nsc = true; strcpy(id->nsc_name, token); chunk_move(fc->chunk, 1); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); if (!is_valid_varname(token)) { if (!crash) @@ -65,7 +65,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { bool lsp = fc->lsp_file && lsp_check(fc); Build *b = fc->b; - char* token = tok(fc, NULL, sameline, allow_space); + char* token = tok(fc, sameline, allow_space); Idf *idf = NULL; @@ -130,7 +130,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { build_end(b, 0); } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); id.has_nsc = false; id.name = token; @@ -150,7 +150,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { char buf[256]; strcpy(buf, token); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); idf = idf_get_from_header(rfc, token, 0); if (!idf) { diff --git a/src/build/macro.c b/src/build/macro.c index 42e52d01..3825823d 100644 --- a/src/build/macro.c +++ b/src/build/macro.c @@ -11,7 +11,7 @@ MacroScope *init_macro_scope(Allocator *alc) { void read_macro(Fc *fc, Allocator *alc, Scope *scope) { // - char *token = tok(fc, NULL, true, false); + char *token = tok(fc, true, false); if (strcmp(token, "if") == 0) { MacroScope *mc = fc->current_macro_scope; @@ -90,7 +90,7 @@ void read_macro(Fc *fc, Allocator *alc, Scope *scope) { bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { // - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); bool result = false; @@ -190,7 +190,7 @@ bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { fc_error(fc); } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); result = map_get(class->props, token) ? true : false; @@ -208,7 +208,7 @@ bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { fc_error(fc); } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); result = map_get(class->funcs, token) ? true : false; @@ -240,19 +240,19 @@ bool macro_resolve_if_value(Fc *fc, Scope *scope, MacroScope *mc) { } result = strcmp(value, "1") == 0; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, "==") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); result = strcmp(value, token) == 0; } else if (strcmp(token, "!=") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); result = strcmp(value, token) != 0; } else { rtok(fc); } } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, "&&") == 0) { result = result && macro_resolve_if_value(fc, scope, mc); } else if (strcmp(token, "||") == 0) { diff --git a/src/build/read.c b/src/build/read.c index 488c4712..fb657e57 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -68,14 +68,11 @@ void chunk_update_col(Chunk *chunk) { chunk->col = col; } -char* tok(Fc *fc, char *token, bool sameline, bool allow_space) { +char* tok(Fc *fc, bool sameline, bool allow_space) { // Chunk *chunk = fc->chunk; *fc->chunk_prev = *chunk; - char *res = tok_next(chunk, sameline, allow_space, true); - if(token) - strcpy(token, res); - return res; + return tok_next(chunk, sameline, allow_space, true); } char* tok_next(Chunk* chunk, bool sameline, bool allow_space, bool update) { @@ -134,7 +131,7 @@ char* tok_read(Chunk* chunk, int *i_ref) { void rtok(Fc *fc) { *fc->chunk = *fc->chunk_prev; } void tok_expect(Fc *fc, char *expect, bool sameline, bool allow_space) { - char* token = tok(fc, NULL, sameline, allow_space); + char* token = tok(fc, sameline, allow_space); if (strcmp(token, expect) != 0) { sprintf(fc->sbuf, "Expected: '%s', but found: '%s'", expect, token); fc_error(fc); diff --git a/src/build/skip.c b/src/build/skip.c index 0471ae5d..b980a5a6 100644 --- a/src/build/skip.c +++ b/src/build/skip.c @@ -53,7 +53,7 @@ void skip_until_char(Fc *fc, char *find) { sprintf(fc->sbuf, "End of file, missing '%s' token", find); fc_error(fc); } - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -76,7 +76,7 @@ void skip_macro_if(Fc *fc) { Chunk *chunk = fc->chunk; int depth = 1; while (true) { - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); if(chunk->token == tok_cc) { if(strcmp(token, "if") == 0) { depth++; @@ -107,7 +107,7 @@ void skip_macro_if(Fc *fc) { void skip_traits(Fc *fc) { // while (true) { - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); if (is_valid_varname_char(token[0])) { rtok(fc); read_id(fc, false, true, true); @@ -125,7 +125,7 @@ void skip_value(Fc *fc) { Chunk *chunk = fc->chunk; while (true) { - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); char t = chunk->token; if (t == tok_string) { @@ -158,18 +158,18 @@ void skip_type(Fc *fc) { // Chunk *chunk = fc->chunk; - char * token = tok(fc, NULL, false, true); + char * token = tok(fc, false, true); if(strcmp(token, "raw") == 0 || strcmp(token, "weak") == 0){ - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } while (strcmp(token, "?") == 0 || strcmp(token, ".") == 0 || strcmp(token, "&") == 0 || strcmp(token, "+") == 0) { - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } if(chunk->token != tok_id && token[0] != ':') { sprintf(fc->sbuf, "Expected a type here"); fc_error(fc); } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); if(token[0] == '[') { skip_body(fc); } else { @@ -181,7 +181,7 @@ void skip_macro_input(Fc *fc, char *end) { while (true) { - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); if (token[0] == '\0') { sprintf(fc->sbuf, "Your macro is missing a closing tag, unexpected end of file"); diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index 5a3e2fcd..e3fe2965 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -30,7 +30,7 @@ void stage_1(Fc *fc) { while (true) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (token[0] == 0) break; @@ -46,7 +46,7 @@ void stage_1(Fc *fc) { bool private = false; if (strcmp(token, "-") == 0) { private = true; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } // Indentifiers @@ -135,12 +135,12 @@ void stage_1_func(Fc *fc, bool is_private) { // Build *b = fc->b; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); bool will_exit = false; if (strcmp(token, "!") == 0) { will_exit = true; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } if (!is_valid_varname(token)) { @@ -186,7 +186,7 @@ void stage_1_func(Fc *fc, bool is_private) { map_set(fc->scope->identifiers, name, idf); } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, "(") == 0) { func->chunk_args = chunk_clone(fc->alc, fc->chunk); skip_body(fc); @@ -209,7 +209,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { // Build *b = fc->b; Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid class name '%s'", token); @@ -251,7 +251,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { class->generic_names = array_make(fc->alc, 4); class->generics = map_make(fc->alc); - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); while (true) { if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid name"); @@ -267,9 +267,9 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { char *name = token; array_push(class->generic_names, name); - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, ",") == 0) - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); else if (strcmp(token, "]") == 0) break; else { @@ -279,21 +279,21 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { } } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); bool track = false; while (strcmp(token, "{") != 0) { if (strcmp(token, "type") == 0) { class->is_rc = false; class->track_ownership = false; tok_expect(fc, ":", true, false); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); if (strcmp(token, "ptr") == 0) { class->type = ct_ptr; class->size = fc->b->ptr_size; } else if (strcmp(token, "int") == 0 || strcmp(token, "float") == 0) { class->type = strcmp(token, "int") == 0 ? ct_int : ct_float; tok_expect(fc, ":", true, false); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); int size = 0; if (strcmp(token, "*") == 0) { size = fc->b->ptr_size; @@ -310,7 +310,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { fc_error(fc); } tok_expect(fc, ":", true, false); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); if (strcmp(token, "true") != 0 && strcmp(token, "false") != 0) { sprintf(fc->sbuf, "Invalid value for is_signed, options: true,false, received: '%s'", token); fc_error(fc); @@ -344,7 +344,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { fc_error(fc); } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } if (track) @@ -358,7 +358,7 @@ void stage_1_class(Fc *fc, bool is_struct, bool is_private) { void stage_1_trait(Fc *fc, bool is_private) { // Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char *token = tok(fc, NULL, true, true); + char *token = tok(fc, true, true); if (fc->is_header) { sprintf(fc->sbuf, "You cannot use 'trait' inside a header file"); @@ -410,7 +410,7 @@ void stage_1_extend(Fc *fc) { void stage_1_enum(Fc *fc, bool is_private) { // Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid enum name '%s'", token); @@ -435,20 +435,20 @@ void stage_1_enum(Fc *fc, bool is_private) { Map *values = map_make(fc->alc); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); while (strcmp(token, "}") != 0) { if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid enum property name '%s'", token); fc_error(fc); } char *name = token; - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ":") == 0) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); bool negative = false; if (strcmp(token, "-") == 0) { negative = true; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } if (is_valid_number(token)) { int value = 0; @@ -466,9 +466,9 @@ void stage_1_enum(Fc *fc, bool is_private) { map_set(values, name, (void *)(intptr_t)value); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ",") == 0) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); continue; } else if (strcmp(token, "}") == 0) { break; @@ -488,7 +488,7 @@ void stage_1_enum(Fc *fc, bool is_private) { val = autov++; } map_set(values, name, (void *)(intptr_t)val); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); continue; } else if (strcmp(token, "}") == 0) { break; @@ -511,7 +511,7 @@ void stage_1_enum(Fc *fc, bool is_private) { void stage_1_header(Fc *fc) { // - char* fn = tok(fc, NULL, true, true); + char* fn = tok(fc, true, true); if(fc->chunk->token != tok_string) { sprintf(fc->sbuf, "Expected a filepath here wrapped in dubbel-quotes, e.g. \"headers/mylib\""); fc_error(fc); @@ -534,7 +534,7 @@ void stage_1_header(Fc *fc) { } else { tok_expect(fc, "as", true, true); - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (!is_valid_varname_char(token[0])) { sprintf(fc->sbuf, "Invalid variable name syntax: '%s'", token); @@ -572,7 +572,7 @@ void stage_1_header(Fc *fc) { void stage_1_link(Fc *fc, int link_type) { // - char* fn = tok(fc, NULL, true, true); + char* fn = tok(fc, true, true); if(fc->chunk->token != tok_string) { sprintf(fc->sbuf, "Expected a library name here wrapped in dubbel-quotes, e.g. \"pthread\""); fc_error(fc); @@ -624,9 +624,9 @@ void stage_1_use(Fc *fc) { } char *as = nsc_name; - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (strcmp(token, "as") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name syntax '%s'", token); fc_error(fc); @@ -648,7 +648,7 @@ void stage_1_use(Fc *fc) { void stage_1_global(Fc *fc, bool shared, bool is_private) { // Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid global name syntax '%s'", token); @@ -698,7 +698,7 @@ void stage_1_alias(Fc *fc, int alias_type, bool is_private) { tok_expect(fc, "as", true, true); - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid alias name syntax '%s'", token); @@ -821,7 +821,7 @@ void stage_1_macro(Fc *fc) { Build *b = fc->b; Allocator *alc = fc->alc; - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid macro name syntax '%s'", token); @@ -858,7 +858,7 @@ void stage_1_macro(Fc *fc) { while (true) { // Read input groups - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, "\"") == 0) { // New group MacroVarGroup *mvg = al(alc, sizeof(MacroVarGroup)); @@ -889,7 +889,7 @@ void stage_1_macro(Fc *fc) { } // int type; - // token = tok(fc, NULL, false, true); + // token = tok(fc, false, true); // if (strcmp(token, "T") == 0) { // type = macro_part_type; // } else if (strcmp(token, "V") == 0) { @@ -899,16 +899,16 @@ void stage_1_macro(Fc *fc) { // fc_error(fc); // } - // token = tok(fc, NULL, true, false); + // token = tok(fc, true, false); // if (strcmp(token, "*") == 0) { // infinite = true; - // token = tok(fc, NULL, true, false); + // token = tok(fc, true, false); // } // if (strcmp(token, ":") != 0) { // sprintf(fc->sbuf, "Expected ':', found: '%s'", pattern); // fc_error(fc); // } - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid input name syntax '%s'", token); fc_error(fc); @@ -927,7 +927,7 @@ void stage_1_macro(Fc *fc) { array_push(mvg->vars, mv); while (true) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, "@repeat") == 0) { repeat = true; diff --git a/src/build/stage-2-2-props.c b/src/build/stage-2-2-props.c index d7df92a7..268f7635 100644 --- a/src/build/stage-2-2-props.c +++ b/src/build/stage-2-2-props.c @@ -37,7 +37,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext while (true) { - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); if (token[0] == 0) { sprintf(fc->sbuf, "Unexpected end of file"); @@ -95,15 +95,15 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext if (strcmp(token, "-") == 0) { act = act_private; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } else if (strcmp(token, "~") == 0) { act = act_readonly; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } Chunk *def_chunk = chunk_clone(fc->alc, fc->chunk); - char* next_token = tok(fc, NULL, true, true); + char* next_token = tok(fc, true, true); if (strcmp(token, "static") == 0 && strcmp(next_token, "fn") == 0) { is_static = true; @@ -123,7 +123,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext } else if (strcmp(token, "fn") == 0) { // Function *def_chunk = *fc->chunk; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); bool borrow = true; bool ref = false; @@ -132,14 +132,14 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext if (strcmp(token, "!") == 0) { will_exit = true; *def_chunk = *fc->chunk; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } if (!is_static) { if (strcmp(token, ">") == 0) { borrow = false; *def_chunk = *fc->chunk; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } } @@ -179,7 +179,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext class->func_before_free = func; } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, "(") == 0) { func->chunk_args = chunk_clone(fc->alc, fc->chunk); skip_body(fc); @@ -221,7 +221,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext tok_expect(fc, ":", true, true); - // token = tok(fc, NULL, true, true); + // token = tok(fc, true, true); // if (strcmp(token, ":") == 0) { Type *type = read_type(fc, fc->alc, scope, true, true, rtc_prop_type); if (type_is_void(type)) { @@ -233,7 +233,7 @@ void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_ext } prop->type = type; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); // } if (strcmp(token, "=") == 0) { diff --git a/src/build/stage-2-6-read-types.c b/src/build/stage-2-6-read-types.c index 8a5fdd46..2f94f566 100644 --- a/src/build/stage-2-6-read-types.c +++ b/src/build/stage-2-6-read-types.c @@ -65,7 +65,7 @@ void stage_2_6_func(Fc *fc, Func *func) { // Args fc->chunk = func->chunk_args; if (func->parse_args) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); while (strcmp(token, ")") != 0) { if (!is_valid_varname(token)) { @@ -86,7 +86,7 @@ void stage_2_6_func(Fc *fc, Func *func) { Type *type = read_type(fc, alc, func->scope->parent, true, true, rtc_func_arg); - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, "=") == 0) { val_chunk = chunk_clone(alc, fc->chunk); skip_value(fc); @@ -94,9 +94,9 @@ void stage_2_6_func(Fc *fc, Func *func) { rtok(fc); } - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ",") == 0) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } else if (strcmp(token, ")") != 0) { sprintf(fc->sbuf, "Unexpected token '%s'", token); fc_error(fc); @@ -113,11 +113,11 @@ void stage_2_6_func(Fc *fc, Func *func) { } // Return type - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, "!") != 0 && strcmp(token, "%") != 0 && strcmp(token, "{") != 0) { rtok(fc); func->rett = read_type(fc, alc, func->scope->parent, true, true, rtc_func_rett); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } if (func->will_exit && !type_is_void(func->rett)) { @@ -133,7 +133,7 @@ void stage_2_6_func(Fc *fc, Func *func) { func->can_error = true; } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid error name '%s'", token); fc_error(fc); @@ -144,13 +144,13 @@ void stage_2_6_func(Fc *fc, Func *func) { } array_push(errors, dups(alc, token)); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } func->errors = errors; while (strcmp(token, "%") == 0) { - token = tok(fc, NULL, false, false); + token = tok(fc, false, false); if (strcmp(token, "hot") == 0) { func->opt_hot = true; } else if (strcmp(token, "inline") == 0) { @@ -160,7 +160,7 @@ void stage_2_6_func(Fc *fc, Func *func) { fc_error(fc); } - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } // Define arguments in AST diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index 37a0bdb9..f4be27fa 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -111,7 +111,7 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { while (true) { // - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (token[0] == 0) { sprintf(fc->sbuf, "Unexpected end of file"); @@ -259,7 +259,7 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { // Assign if (value_is_assignable(left)) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); sprintf(fc->sbuf, ".%s.", token); if (strstr(".=.+=.-=.*=./=.", fc->sbuf)) { char *sign = dups(alc, token); @@ -388,7 +388,7 @@ void read_ast(Fc *fc, Scope *scope, bool single_line) { void token_declare(Allocator *alc, Fc *fc, Scope *scope, bool replace) { // - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); if (is_valid_varname_char(token[0])) { sprintf(fc->sbuf, "Invalid variable name syntax '%s'", token); @@ -412,10 +412,10 @@ void token_declare(Allocator *alc, Fc *fc, Scope *scope, bool replace) { Type *type = NULL; - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ":") == 0) { type = read_type(fc, alc, scope, false, true, rtc_decl); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } if (strcmp(token, "=") != 0) { @@ -574,7 +574,7 @@ void token_throw(Allocator *alc, Fc *fc, Scope *scope) { fc_error(fc); } - char* token = tok(fc, NULL, true, true); + char* token = tok(fc, true, true); int index = array_find(errors, token, arr_find_str); if (index < 0) { sprintf(fc->sbuf, "The function has no error named '%s'", token); @@ -587,7 +587,7 @@ void token_throw(Allocator *alc, Fc *fc, Scope *scope) { throw->code = index + 1; throw->msg = NULL; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, ",") == 0) { Value *msg = read_value(fc, alc, scope, false, 0, false); if (msg->type != v_string) { @@ -614,7 +614,7 @@ void token_if(Allocator *alc, Fc *fc, Scope *scope) { fc_error(fc); } - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); bool single = false; if (strcmp(token, "{") == 0) { } else if (strcmp(token, ":") == 0) { @@ -631,15 +631,15 @@ void token_if(Allocator *alc, Fc *fc, Scope *scope) { read_ast(fc, sub, single); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, "else") == 0 && fc->chunk->token != tok_cc) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); bool has_if = strcmp(token, "if") == 0; if (has_if) { token_if(alc, fc, else_scope); } else { rtok(fc); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); bool single = false; if (strcmp(token, "{") == 0) { } else if (strcmp(token, ":") == 0) { @@ -675,7 +675,7 @@ void token_while(Allocator *alc, Fc *fc, Scope *scope) { fc_error(fc); } - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); bool single = false; if (strcmp(token, "{") == 0) { } else if (strcmp(token, ":") == 0) { @@ -723,7 +723,7 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { } tok_expect(fc, "as", false, true); - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); @@ -733,11 +733,11 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { char *key_name = NULL; char *value_name = dups(alc, token); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ",") == 0) { key_name = value_name; - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); @@ -749,7 +749,7 @@ void token_each(Allocator *alc, Fc *fc, Scope *scope) { name_taken_check(fc, scope, token); value_name = dups(alc, token); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } // Declare next_key @@ -896,7 +896,7 @@ void stage_4_1_gen_main(Fc *fc) { fc->chunk = chunk; chunk_lex_start(chunk); // Skip first character - tok(fc, NULL, false, true); + tok(fc, false, true); read_ast(fc, scope, false); } diff --git a/src/build/type.c b/src/build/type.c index a938aca6..e4a94feb 100644 --- a/src/build/type.c +++ b/src/build/type.c @@ -137,36 +137,36 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ bool weak_ptr = false; bool inline_ = false; - char* token = tok(fc, NULL, sameline, allow_space); + char* token = tok(fc, sameline, allow_space); if (strcmp(token, "async") == 0) { async = true; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } if (strcmp(token, ".") == 0) { inline_ = true; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } else if (strcmp(token, "raw") == 0) { raw_ptr = true; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } else if (strcmp(token, "weak") == 0) { if (context != rtc_prop_type) { sprintf(fc->sbuf, "'weak' types are only allowed for object properties"); fc_error(fc); } weak_ptr = true; - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } else if (strcmp(token, "&") == 0) { borrow = true; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } else if (strcmp(token, "+") == 0) { shared_ref = true; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } if (strcmp(token, "?") == 0) { nullable = true; - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } if (inline_ && (borrow || shared_ref || raw_ptr || weak_ptr || nullable)) { @@ -194,7 +194,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ Array *args = array_make(alc, 2); tok_expect(fc, "(", true, true); - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); while (strcmp(token, ")") != 0) { rtok(fc); @@ -203,9 +203,9 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ Arg *arg = arg_init(alc, "", type); array_push(args, arg); - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, ",") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); } } type->func_args = args; @@ -215,33 +215,6 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ type->func_rett = read_type(fc, alc, scope, true, true, rtc_func_rett); tok_expect(fc, ")", true, true); - // i = token = tok(fc, NULL, true, true); - - // if (strcmp(token, "or") == 0) { - // // Read error codes - // type->func_can_error = true; - // fc->i = i; - - // type->func_error_codes = array_make(2); - // while (true) { - // fc->i = token = tok(fc, NULL, true, true); - // if (!is_valid_varname(token)) { - // fc_error(fc, "Invalid error code syntax: '%s'", token); - // } - // if (array_contains(type->func_error_codes, token, arr_find_str)) { - // fc_error(fc, "Duplicate error code: '%s'", token); - // } - - // array_push(type->func_error_codes, strdup(token)); - - // i = token = tok(fc, NULL, true, true); - // if (strcmp(token, ",") == 0) { - // fc->i = i; - // continue; - // } - // break; - // } - // } } else { rtok(fc); @@ -283,9 +256,9 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ } } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); while (strcmp(token, "[") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); int count = -1; if (is_valid_number(token)) { count = atoi(token); @@ -299,7 +272,7 @@ Type *read_type(Fc *fc, Allocator *alc, Scope *scope, bool sameline, bool allow_ type = type_array_of(alc, fc->b, type, count); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } rtok(fc); // diff --git a/src/build/value.c b/src/build/value.c index 6a6ef544..4939260e 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -22,7 +22,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *v = NULL; Chunk* chunk = fc->chunk; - char* token = tok(fc, NULL, sameline, true); + char* token = tok(fc, sameline, true); char t = chunk->token; bool skip_move = assignable; @@ -234,7 +234,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, fc_error(fc); } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); int op = op_add; if (strcmp(token, "ADD") == 0) { @@ -345,7 +345,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, bool is_negative = strcmp(token, "-") == 0; if (is_negative) { - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } bool is_float = false; @@ -366,7 +366,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, is_float = true; chunk_move(fc->chunk, 1); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); float_str = al(alc, strlen(num_str) + strlen(token) + 2); strcpy(float_str, num_str); @@ -421,7 +421,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, fc_error(fc); } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); while (strcmp(token, ".") == 0 || strcmp(token, "(") == 0 || strcmp(token, "++") == 0 || strcmp(token, "--") == 0 || strcmp(token, "[") == 0) { Type *rett = v->rett; @@ -480,7 +480,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, build_end(b, 0); } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); ClassProp *prop = map_get(class->props, token); if (prop) { @@ -566,11 +566,11 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_array_item(alc, scope, v, index); } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); } rtok(fc); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (prio == 0 || prio > 7) { while (strcmp(token, "@as") == 0) { @@ -582,7 +582,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Type *type = read_type(fc, alc, scope, false, true, rtc_ptrv); v = vgen_cast(alc, v, type); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -624,7 +624,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_this_or_that(alc, v, true_scope, left, false_scope, right, type); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -647,7 +647,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, if (strcmp(token, "?!") == 0) { // ?! - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); bool single_line = strcmp(token, "{") != 0; if (single_line) rtok(fc); @@ -675,7 +675,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_or_value(alc, v, right, usage_scope, else_scope, deref_scope); } - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -691,7 +691,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 10, false); v = value_op(fc, alc, scope, v, right, op); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -705,7 +705,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 20, false); v = value_op(fc, alc, scope, v, right, op); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -719,7 +719,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 25, false); v = value_op(fc, alc, scope, v, right, op); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -798,7 +798,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_compare(alc, b, left, right, op); } - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); sprintf(fc->sbuf, ".%s.", token); } } @@ -815,7 +815,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, Value *right = read_value(fc, alc, scope, false, 35, false); v = value_op(fc, alc, scope, v, right, op); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -842,7 +842,7 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, v = vgen_and_or(alc, b, v, right, op); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } @@ -939,7 +939,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { build_end(b, 0); } - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); Func *func = map_get(class->funcs, token); if (!func || !func->is_static) { sprintf(fc->sbuf, "Unknown static function: '%s'", token); @@ -956,11 +956,11 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { return vgen_fptr(alc, func, NULL); } - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (strcmp(token, "{") == 0) { // Class init Map *values = map_make(alc); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); while (strcmp(token, "}") != 0) { ClassProp *prop = map_get(class->props, token); if (!prop) { @@ -982,9 +982,9 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { map_set(values, name, value); // - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ",") == 0) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } } for (int i = 0; i < class->props->keys->length; i++) { @@ -1011,7 +1011,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { Enum *enu = idf->item; tok_expect(fc, ".", true, false); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); if (!map_contains(enu->values, token)) { sprintf(fc->sbuf, "Enum property does not exist '%s'", token); fc_error(fc); @@ -1025,7 +1025,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { Fc *rfc = idf->item; tok_expect(fc, ".", true, false); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); Idf *idf_ = idf_get_from_header(rfc, token, 0); if (!idf_) { @@ -1040,7 +1040,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { Decl *decl = idf->item; if (get_char(fc, 0) == '#') { chunk_move(fc->chunk, 1); - token = tok(fc, NULL, true, false); + token = tok(fc, true, false); Array *errors = decl->type->func_errors; int index = array_find(errors, token, arr_find_str); if (index < 0) { @@ -1077,7 +1077,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { } int count = 0; - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, mvg->end) == 0) { } else { rtok(fc); @@ -1104,7 +1104,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { } count++; - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, mvg->end) == 0) { break; } else if (strcmp(token, ",") == 0) { @@ -1522,7 +1522,7 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { build_end(fc->b, 0); } - char* token = tok(fc, NULL, false, true); + char* token = tok(fc, false, true); bool named_args = strcmp(token, "{") == 0; if (named_args) { @@ -1552,10 +1552,10 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { type_check(fc, arg->type, val->rett); array_push(values, val); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ",") == 0) { if (fc->lsp_file) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); rtok(fc); } continue; @@ -1588,7 +1588,7 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { if (can_error) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, "!") == 0) { // ! if (!type_is_void(rett)) { @@ -1614,10 +1614,10 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { if (strcmp(token, "!!") == 0) { // !! - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, "|") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); @@ -1625,20 +1625,18 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) { char *err_name = dups(alc, token); char *msg_name = NULL; - // tok_expect(fc, "|", true, true); - // token = tok(fc, NULL, false, true); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); if (strcmp(token, ",") == 0) { - token = tok(fc, NULL, true, true); + token = tok(fc, true, true); if (!is_valid_varname(token)) { sprintf(fc->sbuf, "Invalid variable name '%s'", token); fc_error(fc); } msg_name = dups(alc, token); tok_expect(fc, "|", true, true); - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } else if (strcmp(token, "|") == 0) { - token = tok(fc, NULL, false, true); + token = tok(fc, false, true); } else { sprintf(fc->sbuf, "Expected '|' or ',' but found: '%s'", token); fc_error(fc); diff --git a/src/headers/functions.h b/src/headers/functions.h index 19eeac4c..75ba2c9c 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -184,7 +184,7 @@ void chunk_move(Chunk *chunk, int pos); void chunk_update_col(Chunk *chunk); void chunk_lex_start(Chunk *chunk); void chunk_lex(Chunk* chunk, int err_token_i, int* err_content_i, int* err_line, int* err_col); -char* tok(Fc *fc, char *token, bool sameline, bool allow_space); +char* tok(Fc *fc, bool sameline, bool allow_space); char* tok_next(Chunk* chunk, bool sameline, bool allow_space, bool update); char* tok_read(Chunk* chunk, int *i_ref); void rtok(Fc *fc); From 9d9087c5183c6737516679904c3622cef2b6031e Mon Sep 17 00:00:00 2001 From: ctxcode Date: Tue, 30 Jan 2024 23:27:22 +0100 Subject: [PATCH 09/16] enable linker --- src/build/build.c | 3 +-- src/headers/functions.h | 8 -------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/build/build.c b/src/build/build.c index 93a66ac9..6d132ee6 100644 --- a/src/build/build.c +++ b/src/build/build.c @@ -360,7 +360,7 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { } // Linker stage - // stage_5(b); + stage_5(b); #ifdef WIN32 QueryPerformanceCounter(&end); @@ -382,7 +382,6 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { #endif int i = 0; while (!file_exists(b->path_out)) { - break; sleep_ms(10); i++; if (i == 100) diff --git a/src/headers/functions.h b/src/headers/functions.h index 75ba2c9c..572fdd57 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -169,14 +169,6 @@ void stage_2_6_func(Fc *fc, Func *func); void stage_2_6_class_functions(Fc *fc, Class *class); void stage_3_class(Fc *fc, Class *class); -// void stage_2_class_type_checks(Fc *fc, Class *class); -// void stage_2_class_props(Fc *fc, Class *class, bool is_trait, bool is_extend); -// void stage_2_func(Fc *fc, Func *func); -// void stage_2_1(Fc *fc); -// void stage_3(Fc *); -// void stage_2_3_circular(Build *b, Class *class); -// void stage_2_3_shared_circular_refs(Build *b, Class *class); - // Read Chunk *chunk_init(Allocator *alc, Fc *fc); Chunk *chunk_clone(Allocator *alc, Chunk *chunk); From 715fe9e9a18c9aec96aed9967474214c47b9d6b4 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Wed, 31 Jan 2024 00:43:46 +0100 Subject: [PATCH 10/16] lexer updates --- src/build/fc.c | 36 +++++++++++++++------------- src/build/lexer.c | 15 ++++++------ src/build/read.c | 4 ++-- src/build/stage-1-parse.c | 50 +++++++-------------------------------- src/build/stage-4-1-ast.c | 5 +++- src/headers/functions.h | 1 + test/test-array.ki | 5 +++- test/test-bubble_sort.ki | 13 +++++++++- test/test-operator.ki | 3 ++- 9 files changed, 62 insertions(+), 70 deletions(-) diff --git a/src/build/fc.c b/src/build/fc.c index ca1312e0..f93a0d93 100644 --- a/src/build/fc.c +++ b/src/build/fc.c @@ -150,19 +150,32 @@ void fc_set_cache_paths(Fc *fc) { void fc_error(Fc *fc) { // - Allocator *alc = fc->alc; Chunk *chunk = fc->chunk; + + int line = -1; + int col = -1; + int i; + chunk_lex(chunk, chunk->i, &i, &line, &col); + + display_error(fc->b, chunk, fc->sbuf, i, line, col); +} + +void display_error(Build* b, Chunk *chunk, char* msg, int i, int line, int col) { + + Allocator *alc = b->alc; + Fc *fc = chunk->fc; + char *content = chunk->content; int length = chunk->length; + // printf("content:'%s'\n", content); - Build *b = fc->b; if (b->lsp) { LspData *ld = b->lsp; if (ld->type == lspt_diagnostic) { Array *errors = array_make(alc, 10); FcError *err = al(alc, sizeof(FcError)); - err->line = chunk->line - 1; - err->col = chunk->col - 1; + err->line = line; + err->col = col; err->msg = fc->sbuf; err->path = fc->path_ki; array_push(errors, err); @@ -173,15 +186,6 @@ void fc_error(Fc *fc) { build_end(b, 1); } - // if (is_newline(get_char(fc, 0))) { - // chunk->i--; - // } - - int line = -1; - int col = -1; - int i; - chunk_lex(chunk, chunk->i, &i, &line, &col); - printf("\n"); Chunk *parent = chunk->parent; while (parent) { @@ -191,14 +195,14 @@ void fc_error(Fc *fc) { parent = parent->parent; } printf("File: %s\n", chunk->fc ? chunk->fc->path_ki : "?"); - if (fc->error_class_info) { + if (fc && fc->error_class_info) { printf("Class: %s\n", fc->error_class_info->dname); } - if (fc->error_func_info) { + if (fc && fc->error_func_info) { printf("Function: %s\n", fc->error_func_info->dname); } printf("At: line:%d | col:%d\n", line, col); - printf("Error: %s\n", fc->sbuf); + printf("Error: %s\n", msg); printf("\n"); int start = i - col + 1; diff --git a/src/build/lexer.c b/src/build/lexer.c index 05d3af57..d5dd9f12 100644 --- a/src/build/lexer.c +++ b/src/build/lexer.c @@ -10,6 +10,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, char* content = chunk->content; int length = chunk->length; Fc* fc = chunk->fc; + Build* b = fc->b; int i = 0; int o = 0; @@ -115,7 +116,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, if (cc_depth < 0) { fc->chunk->i = i; sprintf(fc->sbuf, "Using #%s without an #if before it", token); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } } i = x; @@ -179,7 +180,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, if(ch == 0){ chunk->i = i; sprintf(fc->sbuf, "Missing string closing tag '\"', compiler reached end of file"); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } ch = content[++i]; // Extend memory if needed @@ -202,7 +203,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, if(content[i++] != '\'') { chunk->i = i; sprintf(fc->sbuf, "Missing character closing tag ('), found '%c'", content[i - 1]); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } tokens[o++] = tok_char_string; tokens[o++] = ch; @@ -229,12 +230,12 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, if(depth < 0) { chunk->i = i; sprintf(fc->sbuf, "Unexpected closing tag '%c'", ch); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } if(closer_chars[depth] != ch) { chunk->i = i; sprintf(fc->sbuf, "Unexpected closing tag '%c', expected '%c'", ch, closer_chars[depth]); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } tokens[o++] = tok_scope_close; tokens[o++] = ch; @@ -295,13 +296,13 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, chunk->i = i; sprintf(fc->sbuf, "Unexpected token '%c'", ch); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } if(depth > 0) { chunk->i = i; sprintf(fc->sbuf, "Missing closing tag '%c'", closer_chars[depth - 1]); - fc_error(fc); + display_error(b, chunk, fc->sbuf, i, line, col); } tokens[o++] = tok_eof; diff --git a/src/build/read.c b/src/build/read.c index fb657e57..3defcbe1 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -9,8 +9,8 @@ Chunk *chunk_init(Allocator *alc, Fc *fc) { ch->tokens = NULL; ch->length = 0; ch->i = 0; - ch->line = 1; - ch->col = 1; + ch->line = -1; + ch->col = -1; ch->token = 0; ch->scope_end_i = 0; diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index e3fe2965..5f5e7607 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -730,49 +730,16 @@ void stage_1_alias(Fc *fc, int alias_type, bool is_private) { void stage_1_test(Fc *fc) { // int line = fc->chunk->line; - char *token = fc->token; + char buf[512]; Allocator *alc = fc->alc; Build *b = fc->b; - tok_expect(fc, "\"", true, true); - - Chunk *chu = fc->chunk; - char *content = chu->content; - int i = chu->i; - Str *str = str_make(alc, 64); - - bool found = false; - char ch = content[i]; - while (ch != '\0') { - ch = content[i]; - i++; - if (ch == '\n') - break; - if (ch == '\\') { - str_append_char(str, '\\'); - str_append_char(str, ch); - i++; - continue; - } - if (ch == '"') { - found = true; - break; - } - str_append_char(str, ch); - } - if (!found) { - sprintf(fc->sbuf, "Missing end of string"); - fc_error(fc); - } - chu->i = i; - - if (str->length > 128) { - sprintf(fc->sbuf, "Test name too long"); + char *test_name = tok(fc, true, true); + if(fc->chunk->token != tok_string) { + sprintf(fc->sbuf, "Expected a test name here wrapped in dubbel-quotes, e.g. \"My test\""); fc_error(fc); } - char *body = str_to_chars(alc, str); - tok_expect(fc, "{", false, true); // If tests enabled && is main package @@ -781,9 +748,9 @@ void stage_1_test(Fc *fc) { Func *func = func_init(fc->alc, fc->b); func->line = line; - sprintf(token, "ki__TEST_%d__%s", ++fc->test_counter, fc->path_hash); + sprintf(buf, "ki__TEST_%d__%s", ++fc->test_counter, fc->path_hash); - char *name = dups(fc->alc, token); + char *name = dups(fc->alc, buf); char *gname = nsc_gname(fc, name); char *dname = nsc_dname(fc, name); @@ -796,15 +763,16 @@ void stage_1_test(Fc *fc) { func->is_test = true; Test *test = al(alc, sizeof(Test)); - test->name = body; + test->name = test_name; test->func = func; func->test = test; test->expects = NULL; Chunk *chunk = chunk_init(alc, fc); - chunk->content = "ki__test__expect_count: u32[1], ki__test__success_count: u32[1], ki__test__fail_count: u32[1]) void {"; + chunk->content = "(ki__test__expect_count: u32[1], ki__test__success_count: u32[1], ki__test__fail_count: u32[1]) void {}"; chunk->length = strlen(chunk->content); chunk_lex_start(chunk); + tok_next(chunk, false, true, true); func->chunk_args = chunk; func->chunk_body = chunk_clone(fc->alc, fc->chunk); diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index f4be27fa..4bf13acf 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -46,7 +46,7 @@ void stage_4_1(Fc *fc) { } // Write IR - // stage_4_2(fc); + stage_4_2(fc); } void stage_4_1_func(Fc *fc, Func *func) { @@ -933,6 +933,7 @@ void stage_4_1_gen_test_main(Fc *fc) { map_set(scope->identifiers, "os_test_report", ki_lib_get(b, "os", "test_report")); Str *code = str_make(alc, 5000); + str_append_chars(code, "{\n"); str_append_chars(code, "let test_success : u32 = 0;\n"); str_append_chars(code, "let test_fail : u32 = 0;\n"); str_append_chars(code, "let expect_total : u32 = 0;\n"); @@ -980,6 +981,8 @@ void stage_4_1_gen_test_main(Fc *fc) { chunk->content = str_to_chars(alc, code); chunk->length = code->length; chunk_lex_start(chunk); + // Skip first character + tok_next(chunk, false, true, true); func->chunk_body = chunk; } diff --git a/src/headers/functions.h b/src/headers/functions.h index 572fdd57..ad822926 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -146,6 +146,7 @@ char *nsc_dname(Fc *fc, char *name); Fc *fc_init(Build *b, char *path_ki, Nsc *nsc, bool duplicate); void fc_set_cache_paths(Fc *fc); void fc_error(Fc *fc); +void display_error(Build* b, Chunk *chunk, char* msg, int i, int line, int col); void fc_update_cache(Fc *fc); // diff --git a/test/test-array.ki b/test/test-array.ki index c7b0f56d..65792e12 100644 --- a/test/test-array.ki +++ b/test/test-array.ki @@ -1,5 +1,8 @@ test "array" { - let arr = array[String]{ "Hello", "World" }; + // let arr = array[String]{ "Hello", "World" }; + let arr = Array[String].new(); + arr.push("Hello"); + arr.push("World"); @expect (arr.get(0) !? "not found") == "Hello"; @expect (arr.get(1) !? "not found") == "World"; @expect (arr.get(2) !? "not found") == "not found"; diff --git a/test/test-bubble_sort.ki b/test/test-bubble_sort.ki index edd89249..8fc75b9d 100644 --- a/test/test-bubble_sort.ki +++ b/test/test-bubble_sort.ki @@ -1,6 +1,17 @@ test "bubble_sort_dynamic array" { - let arr = array[i32]{ 4, 3, 1, 6, 7, 9, 5, 8, 0, 2 }; + // let arr = array[i32]{ 4, 3, 1, 6, 7, 9, 5, 8, 0, 2 }; + let arr = Array[i32].new(); + arr.push(4); + arr.push(3); + arr.push(1); + arr.push(6); + arr.push(7); + arr.push(9); + arr.push(5); + arr.push(8); + arr.push(0); + arr.push(2); let len = arr.length; let last = len - 1; diff --git a/test/test-operator.ki b/test/test-operator.ki index 4191d52b..453a8de3 100644 --- a/test/test-operator.ki +++ b/test/test-operator.ki @@ -1,3 +1,4 @@ + test "`=`" { @expect 1 == 1; @@ -72,4 +73,4 @@ test "`!`" { @expect !false; } -fn main() i32 { return 0; } \ No newline at end of file +fn main() i32 { return 0; } From 52c4c5f5425beeac8deb0259746c68d3d78f0c7a Mon Sep 17 00:00:00 2001 From: ctxcode Date: Wed, 31 Jan 2024 00:46:51 +0100 Subject: [PATCH 11/16] fix bug --- src/build/read.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/read.c b/src/build/read.c index 3defcbe1..3bbd9fd4 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -272,7 +272,7 @@ char* string_replace_backslash_chars(Allocator* alc, char* body) { int len = strlen(body); if(len == 0) return ""; - char* result = al(alc, len); + char* result = al(alc, len + 1); int i = 0; int ri = 0; while(true) { From 63cfa5f6bfac8ff0f01225dbfce405e2c58468ef Mon Sep 17 00:00:00 2001 From: ctxcode Date: Wed, 31 Jan 2024 00:48:35 +0100 Subject: [PATCH 12/16] test formatting --- test/test-array.ki | 3 ++- test/test-bubble_sort.ki | 3 ++- test/test-map.ki | 3 ++- test/test-operator.ki | 2 -- test/test-string.ki | 3 ++- test/test-variable.ki | 3 ++- test/test-while.ki | 3 ++- 7 files changed, 12 insertions(+), 8 deletions(-) diff --git a/test/test-array.ki b/test/test-array.ki index 65792e12..2942e1c2 100644 --- a/test/test-array.ki +++ b/test/test-array.ki @@ -1,3 +1,4 @@ + test "array" { // let arr = array[String]{ "Hello", "World" }; let arr = Array[String].new(); @@ -6,4 +7,4 @@ test "array" { @expect (arr.get(0) !? "not found") == "Hello"; @expect (arr.get(1) !? "not found") == "World"; @expect (arr.get(2) !? "not found") == "not found"; -} \ No newline at end of file +} diff --git a/test/test-bubble_sort.ki b/test/test-bubble_sort.ki index 8fc75b9d..8c9abd92 100644 --- a/test/test-bubble_sort.ki +++ b/test/test-bubble_sort.ki @@ -1,3 +1,4 @@ + test "bubble_sort_dynamic array" { // let arr = array[i32]{ 4, 3, 1, 6, 7, 9, 5, 8, 0, 2 }; @@ -41,4 +42,4 @@ test "bubble_sort_dynamic array" { @expect (arr.get(7) !? -1) == 7; @expect (arr.get(8) !? -1) == 8; @expect (arr.get(9) !? -1) == 9; -} \ No newline at end of file +} diff --git a/test/test-map.ki b/test/test-map.ki index 8c00ab77..7c9ef0ca 100644 --- a/test/test-map.ki +++ b/test/test-map.ki @@ -1,6 +1,7 @@ + test "map" { let m = Map[i32].new(); m.set("test_id", 1000); @expect (m.get("test_id") !? 0) == 1000; @expect (m.get("runtime_id") !? 0) == 0; -} \ No newline at end of file +} diff --git a/test/test-operator.ki b/test/test-operator.ki index 453a8de3..88492d37 100644 --- a/test/test-operator.ki +++ b/test/test-operator.ki @@ -72,5 +72,3 @@ test "`<` and `>`" { test "`!`" { @expect !false; } - -fn main() i32 { return 0; } diff --git a/test/test-string.ki b/test/test-string.ki index 54a21754..fe92ddee 100644 --- a/test/test-string.ki +++ b/test/test-string.ki @@ -1,6 +1,7 @@ + test "string" { let a = "1"; let b = 2; let s = a + ", " + b.str(); @expect s == "1, 2"; -} \ No newline at end of file +} diff --git a/test/test-variable.ki b/test/test-variable.ki index ddf4810c..6e100c42 100644 --- a/test/test-variable.ki +++ b/test/test-variable.ki @@ -1,3 +1,4 @@ + test "rep" { let a = 5; rep a = 6#u8; @@ -10,4 +11,4 @@ test "let" { let c : u8 = 5; @expect a == b; @expect b == c; -} \ No newline at end of file +} diff --git a/test/test-while.ki b/test/test-while.ki index 91551867..41182fb6 100644 --- a/test/test-while.ki +++ b/test/test-while.ki @@ -1,3 +1,4 @@ + test "while" { let i = 0; let s = 0; @@ -6,4 +7,4 @@ test "while" { i += 1; } @expect s == 45; -} \ No newline at end of file +} From ae948451998b12557e6814ff6ae8946e61bcfe69 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Wed, 31 Jan 2024 12:24:23 +0100 Subject: [PATCH 13/16] clean up --- src/build/build.c | 1 - src/build/{var.c => decl.c} | 8 -------- src/build/fc.c | 1 - src/build/id.c | 19 ------------------- src/headers/functions.h | 1 - src/headers/structs.h | 7 ------- 6 files changed, 37 deletions(-) rename src/build/{var.c => decl.c} (79%) diff --git a/src/build/build.c b/src/build/build.c index 6d132ee6..b243a244 100644 --- a/src/build/build.c +++ b/src/build/build.c @@ -17,7 +17,6 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { b->alc = alc; b->alc_io = alc_io; b->alc_ast = alc_make(); - b->token = al(alc, KI_TOKEN_MAX); b->sbuf = al(alc, 2000); b->lsp = lsp_data; // diff --git a/src/build/var.c b/src/build/decl.c similarity index 79% rename from src/build/var.c rename to src/build/decl.c index 8da78488..0f378b07 100644 --- a/src/build/var.c +++ b/src/build/decl.c @@ -15,14 +15,6 @@ Decl *decl_init(Allocator *alc, Scope *scope, char *name, Type *type, Value *val return v; } -Var *var_init(Allocator *alc, Decl *decl, Type *type) { - // - Var *v = al(alc, sizeof(Var)); - v->decl = decl; - v->type = type; - return v; -} - Arg *arg_init(Allocator *alc, char *name, Type *type) { // Arg *v = al(alc, sizeof(Arg)); diff --git a/src/build/fc.c b/src/build/fc.c index f93a0d93..dc73a664 100644 --- a/src/build/fc.c +++ b/src/build/fc.c @@ -41,7 +41,6 @@ Fc *fc_init(Build *b, char *path_ki, Nsc *nsc, bool duplicate) { fc->alc_ast = b->alc_ast; fc->deps = array_make(alc, 20); fc->sub_headers = array_make(alc, 2); - fc->token = b->token; fc->sbuf = b->sbuf; fc->chunk = chunk_init(alc, fc); fc->chunk_prev = chunk_init(alc, fc); diff --git a/src/build/id.c b/src/build/id.c index e73cf33d..80953ac1 100644 --- a/src/build/id.c +++ b/src/build/id.c @@ -29,11 +29,6 @@ Id *read_id(Fc *fc, bool sameline, bool allow_space, bool crash) { char* token = tok(fc, sameline, allow_space); - // if (token[0] == ':') { - // strcpy(token, "main"); - // fc->i--; - // } - if (!is_valid_varname(token)) { if (!crash) return NULL; @@ -80,14 +75,6 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { idf = idf_by_id(fc, scope, &id, false); if (idf && idf->type == idf_nsc && get_char(fc, 0) == ':') { - // if (!idf) { - // sprintf(fc->sbuf, "Unknown namespace: '%s', most likely a typo or a missing 'use' token", token); - // fc_error(fc); - // } - // if (idf && idf->type != idf_nsc) { - // sprintf(fc->sbuf, "Identifier '%s' is not a namespace", token); - // fc_error(fc); - // } chunk_move(fc->chunk, 1); @@ -135,12 +122,6 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { id.has_nsc = false; id.name = token; idf = idf_by_id(fc, nsc->scope, &id, false); - - // } else { - // Id id; - // id.has_nsc = false; - // id.name = token; - // idf = idf_by_id(fc, scope, &id, false); } if (idf && idf->type == idf_fc && get_char(fc, 0) == '.') { diff --git a/src/headers/functions.h b/src/headers/functions.h index ad822926..8b9fef81 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -274,7 +274,6 @@ Type *type_merge(Build* build, Allocator *alc, Type *a, Type *b); // Var Decl *decl_init(Allocator *alc, Scope *scope, char *name, Type *type, Value *val, bool is_arg); -Var *var_init(Allocator *alc, Decl *decl, Type *type); Arg *arg_init(Allocator *alc, char *name, Type *type); // UsageLine diff --git a/src/headers/structs.h b/src/headers/structs.h index 0cdd49ee..4eb30b3b 100644 --- a/src/headers/structs.h +++ b/src/headers/structs.h @@ -24,7 +24,6 @@ typedef struct Enum Enum; typedef struct Extend Extend; typedef struct Decl Decl; typedef struct DeclOverwrite DeclOverwrite; -typedef struct Var Var; typedef struct Arg Arg; typedef struct UsageLine UsageLine; typedef struct Global Global; @@ -89,7 +88,6 @@ struct Build { char *path_out; char *cache_dir; char *pkg_dir; - char *token; char *sbuf; // Allocator *alc; @@ -150,7 +148,6 @@ struct Fc { char *path_ir; char *path_cache; char *path_hash; - char *token; char *sbuf; char *ir; char *ir_hash; @@ -395,10 +392,6 @@ struct DeclOverwrite { Type *type; Decl *decl; }; -struct Var { - Decl *decl; - Type *type; -}; struct Arg { char *name; Type *type; From 5c479d7476a79adba5856b334bcf5eb9108d1658 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Wed, 31 Jan 2024 21:41:56 +0100 Subject: [PATCH 14/16] track time update --- src/array.c | 23 + src/build/build.c | 42 +- src/build/class.c | 2 +- src/build/fc.c | 10 +- src/build/id.c | 5 + src/build/lexer.c | 3 + src/build/loader.c | 4 +- src/build/pkc.c | 2 +- src/build/read.c | 46 +- src/build/stage-1-parse.c | 7 +- src/build/stage-2-1-alias.c | 53 +- src/build/stage-2-2-props.c | 5 + src/build/stage-2-3-post-props.c | 7 +- src/build/stage-2-4-adjust-types.c | 8 +- src/build/stage-2-5-extend.c | 4 + src/build/stage-2-6-read-types.c | 6 +- src/build/stage-3-values.c | 5 +- src/build/stage-4-1-ast.c | 8 +- src/build/stage-4-2-ir.c | 12 +- src/build/stage-5-link.c | 37 +- src/build/value.c | 7 +- src/functions.c | 60 ++- src/headers/array.h | 1 + src/headers/functions.h | 9 +- src/headers/map.h | 1 + src/headers/structs.h | 5 + src/libs/crc32.c | 748 +++++++++++++++++++++++++++++ src/map.c | 25 +- 28 files changed, 963 insertions(+), 182 deletions(-) create mode 100644 src/libs/crc32.c diff --git a/src/array.c b/src/array.c index 13101a19..da5e7ccc 100644 --- a/src/array.c +++ b/src/array.c @@ -140,3 +140,26 @@ int array_find(Array *arr, void *item, int type) { } return -1; } +int array_find_x(Array *arr, void *item, int type, int start, int end) { + int x = start; + while (x < end) { + uintptr_t *adr = arr->data + (x * sizeof(void *)); + if (type == arr_find_adr) { + if (*adr == (uintptr_t)item) + return x; + } else if (type == arr_find_str) { + char *a = (char *)*adr; + char *b = (char *)item; + if (strcmp(a, b) == 0) + return x; + } else if (type == arr_find_int) { + if ((int)(*adr) == *(int *)item) + return x; + } else { + die("array.c invalid search type"); + } + x++; + } + return -1; +} + diff --git a/src/build/build.c b/src/build/build.c index b243a244..310afb48 100644 --- a/src/build/build.c +++ b/src/build/build.c @@ -198,7 +198,7 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { strcat(cache_buf, optimize ? "1" : "0"); strcat(cache_buf, debug ? "1" : "0"); strcat(cache_buf, test ? "1" : "0"); - simple_hash(cache_buf, cache_hash); + ctxhash(cache_buf, cache_hash); strcpy(cache_dir, get_storage_path()); strcat(cache_dir, "/cache/"); @@ -278,17 +278,9 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { b->link_static = link_static; // b->type_void = type_gen_void(alc); - -#ifdef WIN32 - LARGE_INTEGER frequency; - LARGE_INTEGER start; - LARGE_INTEGER end; - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&start); -#else - struct timeval begin, end; - gettimeofday(&begin, NULL); -#endif + // + b->time_lex = 0; + b->time_fs = 0; // Init main:main Config *main_cfg = NULL; @@ -318,6 +310,8 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { loader_load_nsc(pkc_ki, "mem"); loader_load_nsc(pkc_ki, "os"); + unsigned long start = microtime(); + // Compile ki lib compile_loop(b, 1); // Scan identifiers @@ -346,31 +340,19 @@ void cmd_build(int argc, char *argv[], LspData *lsp_data) { } } -#ifdef WIN32 - QueryPerformanceCounter(&end); - double time_ast = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart; -#else - gettimeofday(&end, NULL); - double time_ast = (double)(end.tv_usec - begin.tv_usec) / 1000000 + (double)(end.tv_sec - begin.tv_sec); -#endif - if (verbose > 0) { - printf("⌚ Parse + IR gen: %.3fs\n", time_ast); + printf("⌚ Lexer: %.3fs\n", (double)b->time_lex / 1000000); + printf("⌚ Parser: %.3fs\n", (double)b->time_parse / 1000000); + printf("⌚ IR Gen: %.3fs\n", (double)b->time_ir / 1000000); + printf("⌚ File IO: %.3fs\n", (double)b->time_fs / 1000000); + // printf("⌚ Other: %.3fs\n", (double)(microtime() - start - b->time_lex - b->time_parse - b->time_fs - b->time_ir) / 1000000); } // Linker stage stage_5(b); -#ifdef WIN32 - QueryPerformanceCounter(&end); - double time_all = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart; -#else - gettimeofday(&end, NULL); - double time_all = (double)(end.tv_usec - begin.tv_usec) / 1000000 + (double)(end.tv_sec - begin.tv_sec); -#endif - if (!run_code || b->verbose > 0) { - printf("✅ Compiled in: %.3fs\n", time_all); + printf("✅ Compiled in: %.3fs\n", (double)(microtime() - start) / 1000000); } // Flush all output diff --git a/src/build/class.c b/src/build/class.c index 54f8badf..ef6d7d13 100644 --- a/src/build/class.c +++ b/src/build/class.c @@ -461,7 +461,7 @@ Class *class_get_generic_class(Class *class, Array *types) { str_append_chars(hash_buf, type_buf); } char *hash_content = str_to_chars(alc, hash_buf); - simple_hash(hash_content, hash); + ctxhash(hash_content, hash); Class *gclass = map_get(class->generics, hash); if (!gclass) { diff --git a/src/build/fc.c b/src/build/fc.c index dc73a664..99019b35 100644 --- a/src/build/fc.c +++ b/src/build/fc.c @@ -42,8 +42,8 @@ Fc *fc_init(Build *b, char *path_ki, Nsc *nsc, bool duplicate) { fc->deps = array_make(alc, 20); fc->sub_headers = array_make(alc, 2); fc->sbuf = b->sbuf; - fc->chunk = chunk_init(alc, fc); - fc->chunk_prev = chunk_init(alc, fc); + fc->chunk = chunk_init(alc, b, fc); + fc->chunk_prev = chunk_init(alc, b, fc); fc->id_buf = id_init(alc); fc->str_buf = str_make(alc, 100); @@ -90,8 +90,10 @@ Fc *fc_init(Build *b, char *path_ki, Nsc *nsc, bool duplicate) { } } if (!content) { + unsigned long start = microtime(); file_get_contents(buf, fc->path_ki); content = str_to_chars(alc, buf); + b->time_fs += microtime() - start; } fc->chunk->content = content; fc->chunk->length = strlen(content); @@ -127,7 +129,7 @@ void fc_set_cache_paths(Fc *fc) { fc->path_cache = path_cache; char *hash = al(alc, 64); - simple_hash(fc->path_ki, hash); + ctxhash(fc->path_ki, hash); fc->path_hash = hash; // Str *buf = str_make(alc, 500); @@ -137,7 +139,9 @@ void fc_set_cache_paths(Fc *fc) { fc->cache = NULL; } if (file_exists(fc->path_cache)) { + unsigned long start = microtime(); file_get_contents(buf, fc->path_cache); + b->time_fs += microtime() - start; char *content = str_to_chars(alc, buf); fc->cache = cJSON_ParseWithLength(content, buf->length); cJSON *item = cJSON_GetObjectItemCaseSensitive(fc->cache, "ir_hash"); diff --git a/src/build/id.c b/src/build/id.c index 80953ac1..c5de17c3 100644 --- a/src/build/id.c +++ b/src/build/id.c @@ -72,7 +72,9 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { Id id; id.has_nsc = false; id.name = token; + unsigned long start = microtime(); idf = idf_by_id(fc, scope, &id, false); + // b->time_parse += microtime() - start; if (idf && idf->type == idf_nsc && get_char(fc, 0) == ':') { @@ -192,6 +194,7 @@ Idf *read_idf(Fc *fc, Scope *scope, bool sameline, bool allow_space) { Idf *idf_by_id(Fc *fc, Scope *scope, Id *id, bool fail) { // + Build* b = fc->b; if (id->has_nsc) { Scope *fc_scope = scope_find(scope, sct_fc); @@ -211,7 +214,9 @@ Idf *idf_by_id(Fc *fc, Scope *scope, Id *id, bool fail) { char *name = id->name; while (!idf) { // + unsigned long start = microtime(); idf = map_get(scope->identifiers, name); + b->time_parse += microtime() - start; // if (!idf) { scope = scope->parent; diff --git a/src/build/lexer.c b/src/build/lexer.c index d5dd9f12..6c31bb48 100644 --- a/src/build/lexer.c +++ b/src/build/lexer.c @@ -2,7 +2,9 @@ #include "../all.h" void chunk_lex_start(Chunk *chunk) { + unsigned long start = microtime(); chunk_lex(chunk, -1, NULL, NULL, NULL); + chunk->b->time_lex += microtime() - start; } void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, int *err_col) { @@ -309,6 +311,7 @@ void chunk_lex(Chunk *chunk, int err_token_i, int *err_content_i, int *err_line, tokens[o++] = '\0'; chunk->tokens = tokens; + chunk->b->LOC += line; // Probably will never happen // if (err_token_i > -1) { diff --git a/src/build/loader.c b/src/build/loader.c index 5058ca07..913ec915 100644 --- a/src/build/loader.c +++ b/src/build/loader.c @@ -56,7 +56,9 @@ Pkc *loader_get_pkc_for_dir(Build *b, char *dir) { return pkc; } + unsigned long start = microtime(); Config *cfg = cfg_load(alc, b->str_buf, cfg_dir); + b->time_fs += microtime() - start; if (cfg) { cJSON *name_ = cJSON_GetObjectItemCaseSensitive(cfg->json, "name"); if (name_ && name_->valuestring) { @@ -69,7 +71,7 @@ Pkc *loader_get_pkc_for_dir(Build *b, char *dir) { pkc = b->pkc_main; } else { name = al(alc, 64); - simple_hash(cfg->dir, name); + ctxhash(cfg->dir, name); } } diff --git a/src/build/pkc.c b/src/build/pkc.c index ace99264..dfea97b2 100644 --- a/src/build/pkc.c +++ b/src/build/pkc.c @@ -17,7 +17,7 @@ Pkc *pkc_init(Allocator *alc, Build *b, char *name, char *dir, Config *cfg) { pkc->header_dirs = array_make(alc, 10); pkc->config = cfg; - simple_hash(pkc->dir, pkc->hash); + ctxhash(pkc->dir, pkc->hash); array_push(b->packages, pkc); diff --git a/src/build/read.c b/src/build/read.c index 3bbd9fd4..574e2d3f 100644 --- a/src/build/read.c +++ b/src/build/read.c @@ -1,9 +1,10 @@ #include "../all.h" -Chunk *chunk_init(Allocator *alc, Fc *fc) { +Chunk *chunk_init(Allocator *alc, Build* b, Fc *fc) { Chunk *ch = al(alc, sizeof(Chunk)); ch->parent = NULL; + ch->b = b; ch->fc = fc; ch->content = NULL; ch->tokens = NULL; @@ -22,50 +23,9 @@ Chunk *chunk_clone(Allocator *alc, Chunk *chunk) { *ch = *chunk; return ch; } + void chunk_move(Chunk *chunk, int pos) { tok_next(chunk, false, true, true); - // - // int i = chunk->i; - // while (pos > 0) { - // pos--; - // char ch = chunk->content[chunk->i]; - // chunk->i++; - // chunk->col++; - // if (ch == '\n') { - // chunk->line++; - // chunk->col = 0; - // } - // } - // while (pos < 0) { - // pos++; - // char ch = chunk->content[chunk->i]; - // chunk->i--; - // chunk->col--; - // if (ch == '\n') { - // chunk->line--; - // int x = chunk->i; - // int col = 0; - // while (x > 0 && chunk->content[x] != '\n') { - // x--; - // col++; - // } - // chunk->col = col; - // } - // } -} -void chunk_update_col(Chunk *chunk) { - // - int col = 1; - int i = chunk->i; - char *content = chunk->content; - while (i > 0) { - i--; - col++; - char ch = content[i]; - if (ch == '\n') - break; - } - chunk->col = col; } char* tok(Fc *fc, bool sameline, bool allow_space) { diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index 5f5e7607..40918458 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -28,6 +28,8 @@ void stage_1(Fc *fc) { char *token; char *t_ = &chunk->token; + unsigned long start = microtime(); + while (true) { token = tok(fc, false, true); @@ -125,8 +127,7 @@ void stage_1(Fc *fc) { fc_error(fc); } - b->LOC += fc->chunk->line; - + b->time_parse += microtime() - start; // chain_add(b->stage_2_1, fc); } @@ -768,7 +769,7 @@ void stage_1_test(Fc *fc) { func->test = test; test->expects = NULL; - Chunk *chunk = chunk_init(alc, fc); + Chunk *chunk = chunk_init(alc, b, fc); chunk->content = "(ki__test__expect_count: u32[1], ki__test__success_count: u32[1], ki__test__fail_count: u32[1]) void {}"; chunk->length = strlen(chunk->content); chunk_lex_start(chunk); diff --git a/src/build/stage-2-1-alias.c b/src/build/stage-2-1-alias.c index 72a6e380..3a753dfb 100644 --- a/src/build/stage-2-1-alias.c +++ b/src/build/stage-2-1-alias.c @@ -8,6 +8,8 @@ void stage_2_1(Fc *fc) { printf("# Stage 2.1 : Read aliasses : %s\n", fc->path_ki); } + unsigned long start = microtime(); + for (int i = 0; i < fc->aliasses->length; i++) { Alias *a = array_get_index(fc->aliasses, i); fc->chunk = a->chunk; @@ -20,54 +22,7 @@ void stage_2_1(Fc *fc) { } } + b->time_parse += microtime() - start; + chain_add(b->stage_2_2, fc); } - -// void stage_2_1(Fc *fc) { -// // -// Build *b = fc->b; -// Allocator *alc = fc->alc; -// Array *extends = fc->extends; - -// for (int i = 0; i < extends->length; i++) { -// Extend *ex = array_get_index(extends, i); - -// fc->chunk = ex->chunk_type; -// Type *type = read_type(fc, alc, fc->scope, true, true, rtc_default); -// if (!type->class) { -// sprintf(fc->sbuf, "This type has no class/struct"); -// fc_error(fc); -// } -// Class *class = type->class; - -// fc->chunk = ex->chunk_body; -// Scope *class_scope = class->scope; -// Scope *extend_scope = scope_init(fc->alc, sct_default, fc->scope, false); -// class->scope = extend_scope; -// stage_2_class_props(fc, class, false, true); -// class->scope = class_scope; -// } - -// for (int i = 0; i < fc->funcs->length; i++) { -// Func *func = array_get_index(fc->funcs, i); -// if (!func->chunk_args) -// continue; -// if (b->verbose > 2) { -// printf("> Scan func types: %s\n", func->dname); -// } -// stage_2_func(fc, func); -// } - -// for (int i = 0; i < fc->classes->length; i++) { -// Class *class = array_get_index(fc->classes, i); -// if (class->is_generic_base) -// continue; -// if (b->verbose > 2) { -// printf("> Class type check internal functions: %s\n", class->dname); -// } -// stage_2_class_type_checks(fc, class); -// } - -// // -// chain_add(b->stage_3, fc); -// } diff --git a/src/build/stage-2-2-props.c b/src/build/stage-2-2-props.c index 268f7635..60c39d61 100644 --- a/src/build/stage-2-2-props.c +++ b/src/build/stage-2-2-props.c @@ -8,6 +8,8 @@ void stage_2_2(Fc *fc) { printf("# Stage 2.2 : Read class properties : %s\n", fc->path_ki); } + unsigned long start = microtime(); + for (int i = 0; i < fc->classes->length; i++) { Class *class = array_get_index(fc->classes, i); if (class->is_generic_base) @@ -17,6 +19,8 @@ void stage_2_2(Fc *fc) { stage_2_2_class_read_props(fc, class, false, false); } + b->time_parse += microtime() - start; + // chain_add(fc->b->stage_2_3, fc); } @@ -24,6 +28,7 @@ void stage_2_2(Fc *fc) { void stage_2_2_class_read_props(Fc *fc, Class *class, bool is_trait, bool is_extend) { // Scope *scope = class->scope; + Build *b = fc->b; Class *prev_error_class_info = fc->error_class_info; fc->error_class_info = class; diff --git a/src/build/stage-2-3-post-props.c b/src/build/stage-2-3-post-props.c index e442f392..8be75ece 100644 --- a/src/build/stage-2-3-post-props.c +++ b/src/build/stage-2-3-post-props.c @@ -8,6 +8,8 @@ void stage_2_3(Fc *fc) { printf("# Stage 2.3 : Post work on class properties : %s\n", fc->path_ki); } + unsigned long start = microtime(); + Array *checks = fc->class_size_checks; while (true) { @@ -32,7 +34,7 @@ void stage_2_3(Fc *fc) { } } if (did_fix_any && last != NULL) { - sprintf(b->sbuf, "Compiler could not figure out all type sizes"); + sprintf(b->sbuf, "Compiler could not figure out the sizes of all types. Most likely due to circular dependency of inline types."); build_error(b, b->sbuf); } if (!had_missing_size) { @@ -54,6 +56,8 @@ void stage_2_3(Fc *fc) { } } + b->time_parse += microtime() - start; + // chain_add(b->stage_2_4, fc); } @@ -100,6 +104,7 @@ void stage_2_3_circular(Build *b, Class *class) { // if (!class->is_rc) return; + bool circular = false; Array *types = class->refers_to_types; Array *names = class->refers_to_names; diff --git a/src/build/stage-2-4-adjust-types.c b/src/build/stage-2-4-adjust-types.c index 4cba76b0..e189eb5c 100644 --- a/src/build/stage-2-4-adjust-types.c +++ b/src/build/stage-2-4-adjust-types.c @@ -9,6 +9,8 @@ void stage_2_4(Fc *fc) { printf("# Stage 2.4 : Update class properties : %s\n", fc->path_ki); } + unsigned long start = microtime(); + Array *classes = fc->classes; for (int i = 0; i < classes->length; i++) { Class *class = array_get_index(classes, i); @@ -18,14 +20,18 @@ void stage_2_4(Fc *fc) { stage_2_4_class_props_update(fc, class); } + b->time_parse += microtime() - start; + chain_add(b->stage_2_5, fc); } void stage_2_4_class_props_update(Fc *fc, Class *class) { // - Build *b = fc->b; if (!class->is_rc) return; + + Build *b = fc->b; + Array *types = class->refers_to_types; Array *names = class->refers_to_names; for (int i = 0; i < types->length; i++) { diff --git a/src/build/stage-2-5-extend.c b/src/build/stage-2-5-extend.c index f0e628ce..acc26782 100644 --- a/src/build/stage-2-5-extend.c +++ b/src/build/stage-2-5-extend.c @@ -8,6 +8,8 @@ void stage_2_5(Fc *fc) { printf("# Stage 2.5 : Extend : %s\n", fc->path_ki); } + unsigned long start = microtime(); + Allocator *alc = fc->alc; Array *extends = fc->extends; @@ -30,5 +32,7 @@ void stage_2_5(Fc *fc) { class->scope = class_scope; } + b->time_parse += microtime() - start; + chain_add(b->stage_2_6, fc); } diff --git a/src/build/stage-2-6-read-types.c b/src/build/stage-2-6-read-types.c index 2f94f566..ee2f2b3b 100644 --- a/src/build/stage-2-6-read-types.c +++ b/src/build/stage-2-6-read-types.c @@ -11,6 +11,8 @@ void stage_2_6(Fc *fc) { printf("# Stage 2.1 : Read aliasses : %s\n", fc->path_ki); } + unsigned long start = microtime(); + for (int i = 0; i < fc->funcs->length; i++) { Func *func = array_get_index(fc->funcs, i); if (!func->chunk_args) @@ -37,6 +39,8 @@ void stage_2_6(Fc *fc) { stage_2_6_class_default_functions(fc, class); } + // b->time_parse += microtime() - start; + chain_add(b->stage_3, fc); } @@ -77,7 +81,7 @@ void stage_2_6_func(Fc *fc, Func *func) { fc_error(fc); } - char *name = dups(alc, token); + char *name = token; tok_expect(fc, ":", true, true); diff --git a/src/build/stage-3-values.c b/src/build/stage-3-values.c index ff9b2632..e3bff80c 100644 --- a/src/build/stage-3-values.c +++ b/src/build/stage-3-values.c @@ -14,6 +14,8 @@ void stage_3(Fc *fc) { printf("# Stage 3 : Read values : %s\n", fc->path_ki); } + unsigned long start = microtime(); + for (int i = 0; i < fc->classes->length; i++) { Class *class = array_get_index(fc->classes, i); if (class->is_generic_base) @@ -28,7 +30,8 @@ void stage_3(Fc *fc) { stage_3_func(fc, func); } - // + // b->time_parse += microtime() - start; + chain_add(b->stage_4_1, fc); } diff --git a/src/build/stage-4-1-ast.c b/src/build/stage-4-1-ast.c index 4bf13acf..c93365ce 100644 --- a/src/build/stage-4-1-ast.c +++ b/src/build/stage-4-1-ast.c @@ -22,6 +22,8 @@ void stage_4_1(Fc *fc) { printf("# Stage 4.1 : AST : %s\n", fc->path_ki); } + unsigned long start = microtime(); + if ((b->main_func && fc == b->main_func->fc) || (!b->main_func && !b->main_fc && fc->nsc == b->nsc_main)) { b->main_fc = fc; if (b->test) { @@ -45,6 +47,8 @@ void stage_4_1(Fc *fc) { return; } + // b->time_parse += microtime() - start; + // Write IR stage_4_2(fc); } @@ -804,7 +808,7 @@ void stage_4_1_gen_main(Fc *fc) { Build *b = fc->b; Allocator *alc = fc->alc; - Chunk *chunk = chunk_init(alc, fc); + Chunk *chunk = chunk_init(alc, b, fc); Func *mfunc = b->main_func; if (mfunc) { // Validate main @@ -977,7 +981,7 @@ void stage_4_1_gen_test_main(Fc *fc) { str_append_chars(code, "}\n"); - Chunk *chunk = chunk_init(alc, fc); + Chunk *chunk = chunk_init(alc, b, fc); chunk->content = str_to_chars(alc, code); chunk->length = code->length; chunk_lex_start(chunk); diff --git a/src/build/stage-4-2-ir.c b/src/build/stage-4-2-ir.c index 49fa28ce..02c78c31 100644 --- a/src/build/stage-4-2-ir.c +++ b/src/build/stage-4-2-ir.c @@ -8,6 +8,8 @@ void stage_4_2(Fc *fc) { printf("# Stage 4.2 : LLVM IR : %s\n", fc->path_ki); } + unsigned long start = microtime(); + Allocator *alc = fc->alc_ast; LB *lb = al(alc, sizeof(LB)); @@ -49,10 +51,13 @@ void stage_4_2(Fc *fc) { llvm_build_ir(lb); - char *ir = str_to_chars(b->alc, lb->ir_final); + b->time_ir += microtime() - start; + + str_append_char(lb->ir_final, 0); + char *ir = lb->ir_final->data; char *ir_hash = al(b->alc, 64); - simple_hash(ir, ir_hash); + ctxhash(ir, ir_hash); if (strcmp(fc->ir_hash, ir_hash) != 0 || b->clear_cache) { @@ -64,7 +69,10 @@ void stage_4_2(Fc *fc) { } fc->ir = ir; + + start = microtime(); write_file(fc->path_ir, fc->ir, false); + b->time_fs += microtime() - start; } // diff --git a/src/build/stage-5-link.c b/src/build/stage-5-link.c index 730f4958..112cd2bb 100644 --- a/src/build/stage-5-link.c +++ b/src/build/stage-5-link.c @@ -43,18 +43,7 @@ void stage_5(Build *b) { Array *o_files = array_make(b->alc, 20); Array *threads = array_make(b->alc, 20); -#ifdef WIN32 - LARGE_INTEGER frequency; - LARGE_INTEGER start; - LARGE_INTEGER end; - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&start); - _flushall(); -#else - struct timeval begin, end; - gettimeofday(&begin, NULL); - sync(); -#endif + unsigned long start = microtime(); for (int o = 0; o < b->namespaces_by_dir->values->length; o++) { Nsc *nsc = array_get_index(b->namespaces_by_dir->values, o); @@ -126,36 +115,20 @@ void stage_5(Build *b) { #endif } -#ifdef WIN32 - QueryPerformanceCounter(&end); - double time_llvm = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart; - QueryPerformanceCounter(&start); -#else - gettimeofday(&end, NULL); - double time_llvm = (double)(end.tv_usec - begin.tv_usec) / 1000000 + (double)(end.tv_sec - begin.tv_sec); - gettimeofday(&begin, NULL); -#endif - if (b->verbose > 0) { - printf("⌚ LLVM build o: %.3fs\n", time_llvm); + printf("⌚ LLVM build o: %.3fs\n", (double)(microtime() - start) / 1000000); } + start = microtime(); + if (!b->main_func && !b->test) { build_error(b, "❌ Missing 'main' function"); } stage_5_link(b, o_files); -#ifdef WIN32 - QueryPerformanceCounter(&end); - double time_link = (double)(end.QuadPart - start.QuadPart) / frequency.QuadPart; -#else - gettimeofday(&end, NULL); - double time_link = (double)(end.tv_usec - begin.tv_usec) / 1000000 + (double)(end.tv_sec - begin.tv_sec); -#endif - if (b->verbose > 0) { - printf("⌚ Link: %.3fs\n", time_link); + printf("⌚ Link: %.3fs\n", (double)(microtime() - start) / 1000000); } } diff --git a/src/build/value.c b/src/build/value.c index 4939260e..e6e5eda9 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -355,8 +355,9 @@ Value *read_value(Fc *fc, Allocator *alc, Scope *scope, bool sameline, int prio, if (strcmp(token, "0") == 0 && get_char(fc, 0) == 'x') { // chunk_move(fc->chunk, 1); - read_hex(fc, token); - iv = hex2int(token); + char buf[256]; + read_hex(fc, buf); + iv = hex2int(buf); } else if (is_number(token[0])) { char *num_str = dups(alc, token); char *float_str = NULL; @@ -1173,7 +1174,7 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { str_append_char(buf, ' '); char *content = str_to_chars(alc, buf); - Chunk *chunk = chunk_init(alc, NULL); + Chunk *chunk = chunk_init(alc, fc->b, NULL); chunk->parent = fc->chunk; chunk->content = content; chunk->length = buf->length; diff --git a/src/functions.c b/src/functions.c index 3436a80e..85e02235 100644 --- a/src/functions.c +++ b/src/functions.c @@ -111,7 +111,9 @@ void sleep_ns(unsigned int ns) { #endif } -void simple_hash(char *content_, char *buf_) { +// simple hash has similar speed to crc32 but returns a string instead of a number +// by: github.com/ctxcode +void ctxhash(char *content_, char *buf_) { unsigned char *content = (unsigned char *)content_; unsigned char *buf = (unsigned char *)buf_; @@ -261,3 +263,59 @@ void run_async(void *func, void *arg, bool wait) { #endif } } + +unsigned long microtime() { + struct timeval tv; + gettimeofday(&tv, NULL); + unsigned long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec; + return time_in_micros; +} + +#ifdef WIN32 +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else + #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag = 0; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + tmpres /= 10; /*convert into microseconds*/ + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} +#endif diff --git a/src/headers/array.h b/src/headers/array.h index 7763d3f6..2ba97ef7 100644 --- a/src/headers/array.h +++ b/src/headers/array.h @@ -24,6 +24,7 @@ void *array_pop(Array *arr); void *array_pop_first(Array *arr); bool array_contains(Array *, void *, int); int array_find(Array *, void *, int); +int array_find_x(Array *arr, void *item, int type, int start, int end); void array_shift(Array *arr, void *item); void *array_get_index(Array *, int); void array_set_index(Array *, int, void *); diff --git a/src/headers/functions.h b/src/headers/functions.h index 8b9fef81..1d9ce288 100644 --- a/src/headers/functions.h +++ b/src/headers/functions.h @@ -7,7 +7,7 @@ int atoi(const char *str); int hex2int(char *hex); void sleep_ns(unsigned int ns); void sleep_ms(unsigned int ms); -void simple_hash(char *content, char *buf); +void ctxhash(char *content, char *buf); Array *explode(Allocator *alc, char *part, char *content); int system_silent(char *cmd); char *str_replace_simple(char *s, const char *s1, const char *s2); @@ -15,6 +15,10 @@ char *str_replace(Allocator *alc, char *orig, char *rep, char *with); void free_delayed(void *item); void rtrim(char *str, char ch); void run_async(void *func, void *arg, bool wait); +unsigned long microtime(); + +// crc32 +unsigned int calculate_crc32c(unsigned int crc32c, const unsigned char *buffer, unsigned int length); // Syntax bool is_alpha_char(char c); @@ -171,10 +175,9 @@ void stage_2_6_class_functions(Fc *fc, Class *class); void stage_3_class(Fc *fc, Class *class); // Read -Chunk *chunk_init(Allocator *alc, Fc *fc); +Chunk *chunk_init(Allocator *alc, Build* b, Fc *fc); Chunk *chunk_clone(Allocator *alc, Chunk *chunk); void chunk_move(Chunk *chunk, int pos); -void chunk_update_col(Chunk *chunk); void chunk_lex_start(Chunk *chunk); void chunk_lex(Chunk* chunk, int err_token_i, int* err_content_i, int* err_line, int* err_col); char* tok(Fc *fc, bool sameline, bool allow_space); diff --git a/src/headers/map.h b/src/headers/map.h index a739c015..c87d2a0a 100644 --- a/src/headers/map.h +++ b/src/headers/map.h @@ -9,6 +9,7 @@ typedef struct Map { Allocator *alc; Array *keys; Array *values; + int find_start; } Map; Map *map_make(Allocator *alc); diff --git a/src/headers/structs.h b/src/headers/structs.h index 4eb30b3b..79d34734 100644 --- a/src/headers/structs.h +++ b/src/headers/structs.h @@ -140,6 +140,10 @@ struct Build { bool clear_cache; bool run_code; bool link_static; + unsigned long time_lex; + unsigned long time_fs; + unsigned long time_ir; + unsigned long time_parse; }; struct Fc { @@ -220,6 +224,7 @@ struct Link { }; struct Chunk { + Build *b; Fc *fc; Chunk *parent; char *content; diff --git a/src/libs/crc32.c b/src/libs/crc32.c new file mode 100644 index 00000000..70b55d17 --- /dev/null +++ b/src/libs/crc32.c @@ -0,0 +1,748 @@ +/*- + * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or + * code or tables extracted from it, as desired without restriction. + */ + +/* + * First, the polynomial itself and its table of feedback terms. The + * polynomial is + * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 + * + * Note that we take it "backwards" and put the highest-order term in + * the lowest-order bit. The X^32 term is "implied"; the LSB is the + * X^31 term, etc. The X^0 term (usually shown as "+1") results in + * the MSB being 1 + * + * Note that the usual hardware shift register implementation, which + * is what we're using (we're merely optimizing it by doing eight-bit + * chunks at a time) shifts bits into the lowest-order term. In our + * implementation, that means shifting towards the right. Why do we + * do it this way? Because the calculated CRC must be transmitted in + * order from highest-order term to lowest-order term. UARTs transmit + * characters in order from LSB to MSB. By storing the CRC this way + * we hand it to the UART in the order low-byte to high-byte; the UART + * sends each low-bit to hight-bit; and the result is transmission bit + * by bit from highest- to lowest-order term without requiring any bit + * shuffling on our part. Reception works similarly + * + * The feedback terms table consists of 256, 32-bit entries. Notes + * + * The table can be generated at runtime if desired; code to do so + * is shown later. It might not be obvious, but the feedback + * terms simply represent the results of eight shift/xor opera + * tions for all combinations of data and CRC register values + * + * The values must be right-shifted by eight bits by the "updcrc + * logic; the shift must be unsigned (bring in zeroes). On some + * hardware you could probably optimize the shift in assembler by + * using byte-swap instructions + * polynomial $edb88320 + * + * + * CRC32 code derived from work by Gary S. Brown. + */ + +#include + +#include +// #include + +const unsigned int crc32_tab[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +/* + * A function that calculates the CRC-32 based on the table above is + * given below for documentation purposes. An equivalent implementation + * of this function that's actually used in the kernel can be found + * in sys/libkern.h, where it can be inlined. + * + * unsigned int + * crc32(const void *buf, size_t size) + * { + * const unsigned char *p = buf; + * unsigned int crc; + * + * crc = ~0U; + * while (size--) + * crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); + * return crc ^ ~0U; + * } + */ + +/* CRC32C routines, these use a different polynomial */ +/*****************************************************************/ +/* */ +/* CRC LOOKUP TABLE */ +/* ================ */ +/* The following CRC lookup table was generated automagically */ +/* by the Rocksoft^tm Model CRC Algorithm Table Generation */ +/* Program V1.0 using the following model parameters: */ +/* */ +/* Width : 4 bytes. */ +/* Poly : 0x1EDC6F41L */ +/* Reverse : TRUE. */ +/* */ +/* For more information on the Rocksoft^tm Model CRC Algorithm, */ +/* see the document titled "A Painless Guide to CRC Error */ +/* Detection Algorithms" by Ross Williams */ +/* (ross@guest.adelaide.edu.au.). This document is likely to be */ +/* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft". */ +/* */ +/*****************************************************************/ + +static const unsigned int crc32Table[256] = { + 0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L, + 0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL, + 0x8AD958CFL, 0x78B2DBCCL, 0x6BE22838L, 0x9989AB3BL, + 0x4D43CFD0L, 0xBF284CD3L, 0xAC78BF27L, 0x5E133C24L, + 0x105EC76FL, 0xE235446CL, 0xF165B798L, 0x030E349BL, + 0xD7C45070L, 0x25AFD373L, 0x36FF2087L, 0xC494A384L, + 0x9A879FA0L, 0x68EC1CA3L, 0x7BBCEF57L, 0x89D76C54L, + 0x5D1D08BFL, 0xAF768BBCL, 0xBC267848L, 0x4E4DFB4BL, + 0x20BD8EDEL, 0xD2D60DDDL, 0xC186FE29L, 0x33ED7D2AL, + 0xE72719C1L, 0x154C9AC2L, 0x061C6936L, 0xF477EA35L, + 0xAA64D611L, 0x580F5512L, 0x4B5FA6E6L, 0xB93425E5L, + 0x6DFE410EL, 0x9F95C20DL, 0x8CC531F9L, 0x7EAEB2FAL, + 0x30E349B1L, 0xC288CAB2L, 0xD1D83946L, 0x23B3BA45L, + 0xF779DEAEL, 0x05125DADL, 0x1642AE59L, 0xE4292D5AL, + 0xBA3A117EL, 0x4851927DL, 0x5B016189L, 0xA96AE28AL, + 0x7DA08661L, 0x8FCB0562L, 0x9C9BF696L, 0x6EF07595L, + 0x417B1DBCL, 0xB3109EBFL, 0xA0406D4BL, 0x522BEE48L, + 0x86E18AA3L, 0x748A09A0L, 0x67DAFA54L, 0x95B17957L, + 0xCBA24573L, 0x39C9C670L, 0x2A993584L, 0xD8F2B687L, + 0x0C38D26CL, 0xFE53516FL, 0xED03A29BL, 0x1F682198L, + 0x5125DAD3L, 0xA34E59D0L, 0xB01EAA24L, 0x42752927L, + 0x96BF4DCCL, 0x64D4CECFL, 0x77843D3BL, 0x85EFBE38L, + 0xDBFC821CL, 0x2997011FL, 0x3AC7F2EBL, 0xC8AC71E8L, + 0x1C661503L, 0xEE0D9600L, 0xFD5D65F4L, 0x0F36E6F7L, + 0x61C69362L, 0x93AD1061L, 0x80FDE395L, 0x72966096L, + 0xA65C047DL, 0x5437877EL, 0x4767748AL, 0xB50CF789L, + 0xEB1FCBADL, 0x197448AEL, 0x0A24BB5AL, 0xF84F3859L, + 0x2C855CB2L, 0xDEEEDFB1L, 0xCDBE2C45L, 0x3FD5AF46L, + 0x7198540DL, 0x83F3D70EL, 0x90A324FAL, 0x62C8A7F9L, + 0xB602C312L, 0x44694011L, 0x5739B3E5L, 0xA55230E6L, + 0xFB410CC2L, 0x092A8FC1L, 0x1A7A7C35L, 0xE811FF36L, + 0x3CDB9BDDL, 0xCEB018DEL, 0xDDE0EB2AL, 0x2F8B6829L, + 0x82F63B78L, 0x709DB87BL, 0x63CD4B8FL, 0x91A6C88CL, + 0x456CAC67L, 0xB7072F64L, 0xA457DC90L, 0x563C5F93L, + 0x082F63B7L, 0xFA44E0B4L, 0xE9141340L, 0x1B7F9043L, + 0xCFB5F4A8L, 0x3DDE77ABL, 0x2E8E845FL, 0xDCE5075CL, + 0x92A8FC17L, 0x60C37F14L, 0x73938CE0L, 0x81F80FE3L, + 0x55326B08L, 0xA759E80BL, 0xB4091BFFL, 0x466298FCL, + 0x1871A4D8L, 0xEA1A27DBL, 0xF94AD42FL, 0x0B21572CL, + 0xDFEB33C7L, 0x2D80B0C4L, 0x3ED04330L, 0xCCBBC033L, + 0xA24BB5A6L, 0x502036A5L, 0x4370C551L, 0xB11B4652L, + 0x65D122B9L, 0x97BAA1BAL, 0x84EA524EL, 0x7681D14DL, + 0x2892ED69L, 0xDAF96E6AL, 0xC9A99D9EL, 0x3BC21E9DL, + 0xEF087A76L, 0x1D63F975L, 0x0E330A81L, 0xFC588982L, + 0xB21572C9L, 0x407EF1CAL, 0x532E023EL, 0xA145813DL, + 0x758FE5D6L, 0x87E466D5L, 0x94B49521L, 0x66DF1622L, + 0x38CC2A06L, 0xCAA7A905L, 0xD9F75AF1L, 0x2B9CD9F2L, + 0xFF56BD19L, 0x0D3D3E1AL, 0x1E6DCDEEL, 0xEC064EEDL, + 0xC38D26C4L, 0x31E6A5C7L, 0x22B65633L, 0xD0DDD530L, + 0x0417B1DBL, 0xF67C32D8L, 0xE52CC12CL, 0x1747422FL, + 0x49547E0BL, 0xBB3FFD08L, 0xA86F0EFCL, 0x5A048DFFL, + 0x8ECEE914L, 0x7CA56A17L, 0x6FF599E3L, 0x9D9E1AE0L, + 0xD3D3E1ABL, 0x21B862A8L, 0x32E8915CL, 0xC083125FL, + 0x144976B4L, 0xE622F5B7L, 0xF5720643L, 0x07198540L, + 0x590AB964L, 0xAB613A67L, 0xB831C993L, 0x4A5A4A90L, + 0x9E902E7BL, 0x6CFBAD78L, 0x7FAB5E8CL, 0x8DC0DD8FL, + 0xE330A81AL, 0x115B2B19L, 0x020BD8EDL, 0xF0605BEEL, + 0x24AA3F05L, 0xD6C1BC06L, 0xC5914FF2L, 0x37FACCF1L, + 0x69E9F0D5L, 0x9B8273D6L, 0x88D28022L, 0x7AB90321L, + 0xAE7367CAL, 0x5C18E4C9L, 0x4F48173DL, 0xBD23943EL, + 0xF36E6F75L, 0x0105EC76L, 0x12551F82L, 0xE03E9C81L, + 0x34F4F86AL, 0xC69F7B69L, 0xD5CF889DL, 0x27A40B9EL, + 0x79B737BAL, 0x8BDCB4B9L, 0x988C474DL, 0x6AE7C44EL, + 0xBE2DA0A5L, 0x4C4623A6L, 0x5F16D052L, 0xAD7D5351L +}; + +static unsigned int +singletable_crc32c(unsigned int crc, const void *buf, size_t size) +{ + const unsigned char *p = buf; + + + while (size--) + crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8); + + return crc; +} + + +/* + * Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved + * + * + * This software program is licensed subject to the BSD License, available at + * http://www.opensource.org/licenses/bsd-license.html. + * + * Abstract: + * + * Tables for software CRC generation + */ + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o32[256] = +{ + 0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4, 0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB, + 0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B, 0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24, + 0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B, 0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384, + 0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54, 0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B, + 0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A, 0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35, + 0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5, 0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA, + 0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45, 0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A, + 0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A, 0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595, + 0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48, 0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957, + 0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687, 0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198, + 0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927, 0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38, + 0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8, 0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7, + 0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096, 0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789, + 0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859, 0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46, + 0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9, 0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6, + 0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36, 0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829, + 0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C, 0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93, + 0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043, 0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C, + 0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3, 0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC, + 0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C, 0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033, + 0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652, 0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D, + 0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D, 0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982, + 0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D, 0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622, + 0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2, 0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED, + 0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530, 0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F, + 0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF, 0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0, + 0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F, 0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540, + 0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90, 0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F, + 0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE, 0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1, + 0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321, 0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E, + 0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81, 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E, + 0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E, 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351 +}; + +/* + * end of the CRC lookup table crc_tableil8_o32 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o40[256] = +{ + 0x00000000, 0x13A29877, 0x274530EE, 0x34E7A899, 0x4E8A61DC, 0x5D28F9AB, 0x69CF5132, 0x7A6DC945, + 0x9D14C3B8, 0x8EB65BCF, 0xBA51F356, 0xA9F36B21, 0xD39EA264, 0xC03C3A13, 0xF4DB928A, 0xE7790AFD, + 0x3FC5F181, 0x2C6769F6, 0x1880C16F, 0x0B225918, 0x714F905D, 0x62ED082A, 0x560AA0B3, 0x45A838C4, + 0xA2D13239, 0xB173AA4E, 0x859402D7, 0x96369AA0, 0xEC5B53E5, 0xFFF9CB92, 0xCB1E630B, 0xD8BCFB7C, + 0x7F8BE302, 0x6C297B75, 0x58CED3EC, 0x4B6C4B9B, 0x310182DE, 0x22A31AA9, 0x1644B230, 0x05E62A47, + 0xE29F20BA, 0xF13DB8CD, 0xC5DA1054, 0xD6788823, 0xAC154166, 0xBFB7D911, 0x8B507188, 0x98F2E9FF, + 0x404E1283, 0x53EC8AF4, 0x670B226D, 0x74A9BA1A, 0x0EC4735F, 0x1D66EB28, 0x298143B1, 0x3A23DBC6, + 0xDD5AD13B, 0xCEF8494C, 0xFA1FE1D5, 0xE9BD79A2, 0x93D0B0E7, 0x80722890, 0xB4958009, 0xA737187E, + 0xFF17C604, 0xECB55E73, 0xD852F6EA, 0xCBF06E9D, 0xB19DA7D8, 0xA23F3FAF, 0x96D89736, 0x857A0F41, + 0x620305BC, 0x71A19DCB, 0x45463552, 0x56E4AD25, 0x2C896460, 0x3F2BFC17, 0x0BCC548E, 0x186ECCF9, + 0xC0D23785, 0xD370AFF2, 0xE797076B, 0xF4359F1C, 0x8E585659, 0x9DFACE2E, 0xA91D66B7, 0xBABFFEC0, + 0x5DC6F43D, 0x4E646C4A, 0x7A83C4D3, 0x69215CA4, 0x134C95E1, 0x00EE0D96, 0x3409A50F, 0x27AB3D78, + 0x809C2506, 0x933EBD71, 0xA7D915E8, 0xB47B8D9F, 0xCE1644DA, 0xDDB4DCAD, 0xE9537434, 0xFAF1EC43, + 0x1D88E6BE, 0x0E2A7EC9, 0x3ACDD650, 0x296F4E27, 0x53028762, 0x40A01F15, 0x7447B78C, 0x67E52FFB, + 0xBF59D487, 0xACFB4CF0, 0x981CE469, 0x8BBE7C1E, 0xF1D3B55B, 0xE2712D2C, 0xD69685B5, 0xC5341DC2, + 0x224D173F, 0x31EF8F48, 0x050827D1, 0x16AABFA6, 0x6CC776E3, 0x7F65EE94, 0x4B82460D, 0x5820DE7A, + 0xFBC3FAF9, 0xE861628E, 0xDC86CA17, 0xCF245260, 0xB5499B25, 0xA6EB0352, 0x920CABCB, 0x81AE33BC, + 0x66D73941, 0x7575A136, 0x419209AF, 0x523091D8, 0x285D589D, 0x3BFFC0EA, 0x0F186873, 0x1CBAF004, + 0xC4060B78, 0xD7A4930F, 0xE3433B96, 0xF0E1A3E1, 0x8A8C6AA4, 0x992EF2D3, 0xADC95A4A, 0xBE6BC23D, + 0x5912C8C0, 0x4AB050B7, 0x7E57F82E, 0x6DF56059, 0x1798A91C, 0x043A316B, 0x30DD99F2, 0x237F0185, + 0x844819FB, 0x97EA818C, 0xA30D2915, 0xB0AFB162, 0xCAC27827, 0xD960E050, 0xED8748C9, 0xFE25D0BE, + 0x195CDA43, 0x0AFE4234, 0x3E19EAAD, 0x2DBB72DA, 0x57D6BB9F, 0x447423E8, 0x70938B71, 0x63311306, + 0xBB8DE87A, 0xA82F700D, 0x9CC8D894, 0x8F6A40E3, 0xF50789A6, 0xE6A511D1, 0xD242B948, 0xC1E0213F, + 0x26992BC2, 0x353BB3B5, 0x01DC1B2C, 0x127E835B, 0x68134A1E, 0x7BB1D269, 0x4F567AF0, 0x5CF4E287, + 0x04D43CFD, 0x1776A48A, 0x23910C13, 0x30339464, 0x4A5E5D21, 0x59FCC556, 0x6D1B6DCF, 0x7EB9F5B8, + 0x99C0FF45, 0x8A626732, 0xBE85CFAB, 0xAD2757DC, 0xD74A9E99, 0xC4E806EE, 0xF00FAE77, 0xE3AD3600, + 0x3B11CD7C, 0x28B3550B, 0x1C54FD92, 0x0FF665E5, 0x759BACA0, 0x663934D7, 0x52DE9C4E, 0x417C0439, + 0xA6050EC4, 0xB5A796B3, 0x81403E2A, 0x92E2A65D, 0xE88F6F18, 0xFB2DF76F, 0xCFCA5FF6, 0xDC68C781, + 0x7B5FDFFF, 0x68FD4788, 0x5C1AEF11, 0x4FB87766, 0x35D5BE23, 0x26772654, 0x12908ECD, 0x013216BA, + 0xE64B1C47, 0xF5E98430, 0xC10E2CA9, 0xD2ACB4DE, 0xA8C17D9B, 0xBB63E5EC, 0x8F844D75, 0x9C26D502, + 0x449A2E7E, 0x5738B609, 0x63DF1E90, 0x707D86E7, 0x0A104FA2, 0x19B2D7D5, 0x2D557F4C, 0x3EF7E73B, + 0xD98EEDC6, 0xCA2C75B1, 0xFECBDD28, 0xED69455F, 0x97048C1A, 0x84A6146D, 0xB041BCF4, 0xA3E32483 +}; + +/* + * end of the CRC lookup table crc_tableil8_o40 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o48[256] = +{ + 0x00000000, 0xA541927E, 0x4F6F520D, 0xEA2EC073, 0x9EDEA41A, 0x3B9F3664, 0xD1B1F617, 0x74F06469, + 0x38513EC5, 0x9D10ACBB, 0x773E6CC8, 0xD27FFEB6, 0xA68F9ADF, 0x03CE08A1, 0xE9E0C8D2, 0x4CA15AAC, + 0x70A27D8A, 0xD5E3EFF4, 0x3FCD2F87, 0x9A8CBDF9, 0xEE7CD990, 0x4B3D4BEE, 0xA1138B9D, 0x045219E3, + 0x48F3434F, 0xEDB2D131, 0x079C1142, 0xA2DD833C, 0xD62DE755, 0x736C752B, 0x9942B558, 0x3C032726, + 0xE144FB14, 0x4405696A, 0xAE2BA919, 0x0B6A3B67, 0x7F9A5F0E, 0xDADBCD70, 0x30F50D03, 0x95B49F7D, + 0xD915C5D1, 0x7C5457AF, 0x967A97DC, 0x333B05A2, 0x47CB61CB, 0xE28AF3B5, 0x08A433C6, 0xADE5A1B8, + 0x91E6869E, 0x34A714E0, 0xDE89D493, 0x7BC846ED, 0x0F382284, 0xAA79B0FA, 0x40577089, 0xE516E2F7, + 0xA9B7B85B, 0x0CF62A25, 0xE6D8EA56, 0x43997828, 0x37691C41, 0x92288E3F, 0x78064E4C, 0xDD47DC32, + 0xC76580D9, 0x622412A7, 0x880AD2D4, 0x2D4B40AA, 0x59BB24C3, 0xFCFAB6BD, 0x16D476CE, 0xB395E4B0, + 0xFF34BE1C, 0x5A752C62, 0xB05BEC11, 0x151A7E6F, 0x61EA1A06, 0xC4AB8878, 0x2E85480B, 0x8BC4DA75, + 0xB7C7FD53, 0x12866F2D, 0xF8A8AF5E, 0x5DE93D20, 0x29195949, 0x8C58CB37, 0x66760B44, 0xC337993A, + 0x8F96C396, 0x2AD751E8, 0xC0F9919B, 0x65B803E5, 0x1148678C, 0xB409F5F2, 0x5E273581, 0xFB66A7FF, + 0x26217BCD, 0x8360E9B3, 0x694E29C0, 0xCC0FBBBE, 0xB8FFDFD7, 0x1DBE4DA9, 0xF7908DDA, 0x52D11FA4, + 0x1E704508, 0xBB31D776, 0x511F1705, 0xF45E857B, 0x80AEE112, 0x25EF736C, 0xCFC1B31F, 0x6A802161, + 0x56830647, 0xF3C29439, 0x19EC544A, 0xBCADC634, 0xC85DA25D, 0x6D1C3023, 0x8732F050, 0x2273622E, + 0x6ED23882, 0xCB93AAFC, 0x21BD6A8F, 0x84FCF8F1, 0xF00C9C98, 0x554D0EE6, 0xBF63CE95, 0x1A225CEB, + 0x8B277743, 0x2E66E53D, 0xC448254E, 0x6109B730, 0x15F9D359, 0xB0B84127, 0x5A968154, 0xFFD7132A, + 0xB3764986, 0x1637DBF8, 0xFC191B8B, 0x595889F5, 0x2DA8ED9C, 0x88E97FE2, 0x62C7BF91, 0xC7862DEF, + 0xFB850AC9, 0x5EC498B7, 0xB4EA58C4, 0x11ABCABA, 0x655BAED3, 0xC01A3CAD, 0x2A34FCDE, 0x8F756EA0, + 0xC3D4340C, 0x6695A672, 0x8CBB6601, 0x29FAF47F, 0x5D0A9016, 0xF84B0268, 0x1265C21B, 0xB7245065, + 0x6A638C57, 0xCF221E29, 0x250CDE5A, 0x804D4C24, 0xF4BD284D, 0x51FCBA33, 0xBBD27A40, 0x1E93E83E, + 0x5232B292, 0xF77320EC, 0x1D5DE09F, 0xB81C72E1, 0xCCEC1688, 0x69AD84F6, 0x83834485, 0x26C2D6FB, + 0x1AC1F1DD, 0xBF8063A3, 0x55AEA3D0, 0xF0EF31AE, 0x841F55C7, 0x215EC7B9, 0xCB7007CA, 0x6E3195B4, + 0x2290CF18, 0x87D15D66, 0x6DFF9D15, 0xC8BE0F6B, 0xBC4E6B02, 0x190FF97C, 0xF321390F, 0x5660AB71, + 0x4C42F79A, 0xE90365E4, 0x032DA597, 0xA66C37E9, 0xD29C5380, 0x77DDC1FE, 0x9DF3018D, 0x38B293F3, + 0x7413C95F, 0xD1525B21, 0x3B7C9B52, 0x9E3D092C, 0xEACD6D45, 0x4F8CFF3B, 0xA5A23F48, 0x00E3AD36, + 0x3CE08A10, 0x99A1186E, 0x738FD81D, 0xD6CE4A63, 0xA23E2E0A, 0x077FBC74, 0xED517C07, 0x4810EE79, + 0x04B1B4D5, 0xA1F026AB, 0x4BDEE6D8, 0xEE9F74A6, 0x9A6F10CF, 0x3F2E82B1, 0xD50042C2, 0x7041D0BC, + 0xAD060C8E, 0x08479EF0, 0xE2695E83, 0x4728CCFD, 0x33D8A894, 0x96993AEA, 0x7CB7FA99, 0xD9F668E7, + 0x9557324B, 0x3016A035, 0xDA386046, 0x7F79F238, 0x0B899651, 0xAEC8042F, 0x44E6C45C, 0xE1A75622, + 0xDDA47104, 0x78E5E37A, 0x92CB2309, 0x378AB177, 0x437AD51E, 0xE63B4760, 0x0C158713, 0xA954156D, + 0xE5F54FC1, 0x40B4DDBF, 0xAA9A1DCC, 0x0FDB8FB2, 0x7B2BEBDB, 0xDE6A79A5, 0x3444B9D6, 0x91052BA8 +}; + +/* + * end of the CRC lookup table crc_tableil8_o48 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o56[256] = +{ + 0x00000000, 0xDD45AAB8, 0xBF672381, 0x62228939, 0x7B2231F3, 0xA6679B4B, 0xC4451272, 0x1900B8CA, + 0xF64463E6, 0x2B01C95E, 0x49234067, 0x9466EADF, 0x8D665215, 0x5023F8AD, 0x32017194, 0xEF44DB2C, + 0xE964B13D, 0x34211B85, 0x560392BC, 0x8B463804, 0x924680CE, 0x4F032A76, 0x2D21A34F, 0xF06409F7, + 0x1F20D2DB, 0xC2657863, 0xA047F15A, 0x7D025BE2, 0x6402E328, 0xB9474990, 0xDB65C0A9, 0x06206A11, + 0xD725148B, 0x0A60BE33, 0x6842370A, 0xB5079DB2, 0xAC072578, 0x71428FC0, 0x136006F9, 0xCE25AC41, + 0x2161776D, 0xFC24DDD5, 0x9E0654EC, 0x4343FE54, 0x5A43469E, 0x8706EC26, 0xE524651F, 0x3861CFA7, + 0x3E41A5B6, 0xE3040F0E, 0x81268637, 0x5C632C8F, 0x45639445, 0x98263EFD, 0xFA04B7C4, 0x27411D7C, + 0xC805C650, 0x15406CE8, 0x7762E5D1, 0xAA274F69, 0xB327F7A3, 0x6E625D1B, 0x0C40D422, 0xD1057E9A, + 0xABA65FE7, 0x76E3F55F, 0x14C17C66, 0xC984D6DE, 0xD0846E14, 0x0DC1C4AC, 0x6FE34D95, 0xB2A6E72D, + 0x5DE23C01, 0x80A796B9, 0xE2851F80, 0x3FC0B538, 0x26C00DF2, 0xFB85A74A, 0x99A72E73, 0x44E284CB, + 0x42C2EEDA, 0x9F874462, 0xFDA5CD5B, 0x20E067E3, 0x39E0DF29, 0xE4A57591, 0x8687FCA8, 0x5BC25610, + 0xB4868D3C, 0x69C32784, 0x0BE1AEBD, 0xD6A40405, 0xCFA4BCCF, 0x12E11677, 0x70C39F4E, 0xAD8635F6, + 0x7C834B6C, 0xA1C6E1D4, 0xC3E468ED, 0x1EA1C255, 0x07A17A9F, 0xDAE4D027, 0xB8C6591E, 0x6583F3A6, + 0x8AC7288A, 0x57828232, 0x35A00B0B, 0xE8E5A1B3, 0xF1E51979, 0x2CA0B3C1, 0x4E823AF8, 0x93C79040, + 0x95E7FA51, 0x48A250E9, 0x2A80D9D0, 0xF7C57368, 0xEEC5CBA2, 0x3380611A, 0x51A2E823, 0x8CE7429B, + 0x63A399B7, 0xBEE6330F, 0xDCC4BA36, 0x0181108E, 0x1881A844, 0xC5C402FC, 0xA7E68BC5, 0x7AA3217D, + 0x52A0C93F, 0x8FE56387, 0xEDC7EABE, 0x30824006, 0x2982F8CC, 0xF4C75274, 0x96E5DB4D, 0x4BA071F5, + 0xA4E4AAD9, 0x79A10061, 0x1B838958, 0xC6C623E0, 0xDFC69B2A, 0x02833192, 0x60A1B8AB, 0xBDE41213, + 0xBBC47802, 0x6681D2BA, 0x04A35B83, 0xD9E6F13B, 0xC0E649F1, 0x1DA3E349, 0x7F816A70, 0xA2C4C0C8, + 0x4D801BE4, 0x90C5B15C, 0xF2E73865, 0x2FA292DD, 0x36A22A17, 0xEBE780AF, 0x89C50996, 0x5480A32E, + 0x8585DDB4, 0x58C0770C, 0x3AE2FE35, 0xE7A7548D, 0xFEA7EC47, 0x23E246FF, 0x41C0CFC6, 0x9C85657E, + 0x73C1BE52, 0xAE8414EA, 0xCCA69DD3, 0x11E3376B, 0x08E38FA1, 0xD5A62519, 0xB784AC20, 0x6AC10698, + 0x6CE16C89, 0xB1A4C631, 0xD3864F08, 0x0EC3E5B0, 0x17C35D7A, 0xCA86F7C2, 0xA8A47EFB, 0x75E1D443, + 0x9AA50F6F, 0x47E0A5D7, 0x25C22CEE, 0xF8878656, 0xE1873E9C, 0x3CC29424, 0x5EE01D1D, 0x83A5B7A5, + 0xF90696D8, 0x24433C60, 0x4661B559, 0x9B241FE1, 0x8224A72B, 0x5F610D93, 0x3D4384AA, 0xE0062E12, + 0x0F42F53E, 0xD2075F86, 0xB025D6BF, 0x6D607C07, 0x7460C4CD, 0xA9256E75, 0xCB07E74C, 0x16424DF4, + 0x106227E5, 0xCD278D5D, 0xAF050464, 0x7240AEDC, 0x6B401616, 0xB605BCAE, 0xD4273597, 0x09629F2F, + 0xE6264403, 0x3B63EEBB, 0x59416782, 0x8404CD3A, 0x9D0475F0, 0x4041DF48, 0x22635671, 0xFF26FCC9, + 0x2E238253, 0xF36628EB, 0x9144A1D2, 0x4C010B6A, 0x5501B3A0, 0x88441918, 0xEA669021, 0x37233A99, + 0xD867E1B5, 0x05224B0D, 0x6700C234, 0xBA45688C, 0xA345D046, 0x7E007AFE, 0x1C22F3C7, 0xC167597F, + 0xC747336E, 0x1A0299D6, 0x782010EF, 0xA565BA57, 0xBC65029D, 0x6120A825, 0x0302211C, 0xDE478BA4, + 0x31035088, 0xEC46FA30, 0x8E647309, 0x5321D9B1, 0x4A21617B, 0x9764CBC3, 0xF54642FA, 0x2803E842 +}; + +/* + * end of the CRC lookup table crc_tableil8_o56 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o64[256] = +{ + 0x00000000, 0x38116FAC, 0x7022DF58, 0x4833B0F4, 0xE045BEB0, 0xD854D11C, 0x906761E8, 0xA8760E44, + 0xC5670B91, 0xFD76643D, 0xB545D4C9, 0x8D54BB65, 0x2522B521, 0x1D33DA8D, 0x55006A79, 0x6D1105D5, + 0x8F2261D3, 0xB7330E7F, 0xFF00BE8B, 0xC711D127, 0x6F67DF63, 0x5776B0CF, 0x1F45003B, 0x27546F97, + 0x4A456A42, 0x725405EE, 0x3A67B51A, 0x0276DAB6, 0xAA00D4F2, 0x9211BB5E, 0xDA220BAA, 0xE2336406, + 0x1BA8B557, 0x23B9DAFB, 0x6B8A6A0F, 0x539B05A3, 0xFBED0BE7, 0xC3FC644B, 0x8BCFD4BF, 0xB3DEBB13, + 0xDECFBEC6, 0xE6DED16A, 0xAEED619E, 0x96FC0E32, 0x3E8A0076, 0x069B6FDA, 0x4EA8DF2E, 0x76B9B082, + 0x948AD484, 0xAC9BBB28, 0xE4A80BDC, 0xDCB96470, 0x74CF6A34, 0x4CDE0598, 0x04EDB56C, 0x3CFCDAC0, + 0x51EDDF15, 0x69FCB0B9, 0x21CF004D, 0x19DE6FE1, 0xB1A861A5, 0x89B90E09, 0xC18ABEFD, 0xF99BD151, + 0x37516AAE, 0x0F400502, 0x4773B5F6, 0x7F62DA5A, 0xD714D41E, 0xEF05BBB2, 0xA7360B46, 0x9F2764EA, + 0xF236613F, 0xCA270E93, 0x8214BE67, 0xBA05D1CB, 0x1273DF8F, 0x2A62B023, 0x625100D7, 0x5A406F7B, + 0xB8730B7D, 0x806264D1, 0xC851D425, 0xF040BB89, 0x5836B5CD, 0x6027DA61, 0x28146A95, 0x10050539, + 0x7D1400EC, 0x45056F40, 0x0D36DFB4, 0x3527B018, 0x9D51BE5C, 0xA540D1F0, 0xED736104, 0xD5620EA8, + 0x2CF9DFF9, 0x14E8B055, 0x5CDB00A1, 0x64CA6F0D, 0xCCBC6149, 0xF4AD0EE5, 0xBC9EBE11, 0x848FD1BD, + 0xE99ED468, 0xD18FBBC4, 0x99BC0B30, 0xA1AD649C, 0x09DB6AD8, 0x31CA0574, 0x79F9B580, 0x41E8DA2C, + 0xA3DBBE2A, 0x9BCAD186, 0xD3F96172, 0xEBE80EDE, 0x439E009A, 0x7B8F6F36, 0x33BCDFC2, 0x0BADB06E, + 0x66BCB5BB, 0x5EADDA17, 0x169E6AE3, 0x2E8F054F, 0x86F90B0B, 0xBEE864A7, 0xF6DBD453, 0xCECABBFF, + 0x6EA2D55C, 0x56B3BAF0, 0x1E800A04, 0x269165A8, 0x8EE76BEC, 0xB6F60440, 0xFEC5B4B4, 0xC6D4DB18, + 0xABC5DECD, 0x93D4B161, 0xDBE70195, 0xE3F66E39, 0x4B80607D, 0x73910FD1, 0x3BA2BF25, 0x03B3D089, + 0xE180B48F, 0xD991DB23, 0x91A26BD7, 0xA9B3047B, 0x01C50A3F, 0x39D46593, 0x71E7D567, 0x49F6BACB, + 0x24E7BF1E, 0x1CF6D0B2, 0x54C56046, 0x6CD40FEA, 0xC4A201AE, 0xFCB36E02, 0xB480DEF6, 0x8C91B15A, + 0x750A600B, 0x4D1B0FA7, 0x0528BF53, 0x3D39D0FF, 0x954FDEBB, 0xAD5EB117, 0xE56D01E3, 0xDD7C6E4F, + 0xB06D6B9A, 0x887C0436, 0xC04FB4C2, 0xF85EDB6E, 0x5028D52A, 0x6839BA86, 0x200A0A72, 0x181B65DE, + 0xFA2801D8, 0xC2396E74, 0x8A0ADE80, 0xB21BB12C, 0x1A6DBF68, 0x227CD0C4, 0x6A4F6030, 0x525E0F9C, + 0x3F4F0A49, 0x075E65E5, 0x4F6DD511, 0x777CBABD, 0xDF0AB4F9, 0xE71BDB55, 0xAF286BA1, 0x9739040D, + 0x59F3BFF2, 0x61E2D05E, 0x29D160AA, 0x11C00F06, 0xB9B60142, 0x81A76EEE, 0xC994DE1A, 0xF185B1B6, + 0x9C94B463, 0xA485DBCF, 0xECB66B3B, 0xD4A70497, 0x7CD10AD3, 0x44C0657F, 0x0CF3D58B, 0x34E2BA27, + 0xD6D1DE21, 0xEEC0B18D, 0xA6F30179, 0x9EE26ED5, 0x36946091, 0x0E850F3D, 0x46B6BFC9, 0x7EA7D065, + 0x13B6D5B0, 0x2BA7BA1C, 0x63940AE8, 0x5B856544, 0xF3F36B00, 0xCBE204AC, 0x83D1B458, 0xBBC0DBF4, + 0x425B0AA5, 0x7A4A6509, 0x3279D5FD, 0x0A68BA51, 0xA21EB415, 0x9A0FDBB9, 0xD23C6B4D, 0xEA2D04E1, + 0x873C0134, 0xBF2D6E98, 0xF71EDE6C, 0xCF0FB1C0, 0x6779BF84, 0x5F68D028, 0x175B60DC, 0x2F4A0F70, + 0xCD796B76, 0xF56804DA, 0xBD5BB42E, 0x854ADB82, 0x2D3CD5C6, 0x152DBA6A, 0x5D1E0A9E, 0x650F6532, + 0x081E60E7, 0x300F0F4B, 0x783CBFBF, 0x402DD013, 0xE85BDE57, 0xD04AB1FB, 0x9879010F, 0xA0686EA3 +}; + +/* + * end of the CRC lookup table crc_tableil8_o64 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o72[256] = +{ + 0x00000000, 0xEF306B19, 0xDB8CA0C3, 0x34BCCBDA, 0xB2F53777, 0x5DC55C6E, 0x697997B4, 0x8649FCAD, + 0x6006181F, 0x8F367306, 0xBB8AB8DC, 0x54BAD3C5, 0xD2F32F68, 0x3DC34471, 0x097F8FAB, 0xE64FE4B2, + 0xC00C303E, 0x2F3C5B27, 0x1B8090FD, 0xF4B0FBE4, 0x72F90749, 0x9DC96C50, 0xA975A78A, 0x4645CC93, + 0xA00A2821, 0x4F3A4338, 0x7B8688E2, 0x94B6E3FB, 0x12FF1F56, 0xFDCF744F, 0xC973BF95, 0x2643D48C, + 0x85F4168D, 0x6AC47D94, 0x5E78B64E, 0xB148DD57, 0x370121FA, 0xD8314AE3, 0xEC8D8139, 0x03BDEA20, + 0xE5F20E92, 0x0AC2658B, 0x3E7EAE51, 0xD14EC548, 0x570739E5, 0xB83752FC, 0x8C8B9926, 0x63BBF23F, + 0x45F826B3, 0xAAC84DAA, 0x9E748670, 0x7144ED69, 0xF70D11C4, 0x183D7ADD, 0x2C81B107, 0xC3B1DA1E, + 0x25FE3EAC, 0xCACE55B5, 0xFE729E6F, 0x1142F576, 0x970B09DB, 0x783B62C2, 0x4C87A918, 0xA3B7C201, + 0x0E045BEB, 0xE13430F2, 0xD588FB28, 0x3AB89031, 0xBCF16C9C, 0x53C10785, 0x677DCC5F, 0x884DA746, + 0x6E0243F4, 0x813228ED, 0xB58EE337, 0x5ABE882E, 0xDCF77483, 0x33C71F9A, 0x077BD440, 0xE84BBF59, + 0xCE086BD5, 0x213800CC, 0x1584CB16, 0xFAB4A00F, 0x7CFD5CA2, 0x93CD37BB, 0xA771FC61, 0x48419778, + 0xAE0E73CA, 0x413E18D3, 0x7582D309, 0x9AB2B810, 0x1CFB44BD, 0xF3CB2FA4, 0xC777E47E, 0x28478F67, + 0x8BF04D66, 0x64C0267F, 0x507CEDA5, 0xBF4C86BC, 0x39057A11, 0xD6351108, 0xE289DAD2, 0x0DB9B1CB, + 0xEBF65579, 0x04C63E60, 0x307AF5BA, 0xDF4A9EA3, 0x5903620E, 0xB6330917, 0x828FC2CD, 0x6DBFA9D4, + 0x4BFC7D58, 0xA4CC1641, 0x9070DD9B, 0x7F40B682, 0xF9094A2F, 0x16392136, 0x2285EAEC, 0xCDB581F5, + 0x2BFA6547, 0xC4CA0E5E, 0xF076C584, 0x1F46AE9D, 0x990F5230, 0x763F3929, 0x4283F2F3, 0xADB399EA, + 0x1C08B7D6, 0xF338DCCF, 0xC7841715, 0x28B47C0C, 0xAEFD80A1, 0x41CDEBB8, 0x75712062, 0x9A414B7B, + 0x7C0EAFC9, 0x933EC4D0, 0xA7820F0A, 0x48B26413, 0xCEFB98BE, 0x21CBF3A7, 0x1577387D, 0xFA475364, + 0xDC0487E8, 0x3334ECF1, 0x0788272B, 0xE8B84C32, 0x6EF1B09F, 0x81C1DB86, 0xB57D105C, 0x5A4D7B45, + 0xBC029FF7, 0x5332F4EE, 0x678E3F34, 0x88BE542D, 0x0EF7A880, 0xE1C7C399, 0xD57B0843, 0x3A4B635A, + 0x99FCA15B, 0x76CCCA42, 0x42700198, 0xAD406A81, 0x2B09962C, 0xC439FD35, 0xF08536EF, 0x1FB55DF6, + 0xF9FAB944, 0x16CAD25D, 0x22761987, 0xCD46729E, 0x4B0F8E33, 0xA43FE52A, 0x90832EF0, 0x7FB345E9, + 0x59F09165, 0xB6C0FA7C, 0x827C31A6, 0x6D4C5ABF, 0xEB05A612, 0x0435CD0B, 0x308906D1, 0xDFB96DC8, + 0x39F6897A, 0xD6C6E263, 0xE27A29B9, 0x0D4A42A0, 0x8B03BE0D, 0x6433D514, 0x508F1ECE, 0xBFBF75D7, + 0x120CEC3D, 0xFD3C8724, 0xC9804CFE, 0x26B027E7, 0xA0F9DB4A, 0x4FC9B053, 0x7B757B89, 0x94451090, + 0x720AF422, 0x9D3A9F3B, 0xA98654E1, 0x46B63FF8, 0xC0FFC355, 0x2FCFA84C, 0x1B736396, 0xF443088F, + 0xD200DC03, 0x3D30B71A, 0x098C7CC0, 0xE6BC17D9, 0x60F5EB74, 0x8FC5806D, 0xBB794BB7, 0x544920AE, + 0xB206C41C, 0x5D36AF05, 0x698A64DF, 0x86BA0FC6, 0x00F3F36B, 0xEFC39872, 0xDB7F53A8, 0x344F38B1, + 0x97F8FAB0, 0x78C891A9, 0x4C745A73, 0xA344316A, 0x250DCDC7, 0xCA3DA6DE, 0xFE816D04, 0x11B1061D, + 0xF7FEE2AF, 0x18CE89B6, 0x2C72426C, 0xC3422975, 0x450BD5D8, 0xAA3BBEC1, 0x9E87751B, 0x71B71E02, + 0x57F4CA8E, 0xB8C4A197, 0x8C786A4D, 0x63480154, 0xE501FDF9, 0x0A3196E0, 0x3E8D5D3A, 0xD1BD3623, + 0x37F2D291, 0xD8C2B988, 0xEC7E7252, 0x034E194B, 0x8507E5E6, 0x6A378EFF, 0x5E8B4525, 0xB1BB2E3C +}; + +/* + * end of the CRC lookup table crc_tableil8_o72 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o80[256] = +{ + 0x00000000, 0x68032CC8, 0xD0065990, 0xB8057558, 0xA5E0C5D1, 0xCDE3E919, 0x75E69C41, 0x1DE5B089, + 0x4E2DFD53, 0x262ED19B, 0x9E2BA4C3, 0xF628880B, 0xEBCD3882, 0x83CE144A, 0x3BCB6112, 0x53C84DDA, + 0x9C5BFAA6, 0xF458D66E, 0x4C5DA336, 0x245E8FFE, 0x39BB3F77, 0x51B813BF, 0xE9BD66E7, 0x81BE4A2F, + 0xD27607F5, 0xBA752B3D, 0x02705E65, 0x6A7372AD, 0x7796C224, 0x1F95EEEC, 0xA7909BB4, 0xCF93B77C, + 0x3D5B83BD, 0x5558AF75, 0xED5DDA2D, 0x855EF6E5, 0x98BB466C, 0xF0B86AA4, 0x48BD1FFC, 0x20BE3334, + 0x73767EEE, 0x1B755226, 0xA370277E, 0xCB730BB6, 0xD696BB3F, 0xBE9597F7, 0x0690E2AF, 0x6E93CE67, + 0xA100791B, 0xC90355D3, 0x7106208B, 0x19050C43, 0x04E0BCCA, 0x6CE39002, 0xD4E6E55A, 0xBCE5C992, + 0xEF2D8448, 0x872EA880, 0x3F2BDDD8, 0x5728F110, 0x4ACD4199, 0x22CE6D51, 0x9ACB1809, 0xF2C834C1, + 0x7AB7077A, 0x12B42BB2, 0xAAB15EEA, 0xC2B27222, 0xDF57C2AB, 0xB754EE63, 0x0F519B3B, 0x6752B7F3, + 0x349AFA29, 0x5C99D6E1, 0xE49CA3B9, 0x8C9F8F71, 0x917A3FF8, 0xF9791330, 0x417C6668, 0x297F4AA0, + 0xE6ECFDDC, 0x8EEFD114, 0x36EAA44C, 0x5EE98884, 0x430C380D, 0x2B0F14C5, 0x930A619D, 0xFB094D55, + 0xA8C1008F, 0xC0C22C47, 0x78C7591F, 0x10C475D7, 0x0D21C55E, 0x6522E996, 0xDD279CCE, 0xB524B006, + 0x47EC84C7, 0x2FEFA80F, 0x97EADD57, 0xFFE9F19F, 0xE20C4116, 0x8A0F6DDE, 0x320A1886, 0x5A09344E, + 0x09C17994, 0x61C2555C, 0xD9C72004, 0xB1C40CCC, 0xAC21BC45, 0xC422908D, 0x7C27E5D5, 0x1424C91D, + 0xDBB77E61, 0xB3B452A9, 0x0BB127F1, 0x63B20B39, 0x7E57BBB0, 0x16549778, 0xAE51E220, 0xC652CEE8, + 0x959A8332, 0xFD99AFFA, 0x459CDAA2, 0x2D9FF66A, 0x307A46E3, 0x58796A2B, 0xE07C1F73, 0x887F33BB, + 0xF56E0EF4, 0x9D6D223C, 0x25685764, 0x4D6B7BAC, 0x508ECB25, 0x388DE7ED, 0x808892B5, 0xE88BBE7D, + 0xBB43F3A7, 0xD340DF6F, 0x6B45AA37, 0x034686FF, 0x1EA33676, 0x76A01ABE, 0xCEA56FE6, 0xA6A6432E, + 0x6935F452, 0x0136D89A, 0xB933ADC2, 0xD130810A, 0xCCD53183, 0xA4D61D4B, 0x1CD36813, 0x74D044DB, + 0x27180901, 0x4F1B25C9, 0xF71E5091, 0x9F1D7C59, 0x82F8CCD0, 0xEAFBE018, 0x52FE9540, 0x3AFDB988, + 0xC8358D49, 0xA036A181, 0x1833D4D9, 0x7030F811, 0x6DD54898, 0x05D66450, 0xBDD31108, 0xD5D03DC0, + 0x8618701A, 0xEE1B5CD2, 0x561E298A, 0x3E1D0542, 0x23F8B5CB, 0x4BFB9903, 0xF3FEEC5B, 0x9BFDC093, + 0x546E77EF, 0x3C6D5B27, 0x84682E7F, 0xEC6B02B7, 0xF18EB23E, 0x998D9EF6, 0x2188EBAE, 0x498BC766, + 0x1A438ABC, 0x7240A674, 0xCA45D32C, 0xA246FFE4, 0xBFA34F6D, 0xD7A063A5, 0x6FA516FD, 0x07A63A35, + 0x8FD9098E, 0xE7DA2546, 0x5FDF501E, 0x37DC7CD6, 0x2A39CC5F, 0x423AE097, 0xFA3F95CF, 0x923CB907, + 0xC1F4F4DD, 0xA9F7D815, 0x11F2AD4D, 0x79F18185, 0x6414310C, 0x0C171DC4, 0xB412689C, 0xDC114454, + 0x1382F328, 0x7B81DFE0, 0xC384AAB8, 0xAB878670, 0xB66236F9, 0xDE611A31, 0x66646F69, 0x0E6743A1, + 0x5DAF0E7B, 0x35AC22B3, 0x8DA957EB, 0xE5AA7B23, 0xF84FCBAA, 0x904CE762, 0x2849923A, 0x404ABEF2, + 0xB2828A33, 0xDA81A6FB, 0x6284D3A3, 0x0A87FF6B, 0x17624FE2, 0x7F61632A, 0xC7641672, 0xAF673ABA, + 0xFCAF7760, 0x94AC5BA8, 0x2CA92EF0, 0x44AA0238, 0x594FB2B1, 0x314C9E79, 0x8949EB21, 0xE14AC7E9, + 0x2ED97095, 0x46DA5C5D, 0xFEDF2905, 0x96DC05CD, 0x8B39B544, 0xE33A998C, 0x5B3FECD4, 0x333CC01C, + 0x60F48DC6, 0x08F7A10E, 0xB0F2D456, 0xD8F1F89E, 0xC5144817, 0xAD1764DF, 0x15121187, 0x7D113D4F +}; + +/* + * end of the CRC lookup table crc_tableil8_o80 + */ + + + +/* + * The following CRC lookup table was generated automagically using the + * following model parameters: + * + * Generator Polynomial = ................. 0x1EDC6F41 + * Generator Polynomial Length = .......... 32 bits + * Reflected Bits = ....................... TRUE + * Table Generation Offset = .............. 32 bits + * Number of Slices = ..................... 8 slices + * Slice Lengths = ........................ 8 8 8 8 8 8 8 8 + * Directory Name = ....................... .\ + * File Name = ............................ 8x256_tables.c + */ + +static const unsigned int sctp_crc_tableil8_o88[256] = +{ + 0x00000000, 0x493C7D27, 0x9278FA4E, 0xDB448769, 0x211D826D, 0x6821FF4A, 0xB3657823, 0xFA590504, + 0x423B04DA, 0x0B0779FD, 0xD043FE94, 0x997F83B3, 0x632686B7, 0x2A1AFB90, 0xF15E7CF9, 0xB86201DE, + 0x847609B4, 0xCD4A7493, 0x160EF3FA, 0x5F328EDD, 0xA56B8BD9, 0xEC57F6FE, 0x37137197, 0x7E2F0CB0, + 0xC64D0D6E, 0x8F717049, 0x5435F720, 0x1D098A07, 0xE7508F03, 0xAE6CF224, 0x7528754D, 0x3C14086A, + 0x0D006599, 0x443C18BE, 0x9F789FD7, 0xD644E2F0, 0x2C1DE7F4, 0x65219AD3, 0xBE651DBA, 0xF759609D, + 0x4F3B6143, 0x06071C64, 0xDD439B0D, 0x947FE62A, 0x6E26E32E, 0x271A9E09, 0xFC5E1960, 0xB5626447, + 0x89766C2D, 0xC04A110A, 0x1B0E9663, 0x5232EB44, 0xA86BEE40, 0xE1579367, 0x3A13140E, 0x732F6929, + 0xCB4D68F7, 0x827115D0, 0x593592B9, 0x1009EF9E, 0xEA50EA9A, 0xA36C97BD, 0x782810D4, 0x31146DF3, + 0x1A00CB32, 0x533CB615, 0x8878317C, 0xC1444C5B, 0x3B1D495F, 0x72213478, 0xA965B311, 0xE059CE36, + 0x583BCFE8, 0x1107B2CF, 0xCA4335A6, 0x837F4881, 0x79264D85, 0x301A30A2, 0xEB5EB7CB, 0xA262CAEC, + 0x9E76C286, 0xD74ABFA1, 0x0C0E38C8, 0x453245EF, 0xBF6B40EB, 0xF6573DCC, 0x2D13BAA5, 0x642FC782, + 0xDC4DC65C, 0x9571BB7B, 0x4E353C12, 0x07094135, 0xFD504431, 0xB46C3916, 0x6F28BE7F, 0x2614C358, + 0x1700AEAB, 0x5E3CD38C, 0x857854E5, 0xCC4429C2, 0x361D2CC6, 0x7F2151E1, 0xA465D688, 0xED59ABAF, + 0x553BAA71, 0x1C07D756, 0xC743503F, 0x8E7F2D18, 0x7426281C, 0x3D1A553B, 0xE65ED252, 0xAF62AF75, + 0x9376A71F, 0xDA4ADA38, 0x010E5D51, 0x48322076, 0xB26B2572, 0xFB575855, 0x2013DF3C, 0x692FA21B, + 0xD14DA3C5, 0x9871DEE2, 0x4335598B, 0x0A0924AC, 0xF05021A8, 0xB96C5C8F, 0x6228DBE6, 0x2B14A6C1, + 0x34019664, 0x7D3DEB43, 0xA6796C2A, 0xEF45110D, 0x151C1409, 0x5C20692E, 0x8764EE47, 0xCE589360, + 0x763A92BE, 0x3F06EF99, 0xE44268F0, 0xAD7E15D7, 0x572710D3, 0x1E1B6DF4, 0xC55FEA9D, 0x8C6397BA, + 0xB0779FD0, 0xF94BE2F7, 0x220F659E, 0x6B3318B9, 0x916A1DBD, 0xD856609A, 0x0312E7F3, 0x4A2E9AD4, + 0xF24C9B0A, 0xBB70E62D, 0x60346144, 0x29081C63, 0xD3511967, 0x9A6D6440, 0x4129E329, 0x08159E0E, + 0x3901F3FD, 0x703D8EDA, 0xAB7909B3, 0xE2457494, 0x181C7190, 0x51200CB7, 0x8A648BDE, 0xC358F6F9, + 0x7B3AF727, 0x32068A00, 0xE9420D69, 0xA07E704E, 0x5A27754A, 0x131B086D, 0xC85F8F04, 0x8163F223, + 0xBD77FA49, 0xF44B876E, 0x2F0F0007, 0x66337D20, 0x9C6A7824, 0xD5560503, 0x0E12826A, 0x472EFF4D, + 0xFF4CFE93, 0xB67083B4, 0x6D3404DD, 0x240879FA, 0xDE517CFE, 0x976D01D9, 0x4C2986B0, 0x0515FB97, + 0x2E015D56, 0x673D2071, 0xBC79A718, 0xF545DA3F, 0x0F1CDF3B, 0x4620A21C, 0x9D642575, 0xD4585852, + 0x6C3A598C, 0x250624AB, 0xFE42A3C2, 0xB77EDEE5, 0x4D27DBE1, 0x041BA6C6, 0xDF5F21AF, 0x96635C88, + 0xAA7754E2, 0xE34B29C5, 0x380FAEAC, 0x7133D38B, 0x8B6AD68F, 0xC256ABA8, 0x19122CC1, 0x502E51E6, + 0xE84C5038, 0xA1702D1F, 0x7A34AA76, 0x3308D751, 0xC951D255, 0x806DAF72, 0x5B29281B, 0x1215553C, + 0x230138CF, 0x6A3D45E8, 0xB179C281, 0xF845BFA6, 0x021CBAA2, 0x4B20C785, 0x906440EC, 0xD9583DCB, + 0x613A3C15, 0x28064132, 0xF342C65B, 0xBA7EBB7C, 0x4027BE78, 0x091BC35F, 0xD25F4436, 0x9B633911, + 0xA777317B, 0xEE4B4C5C, 0x350FCB35, 0x7C33B612, 0x866AB316, 0xCF56CE31, 0x14124958, 0x5D2E347F, + 0xE54C35A1, 0xAC704886, 0x7734CFEF, 0x3E08B2C8, 0xC451B7CC, 0x8D6DCAEB, 0x56294D82, 0x1F1530A5 +}; + +/* + * end of the CRC lookup table crc_tableil8_o88 + */ + + +static unsigned int +crc32c_sb8_64_bit(unsigned int crc, + const unsigned char *p_buf, + unsigned int length, + unsigned int init_bytes) +{ + unsigned int li; + unsigned int term1, term2; + unsigned int running_length; + unsigned int end_bytes; + + running_length = ((length - init_bytes) / 8) * 8; + end_bytes = length - init_bytes - running_length; + + for (li = 0; li < init_bytes; li++) + crc = sctp_crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ + (crc >> 8); + for (li = 0; li < running_length / 8; li++) { +#if BYTE_ORDER == BIG_ENDIAN + crc ^= *p_buf++; + crc ^= (*p_buf++) << 8; + crc ^= (*p_buf++) << 16; + crc ^= (*p_buf++) << 24; +#else + crc ^= *(const unsigned int *) p_buf; + p_buf += 4; +#endif + term1 = sctp_crc_tableil8_o88[crc & 0x000000FF] ^ + sctp_crc_tableil8_o80[(crc >> 8) & 0x000000FF]; + term2 = crc >> 16; + crc = term1 ^ + sctp_crc_tableil8_o72[term2 & 0x000000FF] ^ + sctp_crc_tableil8_o64[(term2 >> 8) & 0x000000FF]; + +#if BYTE_ORDER == BIG_ENDIAN + crc ^= sctp_crc_tableil8_o56[*p_buf++]; + crc ^= sctp_crc_tableil8_o48[*p_buf++]; + crc ^= sctp_crc_tableil8_o40[*p_buf++]; + crc ^= sctp_crc_tableil8_o32[*p_buf++]; +#else + term1 = sctp_crc_tableil8_o56[(*(const unsigned int *) p_buf) & 0x000000FF] ^ + sctp_crc_tableil8_o48[((*(const unsigned int *) p_buf) >> 8) & 0x000000FF]; + + term2 = (*(const unsigned int *) p_buf) >> 16; + crc = crc ^ + term1 ^ + sctp_crc_tableil8_o40[term2 & 0x000000FF] ^ + sctp_crc_tableil8_o32[(term2 >> 8) & 0x000000FF]; + p_buf += 4; +#endif + } + for (li = 0; li < end_bytes; li++) + crc = sctp_crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ + (crc >> 8); + return crc; +} + +static unsigned int multitable_crc32c(unsigned int crc32c, const unsigned char *buffer, unsigned int length) { + unsigned int to_even_word; + + if (length == 0) { + return (crc32c); + } + to_even_word = (4 - (((intptr_t)buffer) & 0x3)); + return (crc32c_sb8_64_bit(crc32c, buffer, length, to_even_word)); +} + +unsigned int calculate_crc32c(unsigned int crc32c, const unsigned char *buffer, unsigned int length) { + if (length < 4) { + return (singletable_crc32c(crc32c, buffer, length)); + } else { + return (multitable_crc32c(crc32c, buffer, length)); + } +} diff --git a/src/map.c b/src/map.c index ded301f5..05ae9950 100644 --- a/src/map.c +++ b/src/map.c @@ -6,22 +6,39 @@ Map *map_make(Allocator *alc) { m->alc = alc; m->keys = array_make(alc, 4); m->values = array_make(alc, 4); + m->find_start = 0; return m; } bool map_contains(Map *map, char *key) { - int i = array_find(map->keys, key, arr_find_str); + // int i = array_find(map->keys, key, arr_find_str); + // if (i == -1) + // return false; + int i = array_find_x(map->keys, key, arr_find_str, map->find_start, map->keys->length); if (i == -1) { - return false; + if(map->find_start > 0) { + i = array_find_x(map->keys, key, arr_find_str, 0, map->find_start); + } + if(i == -1) + return false; } + map->find_start = i; return true; } void *map_get(Map *map, char *key) { - int i = array_find(map->keys, key, arr_find_str); + // int i = array_find(map->keys, key, arr_find_str); + // if (i == -1) + // return NULL; + int i = array_find_x(map->keys, key, arr_find_str, map->find_start, map->keys->length); if (i == -1) { - return NULL; + if(map->find_start > 0) { + i = array_find_x(map->keys, key, arr_find_str, 0, map->find_start); + } + if(i == -1) + return NULL; } + map->find_start = i; return array_get_index(map->values, i); } From 55cb38257c31e47893f21cb5c31d1873995bd0f0 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Thu, 1 Feb 2024 01:53:50 +0100 Subject: [PATCH 15/16] macro changes --- src/build/stage-1-parse.c | 481 ++++++++++++++++++-------------------- src/headers/structs.h | 34 ++- 2 files changed, 246 insertions(+), 269 deletions(-) diff --git a/src/build/stage-1-parse.c b/src/build/stage-1-parse.c index 40918458..3bdac2e3 100644 --- a/src/build/stage-1-parse.c +++ b/src/build/stage-1-parse.c @@ -804,9 +804,9 @@ void stage_1_macro(Fc *fc) { Macro *mac = al(alc, sizeof(Macro)); mac->name = name; mac->dname = dname; - mac->vars = map_make(alc); - mac->groups = array_make(alc, 2); - mac->parts = array_make(alc, 8); + mac->input = array_make(alc, 8); + mac->output = array_make(alc, 8); + mac->valid_var_names = array_make(alc, 8); Idf *idf = idf_init(alc, idf_macro); idf->item = mac; @@ -816,253 +816,234 @@ void stage_1_macro(Fc *fc) { map_set(fc->scope->identifiers, name, idf); } - /////////// - - tok_expect(fc, "{", false, true); - tok_expect(fc, "input", false, true); - tok_expect(fc, "{", false, true); - - Map *vars = mac->vars; - bool repeat = false; - - while (true) { - // Read input groups - token = tok(fc, false, true); - if (strcmp(token, "\"") == 0) { - // New group - MacroVarGroup *mvg = al(alc, sizeof(MacroVarGroup)); - mvg->vars = array_make(alc, 4); - - Str *pat = read_string(fc); - char *pattern = str_to_chars(alc, pat); - if (strcmp(pattern, "[]") == 0) { - } else if (strcmp(pattern, "{}") == 0) { - } else if (strcmp(pattern, "()") == 0) { - } else { - sprintf(fc->sbuf, "Invalid macro pattern: '%s'", pattern); - fc_error(fc); - } - - char sign[2]; - sign[0] = pattern[0]; - sign[1] = '\0'; - mvg->start = dups(alc, sign); - sign[0] = pattern[1]; - mvg->end = dups(alc, sign); - - while (true) { - - if (repeat) { - sprintf(fc->sbuf, "You cannot add more inputs after defining a repeating input '*'"); - fc_error(fc); - } - - // int type; - // token = tok(fc, false, true); - // if (strcmp(token, "T") == 0) { - // type = macro_part_type; - // } else if (strcmp(token, "V") == 0) { - // type = macro_part_type; - // } else { - // sprintf(fc->sbuf, "Expected 'T' or 'V' here, found: '%s'", pattern); - // fc_error(fc); - // } - - // token = tok(fc, true, false); - // if (strcmp(token, "*") == 0) { - // infinite = true; - // token = tok(fc, true, false); - // } - // if (strcmp(token, ":") != 0) { - // sprintf(fc->sbuf, "Expected ':', found: '%s'", pattern); - // fc_error(fc); - // } - token = tok(fc, false, true); - if (!is_valid_varname(token)) { - sprintf(fc->sbuf, "Invalid input name syntax '%s'", token); - fc_error(fc); - } - if (map_contains(vars, token)) { - sprintf(fc->sbuf, "Duplicate input name '%s'", token); - fc_error(fc); - } - - MacroVar *mv = al(alc, sizeof(MacroVar)); - mv->name = token; - mv->replaces = array_make(alc, 4); - mv->repeat = false; - - map_set(vars, mv->name, mv); - array_push(mvg->vars, mv); - - while (true) { - token = tok(fc, false, true); - - if (strcmp(token, "@repeat") == 0) { - repeat = true; - mv->repeat = true; - continue; - } else if (strcmp(token, "@replace") == 0) { - tok_expect(fc, "(", true, false); - tok_expect(fc, "\"", true, true); - - Str *buf = read_string(fc); - char *find = str_to_chars(alc, buf); - tok_expect(fc, ",", true, true); - tok_expect(fc, "\"", true, true); - buf = read_string(fc); - char *with = str_to_chars(alc, buf); - - tok_expect(fc, ")", true, true); - - MacroReplace *rep = al(alc, sizeof(MacroReplace)); - rep->find = find; - rep->with = with; - - array_push(mv->replaces, rep); - continue; - } else { - break; - } - } - if (strcmp(token, ",") == 0) { - continue; - } else if (strcmp(token, ";") == 0) { - break; - } else { - sprintf(fc->sbuf, "Expected ',' or ';', found: '%s'", token); - fc_error(fc); - } - } - - mvg->repeat_last_input = repeat; - array_push(mac->groups, mvg); - - } else if (strcmp(token, "}") == 0) { - break; - } else { - sprintf(fc->sbuf, "Expected '\"' or '}', found: '%s'", token); - fc_error(fc); - } - } - if (mac->groups->length == 0) { - sprintf(fc->sbuf, "No inputs defined"); - fc_error(fc); - } - - tok_expect(fc, "output", false, true); - tok_expect(fc, "{", false, true); - - Chunk *chunk = fc->chunk; - int i = chunk->i; - int col = chunk->col; - int line = chunk->line; - char *content = chunk->content; - int len = chunk->length; - - Str *buf = fc->b->str_buf; - while (i < len) { - char ch = content[i]; - i++; - col++; - if (is_whitespace(ch)) - continue; - if (ch == '}') { - break; - } else if (ch == '"') { - bool loop = false; - Array *sub_parts = array_make(alc, 4); - str_clear(buf); - - // String - while (i < len) { - char ch = content[i]; - i++; - col++; - - if (ch == '\\') { - if (i == len) { - break; - } - char add = content[i]; - if (add == 'n') { - add = '\n'; - } else if (add == 'r') { - add = '\r'; - } else if (add == 't') { - add = '\t'; - } else if (add == 'f') { - add = '\f'; - } else if (add == 'b') { - add = '\b'; - } else if (add == 'v') { - add = '\v'; - } else if (add == 'f') { - add = '\f'; - } else if (add == 'a') { - add = '\a'; - } - i++; - col++; - - str_append_char(buf, add); - continue; - } - - if (ch == '"') { - array_push(sub_parts, str_to_chars(alc, buf)); - str_clear(buf); - break; - } - - if (ch == '%') { - array_push(sub_parts, str_to_chars(alc, buf)); - str_clear(buf); - ch = content[i]; - while (is_valid_varname_char(ch)) { - i++; - col++; - str_append_char(buf, ch); - ch = content[i]; - } - - char *var_name = str_to_chars(alc, buf); - str_clear(buf); - MacroVar *mv = map_get(mac->vars, var_name); - if (!mv) { - sprintf(fc->sbuf, "Unknown macro input: '%s'", var_name); - fc_error(fc); - } - if (repeat && mv->repeat) { - loop = true; - } - array_push(sub_parts, var_name); - continue; - } - - if (is_newline(ch)) { - line++; - col = 1; - } - str_append_char(buf, ch); - } - - MacroPart *part = al(alc, sizeof(MacroPart)); - part->loop = loop; - part->sub_parts = sub_parts; - - array_push(mac->parts, part); - - } else { - sprintf(fc->sbuf, "Expected '\"' or '}', found: '%c'", ch); - fc_error(fc); - } - } - - chunk->i = i; - chunk->col = col; - chunk->line = line; + /////////// - tok_expect(fc, "}", false, true); + // tok_expect(fc, "{", false, true); + // tok_expect(fc, "input", false, true); + // tok_expect(fc, "{", false, true); + + // Map *vars = mac->vars; + // bool repeat = false; + + // while (true) { + // // Read input groups + // token = tok(fc, false, true); + // if (strcmp(token, "\"") == 0) { + // // New group + // MacroVarGroup *mvg = al(alc, sizeof(MacroVarGroup)); + // mvg->vars = array_make(alc, 4); + + // Str *pat = read_string(fc); + // char *pattern = str_to_chars(alc, pat); + // if (strcmp(pattern, "[]") == 0) { + // } else if (strcmp(pattern, "{}") == 0) { + // } else if (strcmp(pattern, "()") == 0) { + // } else { + // sprintf(fc->sbuf, "Invalid macro pattern: '%s'", pattern); + // fc_error(fc); + // } + + // char sign[2]; + // sign[0] = pattern[0]; + // sign[1] = '\0'; + // mvg->start = dups(alc, sign); + // sign[0] = pattern[1]; + // mvg->end = dups(alc, sign); + + // while (true) { + + // if (repeat) { + // sprintf(fc->sbuf, "You cannot add more inputs after defining a repeating input '*'"); + // fc_error(fc); + // } + + // token = tok(fc, false, true); + // if (!is_valid_varname(token)) { + // sprintf(fc->sbuf, "Invalid input name syntax '%s'", token); + // fc_error(fc); + // } + // if (map_contains(vars, token)) { + // sprintf(fc->sbuf, "Duplicate input name '%s'", token); + // fc_error(fc); + // } + + // MacroVar *mv = al(alc, sizeof(MacroVar)); + // mv->name = token; + // mv->replaces = array_make(alc, 4); + // mv->repeat = false; + + // map_set(vars, mv->name, mv); + // array_push(mvg->vars, mv); + + // while (true) { + // token = tok(fc, false, true); + + // if (strcmp(token, "@repeat") == 0) { + // repeat = true; + // mv->repeat = true; + // continue; + // } else if (strcmp(token, "@replace") == 0) { + // tok_expect(fc, "(", true, false); + // tok_expect(fc, "\"", true, true); + + // Str *buf = read_string(fc); + // char *find = str_to_chars(alc, buf); + // tok_expect(fc, ",", true, true); + // tok_expect(fc, "\"", true, true); + // buf = read_string(fc); + // char *with = str_to_chars(alc, buf); + + // tok_expect(fc, ")", true, true); + + // MacroReplace *rep = al(alc, sizeof(MacroReplace)); + // rep->find = find; + // rep->with = with; + + // array_push(mv->replaces, rep); + // continue; + // } else { + // break; + // } + // } + // if (strcmp(token, ",") == 0) { + // continue; + // } else if (strcmp(token, ";") == 0) { + // break; + // } else { + // sprintf(fc->sbuf, "Expected ',' or ';', found: '%s'", token); + // fc_error(fc); + // } + // } + + // mvg->repeat_last_input = repeat; + // array_push(mac->groups, mvg); + + // } else if (strcmp(token, "}") == 0) { + // break; + // } else { + // sprintf(fc->sbuf, "Expected '\"' or '}', found: '%s'", token); + // fc_error(fc); + // } + // } + // if (mac->groups->length == 0) { + // sprintf(fc->sbuf, "No inputs defined"); + // fc_error(fc); + // } + + // tok_expect(fc, "output", false, true); + // tok_expect(fc, "{", false, true); + + // Chunk *chunk = fc->chunk; + // int i = chunk->i; + // int col = chunk->col; + // int line = chunk->line; + // char *content = chunk->content; + // int len = chunk->length; + + // Str *buf = fc->b->str_buf; + // while (i < len) { + // char ch = content[i]; + // i++; + // col++; + // if (is_whitespace(ch)) + // continue; + // if (ch == '}') { + // break; + // } else if (ch == '"') { + + // bool loop = false; + // Array *sub_parts = array_make(alc, 4); + // str_clear(buf); + + // // String + // while (i < len) { + // char ch = content[i]; + // i++; + // col++; + + // if (ch == '\\') { + // if (i == len) { + // break; + // } + // char add = content[i]; + // if (add == 'n') { + // add = '\n'; + // } else if (add == 'r') { + // add = '\r'; + // } else if (add == 't') { + // add = '\t'; + // } else if (add == 'f') { + // add = '\f'; + // } else if (add == 'b') { + // add = '\b'; + // } else if (add == 'v') { + // add = '\v'; + // } else if (add == 'f') { + // add = '\f'; + // } else if (add == 'a') { + // add = '\a'; + // } + // i++; + // col++; + + // str_append_char(buf, add); + // continue; + // } + + // if (ch == '"') { + // array_push(sub_parts, str_to_chars(alc, buf)); + // str_clear(buf); + // break; + // } + + // if (ch == '%') { + // array_push(sub_parts, str_to_chars(alc, buf)); + // str_clear(buf); + // ch = content[i]; + // while (is_valid_varname_char(ch)) { + // i++; + // col++; + // str_append_char(buf, ch); + // ch = content[i]; + // } + + // char *var_name = str_to_chars(alc, buf); + // str_clear(buf); + // MacroVar *mv = map_get(mac->vars, var_name); + // if (!mv) { + // sprintf(fc->sbuf, "Unknown macro input: '%s'", var_name); + // fc_error(fc); + // } + // if (repeat && mv->repeat) { + // loop = true; + // } + // array_push(sub_parts, var_name); + // continue; + // } + + // if (is_newline(ch)) { + // line++; + // col = 1; + // } + // str_append_char(buf, ch); + // } + + // MacroPart *part = al(alc, sizeof(MacroPart)); + // part->loop = loop; + // part->sub_parts = sub_parts; + + // array_push(mac->parts, part); + + // } else { + // sprintf(fc->sbuf, "Expected '\"' or '}', found: '%c'", ch); + // fc_error(fc); + // } + // } + + // chunk->i = i; + // chunk->col = col; + // chunk->line = line; + + // tok_expect(fc, "}", false, true); } diff --git a/src/headers/structs.h b/src/headers/structs.h index 79d34734..7098b8ec 100644 --- a/src/headers/structs.h +++ b/src/headers/structs.h @@ -471,30 +471,26 @@ struct Test { struct Macro { char *name; char *dname; - Map *vars; - Array *groups; - Array *parts; + Array *input; + Array *output; + Array *valid_var_names; }; -struct MacroVarGroup { - char *start; - char *end; - Array *vars; - bool repeat_last_input; +struct MacroInput { + int type; // mi_str, mi_value, mi_type, mi_repeat + void* item; + char* save_as; }; -struct MacroVar { - char *name; - Array *replaces; - bool repeat; -}; -struct MacroReplace { - char *find; - char *with; +struct MacroRepeat { + Array *input; + char* token_repeat; + char* token_end; }; -struct MacroPart { - Array *sub_parts; - bool loop; +struct MacroOutput { + char* text; + char* repeat_var; }; + // Pkg struct PkgCmd { Allocator *alc; From 036a745698cd58b95866984b42fd593e438a54a4 Mon Sep 17 00:00:00 2001 From: ctxcode Date: Sat, 10 Feb 2024 20:37:29 +0100 Subject: [PATCH 16/16] remove macro code --- src/build/value.c | 248 +++++++++++++++++++++++----------------------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/src/build/value.c b/src/build/value.c index e6e5eda9..006d0031 100644 --- a/src/build/value.c +++ b/src/build/value.c @@ -1053,138 +1053,138 @@ Value *value_handle_idf(Fc *fc, Allocator *alc, Scope *scope, Idf *idf) { return value_init(alc, v_decl, decl, decl->type); } - if (idf->type == idf_macro) { - Macro *mac = idf->item; - - Map *values = map_make(alc); - Array *repeat_values = array_make(alc, 8); - Array *repeat_values_empty = array_make(alc, 8); - array_push(repeat_values_empty, NULL); - - Array *groups = mac->groups; - Array *parts = mac->parts; - Map *vars = mac->vars; - Array *input_names = vars->keys; - char *repeat_name = NULL; - - for (int i = 0; i < groups->length; i++) { - MacroVarGroup *mvg = array_get_index(groups, i); - tok_expect(fc, mvg->start, true, false); - - bool does_repeat = mvg->repeat_last_input; - int input_count = mvg->vars->length - (does_repeat ? 1 : 0); - if (does_repeat) { - repeat_name = array_get_index(input_names, input_names->length - 1); - } - - int count = 0; - token = tok(fc, false, true); - if (strcmp(token, mvg->end) == 0) { - } else { - rtok(fc); - while (true) { - skip_whitespace(fc); - - int v_start = fc->chunk->i; - Chunk *v_startc = fc->chunk; - skip_macro_input(fc, mvg->end); - int v_end = fc->chunk->i; - Chunk *v_endc = fc->chunk; - - if (v_startc != v_endc) { - sprintf(fc->sbuf, "Invalid macro input (mixed macros)"); - fc_error(fc); - } - - char *value = read_part(alc, fc, v_start, v_end - v_start); - if (count >= input_count) { - array_push(repeat_values, value); - } else { - char *name = array_get_index(input_names, count); - map_set(values, name, value); - } - count++; + // if (idf->type == idf_macro) { + // Macro *mac = idf->item; + + // Map *values = map_make(alc); + // Array *repeat_values = array_make(alc, 8); + // Array *repeat_values_empty = array_make(alc, 8); + // array_push(repeat_values_empty, NULL); + + // Array *groups = mac->groups; + // Array *parts = mac->parts; + // Map *vars = mac->vars; + // Array *input_names = vars->keys; + // char *repeat_name = NULL; + + // for (int i = 0; i < groups->length; i++) { + // MacroVarGroup *mvg = array_get_index(groups, i); + // tok_expect(fc, mvg->start, true, false); + + // bool does_repeat = mvg->repeat_last_input; + // int input_count = mvg->vars->length - (does_repeat ? 1 : 0); + // if (does_repeat) { + // repeat_name = array_get_index(input_names, input_names->length - 1); + // } - token = tok(fc, false, true); - if (strcmp(token, mvg->end) == 0) { - break; - } else if (strcmp(token, ",") == 0) { - continue; - } else { - sprintf(fc->sbuf, "Expected ',' or '%s', found: '%s'", mvg->end, token); - fc_error(fc); - } - } - } + // int count = 0; + // token = tok(fc, false, true); + // if (strcmp(token, mvg->end) == 0) { + // } else { + // rtok(fc); + // while (true) { + // skip_whitespace(fc); + + // int v_start = fc->chunk->i; + // Chunk *v_startc = fc->chunk; + // skip_macro_input(fc, mvg->end); + // int v_end = fc->chunk->i; + // Chunk *v_endc = fc->chunk; + + // if (v_startc != v_endc) { + // sprintf(fc->sbuf, "Invalid macro input (mixed macros)"); + // fc_error(fc); + // } - if (count < input_count) { - sprintf(fc->sbuf, "Missing macro inputs. Expected%s '%d', found '%d'", does_repeat ? " a minimum of" : "", input_count, count); - fc_error(fc); - } - if (!does_repeat && count > input_count) { - sprintf(fc->sbuf, "Too many macro inputs. Expected '%d', found '%d'", input_count, count); - fc_error(fc); - } - } + // char *value = read_part(alc, fc, v_start, v_end - v_start); + // if (count >= input_count) { + // array_push(repeat_values, value); + // } else { + // char *name = array_get_index(input_names, count); + // map_set(values, name, value); + // } + // count++; + + // token = tok(fc, false, true); + // if (strcmp(token, mvg->end) == 0) { + // break; + // } else if (strcmp(token, ",") == 0) { + // continue; + // } else { + // sprintf(fc->sbuf, "Expected ',' or '%s', found: '%s'", mvg->end, token); + // fc_error(fc); + // } + // } + // } - Str *buf = fc->str_buf; - str_clear(buf); - for (int i = 0; i < parts->length; i++) { - // - MacroPart *part = array_get_index(parts, i); - Array *sub_parts = part->sub_parts; + // if (count < input_count) { + // sprintf(fc->sbuf, "Missing macro inputs. Expected%s '%d', found '%d'", does_repeat ? " a minimum of" : "", input_count, count); + // fc_error(fc); + // } + // if (!does_repeat && count > input_count) { + // sprintf(fc->sbuf, "Too many macro inputs. Expected '%d', found '%d'", input_count, count); + // fc_error(fc); + // } + // } - Array *loop_values = part->loop ? repeat_values : repeat_values_empty; - // - for (int o = 0; o < loop_values->length; o++) { - char *repeat_value = array_get_index(loop_values, o); - for (int u = 0; u < sub_parts->length; u++) { - char *spart = array_get_index(sub_parts, u); - if (u % 2 == 0) { - // String part - str_append_chars(buf, spart); - } else { - // Input - if (repeat_value && strcmp(spart, repeat_name) == 0) { - char *input = repeat_value; - MacroVar *mv = map_get(vars, repeat_name); - for (int x = 0; x < mv->replaces->length; x++) { - MacroReplace *rep = array_get_index(mv->replaces, x); - input = str_replace(alc, input, rep->find, rep->with); - } - str_append_chars(buf, input); - continue; - } - MacroVar *mv = map_get(vars, spart); - char *input = map_get(values, spart); - if (!input || !mv) { - sprintf(fc->sbuf, "Cannot find macro input by name: '%s' (compiler bug)", spart); - fc_error(fc); - } - for (int x = 0; x < mv->replaces->length; x++) { - MacroReplace *rep = array_get_index(mv->replaces, x); - input = str_replace(alc, input, rep->find, rep->with); - } - str_append_chars(buf, input); - } - } - } - } + // Str *buf = fc->str_buf; + // str_clear(buf); + // for (int i = 0; i < parts->length; i++) { + // // + // MacroPart *part = array_get_index(parts, i); + // Array *sub_parts = part->sub_parts; + + // Array *loop_values = part->loop ? repeat_values : repeat_values_empty; + // // + // for (int o = 0; o < loop_values->length; o++) { + // char *repeat_value = array_get_index(loop_values, o); + // for (int u = 0; u < sub_parts->length; u++) { + // char *spart = array_get_index(sub_parts, u); + // if (u % 2 == 0) { + // // String part + // str_append_chars(buf, spart); + // } else { + // // Input + // if (repeat_value && strcmp(spart, repeat_name) == 0) { + // char *input = repeat_value; + // MacroVar *mv = map_get(vars, repeat_name); + // for (int x = 0; x < mv->replaces->length; x++) { + // MacroReplace *rep = array_get_index(mv->replaces, x); + // input = str_replace(alc, input, rep->find, rep->with); + // } + // str_append_chars(buf, input); + // continue; + // } + // MacroVar *mv = map_get(vars, spart); + // char *input = map_get(values, spart); + // if (!input || !mv) { + // sprintf(fc->sbuf, "Cannot find macro input by name: '%s' (compiler bug)", spart); + // fc_error(fc); + // } + // for (int x = 0; x < mv->replaces->length; x++) { + // MacroReplace *rep = array_get_index(mv->replaces, x); + // input = str_replace(alc, input, rep->find, rep->with); + // } + // str_append_chars(buf, input); + // } + // } + // } + // } - str_append_char(buf, ' '); - char *content = str_to_chars(alc, buf); + // str_append_char(buf, ' '); + // char *content = str_to_chars(alc, buf); - Chunk *chunk = chunk_init(alc, fc->b, NULL); - chunk->parent = fc->chunk; - chunk->content = content; - chunk->length = buf->length; - chunk_lex_start(chunk); + // Chunk *chunk = chunk_init(alc, fc->b, NULL); + // chunk->parent = fc->chunk; + // chunk->content = content; + // chunk->length = buf->length; + // chunk_lex_start(chunk); - // printf(">>>%s<<<", content); - fc->chunk = chunk; + // // printf(">>>%s<<<", content); + // fc->chunk = chunk; - return read_value(fc, alc, scope, false, 0, false); - } + // return read_value(fc, alc, scope, false, 0, false); + // } sprintf(fc->sbuf, "Cannot convert identifier to a value"); fc_error(fc);