Page 1 of 1
Code formatter
Posted: Tue Nov 20, 2007 7:24 pm
by cormullion
I'm going to try to write a code formatter script - something that tidies up the layout of my newlisp scripts. Before I get started, has anyone done anything similar before? Are there algorithms and conventions for code layout that I could borrow or refer to?
Posted: Tue Nov 20, 2007 10:10 pm
by newdep
I never use code cleaner because they never do what i want, actualy
the code writer is the best cleaner there is ;-)
I actualy never had troubles reading other newlisp code the use of () if indeed
a personal touch ... Cleaning out ;; [text]..etc.. is of no use i think..
What you could do though is follow the way the newlispDoc handles code..
A kind of configuration option for the user.. but then again..will I use it?
My Personal favorite would be a newlisp "Profiler", something that hints
on slow functions or wrong loops...
But then again, I always walk out of line.. ;-)
Posted: Wed Nov 21, 2007 4:36 am
by Cyril
newdep wrote:I never use code cleaner because they never do what i want, actualy the code writer is the best cleaner there is ;-)
There
is one use for code cleaner -- it alarms you when your own assumptions about code structure (based on indentation) differs from lisp one's (based on parentheses). Yes, I have fallen in this trap!
Posted: Wed Nov 21, 2007 8:12 pm
by Dmi
Hi, Cormullion!
Check
http://en.feautec.pp.ru/store/newlisp-edit against two libs that are loaded from en.feautec.pp.ru and the call to INDENT:indent
You can also check it in action by running it ;-)
Posted: Wed Nov 21, 2007 11:19 pm
by cormullion
Cool - I will study your script. Thanks!
Posted: Sun Nov 25, 2007 4:55 pm
by cormullion
A small brainteaser that's got me stumped. Given this code:
Code: Select all
(set 't "\n"
'result {})
(if (= t "\n")
(push (string t) result -1))
(println result)
How do you get the output to be "\n"? In other words, how to stop the backslashes working, if you're unable to change the incoming string...?
The code formatter is working pretty well, but it's tripping up over these. For example, this code (by Lutz):
Code: Select all
(define (confirm-request conf)
(and
(net-receive socket 'recvbuff 256 "\r\n")
(if debug-flag (println recvbuff) true)
(starts-with recvbuff conf)))
(define (net-send-get-result str conf)
(set 'send-str (append str "\r\n"))
(if debug-flag (println "sent: " send-str))
(net-send socket 'send-str)
(if conf (confirm-request conf) true))
is output by my formatter like this:
Code: Select all
(define (confirm-request conf)
(and
(net-receive socket 'recvbuff 256 "
" )
(if debug-flag
(println recvbuff) true)
(starts-with recvbuff conf)))
(define (net-send-get-result str conf)
(set 'send-str
(append str "
" ))
(if debug-flag
(println "sent: " send-str))
(net-send socket 'send-str)
(if conf
(confirm-request conf) true))
which probably still works, but I want to suppress the backslash expansion (without changing the original code..)
Posted: Sun Nov 25, 2007 10:55 pm
by Dmi
Hmmm... Why do U unable to change incoming string?
In my parser all code is stripped down into a list of lines, and is reassembled after indenting...
Code: Select all
(load "http://en.feautec.pp.ru/store/libs/funlib.lsp")
(load "http://en.feautec.pp.ru/store/indent.lsp")
(set 's (parse (read-file (main-args 2)) "\n"))
(println (join (INDENT:indent s) "\n"))
(exit)
The string parsing is done char-by-char with state machine.
Posted: Sun Nov 25, 2007 11:32 pm
by cormullion
Ah yes - I found this in your indent.lsp
Code: Select all
(define (string-value s)
(context CON:cur-ctx)
(if (string? INDENT:s)
(append "\""
(dolist (INDENT:l '(("\\" "\\\\") ("\"" "\\\"") ("\n" "\\n")
("\t" "\\t")))
(set 's (replace (INDENT:l 0) INDENT:s (INDENT:l 1))))
"\"")
(string INDENT:s)))
I think that's the way! I love all those backslashes.... :\
Thanks!