Context switching in functions

Started by Jeff, September 07, 2007, 10:11:28 AM

Previous topic - Next topic

Jeff

Does anyone know why this doesn't work?


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



http://artfulcode.net\">Artful code

Lutz

#1
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_manual.html#contexts">http://newlisp.org/downloads/newlisp_ma ... l#contexts">http://newlisp.org/downloads/newlisp_manual.html#contexts



and in more detail here:



http://newlisp.org/ExpressionEvaluation.html">http://newlisp.org/ExpressionEvaluation.html



Lutz

Jeff

#2
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...



http://artfulcode.net\">Artful code

Lutz

#3
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:


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

#4
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:


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



http://artfulcode.net\">Artful code

Lutz

#5
QuoteSo 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

#6
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...



http://artfulcode.net\">Artful code