Remove line comment

For the Compleat Fan
Locked
HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Remove line comment

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

Code: Select all

(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
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

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 "}".

Code: Select all

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

Code: Select all

(set 'OUT "outfile.lsp"
(set 'IN "infile.lsp"
(set 'NL "\r\n")
(write-file OUT (join (map uncomment (parse (read-file IN) NL)) NL))

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Thanks Sam,

much more lispy!
I will give it a try.

Later: Works fine!
Hans-Peter

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

Post by Dmi »

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:

Code: Select all

(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
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

My version :)

Code: Select all

(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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Very short and very fast.
Smart idea to use newLISP's own parser.

;-)
Hans-Peter

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

It can be shorter:

Code: Select all

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

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Very nice! I like it!

Fanda

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

I am sorry, it was version 2006.02.08 :-)

Locked