From 1b12d3410e0426f3772564ff8439dd0c5f9a7186 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 27 Dec 2019 11:49:46 -0500 Subject: fixed error reporting; more export --- err.c | 2 +- export.c | 129 -------------------------------------------------------------- main.c | 7 ++-- package.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ parse.c | 3 +- test.toc | 2 +- toc.c | 2 +- types.c | 3 +- 8 files changed, 116 insertions(+), 139 deletions(-) delete mode 100644 export.c create mode 100644 package.c diff --git a/err.c b/err.c index 11a4863..a92a143 100644 --- a/err.c +++ b/err.c @@ -86,7 +86,7 @@ static void err_print_location_text(Location where) { end = strchr(text, '\0'); assert(end); err_fprint("\there: --> "); - if (!has_newline) + if (!text[0]) err_fprint(""); else err_fwrite(text, 1, (size_t)(end - text)); diff --git a/export.c b/export.c deleted file mode 100644 index 28e03e0..0000000 --- a/export.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - Copyright (C) 2019 Leo Tenenbaum. - This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever. - You should have received a copy of the GNU General Public License along with toc. If not, see . -*/ -static void exptr_create(Exporter *exptr, FILE *out) { - exptr->out = out; - exptr->export_locations = true; -} - -static void export_u8(Exporter *ex, U8 u8) { - write_u8(ex->out, u8); -} - -static void export_u16(Exporter *ex, U16 u16) { - write_u16(ex->out, u16); -} - -static void export_u32(Exporter *ex, U32 u32) { - write_u32(ex->out, u32); -} - -static void export_u64(Exporter *ex, U64 u64) { - write_u64(ex->out, u64); -} - -static void export_location(Exporter *ex, Location where) { - if (ex->export_locations) { - export_u32(ex, where.line); - export_u32(ex, where.pos); - } -} - -static void export_ident(Exporter *ex, Identifier i) { - assert(i->id); - export_u64(ex, i->id); -} - -static inline bool type_contains_reference(Type *type) { - assert(type->flags & TYPE_IS_RESOLVED); - switch (type->kind) { - case TYPE_PTR: - case TYPE_SLICE: - case TYPE_FN: - return true; - case TYPE_TUPLE: - arr_foreach(type->tuple, Type, sub) - if (type_contains_reference(sub)) - return true; - return false; - case TYPE_STRUCT: - arr_foreach(type->struc->fields, Field, field) { - if (type_contains_reference(field->type)) - return true; - } - return false; - case TYPE_ARR: - return type_contains_reference(type->arr.of); - case TYPE_BUILTIN: - case TYPE_VOID: - case TYPE_UNKNOWN: - case TYPE_TYPE: - return false; - case TYPE_EXPR: break; - } - assert(0); - return false; -} - -static void export_type(Exporter *ex, Type *type) { -} - -static void export_val(Exporter *ex, Value val, Type *type) { - assert(!type_contains_reference(type)); - export_type(ex, type); - switch (type->kind) { - case TYPE_VOID: break; - case TYPE_TYPE: - export_type(ex, val.type); - break; - case TYPE_UNKNOWN: - case TYPE_EXPR: - assert(0); - break; - } -} - -static void export_expr(Exporter *ex, Expression *e) { -} - -enum { - DECL_EXPORT_NONE, - DECL_EXPORT_EXPR, - DECL_EXPORT_VAL -}; - -static void export_decl(Exporter *ex, Declaration *d) { - if (d->type.kind == TYPE_UNKNOWN) { - warn_print(d->where, "Can't export declaration of unknown type."); - return; - } - export_location(ex, d->where); - export_u16(ex, (U16)arr_len(d->idents)); - arr_foreach(d->idents, Identifier, ident) { - export_ident(ex, *ident); - } - - U8 constness = 0; - if (d->flags & DECL_IS_CONST) constness = 1; - else if (d->flags & DECL_SEMI_CONST) constness = 2; - export_u8(ex, constness); - - U8 expr_kind = 0; - if (d->flags & DECL_HAS_EXPR) expr_kind = DECL_EXPORT_EXPR; - if (d->flags & DECL_FOUND_VAL) { - if (type_contains_reference(&d->type)) - expr_kind = DECL_EXPORT_EXPR; - else - expr_kind = DECL_EXPORT_VAL; - } - - export_u8(ex, expr_kind); - - if (expr_kind == DECL_EXPORT_EXPR) { - export_expr(ex, &d->expr); - } else if (expr_kind == DECL_EXPORT_VAL) { - export_val(ex, d->val, &d->type); - } -} diff --git a/main.c b/main.c index 6cb0998..e7a6fb2 100644 --- a/main.c +++ b/main.c @@ -117,10 +117,8 @@ int main(int argc, char **argv) { err_fprint(TEXT_IMPORTANT("Errors occured while parsing.\n")); return EXIT_FAILURE; } -#if 0 - fprint_parsed_file(stdout, &f); - printf("\n\n-----\n\n"); -#endif + /* fprint_parsed_file(stdout, &f); */ + /* printf("\n\n-----\n\n"); */ tokr_free(&t); @@ -138,6 +136,7 @@ int main(int argc, char **argv) { return false; if (!types_file(&tr, &f)) { + /* TODO(eventually): fix this if the error occured while exporting something */ err_fprint(TEXT_IMPORTANT("Errors occured while determining types.\n")); return EXIT_FAILURE; } diff --git a/package.c b/package.c new file mode 100644 index 0000000..260c9d0 --- /dev/null +++ b/package.c @@ -0,0 +1,107 @@ +/* + Copyright (C) 2019 Leo Tenenbaum. + This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever. + You should have received a copy of the GNU General Public License along with toc. If not, see . +*/ +static void exptr_create(Exporter *exptr, FILE *out) { + exptr->out = out; + exptr->export_locations = true; +} + +static void export_u8(Exporter *ex, U8 u8) { + write_u8(ex->out, u8); +} + +static void export_u16(Exporter *ex, U16 u16) { + write_u16(ex->out, u16); +} + +static void export_u32(Exporter *ex, U32 u32) { + write_u32(ex->out, u32); +} + +static void export_u64(Exporter *ex, U64 u64) { + write_u64(ex->out, u64); +} + +static void export_location(Exporter *ex, Location where) { + if (ex->export_locations) { + export_u32(ex, where.line); + export_u32(ex, where.pos); + } +} + +static void export_ident(Exporter *ex, Identifier i) { + assert(i->id); + export_u64(ex, i->id); +} + +static void export_type(Exporter *ex, Type *type) { +} + +static bool export_val(Exporter *ex, Value val, Type *type, Location where) { + export_type(ex, type); + switch (type->kind) { + case TYPE_VOID: break; + case TYPE_TYPE: + export_type(ex, val.type); + break; + case TYPE_PTR: + err_print(where, "Cannot export pointer."); + return false; + case TYPE_UNKNOWN: + case TYPE_EXPR: + assert(0); + return false; + } + return true; +} + +static bool export_expr(Exporter *ex, Expression *e) { + return true; +} + +enum { + DECL_EXPORT_NONE, + DECL_EXPORT_EXPR, + DECL_EXPORT_VAL +}; + +static bool export_decl(Exporter *ex, Declaration *d) { + if (d->type.kind == TYPE_UNKNOWN) { + err_print(d->where, "Can't export declaration of unknown type."); + return false; + } + export_location(ex, d->where); + size_t n_idents = arr_len(d->idents); + if (n_idents > 65535) { + err_print(d->where, "Too many identifiers in one declaration (the maximum is 65535)."); + return false; + } + export_u16(ex, (U16)arr_len(d->idents)); + arr_foreach(d->idents, Identifier, ident) { + export_ident(ex, *ident); + } + + U8 constness = 0; + if (d->flags & DECL_IS_CONST) constness = 1; + else if (d->flags & DECL_SEMI_CONST) constness = 2; + export_u8(ex, constness); + + U8 expr_kind = 0; + if (d->flags & DECL_HAS_EXPR) + expr_kind = DECL_EXPORT_EXPR; + if (d->flags & DECL_FOUND_VAL) + expr_kind = DECL_EXPORT_VAL; + + export_u8(ex, expr_kind); + + if (expr_kind == DECL_EXPORT_EXPR) { + if (!export_expr(ex, &d->expr)) + return false; + } else if (expr_kind == DECL_EXPORT_VAL) { + if (!export_val(ex, d->val, &d->type, d->where)) + return false; + } + return true; +} diff --git a/parse.c b/parse.c index 23bad30..60298de 100644 --- a/parse.c +++ b/parse.c @@ -1348,6 +1348,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { ++t->token; if (!token_is_kw(t->token, KW_LPAREN)) { err_print(t->token->where, "Expected ( to follow new."); + return false; } ++t->token; if (!parse_type(p, &e->new.type)) return false; @@ -1968,7 +1969,7 @@ static bool parse_file(Parser *p, ParsedFile *f) { return ret; } -#define PARSE_PRINT_LOCATION(l) /* fprintf(out, "[l%lu]", (unsigned long)(l).line); */ +#define PARSE_PRINT_LOCATION(l) /* fprintf(out, "[%lu:%lu]", (unsigned long)(l).line, (unsigned long)(l).pos); */ static void fprint_expr(FILE *out, Expression *e); static void fprint_stmt(FILE *out, Statement *s); diff --git a/test.toc b/test.toc index d5b1c53..ace8f11 100644 --- a/test.toc +++ b/test.toc @@ -1,2 +1,2 @@ #export foo ::= 7; -asdf, dsajkhf, sadjkfh ::= 5; \ No newline at end of file +asdf, dsajkhf, sadjkfh ::= 5; diff --git a/toc.c b/toc.c index a60d667..d0eb2a2 100644 --- a/toc.c +++ b/toc.c @@ -42,7 +42,7 @@ static void print_val(Value v, Type *t); #include "scope.c" #include "eval.c" #include "infer.c" -#include "export.c" +#include "package.c" #include "types.c" static bool cgen_decls_file(CGenerator *g, ParsedFile *f); static bool typedefs_file(CGenerator *g, ParsedFile *f); diff --git a/types.c b/types.c index ffebc1d..202fe3f 100644 --- a/types.c +++ b/types.c @@ -1525,7 +1525,6 @@ static bool types_expr(Typer *tr, Expression *e) { break; } if (!expr_must_lval(of)) { - err_print(e->where, "Cannot take address of non-lvalue."); /* FEATURE: better err */ return false; } if (of_type->kind == TYPE_TUPLE) { @@ -2028,7 +2027,7 @@ static bool types_decl(Typer *tr, Declaration *d) { err_print(d->where, "Declaration marked for exporting, but no package output was specified."); success = false; } else { - export_decl(tr->exptr, d); + success = export_decl(tr->exptr, d); } } } else { -- cgit v1.2.3