]> ocean-lang.org Git - ocean/commitdiff
oceani: move prepare_type handling to after the parse.
authorNeilBrown <neil@brown.name>
Tue, 30 Nov 2021 23:09:34 +0000 (10:09 +1100)
committerNeilBrown <neil@brown.name>
Fri, 3 Dec 2021 08:14:21 +0000 (19:14 +1100)
This will allow global constants to be used before they are declared.

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

index a4a9bd7b8b13415d5d9b70ac579fad456b92e09e..34c17b234efaefda84540c2560a045b1b6a5309a 100644 (file)
@@ -242,6 +242,7 @@ structures can be used.
 
                parse_oceani(ss->code, &context.config, dotrace ? stderr : NULL);
 
+               prepare_types(&context);
                if (!context.parse_error && !analyse_funcs(&context)) {
                        fprintf(stderr, "oceani: type error in program - not running.\n");
                        context.parse_error = 1;
@@ -914,6 +915,15 @@ which might be reported in error messages.
                        fprintf(f, "*Unknown*");                // NOTEST
        }
 
+       static void prepare_types(struct parse_context *c)
+       {
+               struct type *t;
+
+               for (t = c->typelist; t; t = t->next)
+                       if (t->prepare_type)
+                               t->prepare_type(c, t, 1);
+       }
+
 ###### forward decls
 
        static void free_value(struct type *type, struct value *v);
@@ -1858,7 +1868,7 @@ tell if it was set or not later.
        {
                if (!v->global) {
                        if (!c->local || !v->type)
-                               return NULL;                    // NOTEST
+                               return NULL;
                        if (v->frame_pos + v->type->size > c->local_size) {
                                printf("INVALID frame_pos\n");  // NOTEST
                                exit(2);                        // NOTEST
@@ -1884,7 +1894,7 @@ tell if it was set or not later.
                        t->prepare_type(c, t, 1);       // NOTEST
 
                if (c->global_size & (t->align - 1))
-                       c->global_size = (c->global_size + t->align) & ~(t->align-1);
+                       c->global_size = (c->global_size + t->align) & ~(t->align-1);   // NOTEST
                if (!v) {
                        v = &scratch;
                        v->type = t;
@@ -2236,14 +2246,14 @@ with a const size by whether they are prepared at parse time or not.
                struct value *vsize;
                mpz_t q;
                if (type->array.static_size)
-                       return; // NOTEST
+                       return;
                if (type->array.unspec && parse_time)
-                       return; // NOTEST
+                       return;
 
                if (type->array.vsize) {
                        vsize = var_value(c, type->array.vsize);
                        if (!vsize)
-                               return; // NOTEST
+                               return;
                        mpz_init(q);
                        mpz_tdiv_q(q, mpq_numref(vsize->num), mpq_denref(vsize->num));
                        type->array.size = mpz_get_si(q);
@@ -2735,9 +2745,6 @@ function will be needed.
                struct type *t =
                        add_type(c, $2.txt, &structure_prototype);
                t->structure.field_list = $<FB;
-               if (t->prepare_type)
-                       t->prepare_type(c, t, 1);
-
        } }$
 
        $*fieldlist