Code formatter

For the Compleat Fan
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Code formatter

Post 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?

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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.. ;-)
-- (define? (Cornflakes))

Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Post 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!
With newLISP you can grow your lists from the right side!

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post 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 ;-)
WBR, Dmi

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Cool - I will study your script. Thanks!

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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..)

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post 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.
WBR, Dmi

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

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

Locked