From ef6b1177539b51a9d1284b1546c54a57539ab643 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 15 Jul 2020 14:45:11 -0400 Subject: more type information --- main.c | 1 + parse.c | 5 +++++ test.toc | 14 ++++++++++---- types.c | 21 +++++++++++++++++---- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 7455951..8c9edb0 100644 --- a/main.c +++ b/main.c @@ -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 diff --git a/parse.c b/parse.c index aa6169c..6457dec 100644 --- a/parse.c +++ b/parse.c @@ -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)) { diff --git a/test.toc b/test.toc index 4ca9f52..5fcc2a5 100644 --- a/test.toc +++ b/test.toc @@ -91,7 +91,13 @@ write_type ::= fn(f : &File, t :: Type) { if t._is_template { io.fwrites(f, ""); } 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; +} diff --git a/types.c b/types.c index e156821..a196323 100644 --- a/types.c +++ b/types.c @@ -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)) { -- cgit v1.2.3