Reading newLISP from file

Started by cormullion, March 15, 2006, 04:50:59 AM

Previous topic - Next topic

cormullion

if I type this in a console:
(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?

Lutz

#1
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:



(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:



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

Lutz

#2
... 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/">http://newlisper.blogspot.com/ you would have to put everything first into a definition:



(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

cormullion

#3
Thanks, Lutz - I've made some progress now. I'm working in BBEdit/textWrangler. I've got a Unix filter with this:
#!/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:
(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:


(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!

Lutz

#4
(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:



#!/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.

Lutz

#5
... 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

cormullion

#6
Thanks - works great! I'll work out how to get rid of the first bit:


(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.