diff options
-rw-r--r-- | cgen.c | 2 | ||||
-rw-r--r-- | main.c | 5 | ||||
-rw-r--r-- | package.c | 26 | ||||
-rw-r--r-- | std/arr.toc | 18 | ||||
-rw-r--r-- | test.toc | 25 | ||||
-rw-r--r-- | types.h | 1 |
6 files changed, 47 insertions, 30 deletions
@@ -1183,7 +1183,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) { if (!cgen_expr(g, s->to)) return false; } else { - cgen_write(g, "of__.n - 1"); + cgen_write(g, "of__.n"); } cgen_write(g, " - "); cgen_ident_id(g, from_id); @@ -130,9 +130,12 @@ int main(int argc, char **argv) { Typer tr; Evaluator ev; - Exporter exptr; + Exporter exptr = {0}; evalr_create(&ev, &tr, &main_allocr); typer_create(&tr, &ev, &err_ctx, &main_allocr, &idents); +#ifdef TOC_DEBUG + exptr.export_all_ident_names = true; +#endif tr.exptr = &exptr; if (!block_enter(NULL, f.stmts, SCOPE_CHECK_REDECL)) /* enter global scope */ @@ -180,6 +180,13 @@ static Location import_location(Importer *im) { return l; } +static void export_ident_name(Exporter *ex, Identifier ident) { + if (ident->export_name) return; + *(Identifier *)arr_add(&ex->exported_idents) = ident; + ident->export_name = true; +} + + /* handles NULL */ static inline void export_ident(Exporter *ex, Identifier i) { if (!i) { @@ -196,6 +203,8 @@ static inline void export_ident(Exporter *ex, Identifier i) { i->export_id = ++ex->ident_id; } export_vlq(ex, i->export_id); + if (ex->export_all_ident_names) + export_ident_name(ex, i); } static inline Identifier import_ident(Importer *im) { U64 id = import_vlq(im); @@ -278,13 +287,18 @@ static bool import_pkg(Allocator *allocr, Package *p, FILE *f, const char *fname /* read declarations */ size_t ndecls = import_u32(&i); p->stmts = NULL; + /* printf("%lu decls\n",ndecls); */ for (size_t idx = 0; idx < ndecls; ++idx) { + /* printf("-importing one @ %lu\n",ftell(i.in)); */ Statement *s = arr_add(&p->stmts); s->kind = STMT_DECL; import_decl(&i, &s->decl); } - assert(ftell(i.in) == (long)footer_offset); + if (ftell(i.in) != (long)footer_offset) { + err_print(where, "Something strange happened when importing this package. Expected to be at byte #%ld but actually at byte #%ld.", (long)footer_offset, ftell(i.in)); + return false; + } free(i.ident_map); if (!block_enter(NULL, p->stmts, 0)) return false; @@ -937,13 +951,6 @@ static void import_expr(Importer *im, Expression *e) { } } - -static void export_ident_name(Exporter *ex, Identifier ident) { - if (ident->export_name) return; - *(Identifier *)arr_add(&ex->exported_idents) = ident; - ident->export_name = true; -} - static bool export_decl(Exporter *ex, Declaration *d) { if (d->flags & DECL_MARKED_FOR_EXPORTING) { arr_foreach(d->idents, Identifier, ident) { @@ -1177,6 +1184,7 @@ static bool export_struct(Exporter *ex, StructDef *s) { export_bool(ex, s->export.id == 0); if (s->export.id == 0) { s->export.id = ++ex->nexported_structs; + export_vlq(ex, s->export.id); export_ident(ex, s->name); if (s->name) export_ident_name(ex, s->name); @@ -1215,6 +1223,7 @@ static bool exptr_finish(Exporter *ex) { export_u32(ex, 0); /* NOTE: arr_len(ex->decls_to_export) may change during loop! */ for (size_t i = 0; i < arr_len(ex->decls_to_export); ++i) { + /* printf("-exporting one at %lu\n",ftell(ex->out)); */ Declaration *d = ex->decls_to_export[i]; if (!export_decl(ex, d)) return false; @@ -1222,6 +1231,7 @@ static bool exptr_finish(Exporter *ex) { { long back = ftell(ex->out); fseek(ex->out, ndecls_offset, SEEK_SET); + /* printf("%lu decls\n",arr_len(ex->decls_to_export)); */ export_u32(ex, (U32)arr_len(ex->decls_to_export)); fseek(ex->out, back, SEEK_SET); } diff --git a/std/arr.toc b/std/arr.toc index 0e81f3a..12d44f5 100644 --- a/std/arr.toc +++ b/std/arr.toc @@ -4,24 +4,20 @@ pkg "arr"; #export Arr ::= fn (t :: Type) Type { struct { data : []t; - len, cap : int; + cap : int; } }; #export arr_add ::= fn(t ::=, a : &Arr(t), x : t) { - if a.len >= a.cap { + if a.data.len >= a.cap { a.cap = a.cap * 2 + 2; new_data := new(t, a.cap); - for i := 0..a.len-1 { + new_data.len = a.data.len; + for i := 0..a.data.len-1 { new_data[i] = a.data[i]; } a.data = new_data; } - a.data[a.len] = x; - a.len += 1; -}; - -#export arr_foreach ::= fn(t ::=, a : Arr(t), f : fn(&t)) { - for i := 0..a.len-1 { - f(&a.data[i]); - } + a.data[a.data.len] = x; + a.data.len += 1; }; +#export arr_len ::= fn(t ::=, a : Arr(t)) int { a.data.len }; @@ -1,12 +1,19 @@ -io ::= pkg "std/io"; -hw ::= fn() int { - io.puts("Hello, world!"); - 3 -}; arr ::= pkg "std/arr"; +io ::= pkg "std/io"; main ::= fn() { - hw(); - x::=hw(); - x : arr.Arr(int); - arr_add(&x, 10); + x : arr.Arr(char); + arr.arr_add(&x, 'H'); + arr.arr_add(&x, 'e'); + arr.arr_add(&x, 'l'); + arr.arr_add(&x, 'l'); + arr.arr_add(&x, 'o'); + arr.arr_add(&x, '!'); + + s : [1]char; + s[0] = (arr.arr_len(x) as char) + '0'; + io.puts(s[:]); + for c := x.data { + s[0] = c; + io.puts(s[:]); + } };
\ No newline at end of file @@ -917,6 +917,7 @@ typedef struct Typer { typedef struct Exporter { FILE *out; /* .top (toc package) to output to */ bool started; + bool export_all_ident_names; /* export every identifier's name, not just the important ones */ U64 ident_id; File exporting_to; U32 nexported_structs; |