Context switching in functions

For the Compleat Fan
Locked
Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Context switching in functions

Post by Jeff »

Does anyone know why this doesn't work?

Code: Select all

> (context 'bar)
bar
bar> (context MAIN)
MAIN
> (define (test) (context 'bar) (set 'x 100) (context MAIN))
(lambda () (context 'bar) (set 'x 100) (context MAIN))
> x
nil
> (set 'bar:x 10)
10
> (test)
MAIN
> bar:x
10
> x
100
> 
Can you not switch contexts in a function's scope?
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

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

Post by Lutz »

The context statement only changes the context for translation of source into symbols when doing load, sym or eval-string. Once a symbol is translated (after newLISP read your definition of 'test') it stays in the context it was translated in (which was MAIN), this happens before evaluation of (define (test) ...).

Context switches are only important when translating source into symbols.


This is desrcibed here:

http://newlisp.org/downloads/newlisp_ma ... l#contexts

and in more detail here:

http://newlisp.org/ExpressionEvaluation.html

Lutz

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

But bar was defined first, before test. I understood the (context 'sym) syntax to act as a procedural switch to another namespace. I don't understand how switching contexts this way differs from doing it from the outermost lexical enclosure. And if there is a difference, isn't that making the context function itself act within the lexical scope, rather than the dynamic scope from which it was called (as opposed to the contents of the context switched to)?
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

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

Post by Lutz »

The context statement on the top level is functioning during loading of subsequent source. The context statement inside the function is working during function execution:

Code: Select all

(define (test) (context 'bar) (set (sym "x") 10))

> (test)
10
> bar:x
10
> 
These are the steps:

(1) read top-level expression
(2) translate symbols
(3) evaluate top-level expression
(4) goto (1)

please read: http://newlisp.org/ExpressionEvaluation.html

Lutz

ps: Context switches are only important when translating source into symbols. Once a symbol is translated it stays in its context forever.

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

I read that while you were replying. It explains better than the manual. So the (context 'foo) syntax functions almost like a preprocessor command (or as close to that as you can get with an interpreted language). Because it appears inside a lambda, it is not evaluated until the lambda is applied, yes?

If this is so, then I am still somewhat confused; here is an example I am not sure I understand:

Code: Select all

(define (foo x y)
  (context 'bar)
  (local (a) (println (name a true)))
  (context MAIN))
In that example, local a lexically appears after the context switch. Would that be inserted into bar?
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

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

Post by Lutz »

So the (context 'foo) syntax functions almost like a preprocessor command (or as close to that as you can get with an interpreted language). Because it appears inside a lambda, it is not evaluated until the lambda is applied, yes?
yes

Context switches are only important when translating source into symbols. In your example is no 'sym or 'eval-string statement.

All symbols in function foo where translated when reading the toplevel (define (foo x y) ...) definition. When evaluating the definition, the only thing which happened was assigning a lambda expression (which is already in memory translated into a binary structure) to the symbol foo. When evaluating (foo ...) and then (local ...) inside, all symbol translation already has happened.

A context switch only has influence on subsequent 'sym and 'eval-string statements not on 'local and not on any other only 'sym and 'eval-string.

There is no interpretation happening during function evaluation in newLISP. All lexical analysis and translating is done before that.

Lutz

ps: the best practice for 'sys and 'eval-string is to specify the context of translation in the 'sys or 'eval-string statement itself as one of the parameters.

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Ok, I think I have a better understanding of it now. The walkthrough on the low-level docs is helpful.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Locked