Lint and/or code formatting program for newLISP

Started by dukester, February 15, 2024, 06:28:46 AM

Previous topic - Next topic

dukester

Is there one hiding somewhere? I searched this Forum and the Home site w/o any luck.
duke

itistoday

Get your Objective newLISP groove on.


vashushpanov

It is a long time ago I was get this script.


#!/usr/bin/env newlisp
;; indent-newlisp.lsp
;; Author: ssqq
;; http://www.newlisp.cn
;; usage:
;; ~> newlisp indent-newlisp.lsp your-script.lsp 4 > output.lsp

(define (indent-code input-file (indent 2))
    (local (v-level new-level file-txt file-lines indent-line indent-lines)
        (setq out-data "")
        (setq v-level 0)
        (setq file-text (read-file input-file))
        (setq file-lines (map trim (parse file-text "\n")))
        (dolist (v-line file-lines)
            (setq new-level (get-new-level v-line v-level))
            (if (starts-with v-line ")")
                (setq indent-line (get-indent-line new-level v-line indent))
                (setq indent-line (get-indent-line v-level v-line indent))
            )
            (push indent-line indent-lines -1)
            (setq v-level new-level)
        )
        (write-file input-file (join indent-lines "\n"))
    )
)

(define (get-new-level v-line v-level)
    (local (close-amount open-amount freeze-line)
        (setq freeze-str (replace {".*?[^\\]"}  v-line "" 0))
        (setq freeze-str (replace {{.*?[^\\]}}  v-line "" 0))
        (setq freeze-str (replace {\[.*?[^\\]\]} v-line "" 0))
        (setq freeze-str (replace {(?:^|\s)(?:\;|\#).*?$} v-line "" 0))
        (find-all {\(} freeze-str)
        (setq open-amount $count)
        (setq v-level (+ open-amount v-level))
        (find-all {\)} freeze-str)
        (setq close-amount $count)
        (setq v-level (- v-level close-amount))
    )
)
(define (get-indent-line v-level v-line indent)
    (let (indent-space {})
        (setq indent-space (dup " " (mul v-level (int indent))))
        (append indent-space v-line)
    )
)

(setq input-file (main-args 2))
(setq indent 2)
(indent-code input-file indent)

(exit)

dukester

#4
@vashushpanov Thank you for the script! 🙏

I just tried to run your script on a file that I wanted to clean up. Sadly there was zero output.
I'm running newLISP v.10.7.5 64-bit on Linux IPv4/6 UTF-8, so maybe the regex you use in the script need to be updated? I get no error messages - just zero output!
duke

vashushpanov

oh!

#!/usr/bin/env newlisp
;; indent-newlisp.lsp
;; Author: ssqq
;; http://www.newlisp.cn
;; usage:
;; ~> newlisp indent-newlisp.lsp your-script.lsp 4 > output.lsp

(define (indent-code input-file (indent 2))
    (local (v-level new-level file-txt file-lines indent-line indent-lines)
        (setq v-level 0)
        (setq file-text (read-file input-file))
        (setq file-lines (map trim (parse file-text "\n")))
        (dolist (v-line file-lines)
            (setq new-level (get-new-level v-line v-level))
            (if (starts-with v-line ")")
                (setq indent-line (get-indent-line new-level v-line indent))
                (setq indent-line (get-indent-line v-level v-line indent))
            )
            (push indent-line indent-lines -1)
            (setq v-level new-level)
        )
        (println (join indent-lines "\n"))
    )
)

(define (get-new-level v-line v-level)
    (local (close-amount open-amount freeze-line)
        (setq freeze-str (replace {".*?[^\\]"}   v-line "" 0))
        (setq freeze-str (replace {{.*?[^\\]}}   v-line "" 0))
        (setq freeze-str (replace {\[.*?[^\\]\]} v-line "" 0))
        (setq freeze-str (replace {(?:^|\s)(?:\;|\#).*?$} v-line "" 0))
        (find-all {\(} freeze-str)
        (setq open-amount $count)
        (setq v-level (+ open-amount v-level))
        (find-all {\)} freeze-str)
        (setq close-amount $count)
        (setq v-level (- v-level close-amount))
    )
)

(define (get-indent-line v-level v-line indent)
    (let (indent-space {})
        (setq indent-space (dup " " (mul v-level (int indent))))
        (append indent-space v-line)
    )
)

(setq input-file (main-args 2))
(setq indent (main-args 3))
(indent-code input-file indent)

(exit)

This code works.

dukester

Yes! That works well now. Thank you. What was wrong with the code?
duke