newLISP source code processing

Started by Fanda, November 05, 2006, 06:51:36 PM

Previous topic - Next topic

Fanda

If you need to analyze and process the newLISP source code, try code.lsp at:

http://www.intricatevisions.com/index.cgi?page=nlcode">//http://www.intricatevisions.com/index.cgi?page=nlcode



Here is the compact version of cgi.lsp:


(context 'CGI)
(define (put-page file-name , page start end)
(set 'page (read-file file-name))
(set 'start (find "<%" page))
(set 'end (find "%>" page))
(while (and start end)
(print (slice page 0 start))
(context MAIN)
(eval-string (slice page (+ start 2) (- end start 2)))
(context CGI)
(set 'page (slice page (+ end 2)))
(set 'start (find "<%" page))
(set 'end (find "%>" page)))
(print page))
(define (url-translate str)
(replace "+" str " ")
(replace "%([0-9A-F][0-9A-F])" str (char (int (append "0x" $1))) 1))
(define (get-vars input , var value var-value)
(set 'vars (parse input "&"))
(dolist (elmnt vars)
(if (find "=" elmnt)
(begin
(set 'var (first (parse elmnt "=")))
(set 'value ((+ (find "=" elmnt) 1) elmnt)))
(begin
(set 'var elmnt)
(set 'value "")))
(push (list var (url-translate value)) var-value))
var-value)
(set 'params (env "QUERY_STRING"))
(if (not params) (set 'params ""))
(if params
(set 'params (get-vars params)))
(set 'inline (read-line))
(if inline
(set 'params (get-vars inline)))
(if (not params) (set 'params '()))
(if (env "HTTP_COOKIE")
(dolist (elmnt (parse (env "HTTP_COOKIE") ";"))
(set 'var (trim (first (parse elmnt "="))))
(set 'value (trim (last (parse elmnt "="))))
(push (list var value) cookies))
(set 'cookies '()))
(define (set-cookie var value domain path)
(set 'value (string value))
(print (format "Set-Cookie: %s=%s; domain=.%s; path=%s;n" var value domain path)))
(define (get-cookie keystr)
(lookup keystr cookies) )
(define (get keystr)
(lookup keystr params))
(context 'MAIN)


Or even more compact:
(context 'CGI) (define (put-page file-name , page start end) (set 'page (read-file file-name)) (set 'start (find "<%" page)) (set 'end (find "%>" page)) (while (and start end) (print (slice page 0 start)) (context MAIN) (eval-string (slice page (+ start 2) (- end start 2))) (context CGI) (set 'page (slice page (+ end 2))) (set 'start (find "<%" page)) (set 'end (find "%>" page))) (print page)) (define (url-translate str) (replace "+" str " ") (replace "%([0-9A-F][0-9A-F])" str (char (int (append "0x" $1))) 1)) (define (get-vars input , var value var-value) (set 'vars (parse input "&")) (dolist (elmnt vars) (if (find "=" elmnt) (begin (set 'var (first (parse elmnt "="))) (set 'value ((+ (find "=" elmnt) 1) elmnt))) (begin (set 'var elmnt) (set 'value ""))) (push (list var (url-translate value)) var-value)) var-value) (set 'params (env "QUERY_STRING")) (if (not params) (set 'params "")) (if params (set 'params (get-vars params))) (set 'inline (read-line)) (if inline (set 'params (get-vars inline))) (if (not params) (set 'params '())) (if (env "HTTP_COOKIE") (dolist (elmnt (parse (env "HTTP_COOKIE") ";")) (set 'var (trim (first (parse elmnt "=")))) (set 'value (trim (last (parse elmnt "=")))) (push (list var value) cookies)) (set 'cookies '())) (define (set-cookie var value domain path) (set 'value (string value)) (print (format "Set-Cookie: %s=%s; domain=.%s; path=%s;n" var value domain path))) (define (get-cookie keystr) (lookup keystr cookies) ) (define (get keystr) (lookup keystr params)) (context 'MAIN)


:-)))))



Fanda



PS: Tested, but bugs might be still present. Warning: Use only source code with UNIX-like "n" line breaks. (Inspired by HTML source code of http://www.google.com/">//http://www.google.com/)

lisp

#1
I'm not sure I see the purpose of such a thing... Fill me in?

Fanda

#2
When you load the newLISP source code files: (load "cgi.lsp")

it might be little faster without comments and spaces :-)



Also CODE:segment and CODE:line-types could be used for syntax highlighting.



Fanda

Fanda

#3
Changes:

- 'CODE' to 'code'

- code:histogram - code length histogram (code strings comments)

- code:identify - end-of-line symbol (0 Windows, 1 Unix, 2 Mac)

- code:wiki - format code for publishing in wiki (e.g. newLISP on Noodles)

(write-file "wiki.txt" (code:wiki (read-file "myfile.lsp")))



Fanda



PS: Still use only source code with UNIX-like "n" line breaks.

Fanda

#4
Changes:

code:wiki - instead of html 'br' tags, only simple space " " can be used to create a new line.



Or do directly:

(write-file "wiki.txt" (join (map (fn (l) (append " " l)) (parse (read-file "myfile.lsp") "n")) "n"))