Skip to content

Commit

Permalink
Abridge closure strings by default
Browse files Browse the repository at this point in the history
A new 'ABRIDGE_CLOSURES' configuration parameter has been added to
esconfig.h.
It defaults to 0 so a closure string is abridged to

	%closure(a;b;...)@ * {...}

When 'ABRIDGE_CLOSURES' is enabled, some old code that was previously
commented out has been uncommented, so even closures with cyclical
references can be printed:

	; whatis <={@ x y { (x y) = @ cmd { $cmd $y } 1 ; result $x }}
	%closure(x='0 $&nestedbinding';y=1)@ cmd{$cmd $y}
	# 0 $&nestedbinding
	#	the code tree associated with the closure, i.e.
	#	'@ cmd{$cmd $y}' in this case
	# {n} $&nestedbinding
	#	the code tree bound to another closure
	#
	# NOTE: $&nestedbinding doesn't actually exist.
	#       It's purely an indicator of a cyclical reference, like
	#	how 'wryun#1=(a b . wryun#1#)' is used in some Lisps to avoid
	#	printing an endless stream of 'a b'.
  • Loading branch information
memreflect committed Aug 3, 2024
1 parent 6a6492a commit 510345a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
32 changes: 22 additions & 10 deletions conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ static Boolean Lconv(Format *f) {
List *lp, *next;
char *sep;
const char *fmt = (f->flags & FMT_altform) ? "%S%s" : "%s%s";
const char *arg;

lp = va_arg(f->args, List *);
sep = va_arg(f->args, char *);
for (; lp != NULL; lp = next) {
Closure *closure = getclosure(lp->term);
next = lp->next;
fmtprint(f, fmt, getstr(lp->term), next == NULL ? "" : sep);
if (closure == NULL)
arg = getstr(lp->term);
else
arg = str((f->flags & FMT_zeropad) ? "%0C" : "%C", closure);
fmtprint(f, fmt, arg, next == NULL ? "" : sep);
}
return FALSE;
}
Expand Down Expand Up @@ -178,28 +184,33 @@ static Boolean Tconv(Format *f) {
static void enclose(Format *f, Binding *binding, const char *sep) {
if (binding != NULL) {
Binding *next = binding->next;
long flags = f->flags;
enclose(f, next, ";");
fmtprint(f, "%S=%#L%s", binding->name, binding->defn, " ", sep);
#if ABRIDGE_CLOSURES
if (flags & FMT_zeropad)
fmtprint(f, "%S%s", binding->name, sep);
else
#endif
fmtprint(f, "%S=%#L%s", binding->name, binding->defn, " ", sep);
}
}

#if 0
typedef struct Chain Chain;
struct Chain {
Closure *closure;
Chain *next;
};
static Chain *chain = NULL;
#endif

/* %C -- print a closure */
static Boolean Cconv(Format *f) {
Closure *closure = va_arg(f->args, Closure *);
Tree *tree = closure->tree;
Binding *binding = closure->binding;
Boolean altform = (f->flags & FMT_altform) != 0;
Boolean abridgec = (f->flags & FMT_zeropad) != 0;

#if 0
if (!abridgec) {
int i;
Chain me, *cp;
assert(tree->kind == nThunk || tree->kind == nLambda || tree->kind == nPrim);
Expand All @@ -213,22 +224,23 @@ static Boolean Cconv(Format *f) {
me.closure = closure;
me.next = chain;
chain = &me;
#endif
}

if (altform)
fmtprint(f, "%S", str("%C", closure));
fmtprint(f, "%S", str(abridgec ? "%0C" : "%C", closure));
else {
if (binding != NULL) {
long flags = f->flags;
fmtprint(f, "%%closure(");
f->flags = flags;
enclose(f, binding, "");
fmtprint(f, ")");
}
fmtprint(f, "%T", tree);
}

#if 0
chain = chain->next; /* TODO: exception unwinding? */
#endif
if (!abridgec)
chain = chain->next; /* TODO: exception unwinding? */
return FALSE;
}

Expand Down
11 changes: 11 additions & 0 deletions esconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
* line definitions. (That is, remember to do the #ifndef dance.)
*
*
* ABRIDGE_CLOSURES
* if on, produce closure strings without the values bound in a closure:
* %closure(x;y;x)@ * {...}
* else:
* %closure(x=1;y=4;x=9)@ * {...}
* this is particularly useful when dealing with "nested bindings".
*
* ASSERTIONS
* if this is on, asserts will be checked, raising errors on
* actual assertion failure.
Expand Down Expand Up @@ -276,6 +283,10 @@
* default defaults -- don't change this section
*/

#ifndef ABRIDGE_CLOSURES
#define ABRIDGE_CLOSURES 1
#endif

#ifndef ASSERTIONS
#define ASSERTIONS 1
#endif
Expand Down
4 changes: 4 additions & 0 deletions prim-etc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ PRIM(echo) {
} else if (termeq(list->term, "--"))
list = list->next;
}
#if ABRIDGE_CLOSURES
print("%0L%s", list, " ", eol);
#else
print("%L%s", list, " ", eol);
#endif
return true;
}

Expand Down

0 comments on commit 510345a

Please sign in to comment.