diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-27 21:24:32 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-27 21:24:32 -0400 |
commit | 0848ff3bb41572d2e850360f220369ccd62c715b (patch) | |
tree | f94fc3f9a549fce6ce226729d47baf0e5285dd01 | |
parent | 7cf5a8735ecbd37faf5e9f740d5f1a49939eea07 (diff) |
removed StructDef.constants
-rw-r--r-- | copy.c | 7 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | test.toc | 4 | ||||
-rw-r--r-- | tests/misc.toc | 11 | ||||
-rw-r--r-- | tests/misc_expected | 2 | ||||
-rw-r--r-- | types.c | 22 | ||||
-rw-r--r-- | types.h | 1 |
7 files changed, 31 insertions, 17 deletions
@@ -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; @@ -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; @@ -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 @@ -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); @@ -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; |