newLISP Fan Club

Forum => newLISP in the real world => Topic started by: kosh on March 15, 2010, 05:09:16 AM

Title: Illegal symbols cannot restore
Post by: kosh on March 15, 2010, 05:09:16 AM
newLISP v.10.1.12 on Linux IPv4 UTF-8, execute 'newlisp -h' for more info.

> (legal? "#")
nil
> (new Tree (sym "#"))                  ; make context `#'
> (set (sym "one" (sym "#")) 1)         ; `#:one' = 1
> (set (sym "two" (sym "#")) 2)         ; `#:two' = 2
> (source (sym "#"))
"n(context '#)nn(set 'one 1)nn(set 'two 2)nnn(context MAIN)nn"
> (eval-string (source (sym "#")))
ERR: missing parenthesis : "...'one 1)nn(set 'two 2)nnn(context MAI"


Therefore, `save/load' function cannot use illegal symbol too.


(save "_hash.lsp" (sym "#"))
(load "_hash.lsp")                     ;-> ERR: missing parenthesis


--- kosh
Title: Re: Illegal symbols cannot restore
Post by: Lutz on March 15, 2010, 05:34:46 AM
Thanks Kosh, this will be corrected for illegal symbols in contexts. But only context symbols should be forced to be legal. For other symbols it still must be allowed, as it is currently needed, i.e. to save hash name-spaces:


> (new Tree 'Foo)
Foo
> (Foo "# & % " 123)
123
> (save "Foo.lsp" 'Foo)
true
> !cat Foo.lsp

(context 'Foo)

(set  (sym "_# & % " MAIN:Foo)  123)

(context MAIN)

> (load "Foo.lsp")
MAIN
> (Foo "# & % ")
123
>


ps: in case anybody gets confused by this:
> !cat Foo.lsp
on Windows you would do:
> !type Foo.lsp

The '!' as first character on the command-line shells out into the OS.
Title: Re: Illegal symbols cannot restore
Post by: kosh on March 16, 2010, 08:47:54 AM
Thanks Lutz. I'm looking forward to next version release.



On another note the report added about illegal symbol.


> (legal? (uuid))
nil
> (define (gensym) (sym (uuid)))          ; make unique symbol use `uuid'
> (letex (s (gensym) v (gensym))
    (define-macro (my-setq s v)
      (set s (eval v))))
> (print (source 'my-setq))
(define-macro (my-setq 40DD3B92-C5D0-4E3D-BF2C-E63B963DD69B 4596AFF8-31F8-487B-B302-5149CD44AEED)
  (set 40DD3B92-C5D0-4E3D-BF2C-E63B963DD69B (eval 4596AFF8-31F8-487B-B302-5149CD44AEED)))

> (read-expr (source 'my-setq))
; symbol name separated!
(define-macro (my-setq 40 DD3B92-C5D0-4E3D-BF2C-E63B963DD69B 4596 AFF8-31F8-487B-B302-5149CD44AEED)
 (set 40 DD3B92-C5D0-4E3D-BF2C-E63B963DD69B (eval 4596 AFF8-31F8-487B-B302-5149CD44AEED)))


But, to resolve this problem in several ways.


;; 1. use legal symbol
(define (gensym)
  (sym (string "g-" (uuid))))           ; 'g-*** will be legal symbol

;; 2. use context
(context 'gensym)
(define (gensym:gensym)
  (inc *counter*)
  (sym (string "g-" *counter*)))        ; MAIN:g-***,gensym:g-*** will NOT conflict namespace
(context MAIN)

;; 3. use `args' function
(define-macro (my-setq )
  (set (args 0) (eval (args 1))))


--- kosh
Title: Re: Illegal symbols cannot restore
Post by: Lutz on March 16, 2010, 09:57:35 AM
Speed performance wise solutions 3. is the best. Or put the define-macro definition inside its own context to protect against variable capture.


(context 'my-setq)

(define-macro (my-setq:my-setq x v)
  (set x (eval v)))

(context MAIN)