Because I wanted to create executables having (load ...) in them, I created a script which
1) copies each input line of a given input script to an output file except when the first thing on the line is "(load ". In that case the contents of the lsp file in the "(load " will be written to the output
2) start NLINC with only the script name (no path and no extension)
3) the work should be done in the source directory (in my case all sources in one dir) see Windows script
Updates per 20160301:
- some error handling added
- avoiding recursive includes now
The way it works I now can use (load ...) in my scripts and use them as a normal script or as an (expanded) script turned into an executable.
Any comments and remarks are welcome
Regretting now to have been side tracted from newLISP. It is a pleasure writing code in!
Thx,
Arie
Code of newLISP script:
Code: Select all
; ################################################################################
; # NLINC
; #
; # Purpose:
; # - read a newLISP script xxx.lsp
; # - if there are LOAD functions (on the utter left of a line)
; # these will be replaced by the loaded MEMBER itself (INCLUDED)
; # - output will be written to xxx-i.lsp
; ################################################################################
(define (mainargs , osargs srch)
(setq osargs (main-args))
(setq srch (lower-case (osargs 0)))
(if (or (= srch "newlisp") (= srch "newlisp.exe"))
(2 osargs)
(1 osargs)))
(define (check-include line , incmem)
(setq line (lower-case (trim line)))
(if (!= (0 6 line) "(load ")
(setq imcmem nil)
(begin
(setq line (6 line))
(setq incmem ((parse line {"}) 1))))
incmem)
(define (write-incmem incmem out , memfile)
(setq memfile (open incmem "read"))
(write-line out (append "; NLINC: member " incmem " included here") )
(while (read-line memfile)
(write-line out (current-line)))
(close memfile)
)
(define (pass , in out incmem extra-pass)
(setq in (open wf1 "read"))
(setq out (open wf2 "write"))
(setq extra-pass nil)
(println "NLINC: entering pass " (inc passnum) " ...")
(while (read-line in)
(setq line (current-line))
(setq incmem (check-include line))
(cond ((nil? incmem) (write-line out line))
((find incmem inclist) (write-line out (append "; " line)))
(true
(push incmem inclist)
(write-incmem incmem out)
(setq extra-pass true))))
(close in)
(close out)
extra-pass)
(define (nlinc , extra-pass scriptname script wf1 wf2 tmp)
(setq extra-pass true)
(setq scriptname ((mainargs) 0))
(setq script (append scriptname ".lsp"))
(setq wf1 (append scriptname "-i1.lsp"))
(setq wf2 (append scriptname "-i2.lsp"))
(setq scriptout (append scriptname "-i.lsp"))
(setq swapped nil)
(setq inclist '())
(setq passnum 0)
(setq tmp "")
(if (not (file? script))
(begin
(println "NLINC: you may only specify the name part of the script")
(println " without path and extension!")
(exit 999)))
(println {NLINC: processing started for script "} script {"})
(copy-file script wf1)
; Loop over the script and include
; If any script included, repeat until nothing included
; Avoid duplicate includes
(while extra-pass
(setq extra-pass (pass wf1 wf2))
(setq tmp wf2)
(setq wf2 wf1)
(setq wf1 tmp)
(if swapped
(setq swapped nil)
(setq swapped true)))
(delete-file scriptout)
(if swapped
(begin
(rename-file wf1 scriptout)
(delete-file wf2))
(begin
(rename-file wf1 scriptout)
(delete-file wf2))))
(nlinc)
(exit)
Code: Select all
e:
cd \src\newlisp
newlisp nlinc.lsp %1
newlisp -x %1-i.lsp %1.exe
move /y %1.exe e:\scripts
del %1-i.lsp