Code formatter

Started by cormullion, November 20, 2007, 11:24:43 AM

Previous topic - Next topic

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

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

#2
Quote from: "newdep"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

#3
Hi, Cormullion!



Check http://en.feautec.pp.ru/store/newlisp-edit">//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

#4
Cool - I will study your script. Thanks!

cormullion

#5
A small brainteaser that's got me stumped. Given this code:


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


(define (confirm-request conf)
   (and
    (net-receive socket 'recvbuff 256 "rn")
    (if debug-flag (println recvbuff) true)
    (starts-with recvbuff conf)))
 
(define (net-send-get-result str conf)
   (set 'send-str (append str "rn"))
   (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:


(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

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

#7
Ah yes - I found this in your indent.lsp


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