dynamic load

Finally, in this release (v1.7) the dynamic load is available (after 2 attempts in the past years which ended in failure).  Dynamic load requires the ability to do recursive load.  The most intrigue part is how to handle internal states of "lex" (the lexical analyser).  Lex uses many states to do indentation as blocks (see the source lex.c).  After many designs, the best solution comes from treating a file as a unit of compilation.  It means one whole file will be compiled (generated executable codes) until completion before executing any immediate lines (which have already been compiled into executable codes).  During the execution of the immediate lines, loading of another source file may occurs.  It can happen recursively.  With this design, "lex" is reset to a known initial state for each file because one file will be completed before the next.  It is not necessary to save/restore any states, or any buffer.  Hence, the problem is solved!

eval() may be called recursively.  The global state of the interpreter is fp.  fp is used in the manner of LIFO hence needed not to be saved.  ip is local to eval, so eval() is reentrant.  When doing eval() and encounters "load" (syscall 11), this causes the interpreter to start compiling a new file and executing the immediate lines in that file by calling eval() recursively.

A source file is compiled as a unit, therefore any forward reference to a function must be declared properly.  Remember that this file will be compiled to the completion before starting a next one.  See the following example:

<file t1.txt>
aa = 11
to t1 x = x + 1
to t2 x = {}	// forward declare of func "t2"

load "t2.txt"
print t1 2 print t2 3
<end>

<file t2.txt>
bb = 22
to t2 x = x + 2
<end>

The file "t1.txt" loads "t2.txt".  However, "print t2 3" in t1.txt refers to the function "t2" in other source file not yet compiled.  Therefore the function "t2" must be declare forwardly in the file "t1.txt" before it can be used.  (the compiler must know its arity to compile "print t2 3" properly).  To declare a function forwardly, only the function head must be presented, the body is empty.

Dynamic load enables writing a program as "modules".  A module contains related functions.  Each module is in a separate source file.  A "master" file can be written to contain mainly loading other modules.  The dynamic load helps to manage source files and allows their reuse.

9 Jan 2006


  