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
)
			
			
				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 "}".
  (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 'IN "infile.lsp"
(set 'NL "rn")
(write-file OUT (join (map uncomment (parse (read-file IN) NL)) NL))
			
			
				Thanks Sam,
much more lispy!
I will give it a try.
Later: Works fine!
			
			
			
				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:
  (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
			
			
				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
)
			
			
				Very short and very fast.
Smart idea to use newLISP's own parser.
;-)
			
			
			
				It can be shorter:
# by Alex  version 2005.02.08
(define (no-comments src) (rest (chop (string (eval-string (append "'(" src ")"))))))
;)
			
			
			
				Very nice! I like it!
Fanda
			
			
			
				I am sorry, it was version 2006.02.08 :-)