These system variables provide system information, which is difficult to compute outside the interpreter code, but easy from inside, where all needed information is available.
Patch against newlisp-10.6.4.tgz 2015-09-21 16:07 1.6M:
Code: Select all
sr@free:~/newLISP_Git/mirror$ git diff -p inprogress HEAD
diff --git a/mirror/newlisp.c b/mirror/newlisp.c
index 262339d..0190bc5 100644
--- a/mirror/newlisp.c
+++ b/mirror/newlisp.c
@@ -192,6 +192,8 @@ SYMBOL * atSymbol;
SYMBOL * currentFunc;
SYMBOL * argsSymbol;
SYMBOL * mainArgsSymbol;
+SYMBOL * mainArgsLoadIxSymbol;
+SYMBOL * loadListSymbol;
SYMBOL * listIdxSymbol;
SYMBOL * itSymbol;
SYMBOL * sysxSymbol;
@@ -894,6 +896,7 @@ for(idx = 1; idx < argc; idx++)
exit(0);
}
+ mainArgsLoadIxSymbol->contents = (UINT)stuffInteger(idx);
loadFile(argv[idx], 0, 0, mainContext);
}
@@ -1428,6 +1431,8 @@ questionSymbol = translateCreateSymbol("?", CELL_NIL, mainContext, TRUE);
atSymbol = translateCreateSymbol("@", CELL_NIL, mainContext, TRUE);
argsSymbol = translateCreateSymbol("$args", CELL_NIL, mainContext, TRUE);
mainArgsSymbol = translateCreateSymbol("$main-args", CELL_NIL, mainContext, TRUE);
+mainArgsLoadIxSymbol = translateCreateSymbol("$main-args-load-ix", CELL_NIL, mainContext, TRUE);
+loadListSymbol = translateCreateSymbol("$load-list", CELL_NIL, mainContext, TRUE);
listIdxSymbol = translateCreateSymbol("$idx", CELL_NIL, mainContext, TRUE);
itSymbol = translateCreateSymbol("$it", CELL_NIL, mainContext, TRUE);
countSymbol = translateCreateSymbol("$count", CELL_NIL, mainContext, TRUE);
@@ -1454,6 +1459,8 @@ questionSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
atSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
argsSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
mainArgsSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
+mainArgsLoadIxSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
+loadListSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
listIdxSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
itSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
countSymbol->flags |= SYMBOL_GLOBAL | SYMBOL_BUILTIN | SYMBOL_PROTECTED;
@@ -1465,6 +1472,8 @@ argsSymbol->contents = (UINT)getCell(CELL_EXPRESSION);
objSymbol.contents = (UINT)nilCell;
objSymbol.context = mainContext;
objCell = nilCell;
+mainArgsLoadIxSymbol->contents = (UINT)nilCell;
+loadListSymbol->contents = (UINT)getCell(CELL_EXPRESSION);
/* init signal handlers */
for(i = 0; i < 32; i++)
@@ -3291,6 +3300,7 @@ if(my_strnicmp(fileName, "http://", 7) == 0)
pushResult(result);
if(memcmp((char *)result->contents, "ERR:", 4) == 0)
return(errorProcExt2(ERR_ACCESSING_FILE, stuffString((char *)result->contents)));
+ addList((CELL*)loadListSymbol->contents, stuffString(fileName));
result = copyCell(sysEvalString((char *)result->contents, context, nilCell, EVAL_STRING));
currentContext = contextSave;
return(result);
@@ -3314,6 +3324,7 @@ for(i = 0; i<recursionCount; i++) printf(" ");
printf("load: %s\n", fileName);
#endif
+addList((CELL*)loadListSymbol->contents, stuffString(fileName));
result = evaluateStream(&stream, 0, TRUE);
currentContext = contextSave;
- $main-args-load-ix gives index into $main-args of last file/URL tried to be load by main() loop of the interpreter.
During evaluation of a script this need not be an index of its path, because it may itself been loaded indirectly by another script given as command line argument.
But exactly the index of currently evaluated $main-args script is suited for reliable and easy determination of:- scriptname at command line arg: good for output of user info (e.g. errors and logging);
- position of script arguments: after that parsing of script arguments can start;
- fixpoint for relatively located dirs/files: this is good for having an alternative to NEWLISPDIR in file locations related to a user.
- $load-list is a push-back list of all files/URLs already loaded by interpreter main() loop or load or module from newLISP; last element is last loaded script. This may be another script/source as given at command line, because it could have performed a load itself, which would have led to one or more additional elements in this list.
This is good for inspecting,- what have been loaded at all, and
- if there have been any duplicate loads of script/code.
- Good properties of both patches:
- they should work at all supported platforms;
- only minimal code changes;
- no pollution of 'normal' namespace by using symbols with '$' prefix;
- where are the disadvantages?
Code: Select all
sr@free:~/newLISP/Examples$ newlisp
newLISP v.10.6.4 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> $main-args $main-args-load-ix $load-list
("newlisp")
nil
()
>
sr@free:~/newLISP/Examples$ newlisp notExisting
newLISP v.10.6.4 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> $main-args $main-args-load-ix $load-list
("newlisp" "notExisting")
1
()
>
sr@free:~/newLISP/Examples$ touch existing.lsp # empty
sr@free:~/newLISP/Examples$ newlisp -s 1000 existing.lsp --foo
newLISP v.10.6.4 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> $main-args $main-args-load-ix $load-list
("newlisp" "-s" "1000" "existing.lsp" "--foo")
4
("existing.lsp")
>
sr@free:~/newLISP/Examples$ cat printThese.lsp
#!/usr/bin/env newlisp
(println "$main-args: " $main-args)
(println "$main-args-load-ix: " $main-args-load-ix)
(println "$load-list: " $load-list)
sr@free:~/newLISP/Examples$ newlisp -s 1000 printThese.lsp --foo
$main-args: ("newlisp" "-s" "1000" "printThese.lsp" "--foo")
$main-args-load-ix: 3
$load-list: ("printThese.lsp")
newLISP v.10.6.4 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> $main-args $main-args-load-ix $load-list
("newlisp" "-s" "1000" "printThese.lsp" "--foo")
4
("printThese.lsp")
>
Code: Select all
sr@free:~/newLISP/Examples$ newlisp
newLISP v.10.6.4 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> $main-args $main-args-load-ix $load-list
("newlisp")
nil
("/home/sr/.init.lsp" "/home/sr/newLISP/modules/Init.lsp"
"/home/sr/newLISP/modules/FOOPReference.lsp" "/home/sr/newLISP/modules/Util.lsp"
"/home/sr/newLISP/modules/Logger.lsp" "/home/sr/newLISP/modules/LoggerTweakable.lsp"
"/home/sr/newLISP/modules/Libs.lsp")
>
sr@free:~/newLISP/Examples$ newlisp -s 1000 printThese.lsp --foo
$main-args: ("newlisp" "-s" "1000" "printThese.lsp" "--foo")
$main-args-load-ix: 3
$load-list: ("/home/sr/.init.lsp" "/home/sr/newLISP/modules/Init.lsp"
"/home/sr/newLISP/modules/FOOPReference.lsp" "/home/sr/newLISP/modules/Util.lsp"
"/home/sr/newLISP/modules/Logger.lsp" "/home/sr/newLISP/modules/LoggerTweakable.lsp"
"/home/sr/newLISP/modules/Libs.lsp" "printThese.lsp")
newLISP v.10.6.4 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h
> $main-args $main-args-load-ix $load-list
("newlisp" "-s" "1000" "printThese.lsp" "--foo")
4
("/home/sr/.init.lsp" "/home/sr/newLISP/modules/Init.lsp"
"/home/sr/newLISP/modules/FOOPReference.lsp" "/home/sr/newLISP/modules/Util.lsp"
"/home/sr/newLISP/modules/Logger.lsp" "/home/sr/newLISP/modules/LoggerTweakable.lsp"
"/home/sr/newLISP/modules/Libs.lsp" "printThese.lsp")
>
Thanks to the clean code from Lutz, it has been no problem to extend interpreter accordingly.
It would be very nice, if these changes could be reviewed, and so or similar been included into official newLISP.
Further remarks:
- First there has been a change of (load) according the suggestion from Ralph (see viewtopic.php?f=16&t=4756#p23433), so that (load) (without arguments) returns a copy of $load-list. But this has problems, due to making the semantics of load more complicated (in addition the naming of the load func is questionable for this semantics), so it has been discarded.
One example: someone could have the idea to try (load int-ix) in analogue to (main-args int-ix), which would not work. - After creating these patches I think, that $main-args-load-ix is the system information, which is more important for easing the life of developers than $load-list; but the latter is helpful, too.