]> ocean-lang.org Git - ocean/commitdiff
oceani: allow a type to control how it is printed.
authorNeilBrown <neil@brown.name>
Sun, 28 Apr 2019 04:08:23 +0000 (14:08 +1000)
committerNeilBrown <neil@brown.name>
Wed, 1 May 2019 08:09:53 +0000 (18:09 +1000)
this is needed for e.g. printing the program when there
are declared types.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/oceani.mdc

index a0fd796bbcc0478881dc15cadc9948be3f7a1f44..edc3b124ff54fcaf7f39f72e6a4716dc38436494 100644 (file)
@@ -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) {