summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--err.c10
-rw-r--r--identifiers.c20
-rw-r--r--main.c2
-rw-r--r--package.c15
-rw-r--r--test.toc22
-rw-r--r--tokenizer.c11
-rw-r--r--types.c2
-rw-r--r--types.h4
8 files changed, 38 insertions, 48 deletions
diff --git a/err.c b/err.c
index 94d6d64..2472d3d 100644
--- a/err.c
+++ b/err.c
@@ -114,14 +114,12 @@ static void warn_print_header_(Location where) {
}
}
-
-/*
- before_pos and after_pos could be things like TEXT_ERR_START or TEXT_ERR_END.
- they will not be printed if color output is disabled.
-*/
static void err_print_pos_text(ErrCtx *ctx, U32 start_pos, U32 end_pos) {
- (void)end_pos; /* TODO */
char *str = ctx->str;
+ if (!str) {
+ return;
+ }
+
char *start = str + start_pos;
char *line_start = start;
char *end = str + end_pos;
diff --git a/identifiers.c b/identifiers.c
index e960a14..94d65f0 100644
--- a/identifiers.c
+++ b/identifiers.c
@@ -27,17 +27,25 @@ static int isident(int c) {
}
+static Identifier ident_new_anonymous(Identifiers *ids) {
+ IdentTree *node = block_arr_add(&ids->trees);
+ memset(node, 0, sizeof *node);
+ node->anonymous = true;
+ return node;
+}
+
/* used internally to allocate identifiers */
static Identifier ident_new(Identifiers *ids, Identifier parent, unsigned char index_in_parent) {
- IdentTree *tree = block_arr_add(&ids->trees);
- memset(tree, 0, sizeof *tree); /* use zero value of IdentTree */
- tree->parent = parent;
+ IdentTree *node = ident_new_anonymous(ids);
+ node->parent = parent;
if (parent)
- tree->depth = (uint16_t)(parent->depth + 1);
- tree->index_in_parent = index_in_parent;
- return tree;
+ node->depth = (uint16_t)(parent->depth + 1);
+ node->index_in_parent = index_in_parent;
+ node->anonymous = false;
+ return node;
}
+
/* Initialize Identifiers. */
static void idents_create(Identifiers *ids) {
block_arr_create(&ids->trees, 10, sizeof(IdentTree)); /* blocks of 1 << 10 = 1024 */
diff --git a/main.c b/main.c
index 0259f07..5c0e800 100644
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@
/*
TODO:
+test EOF error
packages
---
X ::= newtype(int); or something
@@ -148,7 +149,6 @@ int main(int argc, char **argv) {
block_exit(NULL, f.stmts); /* exit global scope */
- tokr_free(&t);
free(contents - 1); /* -1 because we put a 0 byte at the beginning */
allocr_free_all(&main_allocr);
evalr_free(&ev);
diff --git a/package.c b/package.c
index 24074b7..1ad2c4f 100644
--- a/package.c
+++ b/package.c
@@ -176,12 +176,17 @@ 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) {
+static bool import_pkg(Allocator *allocr, Package *p, FILE *f, const char *fname, Identifiers *parent_idents, ErrCtx *parent_ctx, Location where) {
Importer i = {0};
+ ErrCtx *err_ctx = &i.err_ctx;
idents_create(&p->idents);
i.pkg = p;
i.in = f;
i.allocr = allocr;
+ *err_ctx = *parent_ctx;
+ err_ctx->filename = fname;
+ err_ctx->instance_stack = NULL;
+ err_ctx->str = NULL;
/* read header */
U8 toc[3];
toc[0] = import_u8(&i);
@@ -202,12 +207,12 @@ static bool import_pkg(Allocator *allocr, Package *p, FILE *f, const char *fname
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);
+ p->name = ident_get(parent_idents, 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);
+ err_ctx->str = code;
}
long code_offset = ftell(f);
if (ident_offset > LONG_MAX) {
@@ -729,8 +734,10 @@ static bool import_footer(Importer *im) {
}
for (i = 1; i <= max_ident_id; ++i) {
if (!im->ident_map[i]) {
- /* TODO (maybe generate random identifier -- or just use malloc and set a certain field to indicate it's not part of a tree?) */
+ im->ident_map[i] = ident_new_anonymous(&im->pkg->idents);
}
}
+
+
return true;
}
diff --git a/test.toc b/test.toc
index 16a00d4..e334817 100644
--- a/test.toc
+++ b/test.toc
@@ -6,26 +6,6 @@ putf ::= fn(x: float) {
#C("printf(\"%f\\n\", (double)x);
");
};
-// point ::= pkg "point";
-
-// Foo ::= struct {
- // b: Bar;
-// };
-
-// Bar ::= struct {
- // f: Foo;
-// };
-
-
-Bar ::= struct { f: &Foo; };
-
-Foo ::= struct { b: Bar; };
-
-
-
+point ::= pkg "point";
main ::= fn() {
-f::=fn(t::Type)Type {t};
-x:f(int) = 5;
-
- z : fn(int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int,int, int)=5;
}; \ No newline at end of file
diff --git a/tokenizer.c b/tokenizer.c
index d7a7b1e..b3e300b 100644
--- a/tokenizer.c
+++ b/tokenizer.c
@@ -228,7 +228,7 @@ static inline void *tokr_malloc(Tokenizer *t, size_t bytes) {
}
static Token *tokr_add(Tokenizer *t) {
- Token *token = arr_add(&t->tokens);
+ Token *token = arr_adda(&t->tokens, t->allocr);
tokr_put_start_pos(t, token);
return token;
}
@@ -295,7 +295,7 @@ static bool tokenize_string(Tokenizer *t, char *str) {
tokr_put_end_pos(t, &token);
token.kind = TOKEN_DIRECT;
token.direct = direct;
- *(Token *)arr_add(&t->tokens) = token;
+ *(Token *)arr_adda(&t->tokens, t->allocr) = token;
continue;
}
--t->s; /* go back to # */
@@ -312,7 +312,7 @@ static bool tokenize_string(Tokenizer *t, char *str) {
tokr_put_end_pos(t, &token);
token.kind = TOKEN_KW;
token.kw = kw;
- *(Token *)arr_add(&t->tokens) = token;
+ *(Token *)arr_adda(&t->tokens, t->allocr) = token;
continue;
}
}
@@ -559,8 +559,3 @@ static void tokr_skip_semicolon(Tokenizer *t) {
static inline void tokr_skip_to_eof(Tokenizer *t) {
while (t->token->kind != TOKEN_EOF) ++t->token; /* move to end of file */
}
-
-/* only frees tokens, not string literals (because those are on the allocator). */
-static void tokr_free(Tokenizer *t) {
- arr_clear(&t->tokens);
-}
diff --git a/types.c b/types.c
index 78aa183..4544474 100644
--- a/types.c
+++ b/types.c
@@ -1000,7 +1000,7 @@ static bool types_expr(Typer *tr, Expression *e) {
free(filename);
return false;
}
- if (!import_pkg(tr->allocr, pkg, fp, filename, e->where)) {
+ if (!import_pkg(tr->allocr, pkg, fp, filename, tr->idents, tr->err_ctx, e->where)) {
free(filename);
return false;
}
diff --git a/types.h b/types.h
index 513741b..7ea2c25 100644
--- a/types.h
+++ b/types.h
@@ -83,7 +83,7 @@ typedef U32 IdentID; /* identifier ID for cgen (anonymous variables). not to be
typedef struct ErrCtx {
const char *filename;
- char *str; /* file contents */
+ char *str; /* file contents, or NULL if none are available */
bool enabled;
bool color_enabled;
bool have_errored;
@@ -183,6 +183,7 @@ typedef struct IdentTree {
uint16_t depth;
unsigned char index_in_parent; /* index of this in .parent.children */
bool export_name; /* is this identifier's name important? */
+ bool anonymous; /* is this identifier not part of a tree? */
U64 export_id; /* 0 if there's no exported identifier here, otherwise unique positive integer associated with this identifier */
struct Package *pkg; /* NULL if this is not associated with a package */
struct IdentTree *parent;
@@ -802,6 +803,7 @@ typedef struct Importer {
Package *pkg;
Allocator *allocr;
Identifier *ident_map; /* [i] = value of identifier with ID i */
+ ErrCtx err_ctx;
} Importer;
typedef struct CGenerator {