From 6b0b9db85bba00a1a416b2af14f5c2aa2a5eaf5e Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sat, 18 May 2019 10:01:30 +1000 Subject: [PATCH] oceani: print pre-declared constants when printing program. 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 --- csrc/oceani.mdc | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 58d5d32..c1cfd74 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -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. -- 2.43.0