2009-12-28

nested subprograms translated to c

10.3: adda/nested subprograms translated to c:

. nested subprograms mean that
not only is the child sub'
local to the parent sub'
but also the parent's locals
are visible to the child .
. this straightforward design means
re-impl'ing the stack
rather than using c's native stack;
however,
that is not the most efficient way .

using globals for shared access:
. c allows files to be included,
and globals can be shared with only specific routines,
so that if the parent wants to share any of its locals,
these locals would be declared outside the parent
but inside the parent's file,
and then these extern's
would be exported to the files of its children .

recursion:
. the use of shared access to globals
depends on there being
only one instantiation of the parent,
which is not the case if
the parent can be called recursively .
. in that case,
the child needs to really have access
to parent's locals;
and, this can be done by
extending the child's arg'list,
so that any parent`locals needed by the child
will have to be passed via pointers
during each call to the child:
child( usual args, &parent`local) .

11.5: adda/type"nested subroutines:

. for local sub's easiest to read
is declare a record of sub's externals (ptrs),

and then add just one param
f(... uh, .... forget easy to read : just do

obvious and every external needs to be a ptr to same
but as a bonus project
good to know what you can do with c'structs
. here is adda -> c:
f(x.t)`= ( y.t, g.proc`= ( ... x, y ...) ...)
->
g_arg_struct: (x/.t, y/.t);
g( arg/.g_arg_struct )`=
( arg.x ... )
f(x)
(... my.g_arg_struct= (&x, &y); g(&my) ...) .
::
. the top-level function and it's nested functions
are in the same file;
and, for each nested subroutine,
there is a file-wide struct whose name is
arg nested subroutine's name
and the fields of that struct are the same name as
the locals it needs to access from its client .
. then the top-level declares
a var of that struct,
and declares its fields to be
pointers to the expected locals of clients .
. then the client declares an instance of that record,
and init's the fields with the expected pointers .
. calls to the sub need only
pass a pointer to the struct each time .

. are typedef's local if call(x) static?
anyway
they are named after sub they serve
so easy to stay unique .