+ static void structure_free_type(struct type *t)
+ {
+ int i;
+ for (i = 0; i < t->structure.nfields; i++)
+ free_value(t->structure.fields[i].init);
+ free(t->structure.fields);
+ }
+
+ static struct type structure_prototype = {
+ .prepare = structure_prepare,
+ .init = structure_init,
+ .free = structure_free,
+ .free_type = structure_free_type,
+ .print_type_decl = structure_print_type,
+ };
+
+###### exec type
+ Xfieldref,
+
+###### ast
+ struct fieldref {
+ struct exec;
+ struct exec *left;
+ int index;
+ struct text name;
+ };
+
+###### free exec cases
+ case Xfieldref:
+ free_exec(cast(fieldref, e)->left);
+ free(e);
+ break;
+
+###### variable grammar
+
+ | Variable . IDENTIFIER ${ {
+ struct fieldref *fr = new_pos(fieldref, $2);
+ fr->left = $<1;
+ fr->name = $3.txt;
+ fr->index = -2;
+ $0 = fr;
+ } }$
+
+###### print exec cases
+
+ case Xfieldref:
+ {
+ struct fieldref *f = cast(fieldref, e);
+ print_exec(f->left, -1, bracket);
+ printf(".%.*s", f->name.len, f->name.txt);
+ break;
+ }
+
+###### ast functions
+ static int find_struct_index(struct type *type, struct text field)
+ {
+ int i;
+ for (i = 0; i < type->structure.nfields; i++)
+ if (text_cmp(type->structure.fields[i].name, field) == 0)
+ return i;
+ return -1;
+ }
+
+###### propagate exec cases
+
+ case Xfieldref:
+ {
+ struct fieldref *f = cast(fieldref, prog);
+ struct type *st = propagate_types(f->left, c, ok, NULL, 0);
+
+ if (!st)
+ type_err(c, "error: unknown type for field access", f->left,
+ NULL, 0, NULL);
+ else if (st->prepare != structure_prepare)
+ type_err(c, "error: field reference attempted on %1, not a struct",
+ f->left, st, 0, NULL);
+ else if (f->index == -2) {
+ f->index = find_struct_index(st, f->name);
+ if (f->index < 0)
+ type_err(c, "error: cannot find requested field in %1",
+ f->left, st, 0, NULL);
+ }
+ if (f->index >= 0) {
+ struct type *ft = st->structure.fields[f->index].type;
+ if (!type_compat(type, ft, rules))
+ type_err(c, "error: have %1 but need %2", prog,
+ ft, rules, type);
+ return ft;
+ }
+ break;
+ }
+
+###### interp exec cases
+ case Xfieldref:
+ {
+ struct fieldref *f = cast(fieldref, e);
+ struct value *lleft = linterp_exec(f->left);
+ lrv = &lleft->structure.fields[f->index];
+ break;
+ }
+
+###### ast
+ struct fieldlist {
+ struct fieldlist *prev;
+ struct field f;
+ };
+
+###### ast functions
+ static void free_fieldlist(struct fieldlist *f)
+ {
+ if (!f)
+ return;
+ free_fieldlist(f->prev);
+ free_value(f->f.init);
+ free(f);
+ }
+
+###### top level grammar
+ DeclareStruct -> struct IDENTIFIER FieldBlock ${ {
+ struct type *t =
+ add_type(c, $2.txt, &structure_prototype);
+ int cnt = 0;
+ struct fieldlist *f;
+
+ for (f = $3; f; f=f->prev)
+ cnt += 1;
+
+ t->structure.nfields = cnt;
+ t->structure.fields = calloc(cnt, sizeof(struct field));
+ f = $3;
+ while (cnt > 0) {
+ cnt -= 1;
+ t->structure.fields[cnt] = f->f;
+ f->f.init = val_prepare(Tnone);
+ f = f->prev;
+ }
+ } }$
+ | DeclareStruct NEWLINE
+
+ $void
+ Newlines -> NEWLINE
+ | Newlines NEWLINE
+ Open -> {
+ | Newlines {
+ Close -> }
+ | Newlines }
+ $*fieldlist
+ FieldBlock -> Open FieldList } ${ $0 = $<2; }$
+ | Open SimpleFieldList } ${ $0 = $<2; }$
+ | : FieldList $$NEWLINE ${ $0 = $<2; }$
+
+ FieldList -> FieldLines ${ $0 = $<1; }$
+ | Newlines FieldLines ${ $0 = $<2; }$
+ FieldLines -> SimpleFieldListLine ${ $0 = $<1; }$
+ | FieldLines SimpleFieldListLine ${
+ $2->prev = $<1;
+ $0 = $<2;
+ }$
+
+ SimpleFieldListLine -> SimpleFieldList NEWLINE ${ $0 = $<1; }$
+ | SimpleFieldListLine NEWLINE ${ $0 = $<1; }$
+ | ERROR NEWLINE ${ tok_err(c, "Syntax error in struct field", &$1); }$
+
+ SimpleFieldList -> Field ${ $0 = $<1; }$
+ | SimpleFieldList ; Field ${
+ $3->prev = $<1;
+ $0 = $<3;
+ }$
+ | SimpleFieldList ; ${
+ $0 = $<1;
+ }$
+
+ Field -> IDENTIFIER : Type = Expression ${ {
+ int ok;
+
+ $0 = calloc(1, sizeof(struct fieldlist));
+ $0->f.name = $1.txt;
+ $0->f.type = $<3;
+ $0->f.init = val_prepare($0->f.type);
+ do {
+ ok = 1;
+ propagate_types($<5, c, &ok, $3, 0);
+ } while (ok == 2);
+ if (!ok)
+ c->parse_error = 1;
+ else
+ $0->f.init = interp_exec($5);
+ } }$
+ | IDENTIFIER : Type ${
+ $0 = calloc(1, sizeof(struct fieldlist));
+ $0->f.name = $1.txt;
+ $0->f.type = $<3;
+ $0->f.init = val_init($3);
+ }$
+
+###### forward decls
+ static void structure_print_type(struct type *t, FILE *f);
+
+###### value functions
+ static void structure_print_type(struct type *t, FILE *f)
+ {
+ int i;
+
+ fprintf(f, "struct %.*s:\n", t->name.len, t->name.txt);
+
+ for (i = 0; i < t->structure.nfields; i++) {
+ struct field *fl = t->structure.fields + i;
+ fprintf(f, " %.*s : ", fl->name.len, fl->name.txt);
+ type_print(fl->type, f);
+ if (fl->init.type->print) {
+ fprintf(f, " = ");
+ if (fl->init.type == Tstr)
+ fprintf(f, "\"");
+ print_value(fl->init);
+ if (fl->init.type == Tstr)
+ fprintf(f, "\"");
+ }
+ printf("\n");
+ }
+ }
+
+###### print type decls
+ {
+ struct type *t;
+ int target = -1;
+
+ while (target != 0) {
+ int i = 0;
+ for (t = context.typelist; t ; t=t->next)
+ if (t->print_type_decl) {
+ i += 1;
+ if (i == target)
+ break;
+ }
+
+ if (target == -1) {
+ target = i;
+ } else {
+ t->print_type_decl(t, stdout);
+ target -= 1;
+ }
+ }
+ }
+
+## Executables: the elements of code
+
+Each code element needs to be parsed, printed, analysed,