Page 1 of 1

Remove line comment

Posted: Thu Jul 07, 2005 7:30 am
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
)

Posted: Thu Jul 07, 2005 8:28 pm
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))

Posted: Fri Jul 08, 2005 6:37 am
by HPW
Thanks Sam,

much more lispy!
I will give it a try.

Later: Works fine!

Posted: Fri Jul 08, 2005 4:22 pm
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

Posted: Mon Dec 12, 2005 8:13 am
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
)

Posted: Mon Dec 12, 2005 9:20 am
by HPW
Very short and very fast.
Smart idea to use newLISP's own parser.

;-)

Posted: Mon Feb 13, 2006 1:50 pm
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 ")"))))))
;)

Posted: Tue Feb 14, 2006 6:20 am
by Fanda
Very nice! I like it!

Fanda

Posted: Tue Feb 14, 2006 7:49 pm
by alex
I am sorry, it was version 2006.02.08 :-)