Page 1 of 1
Reading newLISP from file
Posted: Wed Mar 15, 2006 12:50 pm
by cormullion
if I type this in a console:
Code: Select all
(set 'l '(dolist (file-name (2 (main-args))) (set 'file (open file-name "read")) (set 'file-contents (read-file file-name))))
then I can get newLISP to pretty-print the text.
How would I go about putting some newLISP code in a text file and then reading it in and assigning it to a symbol like 'l above, so that I can then get newLISP to pretty-print it?
Posted: Wed Mar 15, 2006 1:32 pm
by Lutz
If your question is about pretty-printing newLISP source, you just can load the source and save it again. While saving everything will be pretty-printed, but comments will disappear:
Code: Select all
(load "myprog.lsp")
(save "myprog-pretty.lsp")
Now look into the file "myprog-pretty.lsp" and everything will be printed formatted by newLISP's pretty printer. But not only the contents of "myprog.lsp" but everything else which was 'define'd or 'set' in a symbol. You also can save/pretty-print just one or a few symbols:
Code: Select all
newLISP v.8.8.0 on OSX UTF-8, execute 'newlisp -h' for more info.
> (define (double x) (+ x x))
(lambda (x) (+ x x))
> (save "double.lsp" 'double)
true
> (exit)
~> cat double.lsp
(define (double x)
(+ x x))
~>
Lutz
ps: see 'save', 'source' and 'pretty-print' for further detail in the manual
Posted: Wed Mar 15, 2006 1:43 pm
by Lutz
... of course this only works on files containing 'define' statements, when using 'load' on an executing script you will only get the results of the evaluation of the statements in the file. I.e. if you want to pretty-print the last entry in the newLISPer blog:
http://newlisper.blogspot.com/ you would have to put everything first into a definition:
Code: Select all
(define (simple-sums)
(set 'file (open "/Users/me/bigdata.txt" "read"))
(set 'prices '("1.99" "2.99" "3.99" "4.99" "5.99" "6.99")
'tally (dup '0 (length prices))
'total 0)
...
...
)
After loading above file, do a (save "simple-sums.lsp" 'simple-sums) and everything will be nicely formatted.
Lutz
Posted: Wed Mar 15, 2006 2:25 pm
by cormullion
Thanks, Lutz - I've made some progress now. I'm working in BBEdit/textWrangler. I've got a Unix filter with this:
Code: Select all
#!/usr/bin/newlisp
(set 't (load (main-args 2)))
(save "/tmp/newlisp.lsp" 't)
(println (load "/tmp/newlisp.lsp"))
Then I run it from within BBEdit on a file containing this:
Code: Select all
(define (scan-file f) (set 'table '()) (set 'in-file (open f "read")) (while (read-line in-file) (if (find "<value>" (current-line)) (begin (set 'the-values (parse (current-line) "<value>|</value>" 1)) (dolist (i the-values) (set 'j (trim i)) (unless (= j "") (if (set 'result (lookup j table 1)) (replace-assoc j table (list j (+ result 1))) (push (list j 1) table -1))))))))
and it produces this:
Code: Select all
(lambda (f) (set 'table '()) (set 'in-file (open f "read"))
(while (read-line in-file)
(if (find "<value>" (current-line))
(begin
(set 'the-values (parse (current-line) "<value>|</value>" 1))
(dolist (i the-values)
(set 'j (trim i))
(unless (= j "")
(if (set 'result (lookup j table 1))
(replace-assoc j table (list j (+ result 1)))
(push (list j 1) table -1))))))))
- which is pretty good, but the function name has been replaced with (lambda (f).
It's tantalizing that newLISP can format functions nicely but I can't get my functions to look as good!
Posted: Wed Mar 15, 2006 2:33 pm
by Lutz
(1) Do not print the return value from the 'load' statement but the contents of the file "/tmp/newlisp.lsp" itself. (2) save evrything not only 't' which was just the return value from the first 'load'. This way your script will work:
Code: Select all
#!/usr/bin/newlisp
(load (main-args 2))
(save "/tmp/newlisp.lsp")
(println (read-file "/tmp/newlisp.lsp"))
(exit)
This way you may have several definitions in the file loaded in the first statement.
Lutz
ps: I am not shure if your BBedit environment requires the '(exit)' but normally it is required.
Posted: Wed Mar 15, 2006 2:40 pm
by Lutz
... you may also see other stuff in the output from your 'init.lsp' file, which was loaded during newLISP startup. Delete it or empty it, its in /usr/share/newlisp/init.lsp , there is probably not anything in it you are interested in anyway.
Lutz
Posted: Wed Mar 15, 2006 3:25 pm
by cormullion
Thanks - works great! I'll work out how to get rid of the first bit:
Code: Select all
(constant (global '$main-args) '("/usr/bin/newlisp" "/Users/me/Library/Application Support/BBEdit/Unix Support/Unix Filters/format-lisp-2.lsp"
"/private/var/tmp/folders.501/Cleanup At Startup/untitled text 19.S"))
I don't seem to have a /usr/share/newlisp/init.lsp file at all.