diff options
-rw-r--r-- | abbrevs.txt | 1 | ||||
-rw-r--r-- | package.c | 72 | ||||
-rw-r--r-- | point.toc | 6 | ||||
-rw-r--r-- | test.toc | 2 | ||||
-rw-r--r-- | types.c | 2 | ||||
-rw-r--r-- | types.h | 1 |
6 files changed, 56 insertions, 28 deletions
diff --git a/abbrevs.txt b/abbrevs.txt index 3c25514..643d89b 100644 --- a/abbrevs.txt +++ b/abbrevs.txt @@ -15,6 +15,7 @@ expr - expression exptr - exporter fn - function ident - identifier +imptr - importer kw - keyword len - length num - number @@ -21,6 +21,10 @@ static void exptr_create(Exporter *ex, FILE *out, char *code) { ex->code = code; } +static inline void *imptr_malloc(Importer *i, size_t n) { + return allocr_malloc(i->allocr, n); +} + static inline void export_u8(Exporter *ex, U8 u8) { write_u8(ex->out, u8); @@ -97,7 +101,7 @@ static inline bool import_char(Importer *i) { static inline void export_vlq(Exporter *ex, U64 x) { write_vlq(ex->out, x); } -static inline bool import_vlq(Importer *i) { +static inline U64 import_vlq(Importer *i) { return read_vlq(i->in); } @@ -110,6 +114,13 @@ static inline void export_str(Exporter *ex, const char *str, size_t len) { #endif } +static inline char *import_str(Importer *i, size_t len) { + char *str = imptr_malloc(i, len+1); + str[len] = 0; + fread(str, 1, len, i->in); + return str; +} + static void export_location(Exporter *ex, Location where) { if (ex->export_locations) { export_vlq(ex, (U64)where.line); @@ -135,6 +146,9 @@ static inline void export_optional_ident(Exporter *ex, Identifier i) { static inline void export_len(Exporter *ex, size_t len) { export_vlq(ex, (U64)len); } +static inline size_t import_len(Importer *i) { + return (size_t)import_vlq(i); +} static const U8 toc_package_indicator[3] = {116, 111, 112}; @@ -160,6 +174,43 @@ static void exptr_start(Exporter *ex, const char *pkg_name, size_t pkg_name_len) } } +/* where = where was this imported */ +static bool import_pkg(Allocator *allocr, Package *p, FILE *f, const char *fname, Location where) { + Importer i = {0}; + idents_create(&p->idents); + i.pkg = p; + i.in = f; + i.allocr = allocr; + /* read header */ + U8 toc[3]; + toc[0] = import_u8(&i); + toc[1] = import_u8(&i); + toc[2] = import_u8(&i); + if (toc[0] != toc_package_indicator[0] || + toc[1] != toc_package_indicator[1] || + toc[2] != toc_package_indicator[2]) { + err_print(where, "%s is not a toc package file.", fname); + return false; + } + U32 version_written = import_u32(&i); + if (version_written != TOP_FMT_VERSION) { + warn_print(where, "Warning: toc version mismatch. Package was created with version " U32_FMT " but version " STRINGIFY(TOP_FMT_VERSION) " is being used.\n" + "The package may be read incorrectly.", + version_written); + } + U64 ident_offset = import_u64(&i); + size_t pkg_name_len = import_len(&i); + char *pkg_name = import_str(&i, pkg_name_len); + puts(pkg_name); + bool has_code = import_bool(&i); + if (has_code) { + size_t code_len = import_len(&i); + char *code = import_str(&i, code_len); + puts(code); + } + return true; +} + static bool export_type(Exporter *ex, Type *type, Location where) { assert(type->flags & TYPE_IS_RESOLVED); export_u8(ex, (U8)type->kind); @@ -645,22 +696,3 @@ static bool exptr_finish(Exporter *ex) { return true; } - -/* where = where was this imported */ -static bool import_pkg(Package *p, FILE *f, const char *fname, Location where) { - Importer i = {0}; - idents_create(&p->idents); - i.pkg = p; - i.in = f; - U8 toc[3]; - toc[0] = import_u8(&i); - toc[1] = import_u8(&i); - toc[2] = import_u8(&i); - if (toc[0] != toc_package_indicator[0] || - toc[1] != toc_package_indicator[1] || - toc[2] != toc_package_indicator[2]) { - err_print(where, "%s is not a toc package file.", fname); - return false; - } - return true; -} @@ -1,10 +1,5 @@ pkg "point"; -#export f ::= 3.123343234; - - -#export g ::= -3.14938244 as f64; -/* #export Point ::= struct { x, y: int; @@ -18,4 +13,3 @@ pkg "point"; mk_point2 ::= fn(x, y:int) p: Point { p = mk_point(x*x, y*y); }; -*/
\ No newline at end of file @@ -6,4 +6,4 @@ putf ::= fn(x: float) { #C("printf(\"%f\\n\", (double)x); "); }; -point ::= pkg "point"; +point ::= pkg "point";
\ No newline at end of file @@ -894,7 +894,7 @@ static bool types_expr(Typer *tr, Expression *e) { free(filename); return false; } - if (!import_pkg(pkg, fp, filename, e->where)) { + if (!import_pkg(tr->allocr, pkg, fp, filename, e->where)) { free(filename); return false; } @@ -787,6 +787,7 @@ typedef struct Exporter { typedef struct Importer { FILE *in; Package *pkg; + Allocator *allocr; } Importer; typedef struct CGenerator { |