commit b17812965514313b0181ea905a1fe09f3fe3f7bb
parent a1134bf5b2b742729c80494d01da18b6576ca3d3
Author: m21c <ho*******@gmail.com>
Date: Sat, 18 Sep 2021 18:52:18 +0200
fixed problem with parsing declarations
Diffstat:
| M | compiler.c | | | 41 | +++++++++++++++++++++++++++++++++-------- |
1 file changed, 33 insertions(+), 8 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -2150,9 +2150,9 @@ readident(Source *source, int flags) {
decl = finddeclaration(source, source->currenv, source->tok.u.key);
gettok(source);
- if (source->currenv->kind == SRECORD) {
+ if (source->currenv->kind == SRECORD && !decl) {
int kind = getkind(source);
- if (!decl && kind != ',' && kind != ';' && kind != '\n' &&
+ if (kind != ',' && kind != ';' && kind != '\n' &&
kind != '\0' && kind != ')' && kind != ']' && kind != '}') {
decl = defertypedeclaration(source, key);
decl->loc = loc;
@@ -2186,12 +2186,12 @@ readident(Source *source, int flags) {
}
static Node *
-readrecord(Source *source, int indent, bool isunion) {
+readrecord(Source *source, bool isunion) {
Node *recordnode;
Decl *module;
+ int indent = source->lastindent;
int key = 0;
- indent = source->lastindent;
recordnode = tokennode(source, NULL);
recordnode->kind = getkind(source);
gettok(source);
@@ -2203,8 +2203,13 @@ readrecord(Source *source, int indent, bool isunion) {
error(getloc(source), "expected identifier");
}
/* recordnode->rhs = recordbody(source, indent, SSCOPE); */
+
+ /* TODO(m21c): check for new-line and only then read body */
recordnode->rhs = stmtlist(source, indent, SRECORD);
+ /* TODO(m21c): validate record body, extract declarations,
+ * compute size and align, resolve aliases */
+
module = makedecl(source, recordnode->lhs->u.key, DTYPE);
module->type = prim + TINT;
@@ -2356,7 +2361,7 @@ readatom(Source *source, int flags) {
case KSTRUCT:
case KUNION:
- lhs = readrecord(source, indent, source->tok.kind == KUNION);
+ lhs = readrecord(source, source->tok.kind == KUNION);
break;
case KNOT:
@@ -2605,6 +2610,9 @@ todeclaration(Node *curr, Node **ty) {
return curr;
}
+/* TODO(m21c): this is stupid! There should be a simpler way to parse the
+ * comma-expressions (comma-operator, param-list, declaration-list,
+ * type-tuples and expression-tuples) */
static Node *
exprlist(Source *source, bool isparam, Type *paramtype) {
Node *lhs;
@@ -2653,9 +2661,26 @@ exprlist(Source *source, bool isparam, Type *paramtype) {
gettok(source);
if (getkind(source) == 'I' && isdeclaration) {
- assert(paramtype);
- rhs = declaration(source, paramtype);
- typetuple = false;
+ Decl *decl = finddeclaration(source, source->currenv, source->tok.u.key);
+
+ if (decl && decl->kind == DTYPE) {
+ Type *type;
+ gettok(source);
+
+ /* FIXME(m21c): this still does not fix it
+ * entirely. in records with
+ * implicit forwad-declaration
+ * it wouldn't recognize it as
+ * Type. it would have to check
+ * it based on the next token. */
+
+ type = gettype(source, decl->type);
+ rhs = declaration(source, type);
+ } else {
+ assert(paramtype);
+ rhs = declaration(source, paramtype);
+ typetuple = false;
+ }
} else {
rhs = readexpr(source, PASSIGN);
typetuple &= rhs->kind == 'T';