From 2dda92f6daf61345fcfc9f482e6725bc5d456c9b Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 5 Feb 2020 21:13:05 -0500 Subject: switching to new ident system and wow it makes everything so much simpler --- data_structures.c | 4 +- eval.c | 108 ++++++++++++++--------------------------- identifiers.c | 58 +++++----------------- parse.c | 24 ++++++---- scope.c | 141 ------------------------------------------------------ toc.c | 1 - tokenizer.c | 15 +++--- types.h | 36 +++++++------- 8 files changed, 88 insertions(+), 299 deletions(-) delete mode 100644 scope.c diff --git a/data_structures.c b/data_structures.c index 7580a92..8efa124 100644 --- a/data_structures.c +++ b/data_structures.c @@ -88,12 +88,12 @@ static void arr_cleara_(void **arr, size_t size, Allocator *allocr) { } } -/* does NOT shrink the array! */ static void arr_set_len_(void **arr, size_t n, size_t item_sz) { if (n > arr_len(*arr)) { arr_resv_(arr, n, item_sz); } arr_hdr(*arr)->len = n; + /* OPTIM: shrink */ } static void arr_set_lena_(void **arr, size_t n, size_t item_sz, Allocator *a) { arr_resva_(arr, n, item_sz, a); @@ -229,6 +229,8 @@ static void arr_test(void) { } #endif +/* string hash table. entries are zero initialized (toc stuff depends on this!) */ + static U64 str_hash(const char *s, size_t len) { U32 x = 0xabcdef01; U32 y = 0x31415926; diff --git a/eval.c b/eval.c index 58fff10..ff17b98 100644 --- a/eval.c +++ b/eval.c @@ -732,13 +732,12 @@ static bool eval_expr_ptr_at_index(Evaluator *ev, Expression *e, void **ptr, Typ } static bool eval_address_of_ident(Identifier i, Type *type, Location where, void **ptr) { - IdentDecl *id = ident_decl(i); - if (!(id->flags & IDECL_HAS_VAL)) { - if (id->kind == IDECL_DECL) { - Declaration *decl = id->decl; + if (!(i->flags & IDENT_HAS_VAL)) { + if (i->decl_kind == IDECL_DECL) { + Declaration *decl = i->decl; if (!(decl->flags & DECL_IS_CONST) || !(decl->flags & DECL_FOUND_VAL)) goto runtime_var; - id->val = decl->val; - id->flags |= IDECL_HAS_VAL; + i->val = decl->val; + i->flags |= IDENT_HAS_VAL; } else { runtime_var: err_print(where, "Cannot take address of run time variable at compile time."); @@ -746,11 +745,11 @@ static bool eval_address_of_ident(Identifier i, Type *type, Location where, void } } if (type->kind == TYPE_ARR) - *ptr = id->val.arr; /* point directly to data */ + *ptr = i->val.arr; /* point directly to data */ else if (type->kind == TYPE_STRUCT) - *ptr = id->val.struc; + *ptr = i->val.struc; else - *ptr = &id->val; + *ptr = &i->val; return true; } @@ -838,12 +837,12 @@ static bool eval_address_of(Evaluator *ev, Expression *e, void **ptr) { static bool eval_set(Evaluator *ev, Expression *set, Value *to) { switch (set->kind) { case EXPR_IDENT: { - IdentDecl *id = ident_decl(set->ident); - if (!(id->flags & IDECL_HAS_VAL)) { + Identifier i = set->ident; + if (!(i->flags & IDENT_HAS_VAL)) { err_print(set->where, "Cannot set value of run time variable at compile time."); return false; } - id->val = *to; + i->val = *to; } break; case EXPR_UNARY_OP: switch (set->unary.op) { @@ -1075,17 +1074,16 @@ static bool val_is_nonnegative(Value *v, Type *t) { } static bool eval_ident(Evaluator *ev, Identifier ident, Value *v, Location where) { - IdentDecl *idecl = ident_decl(ident); - if (!idecl) { + if (ident->decl_kind == IDECL_NONE) { char *s = ident_to_str(ident); err_print(where, "Undeclared identifier: %s.", s); free(s); return false; } - bool is_decl = idecl->kind == IDECL_DECL; + bool is_decl = ident->decl_kind == IDECL_DECL; Declaration *d = NULL; if (is_decl) { - d = idecl->decl; + d = ident->decl; if (d->flags & DECL_FOREIGN) { if (!(d->flags & DECL_FOUND_VAL)) { #if COMPILE_TIME_FOREIGN_FN_SUPPORT @@ -1110,8 +1108,8 @@ static bool eval_ident(Evaluator *ev, Identifier ident, Value *v, Location where assert(d->type.flags & TYPE_IS_RESOLVED); } } - if (idecl->flags & IDECL_HAS_VAL) { - *v = idecl->val; + if (ident->flags & IDENT_HAS_VAL) { + *v = ident->val; } else if (is_decl && (d->flags & DECL_IS_CONST)) { if (!(d->flags & DECL_FOUND_VAL)) { assert(d->flags & DECL_HAS_EXPR); @@ -1310,6 +1308,22 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { } break; case EXPR_FOR: { ForExpr *fo = e->for_; + Value *index_val; + Value *value_val; + if (fo->index) { + Identifier i = fo->index; + i->flags |= IDENT_HAS_VAL; + index_val = &i->val; + } else { + index_val = NULL; + } + if (fo->value) { + Identifier i = fo->value; + i->flags |= IDENT_HAS_VAL; + value_val = &i->val; + } else { + value_val = NULL; + } if (fo->flags & FOR_IS_RANGE) { Value from, to; Value stepval; @@ -1323,23 +1337,6 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { if (fo->range.stepval) stepval = *fo->range.stepval; Value x = from; - Value *index_val; - Value *value_val; - if (!for_enter(e)) return false; - if (fo->index) { - IdentDecl *idecl = ident_decl(fo->index); - idecl->flags |= IDECL_HAS_VAL; - index_val = &idecl->val; - } else { - index_val = NULL; - } - if (fo->value) { - IdentDecl *idecl = ident_decl(fo->value); - idecl->flags |= IDECL_HAS_VAL; - value_val = &idecl->val; - } else { - value_val = NULL; - } bool step_is_negative = fo->range.stepval && !val_is_nonnegative(&stepval, &fo->type); if (index_val) index_val->i64 = 0; while (1) { @@ -1368,23 +1365,6 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { } else { Value of; if (!eval_expr(ev, fo->of, &of)) return false; - Value *index_val, *value_val; - Value i, val; - if (!for_enter(e)) return false; - if (fo->index) { - IdentDecl *idecl = ident_decl(fo->index); - idecl->flags |= IDECL_HAS_VAL; - index_val = &idecl->val; - } else { - index_val = &i; - } - if (fo->value) { - IdentDecl *idecl = ident_decl(fo->value); - idecl->flags |= IDECL_HAS_VAL; - value_val = &idecl->val; - } else { - value_val = &val; - } I64 len; bool uses_ptr = false; Type *of_type = &fo->of->type; @@ -1423,7 +1403,6 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { ++index_val->i64; } } - for_exit(e); } break; case EXPR_BLOCK: if (!eval_block(ev, &e->block, &e->type, v)) return false; @@ -1518,20 +1497,11 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { if (!eval_expr(ev, &e->call.arg_exprs[arg], &arg_val)) return false; Type *type = p->type.kind == TYPE_TUPLE ? &p->type.tuple[idx++] : &p->type; - IdentDecl *id = ident_add_decl(*i, p, &fn->body); - copy_val(NULL, &id->val, &arg_val, type); - id->flags |= IDECL_HAS_VAL; - - arr_set_len(&(*i)->decls, arr_len((*i)->decls)-1); - + copy_val(NULL, &(*i)->val, &arg_val, type); + (*i)->flags |= IDENT_HAS_VAL; ++arg; } } - arr_foreach(params, Declaration, p) { - arr_foreach(p->idents, Identifier, i) { - arr_set_len(&(*i)->decls, arr_len((*i)->decls)+1); - } - } arr_foreach(fn->ret_decls, Declaration, d) { int idx = 0; @@ -1540,18 +1510,14 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { if (!eval_expr(ev, &d->expr, &val)) return false; - if (!add_ident_decls(&fn->body, d, 0)) - return false; arr_foreach(d->idents, Identifier, i) { Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx] : &d->type; - IdentDecl *id = ident_decl(*i); if (d->flags & DECL_HAS_EXPR) { - id->val = d->type.kind == TYPE_TUPLE ? val.tuple[idx] : val; - id->flags |= IDECL_HAS_VAL; + (*i)->val = d->type.kind == TYPE_TUPLE ? val.tuple[idx] : val; } else { - id->flags |= IDECL_HAS_VAL; - id->val = val_zero(type); + (*i)->val = val_zero(type); } + (*i)->flags |= IDENT_HAS_VAL; ++idx; } } diff --git a/identifiers.c b/identifiers.c index ed5e675..229b2ce 100644 --- a/identifiers.c +++ b/identifiers.c @@ -56,7 +56,6 @@ static bool ident_str_eq_str(const char *s, const char *t) { } static inline bool ident_eq_str(Identifier i, const char *s) { - if (i->anonymous) return false; return ident_str_eq_str(i->str, s); } @@ -75,14 +74,6 @@ static IdentSlot **ident_slots_insert(IdentSlot **slots, char *s, size_t i) { return slot; } -static Identifier ident_new_anonymous(Identifiers *ids) { - U32 idx = rand_u32(ids->rseed); - ids->rseed = idx; - IdentSlot *slot = (IdentSlot *)str_hash_table_insert_anonymous_(&ids->table); - slot->anonymous = true; - return slot; -} - /* moves s to the char after the identifier */ /* inserts if does not exist. reads until non-ident char is found. */ /* advances past identifier */ @@ -94,12 +85,6 @@ static Identifier ident_insert(Identifiers *ids, char **s) { } static char *ident_to_str(Identifier i) { - if (i->anonymous) { - char *s = malloc(4); - strcpy(s, "???"); - return s; - } - char *str = err_malloc(i->len + 1); /* for some reason, GCC thinks that i->len is -1 when this is called from type_to_str_ (in release mode) TODO: test this now (some stuff was changed) @@ -122,15 +107,16 @@ static char *ident_to_str(Identifier i) { } +static inline void fprint_ident_str(FILE *out, char *s) { + char *p = s; + fwrite(s, 1, ident_str_len(&p), out); +} + static void fprint_ident(FILE *out, Identifier id) { fwrite(id->str, 1, id->len, out); } static void fprint_ident_debug(FILE *out, Identifier id) { - if (id->anonymous) { - fprintf(out, "???"); - return; - } fprint_ident(out, id); } @@ -142,11 +128,6 @@ static void print_ident(Identifier id) { /* reduced charset = a-z, A-Z, 0-9, _ */ static void fprint_ident_reduced_charset(FILE *out, Identifier id) { assert(id); - if (id->anonymous) { - /* hack to generate unique C identifiers */ - fprintf(out, "a%p__",(void *)id); - return; - } for (const char *s = id->str; is_ident(*s); ++s) { int c = (unsigned char)(*s); if (c > 127) { @@ -166,40 +147,21 @@ static Identifier ident_get(Identifiers *ids, char *s) { /* translate and insert if not already there */ static Identifier ident_translate_forced(Identifier i, Identifiers *to_idents) { - if (!i || i->anonymous) return NULL; char *p = i->str; return ident_insert(to_idents, &p); } /* translate but don't add it if it's not there */ static Identifier ident_translate(Identifier i, Identifiers *to_idents) { - if (!i || i->anonymous) return NULL; return ident_get(to_idents, i->str); } -static IdentDecl *ident_add_decl(Identifier i, struct Declaration *d, struct Block *b) { - IdentDecl *id_decl = arr_add(&i->decls); - id_decl->decl = d; - id_decl->scope = b; - id_decl->flags = 0; - id_decl->kind = IDECL_DECL; - return id_decl; -} - -static IdentDecl *ident_decl(Identifier i) { - return (IdentDecl *)arr_last(i->decls); -} - /* returns true if i and j are equal, even if they're not in the same table */ static bool ident_eq(Identifier i, Identifier j) { return i->len == j->len && memcmp(i->str, j->str, i->len) == 0; } static void idents_free(Identifiers *ids) { - arr_foreach(ids->table.slots, StrHashTableSlotPtr, slotp) { - IdentSlot *slot = *(IdentSlot **)slotp; - if (slot) arr_clear(&slot->decls); - } str_hash_table_free(&ids->table); } @@ -231,13 +193,15 @@ static int ident_index_in_decl(Identifier i, Declaration *d) { return -1; } -static Location idecl_where(IdentDecl *id) { +static Location ident_decl_location(Identifier i) { - switch (id->kind) { + switch (i->decl_kind) { case IDECL_DECL: - return id->decl->where; + return i->decl->where; case IDECL_EXPR: - return id->expr->where; + return i->expr->where; + case IDECL_NONE: + break; } assert(0); Location def = {0}; diff --git a/parse.c b/parse.c index eac681b..092675d 100644 --- a/parse.c +++ b/parse.c @@ -954,6 +954,12 @@ static int op_precedence(Keyword op) { } } +static Identifier parser_ident_insert(Parser *p, char *str) { + /* TODO TODO TODO NOW */ + (void)p; + return NULL; +} + static bool parse_expr(Parser *p, Expression *e, Token *end) { Tokenizer *t = p->tokr; @@ -1006,7 +1012,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { } break; case TOKEN_IDENT: e->kind = EXPR_IDENT; - e->ident = t->token->ident; + e->ident = parser_ident_insert(p, t->token->ident); break; case TOKEN_LITERAL_STR: e->kind = EXPR_LITERAL_STR; @@ -1163,14 +1169,14 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { && t->token[2].kind == TOKEN_IDENT && token_is_kw(t->token + 3, KW_COLON))))) { if (t->token->kind == TOKEN_IDENT) { - fo->value = t->token->ident; + fo->value = parser_ident_insert(p, t->token->ident); if (ident_eq_str(fo->value, "_")) /* ignore value */ fo->value = NULL; ++t->token; if (token_is_kw(t->token, KW_COMMA)) { ++t->token; if (t->token->kind == TOKEN_IDENT) { - fo->index = t->token->ident; + fo->index = parser_ident_insert(p, t->token->ident); if (ident_eq_str(fo->index, "_")) /* ignore index */ fo->index = NULL; ++t->token; @@ -1829,7 +1835,7 @@ static bool parse_decl(Parser *p, Declaration *d, DeclEndKind ends_with, U16 fla tokr_err(t, "Cannot declare non-identifier (%s).", token_kind_to_str(t->token->kind)); goto ret_false; } - *ident = t->token->ident; + *ident = parser_ident_insert(p, t->token->ident); ++t->token; if (token_is_kw(t->token, KW_COMMA)) { ++t->token; @@ -2172,7 +2178,7 @@ static void fprint_args(FILE *out, Argument *args) { arr_foreach(args, Argument, arg) { if (arg != args) fprintf(out, ", "); if (arg->name) { - fprint_ident_debug(out, arg->name); + fprint_ident_str(out, arg->name); fprintf(out, " = "); } fprint_expr(out, &arg->val); @@ -2445,12 +2451,10 @@ static inline Type *decl_type_at_index(Declaration *d, int i) { } static bool ident_is_definitely_const(Identifier i) { - IdentDecl *idecl = ident_decl(i); - assert(idecl); - Declaration *decl = idecl->decl; - if (idecl->kind != IDECL_DECL || !(decl->flags & DECL_IS_CONST)) + Declaration *decl = i->decl; + if (i->decl_kind != IDECL_DECL || !(decl->flags & DECL_IS_CONST)) return false; - if (decl->flags & DECL_FOREIGN) { + if (i->flags & DECL_FOREIGN) { if (decl->foreign.lib && COMPILE_TIME_FOREIGN_FN_SUPPORT) return true; else diff --git a/scope.c b/scope.c deleted file mode 100644 index d04d1d8..0000000 --- a/scope.c +++ /dev/null @@ -1,141 +0,0 @@ -/* - Copyright (C) 2019, 2020 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 . -*/ -enum { - SCOPE_CHECK_REDECL = 0x01, -}; - - -static void val_free(Value *v, Type *t); - -static bool DEBUG_UNDERSCORE(add_ident_decls)(SOURCE_LOCATION_PARAMS Block *b, Declaration *d, U16 flags) { - bool ret = true; - arr_foreach(d->idents, Identifier, ident) { - IdentDecl *decls = (*ident)->decls; - if ((flags & SCOPE_CHECK_REDECL) && arr_len(decls)) { - /* check that it hasn't been declared in this block */ - IdentDecl *prev = arr_last(decls); - if (prev->scope == b) { - err_print(d->where, "Re-declaration of identifier in the same block."); - info_print(prev->decl->where, "Previous declaration was here."); -#ifdef TOC_DEBUG - err_fprint(d->where.file->ctx, "First declaration was done by %s:%d, second was done by %s:%d.\n", prev->src_file, prev->src_line, src_file, src_line); -#endif - ret = false; - continue; - } - } - - IdentDecl *idecl = ident_add_decl(*ident, d, b); -#ifdef TOC_DEBUG - idecl->src_file = src_file; - idecl->src_line = src_line; -#else - (void)idecl; -#endif - } - return ret; -} - -static void remove_ident_decls(Block *b, Declaration *d) { - U64 i = 0; - bool is_tuple = d->type.kind == TYPE_TUPLE; - arr_foreach(d->idents, Identifier, ident) { - IdentSlot *id_info = *ident; - IdentDecl **decls = &id_info->decls; - IdentDecl *last_decl = arr_last(*decls); - if (last_decl && last_decl->scope == b) { - if ((last_decl->flags & IDECL_HAS_VAL) - /* don't free const vals (there's only one per decl) */ - && !(last_decl->decl->flags & DECL_IS_CONST)) { - val_free(&last_decl->val, is_tuple ? &d->type.tuple[i++] : &d->type); - } - arr_remove_last(decls); /* remove that declaration */ - } - } -} - -/* pass NULL for block for global scope */ -static bool block_enter(Block *b, Statement *stmts, U16 flags) { - bool ret = true; - arr_foreach(stmts, Statement, stmt) { - if (stmt->kind == STMT_DECL) { - Declaration *decl = &stmt->decl; - if (!add_ident_decls(b, decl, flags)) - ret = false; - } else if ((stmt->flags & STMT_TYPED) && stmt->kind == STMT_INCLUDE) { - if (!block_enter(b, stmt->inc.stmts, flags)) - return false; - } - } - return ret; -} - -static void block_exit(Block *b, Statement *stmts) { - if (b && (b->flags & BLOCK_IS_NMS)) - return; - - arr_foreach(stmts, Statement, stmt) { - if (stmt->kind == STMT_DECL) { - Declaration *decl = &stmt->decl; - remove_ident_decls(b, decl); - } else if (stmt->kind == STMT_INCLUDE) { - block_exit(b, stmt->inc.stmts); - } - } -} - -/* does NOT enter function's block body */ -static bool fn_enter(FnExpr *f, U16 flags) { - arr_foreach(f->params, Declaration, decl) - if (!add_ident_decls(&f->body, decl, flags)) - return false; - arr_foreach(f->ret_decls, Declaration, decl) - if (!add_ident_decls(&f->body, decl, flags)) - return false; - return true; -} - -static void fn_exit(FnExpr *f) { - arr_foreach(f->params, Declaration, decl) - remove_ident_decls(&f->body, decl); - arr_foreach(f->ret_decls, Declaration, decl) - remove_ident_decls(&f->body, decl); -} - -static bool for_enter(Expression *e) { - assert(e->kind == EXPR_FOR); - ForExpr *fo = e->for_; - if (fo->index && fo->index == fo->value) { - err_print(e->where, "The identifier for the index of a for loop must be different from the identifier for the value."); - return false; - } - if (fo->index) { - IdentDecl *id = arr_add(&fo->index->decls); - id->flags = 0; - id->kind = IDECL_EXPR; - id->scope = &fo->body; - id->expr = e; - } - if (fo->value) { - IdentDecl *id = arr_add(&fo->value->decls); - id->flags = 0; - id->kind = IDECL_EXPR; - id->scope = &fo->body; - id->expr = e; - } - return true; -} - -static void for_exit(Expression *e) { - assert(e->kind == EXPR_FOR); - ForExpr *fo = e->for_; - if (fo->index) { - arr_remove_last(&fo->index->decls); - } - if (fo->value) { - arr_remove_last(&fo->value->decls); - } -} diff --git a/toc.c b/toc.c index d60be8b..ce99c1d 100644 --- a/toc.c +++ b/toc.c @@ -114,7 +114,6 @@ static char *read_file_contents(Allocator *a, const char *filename, Location whe #include "identifiers.c" #include "tokenizer.c" #include "parse.c" -#include "scope.c" #include "foreign.c" #include "eval.c" #include "infer.c" diff --git a/tokenizer.c b/tokenizer.c index 723a4b3..d1b5921 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -87,10 +87,10 @@ static void fprint_token(FILE *out, Token *t) { case TOKEN_KW: fprintf(out, "keyword: %s", kw_to_str(t->kw)); break; - case TOKEN_IDENT: - fprintf(out, "identifier: %p: ", (void*)t->ident); - fprint_ident_debug(out, t->ident); - break; + case TOKEN_IDENT: { + fprintf(out, "identifier: "); + fprint_ident_str(out, t->ident); + } break; case TOKEN_LITERAL_NUM: fprintf(out, "number: "); switch (t->num.kind) { @@ -260,11 +260,10 @@ static void tokr_get_start_pos(Tokenizer *tokr, Token *t) { the allocator you pass it will be used for string literals, so it shouldn't be freed until everything is done */ -static void tokr_create(Tokenizer *t, Identifiers *idents, ErrCtx *err_ctx, Allocator *allocr) { +static void tokr_create(Tokenizer *t, ErrCtx *err_ctx, Allocator *allocr) { t->tokens = NULL; arr_resva(&t->tokens, 256, allocr); t->allocr = allocr; - t->idents = idents; t->err_ctx = err_ctx; } @@ -566,9 +565,9 @@ static bool tokenize_file(Tokenizer *t, File *file) { if (is_ident(*t->s)) { /* it's an identifier */ Token *token = tokr_add(t); - Identifier ident = ident_insert(t->idents, &t->s); token->kind = TOKEN_IDENT; - token->ident = ident; + token->ident = t->s; + while (is_ident(*t->s)) ++t->s; tokr_put_end_pos(t, token); continue; } diff --git a/types.h b/types.h index ba4bcc2..0667549 100644 --- a/types.h +++ b/types.h @@ -167,15 +167,22 @@ typedef union Value { } Value; enum { - IDECL_HAS_VAL = 0x01, + IDENT_HAS_VAL = 0x01 + }; typedef enum { + IDECL_NONE, IDECL_DECL, IDECL_EXPR } IdentDeclKind; -typedef struct IdentDecl { + +typedef struct IdentSlot { + char *str; + size_t len; + /* where this identifier was declared */ + IdentDeclKind decl_kind; union { struct Declaration *decl; struct Expression *expr; /* for example, this identifier is declared in a for expression */ @@ -183,15 +190,7 @@ typedef struct IdentDecl { struct Block *scope; /* NULL for file scope */ Value val; SOURCE_LOCATION - IdentDeclKind kind; U16 flags; -} IdentDecl; - -typedef struct IdentSlot { - char *str; - size_t len; - bool anonymous; /* is this identifier not part of a tree? */ - IdentDecl *decls; /* array of declarations of this identifier */ } IdentSlot; typedef struct StrHashTableSlot { @@ -336,7 +335,7 @@ typedef struct Token { union { Keyword kw; Directive direct; - Identifier ident; + char *ident; NumLiteral num; char chr; StrLiteral str; @@ -370,7 +369,7 @@ typedef struct Tokenizer { ErrCtx *err_ctx; U32 line; Token *token; /* token currently being processed */ - Identifiers *idents; + Identifiers *globals; } Tokenizer; @@ -482,6 +481,7 @@ enum { typedef struct Block { U8 flags; Location where; + Identifiers idents; struct Statement *stmts; struct Expression *ret_expr; /* the return expression of this block, e.g. {foo(); 3} => 3 NULL for no expression. */ } Block; @@ -699,7 +699,7 @@ const char *const builtin_val_names[BUILTIN_VAL_COUNT] = typedef struct Namespace { Block body; - Identifiers idents; /* these do not include local variables */ + Identifiers idents; Identifier associated_ident; /* if this is foo ::= nms { ... }, then associated_ident is foo; can be NULL */ struct { IdentID id; /* used as prefix if prefix is NULL */ @@ -771,7 +771,7 @@ typedef struct Expression { typedef struct Argument { Location where; - Identifier name; /* NULL = no name */ + char *name; /* NULL = no name */ Expression val; } Argument; @@ -893,7 +893,7 @@ typedef struct Evaluator { typedef struct Typer { Allocator *allocr; Evaluator *evalr; - Identifiers *idents; + Identifiers *globals; Expression **in_expr_decls; /* an array of expressions whose declarations (e.g. for **x := foo**) we are currently inside */ Declaration **in_decls; /* array of declarations we are currently inside */ Block *block; @@ -918,10 +918,6 @@ typedef struct CGenerator { FnExpr *fn; /* which function are we in? (NULL for none) - not used during decls */ Evaluator *evalr; Identifier main_ident; - Identifiers *idents; + Identifiers *globals; char *nms_prefix; /* dynamic (null-terminated) array of characters, the current namespace C prefix (e.g. "foo__bar__") */ } CGenerator; - -#ifdef TOC_DEBUG -#define add_ident_decls(b, d, flags) add_ident_decls_(__FILE__, __LINE__, b, d, flags) -#endif -- cgit v1.2.3