Illegal symbols cannot restore

Q&A's, tips, howto's
Locked
kosh
Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan
Contact:

Illegal symbols cannot restore

Post by kosh »

Code: Select all

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 '#)\n\n(set 'one 1)\n\n(set 'two 2)\n\n\n(context MAIN)\n\n"
> (eval-string (source (sym "#")))
ERR: missing parenthesis : "...'one 1)\n\n(set 'two 2)\n\n\n(context MAI"
Therefore, `save/load' function cannot use illegal symbol too.

Code: Select all

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Illegal symbols cannot restore

Post by Lutz »

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:

Code: Select all

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

Code: Select all

> !cat Foo.lsp
on Windows you would do:

Code: Select all

> !type Foo.lsp
The '!' as first character on the command-line shells out into the OS.

kosh
Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan
Contact:

Re: Illegal symbols cannot restore

Post by kosh »

Thanks Lutz. I'm looking forward to next version release.

On another note the report added about illegal symbol.

Code: Select all

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

Code: Select all

;; 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Illegal symbols cannot restore

Post by Lutz »

Speed performance wise solutions 3. is the best. Or put the define-macro definition inside its own context to protect against variable capture.

Code: Select all

(context 'my-setq)

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

(context MAIN)

Locked