executable is something quite specific like a constant or variable name.
So we define a `struct exec` to be a general executable with a type, and
a `struct binode` which is a subclass of `exec`, forms a node in a
-binary tree, and holds an operation. There will be other subclasses,
-and to access these we need to be able to `cast` the `exec` into the
-various other types. The first field in any `struct exec` is the type
-from the `exec_types` enum.
+binary tree, and holds an operation. The simplest operation is "List"
+which can be used to combine several execs together.
+
+There will be other subclasses, and to access these we need to be able
+to `cast` the `exec` into the various other types. The first field in
+any `struct exec` is the type from the `exec_types` enum.
###### macros
#define cast(structname, pointer) ({ \
struct binode {
struct exec;
enum Btype {
+ List,
## Binode types
} op;
struct exec *left, *right;
{
struct binode *b2;
switch(b->op) {
+ case List: abort(); // must be handled by parent NOTEST
## print binode cases
}
}
static void print_exec(struct exec *e, int indent, int bracket)
{
if (!e)
- return;
+ return; // NOTEST
switch (e->type) {
case Xbinode:
print_binode(cast(binode, e), indent, bracket); break;
{
struct binode *b = cast(binode, prog);
switch (b->op) {
+ case List: abort(); // NOTEST
## propagate binode cases
}
break;
struct type *ltype, *rtype;
ltype = rtype = Tnone;
switch (b->op) {
+ case List: abort(); // NOTEST
## interp binode cases
}
free_value(ltype, &left);
Being "complex" the language will naturally have syntax to access
specifics of objects of these types. These will fit into the grammar as
"Terms" which are the things that are combined with various operators to
-form "Expression". Where a Term is formed by some operation on another
+form an "Expression". Where a Term is formed by some operation on another
Term, the subordinate Term will always come first, so for example a
member of an array will be expressed as the Term for the array followed
by an index in square brackets. The strict rule of using postfix
operations makes precedence irrelevant within terms. To provide a place
-to put the grammar for each terms of each type, we will start out by
+to put the grammar for terms of each type, we will start out by
introducing the "Term" grammar production, with contains at least a
simple "Value" (to be explained later).
+We also take this opportunity to introduce the "ExpressionsList" which
+is a simple comma-separated list of expressions - it may be used in
+various places.
+
+###### declare terminals
+ $TERM ,
+
###### Grammar
$*exec
Term -> Value ${ $0 = $<1; }$
| Variable ${ $0 = $<1; }$
## term grammar
+ $*binode
+ ExpressionList -> ExpressionList , Expression ${
+ $0 = new(binode);
+ $0->op = List;
+ $0->left = $<1;
+ $0->right = $<3;
+ }$
+ | Expression ${
+ $0 = new(binode);
+ $0->op = List;
+ $0->left = NULL;
+ $0->right = $<1;
+ }$
+
Thus far the complex types we have are arrays and structs.
#### Arrays
$TERM func
-###### Binode types
- List,
-
###### Grammar
$*variable
}
break;
-### Expression list
-
-We take a brief detour, now that we have expressions, to describe lists
-of expressions. These will be needed for function parameters and
-possibly other situations. They seem generic enough to introduce here
-to be used elsewhere.
-
-And ExpressionList will use the `List` type of `binode`, building up at
-the end. And place where they are used will probably call
-`reorder_bilist()` to get a more normal first/next arrangement.
-
-###### declare terminals
- $TERM ,
-
-`List` execs have no implicit semantics, so they are never propagated or
-interpreted. The can be printed as a comma separate list, which is how
-they are parsed. Note they are also used for function formal parameter
-lists. In that case a separate function is used to print them.
-
-###### print binode cases
- case List:
- while (b) {
- printf(" ");
- print_exec(b->left, -1, bracket);
- if (b->right)
- printf(",");
- b = cast(binode, b->right);
- }
- break;
-
-###### propagate binode cases
- case List: abort(); // NOTEST
-###### interp binode cases
- case List: abort(); // NOTEST
-
-###### Grammar
-
- $*binode
- ExpressionList -> ExpressionList , Expression ${
- $0 = new(binode);
- $0->op = List;
- $0->left = $<1;
- $0->right = $<3;
- }$
- | Expression ${
- $0 = new(binode);
- $0->op = List;
- $0->left = NULL;
- $0->right = $<1;
- }$
-
### Expressions: Boolean
The next class of expressions to use the `binode` will be Boolean
case Print:
do_indent(indent, "print");
- if (b->right) {
- print_exec(b->right, -1, bracket);
+ b2 = cast(binode, b->left ?: b->right);
+ while (b2) {
+ printf(" ");
+ print_exec(b2->left, -1, bracket);
+ if (b2->right)
+ printf(",");
+ b2 = cast(binode, b2->right);
+ }
+ if (b->right)
printf(",");
- } else
- print_exec(b->left, -1, bracket);
if (indent >= 0)
printf("\n");
break;