ref: 2f0d212525179efc4513902c78226af083e5af1b
parent: e6eb15dd1697de78a5be6bfff2dce13002c06b62
author: Ori Bernstein <[email protected]>
date: Sun May 13 08:09:36 EDT 2012
Add struct formatting to output.
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -67,7 +67,7 @@
/* already indented */
outname(k[i], fd);
ty = tystr(gettype(st, k[i]));
- fprintf(fd, " = %s", ty);
+ fprintf(fd, " = %s\n", ty);
free(ty);
}
free(k);
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -164,7 +164,7 @@
{lappend(&file->file.uses, &file->file.nuses, $1);}
| package
| tydef
- {die("tydef unimplemented");}
+ {puttype(file->file.globls, mkname($1.line, $1.name), $1.type);}
| TEndln
;
--- a/parse/node.c
+++ b/parse/node.c
@@ -210,6 +210,14 @@
return n;
}
+char *declname(Node *n)
+{
+ Node *name;
+ assert(n->type == Ndecl);
+ name = n->decl.sym->name;
+ return name->name.parts[name->name.nparts - 1];
+}
+
Type *decltype(Node *n)
{
assert(n->type == Ndecl);
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -318,6 +318,7 @@
Node *mklbl(int line, char *lbl);
/* node util functions */
+char *declname(Node *n);
Type *decltype(Node *n);
void addstmt(Node *file, Node *stmt);
void setns(Node *n, char *name);
@@ -343,5 +344,5 @@
Node *unpickle(FILE *fd);
/* convenience func */
-void lappend(void *l, size_t *len, void *n); /* ugly hack; nl is void* because void*** != @a*** */
+void lappend(void *l, size_t *len, void *n); /* ugly hack; nl is void* because void*** is incompatible with T*** */
void lfree(void *l, size_t *len);
--- a/parse/type.c
+++ b/parse/type.c
@@ -258,6 +258,43 @@
return p - buf;
}
+static int fmtstruct(char *buf, size_t len, Type *t)
+{
+ int i;
+ char *end, *p;
+ char *name, *ty;
+
+ p = buf;
+ end = p + len;
+ p += snprintf(p, end - p, "struct ");
+ for (i = 0; i < t->nsub; i++) {
+ name = declname(t->edecls[i]);
+ ty = tystr(decltype(t->edecls[i]));
+ p += snprintf(p, end - p, "%s:%s; ", name, ty);
+ free(ty);
+ }
+ p += snprintf(p, end - p, ";;");
+ return p - buf;
+}
+
+static int fmtunion(char *buf, size_t len, Type *t)
+{
+ int i;
+ *buf = 0;
+ for (i = 0; i < t->nsub; i++)
+ dump(t->sdecls[i], stdout);
+ return 0;
+}
+
+static int fmtenum(char *buf, size_t len, Type *t)
+{
+ int i;
+ *buf = 0;
+ for (i = 0; i < t->nsub; i++)
+ dump(t->sdecls[i], stdout);
+ return 0;
+}
+
static int tybfmt(char *buf, size_t len, Type *t)
{
char *p;
@@ -336,11 +373,9 @@
p += snprintf(p, end - p, "?"); /* indicate unresolved name. should not be seen by user. */
p += namefmt(p, end - p, t->name);
break;
- case Tystruct:
- case Tyunion:
- case Tyenum:
- snprintf(p, end - p, "TYPE ?");
- break;
+ case Tystruct: p += fmtstruct(p, end - p, t); break;
+ case Tyunion: p += fmtunion(p, end - p, t); break;
+ case Tyenum: p += fmtenum(p, end - p, t); break;
case Ntypes:
die("Ntypes is not a type");
break;