Remove line comment

Started by HPW, July 07, 2005, 12:30:07 AM

Previous topic - Next topic

HPW

I want to compact my lsp-code for delivery.

So I made a newLISP tool to get my sources smaller.

Here is a code to remove the comment at the end of a code line.

Is there a better way of doing this?

Any ideas?



(define (striplinecomment completelinestr      complinelst newlinestr)
(setq complinelst (parse completelinestr ";"))
(if (>(length complinelst)1)
(if (not(find """(last complinelst)))     ;when semicolon not part of a string
(begin
(pop complinelst -1)
(setq newlinestr (trim(join complinelst ";")"" "t"))
)
(setq newlinestr completelinestr)
)
(setq newlinestr completelinestr)
)
newlinestr
)
Hans-Peter

Sammo

#1
Hi Hans-Peter,



Here's how I do it. In this code, I have not accounted for comments beginning with "#" or strings delimited with "{" and "}".
(define (uncomment str)
  (let
    ( loc (explode str)
      result '()
      comment nil
      in-quote nil
    )
  ;body of let
    (until (or comment (empty? loc))
      (let
        ( ch (pop loc) )
      ;body of let
        (case ch
          (""" (push ch result)
                (set 'in-quote (not in-quote)) )
          (";"  (if in-quote
                  (push ch result)
                  (set 'comment true) ))
          (true (push ch result) ))))
  ;return from outer let
    (trim (join (reverse result)) "" " ") ))

which is used like this:
(set 'OUT "outfile.lsp"
(set 'IN "infile.lsp"
(set 'NL "rn")
(write-file OUT (join (map uncomment (parse (read-file IN) NL)) NL))

HPW

#2
Thanks Sam,



much more lispy!

I will give it a try.



Later: Works fine!
Hans-Peter

Dmi

#3
Hmm... I'm very new to newLisp (and Lisp too).

Is the "explode" usage a correct resource-saving method of string handling here?



Say, if I need to similar parsing of syntactical structures that can be several k-bytes in length and goes in uninterruptable stream.



Will be the following version more efficient?

Or can I not to take care of that?



only modified fragment here:
(define (uncomment str)
  (let
    ( pos 0             ; my change here
      result '()
      comment nil
      in-quote nil
    )
  ;body of let
    (until (or comment (empty? (pos 1 str)))  ; my change here
      (let
        ( ch ((- (inc 'pos) 1) 1 str) )       ;my change here
WBR, Dmi

alex

#4
My version :)

(define (no-comments src)
# Remove all comments from newlisp program
# ATTENTION! The program must have RIGHT SYNTAX
# Example:
#     (println (no-comments (read-file "prog.lsp")))
#
  (setq src (append "(let (____ (lambda ()n" src "n))(eval ____))"))
  (regex "^[(] *lambda *[(][)] *(.*) *[)]$" (string (eval-string src)) 4)
  $1
)

HPW

#5
Very short and very fast.

Smart idea to use newLISP's own parser.



;-)
Hans-Peter

alex

#6
It can be shorter:

# by Alex  version 2005.02.08
(define (no-comments src) (rest (chop (string (eval-string (append "'(" src ")"))))))

;)

Fanda

#7
Very nice! I like it!



Fanda

alex

#8
I am sorry, it was version 2006.02.08 :-)