fprintf(stderr, "oceani: no main function found.\n");
context.parse_error = 1;
}
+ if (context.prog && !context.parse_error) {
+ if (!analyse_prog(context.prog, &context)) {
+ fprintf(stderr, "oceani: type error in program - not running.\n");
+ context.parse_error = 1;
+ }
+ }
if (context.prog && doprint) {
## print const decls
## print type 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");
- exit(1);
- }
+ if (context.prog && doexec && !context.parse_error)
interp_prog(&context, context.prog, argc - optind, argv+optind);
- }
free_exec(context.prog);
while (s) {
$void
OpenScope -> ${ scope_push(c); }$
- ClosePara -> ${ var_block_close(c, CloseParallel); }$
Each variable records a scope depth and is in one of four states:
enclosed the declaration, and that has closed.
- "conditionally in scope". The "in scope" block and all parallel
- scopes have closed, and no further mention of the name has been
- seen. This state includes a secondary nest depth which records the
- outermost scope seen since the variable became conditionally in
- scope. If a use of the name is found, the variable becomes "in
- scope" and that secondary depth becomes the recorded scope depth.
- If the name is declared as a new variable, the old variable becomes
- "out of scope" and the recorded scope depth stays unchanged.
+ scopes have closed, and no further mention of the name has been seen.
+ This state includes a secondary nest depth (`min_depth`) which records
+ the outermost scope seen since the variable became conditionally in
+ scope. If a use of the name is found, the variable becomes "in scope"
+ and that secondary depth becomes the recorded scope depth. If the
+ name is declared as a new variable, the old variable becomes "out of
+ scope" and the recorded scope depth stays unchanged.
- "out of scope". The variable is neither in scope nor conditionally
in scope. It is permanently out of scope now and can be removed from
{
struct variable *v;
- if (primary->merged)
- // shouldn't happen
- primary = primary->merged; // NOTEST
+ primary = primary->merged;
for (v = primary->previous; v; v=v->previous)
if (v == secondary || v == secondary->merged ||
v->merged == secondary ||
- (v->merged && v->merged == secondary->merged)) {
+ v->merged == secondary->merged) {
v->scope = OutScope;
v->merged = primary;
}
v->previous = b->var;
b->var = v;
v->name = b;
+ v->merged = v;
v->min_depth = v->depth = c->scope_depth;
v->scope = InScope;
v->in_scope = c->in_scope;
static void var_block_close(struct parse_context *c, enum closetype ct)
{
- /* Close off all variables that are in_scope */
+ /* Close off all variables that are in_scope.
+ * Some variables in c->scope may already be not-in-scope,
+ * such as when a PendingScope variable is hidden by a new
+ * variable with the same name.
+ * So we check for v->name->var != v and drop them.
+ * If we choose to make a variable OutScope, we drop it
+ * immediately too.
+ */
struct variable *v, **vp, *v2;
scope_pop(c);
for (vp = &c->in_scope;
- v = *vp, v && v->depth > c->scope_depth && v->min_depth > c->scope_depth;
- ) {
- if (v->name->var == v) switch (ct) {
+ (v = *vp) && v->min_depth > c->scope_depth;
+ (v->scope == OutScope || v->name->var != v)
+ ? (*vp = v->in_scope, 0)
+ : ( vp = &v->in_scope, 0)) {
+ v->min_depth = c->scope_depth;
+ if (v->name->var != v)
+ /* This is still in scope, but we haven't just
+ * closed the scope.
+ */
+ continue;
+ switch (ct) {
case CloseElse:
case CloseParallel: /* handle PendingScope */
switch(v->scope) {
case InScope:
case CondScope:
if (c->scope_stack->child_count == 1)
+ /* first among parallel branches */
v->scope = PendingScope;
else if (v->previous &&
v->previous->scope == PendingScope)
+ /* all previous branches used name */
v->scope = PendingScope;
else if (v->type == Tlabel) // UNTESTED
+ /* Labels remain pending even when not used */
v->scope = PendingScope; // UNTESTED
- else if (v->name->var == v) // UNTESTED
- v->scope = OutScope; // UNTESTED
if (ct == CloseElse) {
/* All Pending variables with this name
* are now Conditional */
}
break;
case PendingScope:
- for (v2 = v;
- v2 && v2->scope == PendingScope;
- v2 = v2->previous)
- if (v2->type != Tlabel)
- v2->scope = OutScope;
- break;
- case OutScope: break; // UNTESTED
+ /* Not possible as it would require
+ * parallel scope to be nested immediately
+ * in a parallel scope, and that never
+ * happens.
+ */
+ case OutScope:
+ /* Not possible as we already tested for
+ * OutScope
+ */
+ abort(); // NOTEST
}
break;
case CloseSequential:
v2 = v2->previous)
if (v2->type == Tlabel) {
v2->scope = CondScope;
- v2->min_depth = c->scope_depth;
} else
v2->scope = OutScope;
break;
}
break;
}
- if (v->scope == OutScope || v->name->var != v)
- *vp = v->in_scope;
- else
- vp = &v->in_scope;
}
}
struct variable *v;
for (v = b->var; v; v = v->previous) {
struct type *t = v->type;
- if (v->merged && v->merged != v)
+ if (v->merged != v)
continue;
if (v->global)
continue;
rvtype = ret.type = Tnone;
if (!e) {
- ret.lval = lrv; // UNTESTED
- ret.rval = rv; // UNTESTED
- return ret; // UNTESTED
+ ret.lval = lrv;
+ ret.rval = rv;
+ return ret;
}
switch(e->type) {
type_err(c, "%d:BUG: no variable!!", prog, NULL, 0, NULL); // NOTEST
return Tnone; // NOTEST
}
- if (v->merged)
- v = v->merged;
+ v = v->merged;
if (v->constant && (rules & Rnoconstant)) {
type_err(c, "error: Cannot assign to a constant: %v",
prog, NULL, 0, NULL);
struct var *var = cast(var, e);
struct variable *v = var->var;
- if (v->merged)
- v = v->merged; // UNTESTED
+ v = v->merged;
lrv = var_value(c, v);
rvtype = v->type;
break;
do_indent(indent, "");
print_exec(b->left, indent, bracket);
if (cast(var, b->left)->var->constant) {
+ printf("::");
if (v->where_decl == v->where_set) {
- printf("::");
type_print(v->type, stdout);
printf(" ");
- } else
- printf(" ::");
+ }
} else {
+ printf(":");
if (v->where_decl == v->where_set) {
- printf(":");
type_print(v->type, stdout);
printf(" ");
- } else
- printf(" :");
+ }
}
if (b->right) {
printf("= ");
{
struct variable *v = cast(var, b->left)->var;
struct value *val;
- if (v->merged)
- v = v->merged;
+ v = v->merged;
val = var_value(c, v);
free_value(v->type, val);
if (v->type->prepare_type)
`Rboolok` flag which is passed to `propagate_types()`.
The `cond_statement` cannot fit into a `binode` so a new `exec` is
-defined.
+defined. As there are two scopes which cover multiple parts - one for
+the whole statement and one for "while" and "do" - and as we will use
+the 'struct exec' to track scopes, we actually need two new types of
+exec. One is a `binode` for the looping part, the rest is the
+`cond_statement`. The `cond_statement` will use an auxilliary `struct
+casepart` to track a list of case parts.
+
+###### Binode types
+ Loop
###### exec type
Xcond_statement,
};
struct cond_statement {
struct exec;
- struct exec *forpart, *condpart, *dopart, *thenpart, *elsepart;
+ struct exec *forpart, *condpart, *thenpart, *elsepart;
+ struct binode *looppart;
struct casepart *casepart;
};
return;
free_exec(s->forpart);
free_exec(s->condpart);
- free_exec(s->dopart);
+ free_exec(s->looppart);
free_exec(s->thenpart);
free_exec(s->elsepart);
free_casepart(s->casepart);
// may or may not end with EOL
// WhilePart and IfPart include an appropriate Suffix
- // Both ForPart and Whilepart open scopes, and CondSuffix only
- // closes one - so in the first branch here we have another to close.
+ // ForPart, SwitchPart, and IfPart open scopes, o we have to close
+ // them. WhilePart opens and closes its own scope.
CondStatement -> ForPart OptNL ThenPart OptNL WhilePart CondSuffix ${
$0 = $<CS;
$0->forpart = $<FP;
$0->thenpart = $<TP;
- $0->condpart = $WP.condpart; $WP.condpart = NULL;
- $0->dopart = $WP.dopart; $WP.dopart = NULL;
+ $0->looppart = $<WP;
var_block_close(c, CloseSequential);
}$
| ForPart OptNL WhilePart CondSuffix ${
$0 = $<CS;
$0->forpart = $<FP;
- $0->condpart = $WP.condpart; $WP.condpart = NULL;
- $0->dopart = $WP.dopart; $WP.dopart = NULL;
+ $0->looppart = $<WP;
var_block_close(c, CloseSequential);
}$
| WhilePart CondSuffix ${
$0 = $<CS;
- $0->condpart = $WP.condpart; $WP.condpart = NULL;
- $0->dopart = $WP.dopart; $WP.dopart = NULL;
+ $0->looppart = $<WP;
}$
| SwitchPart OptNL CasePart CondSuffix ${
$0 = $<CS;
$0->condpart = $<SP;
$CP->next = $0->casepart;
$0->casepart = $<CP;
+ var_block_close(c, CloseSequential);
}$
| SwitchPart : IN OptNL CasePart CondSuffix OUT Newlines ${
$0 = $<CS;
$0->condpart = $<SP;
$CP->next = $0->casepart;
$0->casepart = $<CP;
+ var_block_close(c, CloseSequential);
}$
| IfPart IfSuffix ${
$0 = $<IS;
CondSuffix -> IfSuffix ${
$0 = $<1;
- // This is where we close scope of the whole
- // "for" or "while" statement
- var_block_close(c, CloseSequential);
}$
| Newlines CasePart CondSuffix ${
$0 = $<CS;
}$
$*exec
- // These scopes are closed in CondSuffix
+ // These scopes are closed in CondStatement
ForPart -> for OpenBlock ${
$0 = $<Bl;
}$
var_block_close(c, CloseSequential);
}$
- $cond_statement
- // This scope is closed in CondSuffix
- WhilePart -> while UseBlock OptNL do Block ${
- $0.condpart = $<UB;
- $0.dopart = $<Bl;
+ $*binode
+ // This scope is closed in CondStatement
+ WhilePart -> while UseBlock OptNL do OpenBlock ${
+ $0 = new(binode);
+ $0->op = Loop;
+ $0->left = $<UB;
+ $0->right = $<OB;
+ var_block_close(c, CloseSequential);
+ var_block_close(c, CloseSequential);
}$
- | while OpenScope Expression ColonBlock ${
- $0.condpart = $<Exp;
- $0.dopart = $<Bl;
+ | while OpenScope Expression OpenScope ColonBlock ${
+ $0 = new(binode);
+ $0->op = Loop;
+ $0->left = $<Exp;
+ $0->right = $<CB;
+ var_block_close(c, CloseSequential);
+ var_block_close(c, CloseSequential);
}$
- IfPart -> if UseBlock OptNL then OpenBlock ClosePara ${
+ $cond_statement
+ IfPart -> if UseBlock OptNL then OpenBlock ${
$0.condpart = $<UB;
- $0.thenpart = $<Bl;
+ $0.thenpart = $<OB;
+ var_block_close(c, CloseParallel);
}$
- | if OpenScope Expression OpenScope ColonBlock ClosePara ${
+ | if OpenScope Expression OpenScope ColonBlock ${
$0.condpart = $<Ex;
- $0.thenpart = $<Bl;
+ $0.thenpart = $<CB;
+ var_block_close(c, CloseParallel);
}$
- | if OpenScope Expression OpenScope OptNL then Block ClosePara ${
+ | if OpenScope Expression OpenScope OptNL then Block ${
$0.condpart = $<Ex;
$0.thenpart = $<Bl;
+ var_block_close(c, CloseParallel);
}$
$*exec
- // This scope is closed in CondSuffix
+ // This scope is closed in CondStatement
SwitchPart -> switch OpenScope Expression ${
$0 = $<Ex;
}$
$0 = $<Bl;
}$
+###### print binode cases
+ case Loop:
+ if (b->left && b->left->type == Xbinode &&
+ cast(binode, b->left)->op == Block) {
+ if (bracket)
+ do_indent(indent, "while {\n");
+ else
+ do_indent(indent, "while\n");
+ print_exec(b->left, indent+1, bracket);
+ if (bracket)
+ do_indent(indent, "} do {\n");
+ else
+ do_indent(indent, "do\n");
+ print_exec(b->right, indent+1, bracket);
+ if (bracket)
+ do_indent(indent, "}\n");
+ } else {
+ do_indent(indent, "while ");
+ print_exec(b->left, 0, bracket);
+ if (bracket)
+ printf(" {\n");
+ else
+ printf(":\n");
+ print_exec(b->right, indent+1, bracket);
+ if (bracket)
+ do_indent(indent, "}\n");
+ }
+ break;
+
###### print exec cases
case Xcond_statement:
}
if (bracket) do_indent(indent, "}\n");
}
- if (cs->dopart) {
- // a loop
- if (cs->condpart && cs->condpart->type == Xbinode &&
- cast(binode, cs->condpart)->op == Block) {
- if (bracket)
- do_indent(indent, "while {\n");
- else
- do_indent(indent, "while\n");
- print_exec(cs->condpart, indent+1, bracket);
- if (bracket)
- do_indent(indent, "} do {\n");
- else
- do_indent(indent, "do\n");
- print_exec(cs->dopart, indent+1, bracket);
- if (bracket)
- do_indent(indent, "}\n");
- } else {
- do_indent(indent, "while ");
- print_exec(cs->condpart, 0, bracket);
- if (bracket)
- printf(" {\n");
- else
- printf(":\n");
- print_exec(cs->dopart, indent+1, bracket);
- if (bracket)
- do_indent(indent, "}\n");
- }
+ if (cs->looppart) {
+ print_exec(cs->looppart, indent, bracket);
} else {
// a condition
if (cs->casepart)
do_indent(indent, "if");
if (cs->condpart && cs->condpart->type == Xbinode &&
cast(binode, cs->condpart)->op == Block) {
- if (bracket) // UNTESTED
- printf(" {\n"); // UNTESTED
+ if (bracket)
+ printf(" {\n");
else
- printf(":\n"); // UNTESTED
- print_exec(cs->condpart, indent+1, bracket); // UNTESTED
- if (bracket) // UNTESTED
- do_indent(indent, "}\n"); // UNTESTED
- if (cs->thenpart) { // UNTESTED
- do_indent(indent, "then:\n"); // UNTESTED
- print_exec(cs->thenpart, indent+1, bracket); // UNTESTED
+ printf("\n");
+ print_exec(cs->condpart, indent+1, bracket);
+ if (bracket)
+ do_indent(indent, "}\n");
+ if (cs->thenpart) {
+ do_indent(indent, "then\n");
+ print_exec(cs->thenpart, indent+1, bracket);
}
} else {
printf(" ");
break;
}
+###### propagate binode cases
+ case Loop:
+ t = propagate_types(b->right, c, ok, Tnone, 0);
+ if (!type_compat(Tnone, t, 0))
+ *ok = 0; // UNTESTED
+ return propagate_types(b->left, c, ok, type, rules);
+
###### propagate exec cases
case Xcond_statement:
{
- // forpart and dopart must return Tnone
- // thenpart must return Tnone if there is a dopart,
+ // forpart and looppart->right must return Tnone
+ // thenpart must return Tnone if there is a loopart,
// otherwise it is like elsepart.
// condpart must:
// be bool if there is no casepart
t = propagate_types(cs->forpart, c, ok, Tnone, 0);
if (!type_compat(Tnone, t, 0))
*ok = 0; // UNTESTED
- t = propagate_types(cs->dopart, c, ok, Tnone, 0);
- if (!type_compat(Tnone, t, 0))
- *ok = 0; // UNTESTED
- if (cs->dopart) {
+
+ if (cs->looppart) {
t = propagate_types(cs->thenpart, c, ok, Tnone, 0);
if (!type_compat(Tnone, t, 0))
*ok = 0; // UNTESTED
}
- if (cs->casepart == NULL)
+ if (cs->casepart == NULL) {
propagate_types(cs->condpart, c, ok, Tbool, 0);
- else {
+ propagate_types(cs->looppart, c, ok, Tbool, 0);
+ } else {
/* Condpart must match case values, with bool permitted */
t = NULL;
for (cp = cs->casepart;
t = propagate_types(cp->value, c, ok, NULL, 0);
if (!t && cs->condpart)
t = propagate_types(cs->condpart, c, ok, NULL, Rboolok); // UNTESTED
+ if (!t && cs->looppart)
+ t = propagate_types(cs->looppart, c, ok, NULL, Rboolok); // UNTESTED
// Now we have a type (I hope) push it down
if (t) {
for (cp = cs->casepart; cp; cp = cp->next)
propagate_types(cp->value, c, ok, t, 0);
propagate_types(cs->condpart, c, ok, t, Rboolok);
+ propagate_types(cs->looppart, c, ok, t, Rboolok);
}
}
// (if)then, else, and case parts must return expected type.
- if (!cs->dopart && !type)
+ if (!cs->looppart && !type)
type = propagate_types(cs->thenpart, c, ok, NULL, rules);
if (!type)
type = propagate_types(cs->elsepart, c, ok, NULL, rules);
cp = cp->next) // UNTESTED
type = propagate_types(cp->action, c, ok, NULL, rules); // UNTESTED
if (type) {
- if (!cs->dopart)
+ if (!cs->looppart)
propagate_types(cs->thenpart, c, ok, type, rules);
propagate_types(cs->elsepart, c, ok, type, rules);
for (cp = cs->casepart; cp ; cp = cp->next)
return NULL;
}
+###### interp binode cases
+ case Loop:
+ // This just performs one iterration of the loop
+ rv = interp_exec(c, b->left, &rvtype);
+ if (rvtype == Tnone ||
+ (rvtype == Tbool && rv.bool != 0))
+ // cnd is Tnone or Tbool, doesn't need to be freed
+ interp_exec(c, b->right, NULL);
+ break;
+
###### interp exec cases
case Xcond_statement:
{
if (cs->forpart)
interp_exec(c, cs->forpart, NULL);
- do {
- if (cs->condpart)
- cnd = interp_exec(c, cs->condpart, &cndtype);
- else
- cndtype = Tnone; // UNTESTED
- if (!(cndtype == Tnone ||
- (cndtype == Tbool && cnd.bool != 0)))
- break;
- // cnd is Tnone or Tbool, doesn't need to be freed
- if (cs->dopart)
- interp_exec(c, cs->dopart, NULL);
-
- if (cs->thenpart) {
+ if (cs->looppart) {
+ while ((cnd = interp_exec(c, cs->looppart, &cndtype)),
+ cndtype == Tnone || (cndtype == Tbool && cnd.bool != 0))
+ interp_exec(c, cs->thenpart, NULL);
+ } else {
+ cnd = interp_exec(c, cs->condpart, &cndtype);
+ if ((cndtype == Tnone ||
+ (cndtype == Tbool && cnd.bool != 0))) {
+ // cnd is Tnone or Tbool, doesn't need to be freed
rv = interp_exec(c, cs->thenpart, &rvtype);
- if (rvtype != Tnone || !cs->dopart)
- goto Xcond_done;
- free_value(rvtype, &rv);
- rvtype = Tnone;
+ // skip else (and cases)
+ goto Xcond_done;
}
- } while (cs->dopart);
-
+ }
for (cp = cs->casepart; cp; cp = cp->next) {
v = interp_exec(c, cp->value, &vtype);
if (value_cmp(cndtype, vtype, &v, &cnd) == 0) {
}
al = cast(binode, al->right);
}
- v = interp_exec(c, p->right, &vtype);
+ v = interp_exec(c, p, &vtype);
free_value(vtype, &v);
}
###### interp binode cases
- case List:
- case Func: abort(); // NOTEST
+ case List: abort(); // NOTEST
+
+ case Func:
+ rv = interp_exec(c, b->right, &rvtype);
+ break;
## And now to test it out.
else
hi = mid
if hi - lo < 1:
+ lo = mid
use GiveUp
use True
do pass
case Found:
print "Yay, I found", target
case GiveUp:
- print "Closest I found was", mid
+ print "Closest I found was", lo
size::= 10
list:[size]number