diff options
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parse.c | 5 | ||||
-rw-r--r-- | test.toc | 14 | ||||
-rw-r--r-- | types.c | 21 |
4 files changed, 33 insertions, 8 deletions
@@ -8,6 +8,7 @@ see development.md for development information @TODO: +fix problem with a : b; #include "file_which_defines_X"; b ::= struct { a: X; } see note in types.c : we probably shouldn't go to the trouble of evaluating this (just do this stuff if it's an ident) ensure that in s[a:b] s is an l-value if it's an array make sure you can't pass a function template or a tuple to varargs @@ -2154,6 +2154,11 @@ static Status parse_decl(Parser *p, Declaration *d, U16 flags) { t->token = end; // move to ; goto ret_false; } + if (e->kind == EXPR_TYPE + && e->typeval->kind == TYPE_STRUCT + && p->block == NULL) { + e->typeval->struc->name = d->idents[0]; + } if ((flags & DECL_CAN_END_WITH_SEMICOLON) && end > t->tokens && token_is_kw(end - 1, KW_RBRACE)) { // allow semicolon to be ommitted, e.g. f ::= fn() {} } else if (ends_decl(t->token, flags)) { @@ -91,7 +91,13 @@ write_type ::= fn(f : &File, t :: Type) { if t._is_template { io.fwrites(f, "<struct template>"); } else { - io.fwrites(f, "struct { "); + io.fwrites(f, "struct "); + name ::= t._name; + if name { + io.fwrites(f, name); + io.fwritec(f, ' '); + } + io.fwrites(f, "{ "); member_names ::= t._member_names; member_types ::= t._member_types; for i ::= 0.,member_names.len { @@ -153,12 +159,12 @@ main ::= fn() { xx ::= fn() (int, int) { return 3, 5; } - Point2D ::= struct(t :: Type) { - x, y : t; - } Point ::= struct(t :: Type) { use p: Point2D(t); z: float; } } main(); +Point2D ::= struct(t :: Type) { + x, y : t; +} @@ -2958,6 +2958,23 @@ static Status types_expr(Typer *tr, Expression *e) { e->val.slice.len = (I64)nfields; e->val.slice.data = type_ptrs; break; + } else if (str_eq_cstr(member, "_name")) { + if (ltype->kind != TYPE_STRUCT) { + err_print(e->where, "This type doesn't have a '_name' member (only structs do)."); + return false; + } + e->kind = EXPR_VAL; + construct_resolved_slice_of_builtin(tr->allocr, t, BUILTIN_CHAR); + Slice *s = &e->val.slice; + Identifier name = ltype->struc->name; + if (name) { + s->data = name->str; + s->len = (I64)name->len; + } else { + s->data = NULL; + s->len = 0; + } + break; } } @@ -3232,10 +3249,6 @@ static Status types_decl(Typer *tr, Declaration *d) { } else if (e->kind == EXPR_NMS) { if (typer_is_at_top_level(tr)) e->nms->associated_ident = d->idents[0]; - } else if (e->kind == EXPR_TYPE - && e->typeval->kind == TYPE_STRUCT - && tr->fn == NULL) { - e->typeval->struc->name = d->idents[0]; } if (!types_expr(tr, e)) { |