diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-12-02 21:10:26 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-12-02 21:10:26 -0500 |
commit | 38dd2a6e3389db247ce15faafb5ffc75a82ae50d (patch) | |
tree | f4c08cfb710336162b86f577e6c5215346ca6041 | |
parent | e252df25d86bb17d73bcbacbf1208a6223093ec1 (diff) |
cleanup
-rw-r--r-- | allocator.c | 2 | ||||
-rw-r--r-- | cgen.c | 18 | ||||
-rw-r--r-- | copy.c | 10 | ||||
-rw-r--r-- | decls_cgen.c | 2 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | parse.c | 8 | ||||
-rw-r--r-- | test.toc | 14 | ||||
-rw-r--r-- | toc.c | 6 | ||||
-rw-r--r-- | typedefs_cgen.c | 16 | ||||
-rw-r--r-- | types.c | 4 |
10 files changed, 46 insertions, 36 deletions
diff --git a/allocator.c b/allocator.c index 68a38cf..beb83dd 100644 --- a/allocator.c +++ b/allocator.c @@ -1,7 +1,7 @@ static void *err_malloc(size_t bytes); static void *err_calloc(size_t n, size_t sz); static void *err_realloc(void *prev, size_t new_size); -/* #define NO_ALLOCATOR 1 /\* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator *\/ */ +#define NO_ALLOCATOR 1 /* useful for debugging; valgrind (maybe) checks writing past the end of a malloc, but that won't work with an allocator */ /* number of bytes a page hold, not including the header */ #define PAGE_BYTES (16384 - sizeof(Page)) #define PAGE_MAX_ALIGNS (PAGE_BYTES / sizeof(MaxAlign)) @@ -318,22 +318,6 @@ static bool cgen_type_pre(CGenerator *g, Type *t, Location where) { } else if (t->struc->c.id) { cgen_ident_id(g, t->struc->c.id); } else { - #if 0 - /* TODO: DELME */ - cgen_write(g, "struct {"); - g->indent_lvl++; - cgen_nl(g); - arr_foreach(t->struc.fields, Field, f) { - if (!cgen_type_pre(g, f->type, where)) return false; - cgen_write(g, " "); - cgen_ident(g, f->name); - if (!cgen_type_post(g, f->type, where)) return false; - cgen_write(g, ";"); - cgen_nl(g); - } - g->indent_lvl--; - cgen_write(g, "}"); - #endif assert(0); } break; @@ -1944,6 +1928,8 @@ static bool cgen_defs_block(CGenerator *g, Block *b) { if (!cgen_defs_stmt(g, s)) return false; } + if (b->ret_expr && !cgen_defs_expr(g, b->ret_expr)) + return false; return true; } @@ -60,10 +60,9 @@ static void copy_val(Allocator *a, Value *out, Value *in, Type *t) { } } -/* does not work on resolved types */ +/* only works on unresolved types */ static void copy_type(Copier *c, Type *out, Type *in) { assert(!(in->flags & TYPE_IS_RESOLVED)); - *out = *in; switch (in->kind) { case TYPE_BUILTIN: @@ -91,7 +90,9 @@ static void copy_type(Copier *c, Type *out, Type *in) { } } break; case TYPE_ARR: - /* if (!(in->flags & TYPE_IS_RESOLVED)) { */ + /* if (in->flags & TYPE_IS_RESOLVED) { */ + /* out->arr.n = in->arr.n; */ + /* } else { */ out->arr.n_expr = allocr_malloc(c->allocr, sizeof *out->arr.n_expr); copy_expr(c, out->arr.n_expr, in->arr.n_expr); /* } */ @@ -111,12 +112,13 @@ static void copy_type(Copier *c, Type *out, Type *in) { *out->struc = *in->struc; size_t nfields = arr_len(in->struc->fields); out->struc->fields = NULL; + arr_set_lena(&out->struc->fields, nfields, c->allocr); for (size_t i = 0; i < nfields; i++) { Field *fout = &out->struc->fields[i]; Field *fin = &in->struc->fields[i]; *fout = *fin; - copy_type(c, fout->type, fin->type); + copy_type(c, fout->type = allocr_malloc(c->allocr, sizeof *fout->type), fin->type); } } break; } diff --git a/decls_cgen.c b/decls_cgen.c index 91ad40a..82bf56f 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -62,6 +62,8 @@ static bool cgen_decls_block(CGenerator *g, Block *b) { arr_foreach(b->stmts, Statement, s) if (!cgen_decls_stmt(g, s)) return false; + if (b->ret_expr && !cgen_decls_expr(g, b->ret_expr)) + return false; cgen_block_exit(g, prev); return true; } @@ -1,7 +1,7 @@ /* TODO: -fix VBS with structs test ArrInt @= Arr(int); +new version of copy_val for copying types there are probably places where we enter a function and never exit (in typing?) if there's an error packages @@ -167,10 +167,10 @@ static Keyword builtin_type_to_kw(BuiltinType t) { /* returns the number of characters written, not including the null character */ static size_t type_to_str_(Type *t, char *buffer, size_t bufsize) { - if ((t->flags & TYPE_IS_RESOLVED) && t->was_expr) { - /* TODO: improve this (see also: case TYPE_EXPR) */ - return str_copy(buffer, bufsize, "<type expression>"); - } + /* if ((t->flags & TYPE_IS_RESOLVED) && t->was_expr) { */ + /* /\* TODO: improve this (see also: case TYPE_EXPR) *\/ */ + /* return str_copy(buffer, bufsize, "<type expression>"); */ + /* } */ switch (t->kind) { case TYPE_VOID: return str_copy(buffer, bufsize, "void"); @@ -23,11 +23,11 @@ y:s; }; main @= fn() { -x : pair(int); -y : pair(int); -z : pair(float); -x[0] = 7; -puti(x[0]); -z[0] = 3.3; -putf(z[0]); +// a : pair(int); +b : pair(int); +c : pair(float); +// a.x = 5; +// puti(a.x); +c.x = 13.3; +// putf(c.x); }; @@ -1,15 +1,15 @@ /* Includes all of toc's files */ #include <assert.h> +#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> -#include <ctype.h> #include <limits.h> -#include <inttypes.h> -#include <stdbool.h> #include <float.h> +#include <stdbool.h> +#include <inttypes.h> #include "types.h" #include "allocator.c" diff --git a/typedefs_cgen.c b/typedefs_cgen.c index 0feee25..9da3121 100644 --- a/typedefs_cgen.c +++ b/typedefs_cgen.c @@ -1,13 +1,17 @@ static bool typedefs_stmt(CGenerator *g, Statement *s); static bool typedefs_decl(CGenerator *g, Declaration *d); +static bool typedefs_expr(CGenerator *g, Expression *e); static bool typedefs_block(CGenerator *g, Block *b) { Block *prev = g->block; + printf("%s\n",b->stmts[0].where.code); if (!cgen_block_enter(g, b)) return false; arr_foreach(b->stmts, Statement, s) if (!typedefs_stmt(g, s)) return false; + if (b->ret_expr && !typedefs_expr(g, b->ret_expr)) + return false; cgen_block_exit(g, prev); return true; } @@ -18,6 +22,18 @@ static bool typedefs_expr(CGenerator *g, Expression *e) { /* needs to go before decls_cgen.c... */ e->fn.c.id = g->ident_counter++; } + if (e->kind == EXPR_TYPE && e->typeval.kind == TYPE_STRUCT) { + StructDef *sdef = e->typeval.struc; + if (sdef->c.id || sdef->c.name) { + /* we've already done this */ + } else { + cgen_write(g, "struct "); + IdentID id = g->ident_counter++; + cgen_ident_id(g, id); + sdef->c.id = id; + cgen_write(g, ";"); + } + } return true; } @@ -1407,6 +1407,10 @@ static bool types_expr(Typer *tr, Expression *e) { case BINARY_EQ: case BINARY_NE: { bool valid = false; + assert(lhs_type->flags & TYPE_IS_RESOLVED); + assert(rhs_type->flags & TYPE_IS_RESOLVED); + + if (o == BINARY_SET) { valid = type_eq(lhs_type, rhs_type); if (lhs_type->kind == TYPE_TYPE) { |