From: NeilBrown Date: Sun, 28 Apr 2019 04:08:23 +0000 (+1000) Subject: oceani: allow a type to control how it is printed. X-Git-Tag: JamisonCreek-3~34 X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=d2f0bd7a9928b46f503c2409a6ead18e42b16a80 oceani: allow a type to control how it is printed. this is needed for e.g. printing the program when there are declared types. Signed-off-by: NeilBrown --- diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index a0fd796..edc3b12 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -378,17 +378,10 @@ context so indicate that parsing failed. case '%': fputc(*fmt, stderr); break; default: fputc('?', stderr); break; case '1': - if (t1) - fprintf(stderr, "%.*s", t1->name.len, t1->name.txt); - else - fputs("*unknown*", stderr); + type_print(t1, stderr); break; case '2': - if (t2) - fprintf(stderr, "%.*s", t2->name.len, t2->name.txt); - else - fputs("*unknown*", stderr); - break; + type_print(t2, stderr); break; ## format cases } @@ -450,6 +443,7 @@ which are often passed around by value. struct value (*prepare)(struct type *type); struct value (*parse)(struct type *type, char *str); void (*print)(struct value val); + void (*print_type)(struct type *type, FILE *f); int (*cmp_order)(struct value v1, struct value v2); int (*cmp_eq)(struct value v1, struct value v2); struct value (*dup)(struct value val); @@ -520,6 +514,18 @@ which are often passed around by value. return require == have; } + static void type_print(struct type *type, FILE *f) + { + if (!type) + fputs("*unknown*type*", f); + else if (type->name.len) + fprintf(f, "%.*s", type->name.len, type->name.txt); + else if (type->print_type) + type->print_type(type, f); + else + fputs("*invalid*type*", f); + } + static struct value val_prepare(struct type *type) { struct value rv; @@ -2532,16 +2538,18 @@ it is declared, and error will be raised as the name is created as do_indent(indent, ""); print_exec(b->left, indent, 0); if (cast(var, b->left)->var->constant) { - if (v->where_decl == v->where_set) - printf("::%.*s ", v->val.type->name.len, - v->val.type->name.txt); - else + if (v->where_decl == v->where_set) { + printf("::"); + type_print(v->val.type, stdout); + printf(" "); + } else printf(" ::"); } else { - if (v->where_decl == v->where_set) - printf(":%.*s ", v->val.type->name.len, - v->val.type->name.txt); - else + if (v->where_decl == v->where_set) { + printf(":"); + type_print(v->val.type, stdout); + printf(" "); + } else printf(" :"); } if (b->right) {