]> ocean-lang.org Git - ocean/commitdiff
oceani: print pre-declared constants when printing program.
authorNeilBrown <neil@brown.name>
Sat, 18 May 2019 00:01:30 +0000 (10:01 +1000)
committerNeilBrown <neil@brown.name>
Sat, 18 May 2019 00:01:30 +0000 (10:01 +1000)
The program is no more than just an 'exec'.  We need to print
the declarations too. As yet, there are only constants.

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

index 58d5d32009f297102359c4feabeed21c7ea4f15f..c1cfd745a35226da85060feafaeb747a0c94dfe7 100644 (file)
@@ -237,8 +237,10 @@ structures can be used.
                        fprintf(stderr, "oceani: no program found.\n");
                        context.parse_error = 1;
                }
-               if (context.prog && doprint)
+               if (context.prog && doprint) {
+                       ## print decls
                        print_exec(context.prog, 0, brackets);
+               }
                if (context.prog && doexec && !context.parse_error) {
                        if (!analyse_prog(context.prog, &context)) {
                                fprintf(stderr, "oceani: type error in program - not running.\n");
@@ -3593,6 +3595,12 @@ make it clear that they are constants.  Type can also be given: if
 not, the type will be determined during analysis, as with other
 constants.
 
+As the types constants are inserted at the head of a list, printing
+them in the same order that they were read is not straight forward.
+We take a quadratic approach here and count the number of constants
+(variables of depth 0), then count down from there, each time
+searching through for the Nth constant for decreasing N.
+
 ###### top level grammar
 
        DeclareConstant -> const Open ConstList Close
@@ -3642,6 +3650,38 @@ constants.
                }
        } }$
 
+###### print decls
+       {
+               struct variable *v;
+               int target = -1;
+
+               while (target != 0) {
+                       int i = 0;
+                       for (v = context.in_scope; v; v=v->in_scope)
+                               if (v->depth == 0) {
+                                       i += 1;
+                                       if (i == target)
+                                               break;
+                               }
+
+                       if (target == -1) {
+                               if (i)
+                                       printf("const:\n");
+                               target = i;
+                       } else {
+                               printf("    %.*s :: ", v->name->name.len, v->name->name.txt);
+                               type_print(v->val.type, stdout);
+                               printf(" = ");
+                               if (v->val.type == Tstr)
+                                       printf("\"");
+                               print_value(v->val);
+                               if (v->val.type == Tstr)
+                                       printf("\"");
+                               printf("\n");
+                               target -= 1;
+                       }
+               }
+       }
 
 ### Finally the whole program.