From 0848ff3bb41572d2e850360f220369ccd62c715b Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 27 Mar 2020 21:24:32 -0400 Subject: removed StructDef.constants --- copy.c | 7 ------- main.c | 1 - test.toc | 4 +++- tests/misc.toc | 11 +++++++++++ tests/misc_expected | 2 ++ types.c | 22 +++++++++++++++------- types.h | 1 - 7 files changed, 31 insertions(+), 17 deletions(-) diff --git a/copy.c b/copy.c index 88fb1ee..7c923ee 100644 --- a/copy.c +++ b/copy.c @@ -107,13 +107,6 @@ static void copy_struct(Copier *c, StructDef *out, StructDef *in) { *fout = *fin; fout->type = copy_type_(c, fin->type); } - out->constants = NULL; - size_t nconstants = arr_len(in->constants); - arr_set_lena(&out->constants, nconstants, c->allocr); - for (size_t i = 0; i < nconstants; ++i) { - copy_decl(c, out->constants[i] = allocr_malloc(c->allocr, sizeof *out->constants[i]), - in->constants[i]); - } } size_t nparams = arr_len(in->params); out->params = NULL; diff --git a/main.c b/main.c index a54d5c0..4e3e81a 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,6 @@ /* TODO: -do we really need StructDef.constants all big Statement members should be pointers use - use with a decl, e.g. use p : Point; diff --git a/test.toc b/test.toc index 6e9af49..77708b6 100644 --- a/test.toc +++ b/test.toc @@ -1,10 +1,12 @@ s ::= fn(t:: Type) Type { struct { + c :: t = 7 as t; m: t; } } main ::= fn() { o: s(int); - x := o.m; + x := o["m"]; + y ::= o["c"]; } diff --git a/tests/misc.toc b/tests/misc.toc index a9f6dc1..26ecf3e 100644 --- a/tests/misc.toc +++ b/tests/misc.toc @@ -25,5 +25,16 @@ main ::= fn() { a[1] = y; a[2] = z; }; + + s ::= struct { + foo, e: int; + bar ::= 3; + baz: float; + } + + p: s; + p.e = 100; + io.puti(p["bar"]); + io.puti(p["e"]); }; diff --git a/tests/misc_expected b/tests/misc_expected index 92e0278..e35eecf 100644 --- a/tests/misc_expected +++ b/tests/misc_expected @@ -2,3 +2,5 @@ Hello! 116 6 0 +3 +100 diff --git a/types.c b/types.c index 3ecb90b..a9e3ef6 100644 --- a/types.c +++ b/types.c @@ -780,7 +780,6 @@ static Status add_block_to_struct(Typer *tr, Block *b, StructDef *s, Statement * err_print(d->where, "struct members can't be inferred."); return false; } - *(Declaration **)typer_arr_add(tr, &s->constants) = d; *(Statement *)typer_arr_add(tr, new_stmts) = *stmt; } else { if (flags & DECL_SEMI_CONST) { @@ -887,7 +886,6 @@ static Status type_resolve(Typer *tr, Type *t, Location where) { if (!types_block(tr, &s->body)) return false; s->fields = NULL; - s->constants = NULL; Statement *new_stmts = NULL; if (!add_block_to_struct(tr, &s->body, s, &new_stmts)) return false; @@ -2889,9 +2887,6 @@ static Status types_expr(Typer *tr, Expression *e) { return false; } Value field_name; - - /* replace with BINARY_DOT */ - e->binary.op = BINARY_DOT; if (!eval_expr(tr->evalr, rhs, &field_name)) return false; /* get the field, if it exists */ @@ -2899,8 +2894,21 @@ static Status types_expr(Typer *tr, Expression *e) { field_name.slice.data, (size_t)field_name.slice.n); if (ident_is_declared(ident)) { assert(ident->decl_kind == IDECL_DECL); - Field *f = ident->decl->field + ident_index_in_decl(ident, ident->decl); - e->binary.dot.field = f; + Declaration *decl = ident->decl; + if (decl->flags & DECL_IS_CONST) { + /* replace with value of struct constant */ + e->kind = EXPR_VAL; + assert(decl->flags & DECL_FOUND_VAL); + int idx = ident_index_in_decl(ident, decl); + *t = *decl_type_at_index(decl, idx); + copy_val(tr->allocr, &e->val, *decl_val_at_index(decl, idx), t); + } else { + /* replace with BINARY_DOT */ + e->binary.op = BINARY_DOT; + Field *f = decl->field + ident_index_in_decl(ident, decl); + e->binary.dot.field = f; + *t = *f->type; + } } else { char *fstr = err_malloc((size_t)(field_name.slice.n + 1)); memcpy(fstr, field_name.slice.data, (size_t)field_name.slice.n); diff --git a/types.h b/types.h index c71b5fd..a242ee7 100644 --- a/types.h +++ b/types.h @@ -518,7 +518,6 @@ enum { typedef struct StructDef { /* these two only exist after resolving (before then, it's scope.stmts) */ Field *fields; - struct Declaration **constants; Location where; U8 flags; -- cgit v1.2.3