ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

commit ed4e141792e06b134394727a0c8f8e107c8d0afd
parent 2f61c2b50ac165f96b91096ab1471b6c46effe43
Author: citbl <citbl@citbl.org>
Date:   Mon, 13 Oct 2025 21:10:45 +1000

re-reformat

Diffstat:
M.clang-format | 2+-
Mex9.ox | 2+-
Msrc/file.h | 8++++----
Msrc/gen.h | 22+++++++++++-----------
Msrc/hmap.c | 40++++++++++++++++++++--------------------
Msrc/hmap.h | 18+++++++++---------
Msrc/hmap_test.c | 12++++++------
Msrc/lexer.c | 34+++++++++++++++++-----------------
Msrc/lexer.h | 4++--
Msrc/main.c | 10+++++-----
Msrc/parser.h | 108++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/parser/ast.c | 46+++++++++++++++++++++++-----------------------
Msrc/parser/decl.c | 22+++++++++++-----------
Msrc/parser/expr.c | 74+++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/parser/parser.c | 120++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/parser/parser_utils.c | 24++++++++++++------------
Msrc/parser/stmt.c | 64++++++++++++++++++++++++++++++++--------------------------------
Msrc/sem.c | 54+++++++++++++++++++++++++++---------------------------
Msrc/sem.h | 18+++++++++---------
Msrc/types.h | 64++++++++++++++++++++++++++++++++--------------------------------
Msrc/utils.c | 4++--
Msrc/utils.h | 4++--
22 files changed, 377 insertions(+), 377 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -3,7 +3,7 @@ IndentWidth: 8 ContinuationIndentWidth: 8 UseTab: AlignWithSpaces -PointerAlignment: Right +# PointerAlignment: Right AlignTrailingComments: true SpacesBeforeTrailingComments: 1 diff --git a/ex9.ox b/ex9.ox @@ -1,7 +1,7 @@ int jack = 111; void test() { - float alice = 222; + float alice = 222.0; } void main() { diff --git a/src/file.h b/src/file.h @@ -5,10 +5,10 @@ #include "utils.h" -char * -readfile(const char *file_path) +char* +readfile(const char* file_path) { - FILE *fp = fopen(file_path, "rb"); + FILE* fp = fopen(file_path, "rb"); if (fp == NULL) { perror("Failed to read file"); return NULL; @@ -37,7 +37,7 @@ readfile(const char *file_path) return NULL; } - char *contents = (char *)calloc(1, (size_t)file_size + 1); + char* contents = (char*)calloc(1, (size_t)file_size + 1); if (contents == NULL) { panic("Failed to allocate memory to read file"); fclose(fp); diff --git a/src/gen.h b/src/gen.h @@ -6,17 +6,17 @@ #include "types.h" typedef struct { - gcc_jit_context *ctx; - gcc_jit_function *prev_func; - gcc_jit_function *curr_func; - gcc_jit_function *printf_fn; - gcc_jit_function *puts_fn; - gcc_jit_block *prev_block; - gcc_jit_block *curr_block; + gcc_jit_context* ctx; + gcc_jit_function* prev_func; + gcc_jit_function* curr_func; + gcc_jit_function* printf_fn; + gcc_jit_function* puts_fn; + gcc_jit_block* prev_block; + gcc_jit_block* curr_block; // gcc_jit_type *type_kind; need type too? - Scope *scope; - const char *src; + Scope* scope; + const char* src; } Gen; -Gen gen_init(Scope *, const char *, Node *, bool); -void gen_next(Gen *, Node *); +Gen gen_init(Scope*, const char*, Node*, bool); +void gen_next(Gen*, Node*); diff --git a/src/hmap.c b/src/hmap.c @@ -6,11 +6,11 @@ #define INITIAL_BUCKETS 8 #define LOAD_FACTOR 0.75 -static void hmap_grow(HashMap *map); +static void hmap_grow(HashMap* map); // Simple string hash function (djb2) static unsigned long -hash(const char *str) +hash(const char* str) { unsigned long h = 5381; unsigned char c; @@ -19,15 +19,15 @@ hash(const char *str) return h; } -HashMap * +HashMap* hmap_create(size_t value_size) { - HashMap *map = calloc(1, sizeof(HashMap)); + HashMap* map = calloc(1, sizeof(HashMap)); if (map == NULL) { fprintf(stderr, "hmap_create: map: could not alloc\n"); } map->bucket_count = INITIAL_BUCKETS; map->size = 0; map->value_size = value_size; - map->buckets = calloc(map->bucket_count, sizeof(HashNode *)); + map->buckets = calloc(map->bucket_count, sizeof(HashNode*)); if (map->buckets == NULL) { fprintf(stderr, "hmap_create: bucket: could not alloc\n"); exit(1); @@ -36,11 +36,11 @@ hmap_create(size_t value_size) } void -hmap_put(HashMap *map, const char *key, const void *value) +hmap_put(HashMap* map, const char* key, const void* value) { if ((float)(map->size + 1) / map->bucket_count > LOAD_FACTOR) { hmap_grow(map); } unsigned long h = hash(key) % map->bucket_count; - HashNode *node = map->buckets[h]; + HashNode* node = map->buckets[h]; while (node) { if (strcmp(node->key, key) == 0) { memcpy(node->value, value, map->value_size); @@ -48,7 +48,7 @@ hmap_put(HashMap *map, const char *key, const void *value) } node = node->next; } - HashNode *new_node = calloc(1, sizeof(HashNode)); + HashNode* new_node = calloc(1, sizeof(HashNode)); if (new_node == NULL) { fprintf(stderr, "hmap_put: new_node: could not alloc\n"); exit(1); @@ -66,10 +66,10 @@ hmap_put(HashMap *map, const char *key, const void *value) } bool -hmap_get(HashMap *map, const char *key, void *out) +hmap_get(HashMap* map, const char* key, void* out) { unsigned long h = hash(key) % map->bucket_count; - HashNode *node = map->buckets[h]; + HashNode* node = map->buckets[h]; while (node) { if (strcmp(node->key, key) == 0) { memcpy(out, node->value, map->value_size); @@ -81,11 +81,11 @@ hmap_get(HashMap *map, const char *key, void *out) } bool -hmap_remove(HashMap *map, const char *key) +hmap_remove(HashMap* map, const char* key) { unsigned long h = hash(key) % map->bucket_count; - HashNode *node = map->buckets[h]; - HashNode *prev = NULL; + HashNode* node = map->buckets[h]; + HashNode* prev = NULL; while (node) { if (strcmp(node->key, key) == 0) { if (prev) { @@ -106,18 +106,18 @@ hmap_remove(HashMap *map, const char *key) } static void -hmap_grow(HashMap *map) +hmap_grow(HashMap* map) { size_t new_bucket_count = map->bucket_count * 2; - HashNode **new_buckets = calloc(new_bucket_count, sizeof(HashNode *)); + HashNode** new_buckets = calloc(new_bucket_count, sizeof(HashNode*)); if (new_buckets == NULL) { fprintf(stderr, "hmap_grow: could not alloc\n"); exit(1); } for (size_t i = 0; i < map->bucket_count; i++) { - HashNode *node = map->buckets[i]; + HashNode* node = map->buckets[i]; while (node) { - HashNode *next = node->next; + HashNode* next = node->next; unsigned long h = hash(node->key) % new_bucket_count; node->next = new_buckets[h]; new_buckets[h] = node; @@ -130,12 +130,12 @@ hmap_grow(HashMap *map) } void -hmap_free(HashMap *map) +hmap_free(HashMap* map) { for (size_t i = 0; i < map->bucket_count; i++) { - HashNode *node = map->buckets[i]; + HashNode* node = map->buckets[i]; while (node) { - HashNode *next = node->next; + HashNode* next = node->next; free(node->key); free(node->value); free(node); diff --git a/src/hmap.h b/src/hmap.h @@ -4,20 +4,20 @@ #include <stdlib.h> typedef struct HashNode { - char *key; - void *value; - struct HashNode *next; + char* key; + void* value; + struct HashNode* next; } HashNode; typedef struct HashMap { size_t bucket_count; size_t size; size_t value_size; - struct HashNode **buckets; + struct HashNode** buckets; } HashMap; -HashMap *hmap_create(size_t); -void hmap_put(HashMap *map, const char *key, const void *value); -bool hmap_get(HashMap *map, const char *key, void *out); -bool hmap_remove(HashMap *map, const char *key); -void hmap_free(HashMap *map); +HashMap* hmap_create(size_t); +void hmap_put(HashMap* map, const char* key, const void* value); +bool hmap_get(HashMap* map, const char* key, void* out); +bool hmap_remove(HashMap* map, const char* key); +void hmap_free(HashMap* map); diff --git a/src/hmap_test.c b/src/hmap_test.c @@ -10,10 +10,10 @@ hmap_test_basic(void) int i; printf("Testing dict of integers...\n"); - HashMap *map = hmap_create(sizeof(int)); + HashMap* map = hmap_create(sizeof(int)); struct { - const char *key; + const char* key; int value; } items[] = { { "one", 1 }, @@ -54,7 +54,7 @@ hmap_test_T(void) float score; }; - HashMap *map = hmap_create(sizeof(struct T)); + HashMap* map = hmap_create(sizeof(struct T)); struct T items[] = { { 1, "alice", 30, "alice@example.com", 95.5f }, @@ -93,7 +93,7 @@ hmap_test_memory_bumping(void) int i; printf("Testing memory bumping...\n"); - HashMap *map = hmap_create(sizeof(int)); + HashMap* map = hmap_create(sizeof(int)); const int N = 1000; // Large enough to trigger resizing char key[32]; @@ -126,10 +126,10 @@ hmap_test_removal(void) int i; printf("Testing removal...\n"); - HashMap *map = hmap_create(sizeof(int)); + HashMap* map = hmap_create(sizeof(int)); struct { - const char *key; + const char* key; int value; } items[] = { { "alpha", 10 }, diff --git a/src/lexer.c b/src/lexer.c @@ -9,14 +9,14 @@ #include <assert.h> static char -peek(Lexer *lex) +peek(Lexer* lex) { char c = lex->src[lex->pos]; return c ? c : 0; } static char -peek2(Lexer *lex) +peek2(Lexer* lex) { char c = lex->src[lex->pos]; if (!c) return 0; @@ -25,7 +25,7 @@ peek2(Lexer *lex) } static char -nudge(Lexer *lex) +nudge(Lexer* lex) { const char c = peek(lex); if (!c) return 0; @@ -40,7 +40,7 @@ nudge(Lexer *lex) } static void -skip_space_and_comments(Lexer *lex) +skip_space_and_comments(Lexer* lex) { for (;;) { for (;;) { @@ -62,7 +62,7 @@ skip_space_and_comments(Lexer *lex) } static Token -make_ident(Lexer *lex, size_t pos, size_t line, size_t col) +make_ident(Lexer* lex, size_t pos, size_t line, size_t col) { for (;;) { char c = peek(lex); @@ -110,7 +110,7 @@ make_ident(Lexer *lex, size_t pos, size_t line, size_t col) return (Token) { .type = type, .start = pos, .line = line, .col = col, .end = lex->pos }; } static Token -make_number(Lexer *lex, size_t pos, size_t line, size_t col) +make_number(Lexer* lex, size_t pos, size_t line, size_t col) { bool is_float = false; while (isdigit(peek(lex))) @@ -125,7 +125,7 @@ make_number(Lexer *lex, size_t pos, size_t line, size_t col) } static Token -make_string(Lexer *lex, size_t pos, size_t line, size_t col) +make_string(Lexer* lex, size_t pos, size_t line, size_t col) { nudge(lex); // " start while (peek(lex) != '"' && peek(lex) != 0) @@ -135,7 +135,7 @@ make_string(Lexer *lex, size_t pos, size_t line, size_t col) } static Token -next_token(Lexer *lex) +next_token(Lexer* lex) { skip_space_and_comments(lex); size_t start = lex->pos; @@ -260,9 +260,9 @@ next_token(Lexer *lex) } static void -print_token(const Token *t, const char *contents) +print_token(const Token* t, const char* contents) { - static const char *TYPES[] = { [TOKEN_IDENT] = "ident/type", + static const char* TYPES[] = { [TOKEN_IDENT] = "ident/type", [TOKEN_LPAREN] = "open paren", [TOKEN_RPAREN] = "close paren", [TOKEN_LBRACE] = "open brace", @@ -302,17 +302,17 @@ print_token(const Token *t, const char *contents) } static void -add_token(Lexer *lex, Token tok) +add_token(Lexer* lex, Token tok) { if (lex->token_count >= lex->token_cap) { lex->token_cap *= 2; - lex->tokens = (Token *)realloc(lex->tokens, sizeof(Token) * lex->token_cap); + lex->tokens = (Token*)realloc(lex->tokens, sizeof(Token) * lex->token_cap); } lex->tokens[lex->token_count++] = tok; } void -lexer_print(Lexer *lex) +lexer_print(Lexer* lex) { for (size_t i = 0; i < lex->token_count; i++) { print_token(&lex->tokens[i], lex->src); @@ -320,14 +320,14 @@ lexer_print(Lexer *lex) } void -lexer_lex(Lexer *lex, const char *filename, const char *contents) +lexer_lex(Lexer* lex, const char* filename, const char* contents) { lex->line = 1; lex->col = 1; lex->pos = 0; lex->token_cap = 128; lex->token_count = 0; - lex->tokens = (Token *)calloc(lex->token_cap, sizeof(Token)); + lex->tokens = (Token*)calloc(lex->token_cap, sizeof(Token)); if (lex->tokens == NULL) panic("lexer_lex: could not alloc"); lex->filename = filename; lex->src = contents; @@ -339,10 +339,10 @@ lexer_lex(Lexer *lex, const char *filename, const char *contents) } } -const char * +const char* token_type_str(TokenType t) { - static const char *type_strings[] = { [TOKEN_IDENT] = "TOKEN_IDENT", + static const char* type_strings[] = { [TOKEN_IDENT] = "TOKEN_IDENT", [TOKEN_LPAREN] = "TOKEN_LPAREN", [TOKEN_RPAREN] = "TOKEN_RPAREN", [TOKEN_LBRACE] = "TOKEN_LBRACE", diff --git a/src/lexer.h b/src/lexer.h @@ -3,5 +3,5 @@ #include <stdlib.h> #include "types.h" -void lexer_lex(Lexer *, const char *filename, const char *contents); -void lexer_print(Lexer *); +void lexer_lex(Lexer*, const char* filename, const char* contents); +void lexer_print(Lexer*); diff --git a/src/main.c b/src/main.c @@ -11,16 +11,16 @@ #include "gen.h" int -main(int argc, char *argv[]) +main(int argc, char* argv[]) { if (argc < 2) { printf("Usage: %s <file>\n", argv[0]); return 1; } - const char *contents = NULL; - const char *filename = NULL; - const char *flag = NULL; + const char* contents = NULL; + const char* filename = NULL; + const char* flag = NULL; filename = argv[1]; switch (argc) { @@ -67,7 +67,7 @@ main(int argc, char *argv[]) Gen gen = gen_init(&program_scope, contents, ast.node, quiet_flag); gen_next(&gen, ast.node); - gcc_jit_result *result; + gcc_jit_result* result; /* Compile the code. */ result = gcc_jit_context_compile(gen.ctx); diff --git a/src/parser.h b/src/parser.h @@ -7,57 +7,57 @@ #define IDENTSZ 256 -Parser parser_init(Lexer *); -void parser_parse(Ast *, Parser *); -void ast_print(Ast *); - -Token peek(Parser *); -Token peek2(Parser *); -Token consume(Parser *); -Token expect(Parser *, TokenType); -bool match(Parser *, TokenType); -bool check(Parser *, TokenType); - -Node *parse_declarations(Parser *); - -Node *parse_number(Parser *); -Node *parse_ident(Parser *); -Node *parse_primary(Parser *); -Node *parse_postfix(Parser *); -Node *parse_primary(Parser *); -Node *parse_unary(Parser *); -Node *parse_term(Parser *); -Node *parse_expression(Parser *); -Node *parse_expression_statement(Parser *); -Node *parse_statement(Parser *); -Node *parse_block(Parser *); -Node *parse_declaration_statement(Parser *); -Node *parse_decl_or_func_decl(Parser *); -NodeVec parse_param_list(Parser *); -Node *parse_type(Parser *); -Node *parse_func_call(Parser *); -NodeVec parse_func_arguments(Parser *); -Node *parse_if(Parser *); -Node *parse_while(Parser *); -Node *parse_for(Parser *); -Node *parse_assignment(Parser *); -Node *parse_break(Parser *); -Node *parse_continue_statement(Parser *); -Node *parse_expression(Parser *); -Node *make_program_node(Parser *); -Node *make_ident_node(Span name); -Node *make_param_decl(Parser *); -Node *make_postfix_node(UnaryOp, Node *); -Node *make_subscript_node(Node *, Node *); -Node *make_ident_node(Span); -Node *make_postfix_node(UnaryOp, Node *); -Node *make_number_node(Parser *); -Node *make_unary_node(UnaryOp, Node *); -Node *make_string_node(Parser *); -Node *make_binary_node(OpType, Node *, Node *); -Node *parse_return_statement(Parser *); -Node *make_empty_statement(void); -Node *make_call_node(Node *, NodeVec); - -const char *span_str(const char *src, Span s, char *stack_alloc_chptr); -const char *range_str(const char *src, size_t start, size_t end, char *stack_alloc_chptr); +Parser parser_init(Lexer*); +void parser_parse(Ast*, Parser*); +void ast_print(Ast*); + +Token peek(Parser*); +Token peek2(Parser*); +Token consume(Parser*); +Token expect(Parser*, TokenType); +bool match(Parser*, TokenType); +bool check(Parser*, TokenType); + +Node* parse_declarations(Parser*); + +Node* parse_number(Parser*); +Node* parse_ident(Parser*); +Node* parse_primary(Parser*); +Node* parse_postfix(Parser*); +Node* parse_primary(Parser*); +Node* parse_unary(Parser*); +Node* parse_term(Parser*); +Node* parse_expression(Parser*); +Node* parse_expression_statement(Parser*); +Node* parse_statement(Parser*); +Node* parse_block(Parser*); +Node* parse_declaration_statement(Parser*); +Node* parse_decl_or_func_decl(Parser*); +NodeVec parse_param_list(Parser*); +Node* parse_type(Parser*); +Node* parse_func_call(Parser*); +NodeVec parse_func_arguments(Parser*); +Node* parse_if(Parser*); +Node* parse_while(Parser*); +Node* parse_for(Parser*); +Node* parse_assignment(Parser*); +Node* parse_break(Parser*); +Node* parse_continue_statement(Parser*); +Node* parse_expression(Parser*); +Node* make_program_node(Parser*); +Node* make_ident_node(Span name); +Node* make_param_decl(Parser*); +Node* make_postfix_node(UnaryOp, Node*); +Node* make_subscript_node(Node*, Node*); +Node* make_ident_node(Span); +Node* make_postfix_node(UnaryOp, Node*); +Node* make_number_node(Parser*); +Node* make_unary_node(UnaryOp, Node*); +Node* make_string_node(Parser*); +Node* make_binary_node(OpType, Node*, Node*); +Node* parse_return_statement(Parser*); +Node* make_empty_statement(void); +Node* make_call_node(Node*, NodeVec); + +const char* span_str(const char* src, Span s, char* stack_alloc_chptr); +const char* range_str(const char* src, size_t start, size_t end, char* stack_alloc_chptr); diff --git a/src/parser/ast.c b/src/parser/ast.c @@ -6,10 +6,10 @@ #include <stdbool.h> #include <assert.h> -Node * -make_postfix_node(UnaryOp op, Node *operand) +Node* +make_postfix_node(UnaryOp op, Node* operand) { - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_postfix_node: could not alloc"); node->type = NODE_UNARY_EXPR; node->scope = NULL; @@ -20,10 +20,10 @@ make_postfix_node(UnaryOp op, Node *operand) return node; } -Node * -make_subscript_node(Node *array, Node *index) +Node* +make_subscript_node(Node* array, Node* index) { - Node *node = calloc(1, sizeof(Node)); + Node* node = calloc(1, sizeof(Node)); if (node == NULL) panic("make_subscript_node: could not alloc"); node->type = NODE_SUBSCRIPT_EXPR; node->scope = NULL; @@ -33,8 +33,8 @@ make_subscript_node(Node *array, Node *index) return node; } -Node * -make_number_node(Parser *par) +Node* +make_number_node(Parser* par) { Token tok = consume(par); assert(tok.type == TOKEN_INT_LITERAL || tok.type == TOKEN_FLOAT_LITERAL); @@ -44,7 +44,7 @@ make_number_node(Parser *par) buf[i] = par->src[tok.start + i]; buf[len] = '\0'; double value = strtod(buf, NULL); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_number_node: could not alloc"); node->type = tok.type == TOKEN_INT_LITERAL ? NODE_INT_LITERAL : NODE_FLOAT_LITERAL; node->scope = NULL; @@ -52,10 +52,10 @@ make_number_node(Parser *par) return node; } -Node * -make_unary_node(UnaryOp op, Node *operand) +Node* +make_unary_node(UnaryOp op, Node* operand) { - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_unary_node: could not alloc"); node->type = NODE_UNARY_EXPR; node->scope = NULL; @@ -66,11 +66,11 @@ make_unary_node(UnaryOp op, Node *operand) return node; } -Node * -make_string_node(Parser *par) +Node* +make_string_node(Parser* par) { Token tok = consume(par); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_string_node: could not alloc"); node->type = NODE_STRING_LITERAL; node->scope = NULL; @@ -79,10 +79,10 @@ make_string_node(Parser *par) return node; } -Node * -make_binary_node(OpType op, Node *lhs, Node *rhs) +Node* +make_binary_node(OpType op, Node* lhs, Node* rhs) { - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_binary_node: could not alloc"); node->type = NODE_BINARY_EXPR; node->scope = NULL; @@ -93,10 +93,10 @@ make_binary_node(OpType op, Node *lhs, Node *rhs) return node; } -Node * +Node* make_empty_statement(void) { - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_empty_statement: could not alloc"); assert(node != NULL); node->type = NODE_EMPTY_STATEMENT; @@ -104,10 +104,10 @@ make_empty_statement(void) return node; } -Node * -make_call_node(Node *callee, NodeVec args) +Node* +make_call_node(Node* callee, NodeVec args) { - Node *call = (Node *)calloc(1, sizeof(Node)); + Node* call = (Node*)calloc(1, sizeof(Node)); if (call == NULL) panic("make_call_node: could not alloc"); assert(call != NULL); call->type = NODE_CALL_EXPR; diff --git a/src/parser/decl.c b/src/parser/decl.c @@ -6,8 +6,8 @@ #include <stdbool.h> #include <assert.h> -Node * -parse_type(Parser *par) +Node* +parse_type(Parser* par) { Token tok = expect(par, TOKEN_IDENT); @@ -18,7 +18,7 @@ parse_type(Parser *par) // // assume this is a user type // } - Node *node = calloc(1, sizeof(Node)); + Node* node = calloc(1, sizeof(Node)); if (node == NULL) panic("parse_type: alloc failed"); node->type = NODE_TYPE; node->scope = NULL; @@ -27,13 +27,13 @@ parse_type(Parser *par) } // <TYPE> name:<IDENT> -Node * -make_param_decl(Parser *par) +Node* +make_param_decl(Parser* par) { - Node *type = parse_type(par); + Node* type = parse_type(par); Token param_name = expect(par, TOKEN_IDENT); Span ident_name = { .start = param_name.start, .end = param_name.end }; - Node *param = (Node *)calloc(1, sizeof(Node)); + Node* param = (Node*)calloc(1, sizeof(Node)); if (param == NULL) panic("make_param_decl alloc failed"); param->type = NODE_PARAM; param->scope = NULL; @@ -43,22 +43,22 @@ make_param_decl(Parser *par) } NodeVec -parse_param_list(Parser *par) +parse_param_list(Parser* par) { NodeVec v = { 0 }; if (peek(par).type == TOKEN_RPAREN) return v; // found `)` no parameters v.cap = 4; - v.items = (Node **)calloc(v.cap, sizeof(Node *)); + v.items = (Node**)calloc(v.cap, sizeof(Node*)); if (v.items == NULL) panic("parse_param_list: could not alloc"); for (;;) { - Node *param = make_param_decl(par); + Node* param = make_param_decl(par); if (v.len == v.cap) { v.cap *= 2; - v.items = (Node **)realloc(v.items, v.cap * sizeof(Node *)); + v.items = (Node**)realloc(v.items, v.cap * sizeof(Node*)); } v.items[v.len++] = param; diff --git a/src/parser/expr.c b/src/parser/expr.c @@ -6,34 +6,34 @@ #include <stdbool.h> #include <assert.h> -Node * -parse_func_call(Parser *par) +Node* +parse_func_call(Parser* par) { Token tok = expect(par, TOKEN_IDENT); Span callee = { .start = tok.start, .end = tok.end }; - Node *ident = make_ident_node(callee); + Node* ident = make_ident_node(callee); - const char *name = span_str(par->src, ident->data.ident.name, (char[IDENTSZ]) { 0 }); + const char* name = span_str(par->src, ident->data.ident.name, (char[IDENTSZ]) { 0 }); printf("parse_func_call: %s\n", name); expect(par, TOKEN_LPAREN); - Node *call = (Node *)calloc(1, sizeof(Node)); + Node* call = (Node*)calloc(1, sizeof(Node)); if (call == NULL) panic("parse_func_call: alloc failed"); // start parse arguments if (peek(par).type != TOKEN_RPAREN) { - call->data.call_expr.args = (Node **)calloc(4, sizeof(Node *)); + call->data.call_expr.args = (Node**)calloc(4, sizeof(Node*)); if (call->data.call_expr.args == NULL) panic("parse_func_call: args: could not alloc"); call->data.call_expr.cap = 4; call->data.call_expr.len = 0; for (;;) { - Node *arg = parse_expression(par); + Node* arg = parse_expression(par); if (call->data.call_expr.len == call->data.call_expr.cap) { call->data.call_expr.cap *= 2; - call->data.call_expr.args = (Node **)realloc(call->data.call_expr.args, call->data.call_expr.cap * sizeof(Node *)); + call->data.call_expr.args = (Node**)realloc(call->data.call_expr.args, call->data.call_expr.cap * sizeof(Node*)); } call->data.call_expr.args[call->data.call_expr.len++] = arg; @@ -50,8 +50,8 @@ parse_func_call(Parser *par) return call; } -Node * -parse_number(Parser *par) +Node* +parse_number(Parser* par) { Token tok = consume(par); assert(tok.type == TOKEN_INT_LITERAL || tok.type == TOKEN_FLOAT_LITERAL); @@ -63,7 +63,7 @@ parse_number(Parser *par) buf[len] = '\0'; double value = strtod(buf, NULL); - Node *num_node = (Node *)calloc(1, sizeof(Node)); + Node* num_node = (Node*)calloc(1, sizeof(Node)); if (num_node == NULL) panic("parse_number: alloc failed"); num_node->type = tok.type == TOKEN_INT_LITERAL ? NODE_INT_LITERAL : NODE_FLOAT_LITERAL; num_node->scope = NULL; @@ -71,12 +71,12 @@ parse_number(Parser *par) return num_node; } -Node * -parse_ident(Parser *par) +Node* +parse_ident(Parser* par) { Token tok = consume(par); assert(tok.type == TOKEN_IDENT); - Node *ident_node = (Node *)calloc(1, sizeof(Node)); + Node* ident_node = (Node*)calloc(1, sizeof(Node)); if (ident_node == NULL) panic("parse_ident: alloc failed"); ident_node->type = NODE_IDENT; ident_node->scope = NULL; @@ -90,21 +90,21 @@ parse_ident(Parser *par) } NodeVec -parse_func_arguments(Parser *par) +parse_func_arguments(Parser* par) { NodeVec v = { 0 }; if (peek(par).type == TOKEN_RPAREN) return v; // found `)` no arguments v.cap = 4; - v.items = (Node **)calloc(v.cap, sizeof(Node *)); + v.items = (Node**)calloc(v.cap, sizeof(Node*)); if (v.items == NULL) panic("parse_func_arguments: could not alloc"); for (;;) { - Node *arg = parse_expression(par); + Node* arg = parse_expression(par); if (v.len == v.cap) { v.cap *= 2; - v.items = (Node **)realloc(v.items, v.cap * sizeof(Node *)); + v.items = (Node**)realloc(v.items, v.cap * sizeof(Node*)); } v.items[v.len++] = arg; @@ -114,10 +114,10 @@ parse_func_arguments(Parser *par) return v; } -Node * -parse_postfix(Parser *par) +Node* +parse_postfix(Parser* par) { - Node *node = parse_primary(par); + Node* node = parse_primary(par); for (;;) { if (match(par, TOKEN_PLUSPLUS)) { @@ -125,7 +125,7 @@ parse_postfix(Parser *par) } else if (match(par, TOKEN_MINUSMINUS)) { node = make_postfix_node(OPER_POSTDEC, node); } else if (match(par, TOKEN_LBRACKET)) { - Node *index = parse_expression(par); // parse inside brackets + Node* index = parse_expression(par); // parse inside brackets expect(par, TOKEN_RBRACKET); node = make_subscript_node(node, index); // } else if (match(par, TOKEN_DOT)) { // TODO dot members and arrows @@ -147,10 +147,10 @@ parse_postfix(Parser *par) #define STARTING_ROOT_NODES 32 -Node * -make_program_node(Parser *par) +Node* +make_program_node(Parser* par) { - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_program_node: alloc failed"); node->type = NODE_PROGRAM; node->filename = par->filename; @@ -158,15 +158,15 @@ make_program_node(Parser *par) node->next = NULL; node->data.program.cap = STARTING_ROOT_NODES; node->data.program.len = 0; - node->data.program.decl = (Node **)calloc(STARTING_ROOT_NODES, sizeof(Node)); + node->data.program.decl = (Node**)calloc(STARTING_ROOT_NODES, sizeof(Node)); if (node->data.program.decl == NULL) panic("make_program_node: decls: alloc failed"); return node; } -Node * +Node* make_ident_node(Span name) { - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("make_ident_node: alloc failed"); node->type = NODE_IDENT; node->scope = NULL; @@ -175,8 +175,8 @@ make_ident_node(Span name) return node; } -Node * -parse_primary(Parser *par) +Node* +parse_primary(Parser* par) { Token tok = peek(par); if (tok.type == TOKEN_STRING_LITERAL) { return make_string_node(par); } @@ -184,21 +184,21 @@ parse_primary(Parser *par) if (tok.type == TOKEN_IDENT) { return parse_ident(par); } if (tok.type == TOKEN_LPAREN) { consume(par); // consume '(' - Node *node = parse_expression(par); + Node* node = parse_expression(par); expect(par, TOKEN_RPAREN); return node; } - const char *name = span_str(par->src, (Span) { .start = tok.start, .end = tok.end }, (char[IDENTSZ]) { 0 }); + const char* name = span_str(par->src, (Span) { .start = tok.start, .end = tok.end }, (char[IDENTSZ]) { 0 }); panic("Expected Primary Expr, but found '%s' (%s at %s:%zu:%zu", name, token_type_str(tok.type), par->filename, tok.line, tok.col); return NULL; } -Node * -parse_unary(Parser *par) +Node* +parse_unary(Parser* par) { - Node *inner = NULL; + Node* inner = NULL; switch (peek(par).type) { case TOKEN_MINUS: consume(par); @@ -226,8 +226,8 @@ parse_unary(Parser *par) } // called by parse_multiplicative -Node * -parse_term(Parser *par) +Node* +parse_term(Parser* par) { return parse_unary(par); } diff --git a/src/parser/parser.c b/src/parser/parser.c @@ -9,7 +9,7 @@ // TODO make sure ALL callocs have been successful Parser -parser_init(Lexer *lex) +parser_init(Lexer* lex) { return (Parser) { .pos = 0, .tokens = lex->tokens, .token_count = lex->token_count, .src = lex->src, .src_len = lex->src_len, .filename = lex->filename @@ -17,14 +17,14 @@ parser_init(Lexer *lex) } Token -peek(Parser *par) +peek(Parser* par) { Token t = par->tokens[par->pos]; return t.type ? t : (Token) { .type = TOKEN_EOF }; } Token -peek2(Parser *par) +peek2(Parser* par) { if (par->pos + 1 >= par->token_count) return (Token) { .type = TOKEN_EOF }; Token t = par->tokens[par->pos + 1]; @@ -32,7 +32,7 @@ peek2(Parser *par) } Token -consume(Parser *par) +consume(Parser* par) { Token t = par->tokens[par->pos]; if (!t.type) return (Token) { .type = TOKEN_EOF }; @@ -41,17 +41,17 @@ consume(Parser *par) } bool -check(Parser *p, TokenType type) +check(Parser* p, TokenType type) { return (peek(p).type == type); } Token -expect(Parser *par, TokenType type) +expect(Parser* par, TokenType type) { Token tok = peek(par); if (tok.type != type) { - const char *name = range_str(par->src, tok.start, tok.end, (char[IDENTSZ]) { 0 }); + const char* name = range_str(par->src, tok.start, tok.end, (char[IDENTSZ]) { 0 }); panic("Expected %d got '%s' (%d) at %s:%zu:%zu", token_type_str(type), name, tok.type, par->filename, tok.line, tok.col); assert(tok.type == type); } @@ -59,7 +59,7 @@ expect(Parser *par, TokenType type) } bool -match(Parser *p, TokenType type) +match(Parser* p, TokenType type) { // printf("matching type %d\n", type); if (peek(p).type == type) { @@ -69,20 +69,20 @@ match(Parser *p, TokenType type) return false; } -static Node * -parse_multiplicative(Parser *par) +static Node* +parse_multiplicative(Parser* par) { - Node *node = parse_term(par); + Node* node = parse_term(par); for (;;) { if (match(par, TOKEN_STAR)) { - Node *rhs = parse_unary(par); + Node* rhs = parse_unary(par); node = make_binary_node(OP_MUL, node, rhs); } else if (match(par, TOKEN_SLASH)) { - Node *rhs = parse_unary(par); + Node* rhs = parse_unary(par); node = make_binary_node(OP_DIV, node, rhs); } else if (match(par, TOKEN_PERCENT)) { - Node *rhs = parse_unary(par); + Node* rhs = parse_unary(par); node = make_binary_node(OP_MOD, node, rhs); } else break; @@ -91,16 +91,16 @@ parse_multiplicative(Parser *par) return node; } // additive: +, - -static Node * -parse_additive(Parser *par) +static Node* +parse_additive(Parser* par) { - Node *node = parse_multiplicative(par); + Node* node = parse_multiplicative(par); for (;;) { if (match(par, TOKEN_PLUS)) { - Node *rhs = parse_multiplicative(par); + Node* rhs = parse_multiplicative(par); node = make_binary_node(OP_PLUS, node, rhs); } else if (match(par, TOKEN_MINUS)) { - Node *rhs = parse_multiplicative(par); + Node* rhs = parse_multiplicative(par); node = make_binary_node(OP_MINUS, node, rhs); } else break; @@ -108,22 +108,22 @@ parse_additive(Parser *par) return node; } -static Node * -parse_relational(Parser *par) +static Node* +parse_relational(Parser* par) { - Node *node = parse_additive(par); + Node* node = parse_additive(par); for (;;) { if (match(par, TOKEN_LT)) { - Node *rhs = parse_additive(par); + Node* rhs = parse_additive(par); node = make_binary_node('<', node, rhs); } else if (match(par, TOKEN_LT_EQ)) { - Node *rhs = parse_additive(par); + Node* rhs = parse_additive(par); node = make_binary_node(OP_LT_EQ, node, rhs); } else if (match(par, TOKEN_GT)) { - Node *rhs = parse_additive(par); + Node* rhs = parse_additive(par); node = make_binary_node('>', node, rhs); } else if (match(par, TOKEN_GT_EQ)) { - Node *rhs = parse_additive(par); + Node* rhs = parse_additive(par); node = make_binary_node(OP_GT_EQ, node, rhs); } else break; @@ -131,16 +131,16 @@ parse_relational(Parser *par) return node; } -static Node * -parse_equality(Parser *par) +static Node* +parse_equality(Parser* par) { - Node *node = parse_relational(par); + Node* node = parse_relational(par); for (;;) { if (match(par, TOKEN_EQUALITY)) { // "==" - Node *rhs = parse_relational(par); + Node* rhs = parse_relational(par); node = make_binary_node(OP_EQUALITY, node, rhs); } else if (match(par, TOKEN_INEQUALITY)) { // "!=" - Node *rhs = parse_relational(par); + Node* rhs = parse_relational(par); node = make_binary_node(OP_INEQUALITY, node, rhs); } else break; @@ -148,19 +148,19 @@ parse_equality(Parser *par) return node; } -Node * -parse_expression(Parser *par) +Node* +parse_expression(Parser* par) { return parse_equality(par); } -Node * -parse_expression_statement(Parser *par) +Node* +parse_expression_statement(Parser* par) { - Node *expr = parse_expression(par); + Node* expr = parse_expression(par); expect(par, TOKEN_SEMICOLON); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("parse_expression_statement: could not alloc"); node->type = NODE_EXPR_STATEMENT; node->scope = NULL; @@ -172,8 +172,8 @@ parse_expression_statement(Parser *par) // // parse_statement // -Node * -parse_statement(Parser *par) +Node* +parse_statement(Parser* par) { Token tok = peek(par), tok2 = peek2(par); @@ -210,11 +210,11 @@ parse_statement(Parser *par) } } -Node * -parse_block(Parser *par) +Node* +parse_block(Parser* par) { - Node *stmt; - Node *block = (Node *)calloc(1, sizeof(Node)); + Node* stmt; + Node* block = (Node*)calloc(1, sizeof(Node)); if (block == NULL) panic("parse_block: could not alloc"); block->type = NODE_BLOCK; block->scope = NULL; @@ -223,7 +223,7 @@ parse_block(Parser *par) if (block->data.block.cap == block->data.block.len) { block->data.block.cap = block->data.block.cap == 0 ? 4 : block->data.block.cap * 2; - block->data.block.stmts = realloc(block->data.block.stmts, block->data.block.cap * sizeof(Node *)); + block->data.block.stmts = realloc(block->data.block.stmts, block->data.block.cap * sizeof(Node*)); if (block->data.block.stmts == NULL) { panic("realloc failed in parse_block"); } } @@ -235,14 +235,14 @@ parse_block(Parser *par) return block; } -Node * -parse_declaration_statement(Parser *par) +Node* +parse_declaration_statement(Parser* par) { - Node *type = parse_type(par); // consumes the type (e.g., "float") + Node* type = parse_type(par); // consumes the type (e.g., "float") Token ident = expect(par, TOKEN_IDENT); // variable or function name if (match(par, TOKEN_LPAREN)) { perror("called a var decl but this looks to be a func decl"); } - Node *var = calloc(1, sizeof(Node)); + Node* var = calloc(1, sizeof(Node)); if (var == NULL) panic("parse_declaration_statement: could not alloc"); var->type = NODE_VAR_DECL; var->scope = NULL; @@ -260,14 +260,14 @@ parse_declaration_statement(Parser *par) return var; } -Node * -parse_decl_or_func_decl(Parser *par) +Node* +parse_decl_or_func_decl(Parser* par) { - Node *type = parse_type(par); // consumes the type (e.g., "float") + Node* type = parse_type(par); // consumes the type (e.g., "float") Token ident = expect(par, TOKEN_IDENT); // variable or function name if (match(par, TOKEN_LPAREN)) { // function - Node *fn = calloc(1, sizeof(Node)); + Node* fn = calloc(1, sizeof(Node)); if (fn == NULL) panic("parse_decl_or_func_decl: func: could not alloc"); fn->type = NODE_FUNCTION_DECL; @@ -281,7 +281,7 @@ parse_decl_or_func_decl(Parser *par) expect(par, TOKEN_RPAREN); expect(par, TOKEN_LBRACE); - Node *body = parse_block(par); + Node* body = parse_block(par); fn->data.function_decl.body = body; fn->data.function_decl.name = (Span) { ident.start, ident.end }; @@ -292,7 +292,7 @@ parse_decl_or_func_decl(Parser *par) return fn; } else { // variable - Node *var = calloc(1, sizeof(Node)); + Node* var = calloc(1, sizeof(Node)); if (var == NULL) panic("parse_decl_or_func_decl: var: could not alloc"); var->type = NODE_VAR_DECL; var->scope = NULL; @@ -313,8 +313,8 @@ parse_decl_or_func_decl(Parser *par) } } -Node * -parse_declarations(Parser *par) +Node* +parse_declarations(Parser* par) { Token tok = peek(par); if (tok.type == TOKEN_EOF) return NULL; @@ -331,17 +331,17 @@ parse_declarations(Parser *par) } void -parser_parse(Ast *ast, Parser *par) +parser_parse(Ast* ast, Parser* par) { assert(par->token_count > 0 && "no tokens to parse"); - Node *node; - Node *program = make_program_node(par); + Node* node; + Node* program = make_program_node(par); for (;;) { node = parse_declarations(par); if (node == NULL) break; if (program->data.program.len == program->data.program.cap) { program->data.program.cap *= 2; - program->data.program.decl = (Node **)realloc(program->data.program.decl, program->data.program.cap * sizeof(Node *)); + program->data.program.decl = (Node**)realloc(program->data.program.decl, program->data.program.cap * sizeof(Node*)); assert(program->data.program.decl != NULL && "realloc failed"); } program->data.program.decl[program->data.program.len++] = node; diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c @@ -6,8 +6,8 @@ #include <stdbool.h> /* basic range to str */ -const char * -range_str(const char *src, size_t start, size_t end, char *stack_alloc_chptr) +const char* +range_str(const char* src, size_t start, size_t end, char* stack_alloc_chptr) { const size_t len = end - start; if (!src || !stack_alloc_chptr) return NULL; @@ -18,8 +18,8 @@ range_str(const char *src, size_t start, size_t end, char *stack_alloc_chptr) return stack_alloc_chptr; } -const char * -span_str(const char *src, Span s, char *stack_alloc_chptr) +const char* +span_str(const char* src, Span s, char* stack_alloc_chptr) { return range_str(src, s.start, s.end, stack_alloc_chptr); } @@ -60,12 +60,12 @@ span_str(const char *src, Span s, char *stack_alloc_chptr) // } static void -print_node(const char *source, Node *node, int level) +print_node(const char* source, Node* node, int level) { assert(node != NULL); assert(level < 192); - const char *name; + const char* name; switch (node->type) { case NODE_FUNCTION_DECL: name = range_str(source, node->data.function_decl.name.start, node->data.function_decl.name.end, (char[IDENTSZ]) { 0 }); @@ -77,7 +77,7 @@ print_node(const char *source, Node *node, int level) if (node->data.function_decl.params) { printf("%*s ↳ params:\n", level * 2, ""); for (size_t i = 0; i < node->data.function_decl.p_len; i++) { - Node *param = node->data.function_decl.params[i]; + Node* param = node->data.function_decl.params[i]; print_node(source, param, level + 1); } } else { @@ -130,7 +130,7 @@ print_node(const char *source, Node *node, int level) if (node->data.call_expr.args) { printf("%*s ↳ args:\n", level * 2, ""); for (size_t i = 0; i < node->data.call_expr.len; i++) { - Node *arg = node->data.call_expr.args[i]; + Node* arg = node->data.call_expr.args[i]; print_node(source, arg, level + 1); } } @@ -150,7 +150,7 @@ print_node(const char *source, Node *node, int level) printf("%*s ↳ LITERAL FLOAT NUMBER value=%f\n", level * 2, "", node->data.number.value); break; case NODE_STRING_LITERAL: { - const char *lit = span_str(source, node->data.string.value, (char[IDENTSZ]) { 0 }); + const char* lit = span_str(source, node->data.string.value, (char[IDENTSZ]) { 0 }); printf("%*s ↳ LITERAL STRING value=\"%s\"\n", level * 2, "", lit); break; } @@ -280,7 +280,7 @@ print_node(const char *source, Node *node, int level) } void -ast_print(Ast *ast) +ast_print(Ast* ast) { print_node(ast->src, ast->node, 0); } @@ -291,10 +291,10 @@ print_node_type_str(NodeType t) printf("print_node_type_str: %s\n", node_type_str(t)); } -const char * +const char* node_type_str(NodeType t) { - static const char *type_strings[] = { [NODE_PROGRAM] = "NODE_PROGRAM", + static const char* type_strings[] = { [NODE_PROGRAM] = "NODE_PROGRAM", [NODE_FUNCTION_DECL] = "NODE_FUNCTION_DECL", [NODE_PARAM] = "NODE_PARAM", [NODE_VAR_DECL] = "NODE_VAR_DECL", diff --git a/src/parser/stmt.c b/src/parser/stmt.c @@ -6,20 +6,20 @@ #include <stdbool.h> #include <assert.h> -Node * -parse_if(Parser *par) +Node* +parse_if(Parser* par) { expect(par, TOKEN_IF); expect(par, TOKEN_LPAREN); // @later remove necessity for parens - Node *cond = parse_expression(par); + Node* cond = parse_expression(par); expect(par, TOKEN_RPAREN); - Node *then_body = parse_statement(par); + Node* then_body = parse_statement(par); - Node *else_body = NULL; + Node* else_body = NULL; if (match(par, TOKEN_ELSE)) else_body = parse_statement(par); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("parse_if: could not alloc"); node->type = NODE_IF; node->scope = NULL; @@ -29,17 +29,17 @@ parse_if(Parser *par) return node; } -Node * -parse_while(Parser *par) +Node* +parse_while(Parser* par) { expect(par, TOKEN_WHILE); expect(par, TOKEN_LPAREN); - Node *cond = parse_expression(par); + Node* cond = parse_expression(par); expect(par, TOKEN_RPAREN); - Node *body = parse_statement(par); + Node* body = parse_statement(par); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("parse_while: could not alloc"); node->type = NODE_WHILE; node->scope = NULL; @@ -48,14 +48,14 @@ parse_while(Parser *par) return node; } -Node * -parse_for(Parser *par) +Node* +parse_for(Parser* par) { expect(par, TOKEN_FOR); expect(par, TOKEN_LPAREN); // init can be empty, a decl, or a expr statement - Node *init = NULL; // int i = 0 ... conditional expression stment + Node* init = NULL; // int i = 0 ... conditional expression stment if (!check(par, TOKEN_SEMICOLON)) { Token tok2 = peek2(par); if (tok2.type == TOKEN_IDENT) { @@ -66,17 +66,17 @@ parse_for(Parser *par) } else expect(par, TOKEN_SEMICOLON); - Node *cond = NULL; // i < len ... optional expression + Node* cond = NULL; // i < len ... optional expression if (!check(par, TOKEN_SEMICOLON)) cond = parse_expression(par); expect(par, TOKEN_SEMICOLON); - Node *inc = NULL; // i++ ... optional expression + Node* inc = NULL; // i++ ... optional expression if (!check(par, TOKEN_RPAREN)) { inc = parse_expression(par); } expect(par, TOKEN_RPAREN); - Node *body = parse_statement(par); + Node* body = parse_statement(par); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); node->type = NODE_FOR; node->scope = NULL; node->data.for_statement.init = init; @@ -88,20 +88,20 @@ parse_for(Parser *par) return node; } -Node * -parse_assignment(Parser *par) +Node* +parse_assignment(Parser* par) { Token ident = expect(par, TOKEN_IDENT); Span name = { .start = ident.start, .end = ident.end }; expect(par, TOKEN_EQUAL); - Node *expr = parse_expression(par); + Node* expr = parse_expression(par); - Node *assign = (Node *)calloc(1, sizeof(Node)); + Node* assign = (Node*)calloc(1, sizeof(Node)); if (assign == NULL) panic("parse_assignment: could not alloc"); assign->type = NODE_VAR_ASSIGN; assign->scope = NULL; - assign->data.var_assign.lhs = (Node *)calloc(1, sizeof(Node)); + assign->data.var_assign.lhs = (Node*)calloc(1, sizeof(Node)); if (assign->data.var_assign.lhs == NULL) panic("parse_for: lhs: could not alloc"); /* identifier @@ -125,25 +125,25 @@ parse_assignment(Parser *par) return assign; } -Node * -parse_break(Parser *par) +Node* +parse_break(Parser* par) { expect(par, TOKEN_BREAK); expect(par, TOKEN_SEMICOLON); - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("parse_break: could not alloc"); node->type = NODE_BREAK; node->scope = NULL; return node; } -Node * -parse_continue_statement(Parser *par) +Node* +parse_continue_statement(Parser* par) { expect(par, TOKEN_CONTINUE); // consume 'continue' - Node *node = (Node *)calloc(1, sizeof(Node)); + Node* node = (Node*)calloc(1, sizeof(Node)); if (node == NULL) panic("parse_continue_statemenet: could not alloc"); node->type = NODE_CONTINUE; node->scope = NULL; @@ -159,11 +159,11 @@ parse_continue_statement(Parser *par) return node; } -Node * -parse_return_statement(Parser *par) +Node* +parse_return_statement(Parser* par) { expect(par, TOKEN_RETURN); // consume 'return' - Node *ret = (Node *)calloc(1, sizeof(Node)); + Node* ret = (Node*)calloc(1, sizeof(Node)); if (ret == NULL) panic("parse_return_statemenet: could not alloc"); ret->type = NODE_RETURN; ret->scope = NULL; diff --git a/src/sem.c b/src/sem.c @@ -12,11 +12,11 @@ static int next_id = 100; Scope -scope_init(Node *node) +scope_init(Node* node) { Scope s = (Scope) { .parent = NULL, - .symbols = (Symbol **)calloc(CALLOC_SZ, sizeof(Symbol *)), - .children = (Scope **)calloc(CALLOC_SZ, sizeof(Scope *)), + .symbols = (Symbol**)calloc(CALLOC_SZ, sizeof(Symbol*)), + .children = (Scope**)calloc(CALLOC_SZ, sizeof(Scope*)), .cap = CALLOC_SZ, .len = 0, .ch_cap = CALLOC_SZ, @@ -30,11 +30,11 @@ scope_init(Node *node) return s; } -static Scope * -new_scope_from_scope(Scope *parent_scope, Node *node) +static Scope* +new_scope_from_scope(Scope* parent_scope, Node* node) { // new scope - Scope *scope = (Scope *)calloc(1, sizeof(Scope)); + Scope* scope = (Scope*)calloc(1, sizeof(Scope)); if (scope == NULL) panic("new_scope_from_scope: could not alloc"); scope->id = next_id++; @@ -42,12 +42,12 @@ new_scope_from_scope(Scope *parent_scope, Node *node) node->scope = scope; // init symbols list - scope->symbols = (Symbol **)calloc(CALLOC_SZ, sizeof(Symbol *)); + scope->symbols = (Symbol**)calloc(CALLOC_SZ, sizeof(Symbol*)); if (scope->symbols == NULL) panic("new_scope_from_scope: symbols: could not alloc"); scope->cap = CALLOC_SZ; scope->len = 0; - scope->children = (Scope **)calloc(CALLOC_SZ, sizeof(Scope *)); + scope->children = (Scope**)calloc(CALLOC_SZ, sizeof(Scope*)); if (scope->children == NULL) panic("new_scope_from_scope: children: could not alloc"); scope->ch_cap = CALLOC_SZ; scope->ch_len = 0; @@ -59,7 +59,7 @@ new_scope_from_scope(Scope *parent_scope, Node *node) assert(parent_scope->children != NULL); if (parent_scope->ch_len == parent_scope->ch_cap) { parent_scope->ch_cap *= 2; - parent_scope->children = (Scope **)realloc(parent_scope->children, parent_scope->ch_cap * sizeof(Scope *)); + parent_scope->children = (Scope**)realloc(parent_scope->children, parent_scope->ch_cap * sizeof(Scope*)); assert(parent_scope->children != NULL && "realloc failed"); } parent_scope->children[parent_scope->ch_len++] = scope; @@ -72,25 +72,25 @@ new_scope_from_scope(Scope *parent_scope, Node *node) } static void -add_to_scope(Scope *scope, Symbol *sym) +add_to_scope(Scope* scope, Symbol* sym) { if (scope->len >= scope->cap) { scope->cap *= 2; - scope->symbols = (Symbol **)realloc(scope->symbols, scope->cap * sizeof(Symbol *)); + scope->symbols = (Symbol**)realloc(scope->symbols, scope->cap * sizeof(Symbol*)); } scope->symbols[scope->len++] = sym; } static void -scope_var(Scope *scope, Ast *ast, Node *node) +scope_var(Scope* scope, Ast* ast, Node* node) { - const char *var_name = span_str(ast->src, node->data.var_decl.name, (char[IDENTSZ]) { 0 }); - const char *type_name = span_str(ast->src, node->data.var_decl.type->data.ident.name, (char[IDENTSZ]) { 0 }); + const char* var_name = span_str(ast->src, node->data.var_decl.name, (char[IDENTSZ]) { 0 }); + const char* type_name = span_str(ast->src, node->data.var_decl.type->data.ident.name, (char[IDENTSZ]) { 0 }); - Symbol *sym = (Symbol *)calloc(1, sizeof(Symbol)); + Symbol* sym = (Symbol*)calloc(1, sizeof(Symbol)); if (sym == NULL) panic("scope_var: symbol: could not alloc"); - TypeInfo *type = (TypeInfo *)calloc(1, sizeof(TypeInfo)); + TypeInfo* type = (TypeInfo*)calloc(1, sizeof(TypeInfo)); if (type == NULL) panic("scope_var: type: could not alloc"); if (strcmp(type_name, "float") == 0) { @@ -119,13 +119,13 @@ scope_var(Scope *scope, Ast *ast, Node *node) } static void -scope_func(Scope *parent_scope, Ast *ast, Node *node) +scope_func(Scope* parent_scope, Ast* ast, Node* node) { - Scope *scope = new_scope_from_scope(parent_scope, node); + Scope* scope = new_scope_from_scope(parent_scope, node); node->scope = scope; for (size_t i = 0; i < node->data.block.len; i++) { - Node *stmt = node->data.block.stmts[i]; + Node* stmt = node->data.block.stmts[i]; stmt->scope = scope; switch (stmt->type) { case NODE_VAR_DECL: { @@ -139,10 +139,10 @@ scope_func(Scope *parent_scope, Ast *ast, Node *node) } void -scope_build(Scope *scope, Ast *ast) +scope_build(Scope* scope, Ast* ast) { for (size_t i = 0; i < ast->node->data.program.len; i++) { - Node *node = ast->node->data.program.decl[i]; + Node* node = ast->node->data.program.decl[i]; node->scope = scope; switch (node->type) { case NODE_VAR_DECL: @@ -158,13 +158,13 @@ scope_build(Scope *scope, Ast *ast) } void -scope_print(Scope *scope, Ast *ast) +scope_print(Scope* scope, Ast* ast) { if (scope == NULL || scope->symbols == NULL) return; for (size_t i = 0; i < scope->len; i++) { - Symbol *sym = scope->symbols[i]; - const char *name = span_str(ast->src, sym->name, (char[IDENTSZ]) { 0 }); + Symbol* sym = scope->symbols[i]; + const char* name = span_str(ast->src, sym->name, (char[IDENTSZ]) { 0 }); int parent = -1; if (scope->parent != NULL) parent = scope->parent->id; bool has_owner_node = false; @@ -181,15 +181,15 @@ scope_print(Scope *scope, Ast *ast) if (scope->ch_len == 0) return; for (size_t j = 0; j < scope->ch_len; j++) { - Scope *child_scope = scope->children[j]; + Scope* child_scope = scope->children[j]; scope_print(child_scope, ast); } } -const char * +const char* type_kind_str(SymbolType t) { - static const char *type_strings[] = { + static const char* type_strings[] = { [SYMTYPE_VOID] = "TYPE_VOID", [SYMTYPE_INT] = "TYPE_INT", [SYMTYPE_UINT] = "TYPE_UINT", diff --git a/src/sem.h b/src/sem.h @@ -6,17 +6,17 @@ #include "types.h" // Symbol table functions -void symbol_add(const char *name, TypeInfo *type); -TypeInfo *symbol_get_type(const char *name); -Symbol *symbol_find(const char *name); +void symbol_add(const char* name, TypeInfo* type); +TypeInfo* symbol_get_type(const char* name); +Symbol* symbol_find(const char* name); // Scope management functions -Scope scope_init(Node *); -void scope_add_symbol(Scope *scope, const char *name, TypeInfo *type); -Symbol *scope_find_symbol(Scope *scope, const char *name); +Scope scope_init(Node*); +void scope_add_symbol(Scope* scope, const char* name, TypeInfo* type); +Symbol* scope_find_symbol(Scope* scope, const char* name); // Type checking functions -int types_equal(TypeInfo *a, TypeInfo *b); +int types_equal(TypeInfo* a, TypeInfo* b); -void scope_build(Scope *, Ast *); -void scope_print(Scope *, Ast *); +void scope_build(Scope*, Ast*); +void scope_print(Scope*, Ast*); diff --git a/src/types.h b/src/types.h @@ -43,7 +43,7 @@ typedef enum { TOKEN_EOF } TokenType; // NOTE also update token_type_str! -const char *token_type_str(TokenType t); +const char* token_type_str(TokenType t); typedef struct { size_t start; @@ -54,15 +54,15 @@ typedef struct { } Token; typedef struct { - Token *tokens; + Token* tokens; size_t token_count; size_t token_cap; size_t pos; size_t line; size_t col; - const char *src; + const char* src; size_t src_len; - const char *filename; + const char* filename; } Lexer; typedef enum { @@ -92,7 +92,7 @@ typedef enum { NODE_UNKNOWN, } NodeType; // note: if changed, edit node_type_str! -const char *node_type_str(NodeType); +const char* node_type_str(NodeType); void print_node_type_str(NodeType); /* @@ -147,9 +147,9 @@ typedef struct { typedef struct Node { NodeType type; - struct Node *next; - struct Scope *scope; - const char *filename; + struct Node* next; + struct Scope* scope; + const char* filename; int line, col; /* NOTE we will eventually add spans for condition info, etc. to print out in errors */ @@ -180,21 +180,21 @@ typedef struct Node { } Node; typedef struct { - Token *tokens; + Token* tokens; size_t token_count; size_t pos; - const char *src; + const char* src; size_t src_len; - const char *filename; + const char* filename; } Parser; typedef struct { - Node *node; - const char *src; + Node* node; + const char* src; } Ast; typedef struct { - Node **items; + Node** items; size_t len, cap; } NodeVec; @@ -213,7 +213,7 @@ typedef enum { SYMTYPE_TODO, } SymbolType; // note also update type_kind_str! -const char *type_kind_str(SymbolType); +const char* type_kind_str(SymbolType); typedef enum { ENUM_VALUE_INT, @@ -221,23 +221,23 @@ typedef enum { } EnumValueKind; typedef struct StructField { - char *name; - struct Type *type; + char* name; + struct Type* type; } StructField; typedef struct EnumField { - char *name; + char* name; EnumValueKind kind; union { // not used? int int_value; - char *string_value; + char* string_value; } val; } EnumField; typedef struct StructMethod { - char *name; - struct Type *return_type; - struct Type **param_types; + char* name; + struct Type* return_type; + struct Type** param_types; int params_count; int params_cap; // TODO add ptr to func decl of this struct method @@ -272,24 +272,24 @@ typedef struct Type { typedef struct Symbol { Span name; - Node *decl; - TypeInfo *type; // todo print all found in gen + Node* decl; + TypeInfo* type; // todo print all found in gen - gcc_jit_type *ctype; + gcc_jit_type* ctype; union { - gcc_jit_lvalue *lvalue; - gcc_jit_param *param; - gcc_jit_rvalue *const_rvalue; + gcc_jit_lvalue* lvalue; + gcc_jit_param* param; + gcc_jit_rvalue* const_rvalue; } d; } Symbol; typedef struct Scope { - struct Node *owner; - struct Scope *parent; - Symbol **symbols; + struct Node* owner; + struct Scope* parent; + Symbol** symbols; size_t len; size_t cap; - struct Scope **children; + struct Scope** children; size_t ch_len; size_t ch_cap; int depth; diff --git a/src/utils.c b/src/utils.c @@ -7,7 +7,7 @@ #include <string.h> void -panic(const char *fmt, ...) +panic(const char* fmt, ...) { va_list args; int saved_errno; @@ -23,7 +23,7 @@ panic(const char *fmt, ...) } void -softpanic(const char *fmt, ...) +softpanic(const char* fmt, ...) { va_list args; va_start(args, fmt); diff --git a/src/utils.h b/src/utils.h @@ -2,5 +2,5 @@ #include <stdlib.h> -void panic(const char *fmt, ...); -void softpanic(const char *fmt, ...); +void panic(const char* fmt, ...); +void softpanic(const char* fmt, ...);