Lexical Binding
From
Lawrence D'Oliveiro@ldo@nz.invalid to
comp.lang.postscript on Tue Jun 4 05:59:08 2024
From Newsgroup: comp.lang.postscript
In GXScript, I have renamed rCLdefrCY, rCLloadrCY, rCLstorerCY and rCLwhererCY to rCLddefrCY,
rCLdloadrCY, rCLdstorerCY and rCLdwhererCY (rCLdrCY for rCLdynamicrCY) and added corresponding
lexically-bound variants rCLldefrCY, rCLlloadrCY, rCLlstorerCY and rCLlwhererCY. In
effect, GXScript is a rCLblock-structuredrCY language.
Example:
/Count 99 ddef
{
/Count 1 ldef
3
{
/Count dup lload 1 add lstore
(local Count = ) print /Count lload =
(global Count = ) print /Count dload =
(whichever Count = ) print Count =
}
repeat
} exec
produces output
local Count = 2
global Count = 99
whichever Count = 2
local Count = 3
global Count = 99
whichever Count = 3
local Count = 4
global Count = 99
whichever Count = 4
and you can define function factories, e.g.
/Count 99 ddef
/metatry
{ # provides context for nonlocals
dup
/Name exch ldef
/Count 0 ldef
{ # actual proc
/Count dup lload 1 add lstore
/Count dup dload 1 add dstore
Name =
(local Count = ) print /Count lload =
(global Count = ) print /Count dload =
(whichever Count = ) print Count =
}
}
ddef
/try1 metatry ddef
/try2 metatry ddef
try1
try2
try1
try2
produces output
try1
local Count = 1
global Count = 100
whichever Count = 1
try2
local Count = 1
global Count = 101
whichever Count = 1
try1
local Count = 2
global Count = 102
whichever Count = 2
try2
local Count = 2
global Count = 103
whichever Count = 2
--- Synchronet 3.21d-Linux NewsLink 1.2