unattended symbol

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

unattended symbol

Post by Dmi »

here the code (modified newdep's example):

Code: Select all

(context 'NCR)
(set 'ncfuncs '(
  "initscr" "box" "newwin" "endwin" "delwin" "wgetch" "wrefresh" "mvwprintw"
  "wprintw" "refresh" "wmove" "scrollok" "nl" "werase" "LINES" "COLS" "noecho"
  "delch" "waddch" "keypad" "wclrtoeol"))

;;; import library functions
(define (import-ncurses)
  (let (ctx (context))
    (dolist (x ncfuncs) (import "/lib/libncurses.so.5" x))
    (context ctx)))
(context 'MAIN)
when I load it, I have following contents of NCR context:

Code: Select all

newLISP v.8.7.0 on linux, execute 'newlisp -h' for more info.
> (NCR:import-ncurses)
MAIN 
> (symbols NCR)
(NCR:COLS NCR:LINES NCR:attach-ncurses NCR:cons-win NCR:ctx NCR:import-ncurses 
 NCR:init-ncurses NCR:ncfuncs NCR:str NCR:termsize NCR:wappend NCR:werase 
 NCR:win NCR:wprintw NCR:wrefresh NCR:wset NCR:x) 
> 
Note about the last NCR:x symbol. Where is it from?
I think it must not be present... Or I have a mistake somewhere?
WBR, Dmi

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

Post by Dmi »

Hmm... moreover, as I can see, "import" always creates symbols directly in MAIN.
Is it right?
WBR, Dmi

PaipoJim
Posts: 20
Joined: Fri Jun 03, 2005 3:21 pm
Location: Oregon

Re: unattended symbol

Post by PaipoJim »

Dmi wrote: Note about the last NCR:x symbol. Where is it from?
I think it must not be present... Or I have a mistake somewhere?
The NCR:x is the symbol x in the dolist and was created along with NCR:ctx and NCR:import-ncurses when the function "import-ncurses" was *defined*.

The various strings were used to create symbols like NCR:COLS etc... during the *execution* of "import-ncurses".
-

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

Post by Dmi »

Oh... really defined when function is declared!
Nice side effect, that isn't documented (afaik) :-)
WBR, Dmi

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

Post by Lutz »

This is the way it will work:

Code: Select all

(context 'NCR)

(set 'ncfuncs '("initscr" "box"))

(define (import-ncurses)
  (set 'ctx (context))  ; save callers context
  (context 'NCR)        ; set translation/import context to NCR
  (dolist (x ncfuncs) (import "/lib/libncurses.so.5" x))
  (context ctx)          ; switch back to calling context
)

(context 'MAIN)

(NCR:import-ncurses)  ; all imports will be created in NCR

(NCR:initscr ...)         ; uses the functions

You where thinking in the right directtion in your code, trying to set the context before the 'import' statement. But when you call '(NCR:import-ncurses)' from MAIN then the code executing in NCR will also execute from MAIN, but all symbols inside NCR where created when loading the NCR file in NCR. The context set with (context ...) will set the context to use when creating symbols via 'import', 'sym' 'load' etc., but you context of execution still is the one when you called the function.

Lutz

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

Post by Dmi »

Yes!! I forgot to switch context :-))

Thanks!
WBR, Dmi

Locked