Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Scope_modifiers
note: Basic language learning

View file

@ -0,0 +1,8 @@
Most programming languages offer support for [[Creating a function|subroutines]].
When execution changes between subroutines, different sets of variables and functions ("scopes") are available to the program.
Frequently these sets are defined by the placement of the variable and function declarations ("static scoping" or "lexical scoping").
These sets may also be defined by special modifiers to the variable and function declarations.
Show the different scope modifiers available in your language and briefly explain how they change the scope of their variable or function.
If your language has no scope modifiers, note it.

View file

@ -0,0 +1,19 @@
V x = From global scope
F outerfunc()
V x = From scope at outerfunc
F scoped_local()
V x = scope local
R scoped_local scope gives x = x
print(scoped_local())
F scoped_nonlocal()
R scoped_nonlocal scope gives x = @x
print(scoped_nonlocal())
F scoped_global()
R scoped_global scope gives x = :x
print(scoped_global())
outerfunc()

View file

@ -0,0 +1,22 @@
macro LDIR,source,dest,count
;LoaD, Increment, Repeat
lda #<source
sta $00
lda #>source
sta $01
lda #<dest
sta $02
lda #>dest
sta $03
ldx count
ldy #0
\@: ;this is a local label
lda ($00),y ;load a byte from the source address
sta ($02),y ;store in destination address
iny ;increment
dex
bne \@ ;repeat until x=0
endm

View file

@ -0,0 +1,6 @@
foo:
MOVE.L #$DEADBEEF,D0
MOVE.L #$16-1,D1
.bar:
DBRA D1,.bar ;any code outside "foo" cannot JMP, Bxx, BRA, or JSR/BSR here by using the name ".bar"
RTS

View file

@ -0,0 +1,5 @@
package P is
... -- Declarations placed here are publicly visible
private
... -- These declarations are visible only to the children of P
end P;

View file

@ -0,0 +1,11 @@
package P is
type T is private; -- No components visible
procedure F (X : in out T); -- The only visible operation
N : constant T; -- A constant, which value is hidden
private
type T is record -- The implementation, visible to children only
Component : Integer;
end record;
procedure V (X : in out T); -- Operation used only by children
N : constant T := (Component => 0); -- Constant implementation
end P;

View file

@ -0,0 +1,4 @@
package body P is
-- The implementation of P, invisible to anybody
procedure W (X : in out T); -- Operation used only internally
end P;

View file

@ -0,0 +1,5 @@
private package P.Q is
... -- Visible to the siblings only
private
... -- Visible to the children only
end P.Q;

View file

@ -0,0 +1,5 @@
10 X = 1
20 DEF FN F(X) = X
30 DEF FN G(N) = X
40 PRINT FN F(2)
50 PRINT FN G(3)

View file

@ -0,0 +1,25 @@
singleton = "global variable"
assume_global()
{
Global ; assume all variables declared in this function are global in scope
Static callcount := 0 ; except this one declared static, initialized once only
MsgBox % singleton ; usefull to initialize a bunch of singletons
callcount++
}
assume_global2()
{
Local var1 ; assume global except for var1 (similar to global scope declaration)
MsgBox % singleton
}
object(member, value = 0, null = 0)
{
Static ; assume all variables in this function to be static
If value ; can be used to simulate objects
_%member% := value
Else If null
_%member% := ""
Return (_%member%)
}

View file

@ -0,0 +1,33 @@
var1$ = "Global1"
var2$ = "Global2"
PRINT "Before function call:"
PRINT "var1$ = """ var1$ """"
PRINT "var2$ = """ var2$ """"
PROCtestscope(var1$)
PROCtestscope(var1$)
PRINT "After function call:"
PRINT "var1$ = """ var1$ """"
PRINT "var2$ = """ var2$ """"
END
DEF PROCtestscope(var2$)
PRINT "On entry to function:"
PRINT "var1$ = """ var1$ """"
PRINT "var2$ = """ var2$ """"
LOCAL var1$
PRIVATE var2$
PRINT "After LOCAL/PRIVATE:"
PRINT "var1$ = """ var1$ """"
PRINT "var2$ = """ var2$ """"
var1$ = "Local"
var2$ = "Private"
PRINT "After assignments:"
PRINT "var1$ = """ var1$ """"
PRINT "var2$ = """ var2$ """"
ENDPROC

View file

@ -0,0 +1,40 @@
define g(a) {
auto b
b = 3
"Inside g: a = "; a
"Inside g: b = "; b
"Inside g: c = "; c
"Inside g: d = "; d
a = 3; b = 3; c = 3; d = 3
}
define f(a) {
auto b, c
b = 2; c = 2
"Inside f (before call): a = "; a
"Inside f (before call): b = "; b
"Inside f (before call): c = "; c
"Inside f (before call): d = "; d
x = g(2) /* Assignment prevents output of the return value */
"Inside f (after call): a = "; a
"Inside f (after call): b = "; b
"Inside f (after call): c = "; c
"Inside f (after call): d = "; d
a = 2; b = 2; c = 2; d = 2
}
a = 1; b = 1; c = 1; d = 1
"Global scope (before call): a = "; a
"Global scope (before call): b = "; b
"Global scope (before call): c = "; c
"Global scope (before call): d = "; d
x = f(1)
"Global scope (before call): a = "; a
"Global scope (before call): b = "; b
"Global scope (before call): c = "; c
"Global scope (before call): d = "; d

View file

@ -0,0 +1,13 @@
67:?x {x has global scope}
& 77:?y { y has global scope }
& ( double
=
. !y+!y { y refers to the variable declared in myFunc, which
shadows the global variable with the same name }
)
& ( myFunc
= y,z { y and z have dynamic scope. z is never used. }
. !arg:?y { arg is dynamically scoped }
& double$
& !x+!y
)

View file

@ -0,0 +1 @@
/('(x./('(y.$x+$y))$3))$5 { x and y have lexical scope }

View file

@ -0,0 +1,18 @@
public //visible to anything.
protected //visible to current class and to derived classes.
internal //visible to anything inside the same assembly (.dll/.exe).
protected internal //visible to anything inside the same assembly and also to derived classes outside the assembly.
private //visible only to the current class.
//C# 7.2 adds:
private protected //visible to current class and to derived classes inside the same assembly.
// | | subclass | other class || subclass | other class
//Modifier | class | in same assembly | in same assembly || outside assembly | outside assembly
//-------------------------------------------------------------------------------------------------------
//public | Yes | Yes | Yes || Yes | Yes
//protected internal | Yes | Yes | Yes || Yes | No
//protected | Yes | Yes | No || Yes | No
//internal | Yes | Yes | Yes || No | No
//private | Yes | No | No || No | No
// C# 7.2:
//private protected | Yes | Yes | No || No | No

View file

@ -0,0 +1,21 @@
public interface IPrinter
{
void Print();
}
public class IntPrinter : IPrinter
{
void IPrinter.Print() { // explicit implementation
Console.WriteLine(123);
}
public static void Main() {
//====Error====
IntPrinter p = new IntPrinter();
p.Print();
//====Valid====
IPrinter p = new IntPrinter();
p.Print();
}
}

View file

@ -0,0 +1,24 @@
int a; // a is global
static int p; // p is "locale" and can be seen only from file1.c
extern float v; // a global declared somewhere else
// a "global" function
int code(int arg)
{
int myp; // 1) this can be seen only from inside code
// 2) In recursive code this variable will be in a
// different stack frame (like a closure)
static int myc; // 3) still a variable that can be seen only from
// inside code, but its value will be kept
// among different code calls
// 4) In recursive code this variable will be the
// same in every stack frame - a significant scoping difference
}
// a "local" function; can be seen only inside file1.c
static void code2(void)
{
v = v * 1.02; // update global v
// ...
}

View file

@ -0,0 +1,8 @@
float v; // a global to be used from file1.c too
static int p; // a file-scoped p; nothing to share with static p
// in file1.c
int code(int); // this is enough to be able to use global code defined in file1.c
// normally these things go into a header.h
// ...

View file

@ -0,0 +1,12 @@
;; *bug* shall have a dynamic binding.
(declaim (special *bug*))
(let ((shape "triangle") (*bug* "ant"))
(flet ((speak ()
(format t "~% There is some ~A in my ~A!" *bug* shape)))
(format t "~%Put ~A in your ~A..." *bug* shape)
(speak)
(let ((shape "circle") (*bug* "cockroach"))
(format t "~%Put ~A in your ~A..." *bug* shape)
(speak))))

View file

@ -0,0 +1 @@
private

View file

@ -0,0 +1 @@
protected

View file

@ -0,0 +1 @@
public

View file

@ -0,0 +1 @@
protected

View file

@ -0,0 +1 @@
automated

View file

@ -0,0 +1,2 @@
strict private
strict protected

View file

@ -0,0 +1,21 @@
feature
some_procedure(int: INTEGER; char: CHARACTER)
local
r: REAL
i: INTEGER
do
-- r, i and s have scope here
-- as well as int and char
-- some_procedure and some_function additionally have scope here
end
s: STRING
some_function(int: INTEGER): INTEGER
do
-- s and Result have scope here
-- as well as int (int here differs from the int of some_procedure)
-- some_procedure and some_function additionally have scope here
end
-- s, some_procedure and some_function have scope here

View file

@ -0,0 +1,5 @@
pi # private
pi = 3.14159
sum # private
sum x y = x + y

View file

@ -0,0 +1,9 @@
-module( a_module ).
-export( [double/1] ).
double( N ) -> add( N, N ).
add( N, N ) -> N + N.

View file

@ -0,0 +1,26 @@
'Declares a integer variable and reserves memory to accommodate it
Dim As Integer baseAge = 10
'Define a variable that has static storage
Static As String person
person = "Amy"
'Declare variables that are both accessible inside and outside procedures
Dim Shared As String friend
friend = "Susan"
Dim Shared As Integer ageDiff = 3
Dim Shared As Integer extraYears = 5
Sub test()
'Declares a integer variable and reserves memory to accommodate it
Dim As Integer baseAge = 30
'Define a variable that has static storage
Static As String person
person = "Bob"
'Declare a local variable distinct from a variable with global scope having the same name
Static As Integer extraYears = 2
Print person; " and "; friend; " are"; baseAge; " and"; baseAge + ageDiff + extraYears; " years old."
End Sub
test()
Print person; " and "; friend; " are"; baseAge; " and"; baseAge + ageDiff + extraYears; " years old."
Sleep

View file

@ -0,0 +1,6 @@
global var1 # used outside of procedures
procedure one() # a global procedure (the only kind)
local var2 # used inside of procedures
static var3 # also used inside of procedures
end

View file

@ -0,0 +1,17 @@
A=: 1
B=: 2
C=: 3
F=: verb define
A=:4
B=.5
D=.6
A+B+C+D
)
F ''
18
A
4
B
2
D
|value error

View file

@ -0,0 +1,35 @@
public //any class may access this member directly
protected //only this class, subclasses of this class,
//and classes in the same package may access this member directly
private //only this class may access this member directly
static //for use with other modifiers
//limits this member to one reference for the entire JVM
//adding no modifier (sometimes called "friendly") allows access to the member by classes in the same package
// Modifier | Class | Package | Subclass | World
// ------------|-------|---------|----------|-------
// public | Y | Y | Y | Y
// protected | Y | Y | Y | N
// no modifier | Y | Y | N | N
// private | Y | N | N | N
//method parameters are available inside the entire method
//Other declarations follow lexical scoping,
//being in the scope of the innermost set of braces ({}) to them.
//You may also create local scopes by surrounding blocks of code with braces.
public void function(int x){
//can use x here
int y;
//can use x and y here
{
int z;
//can use x, y, and z here
}
//can use x and y here, but NOT z
}

View file

@ -0,0 +1,12 @@
julia> function foo(n)
x = 0
for i = 1:n
local x # introduce a loop-local x
x = i
end
x
end
foo (generic function with 1 method)
julia> foo(10)
0

View file

@ -0,0 +1,22 @@
// version 1.1.2
class SomeClass {
val id: Int
companion object {
private var lastId = 0
val objectsCreated get() = lastId
}
init {
id = ++lastId
}
}
fun main(args: Array<String>) {
val sc1 = SomeClass()
val sc2 = SomeClass()
println(sc1.id)
println(sc2.id)
println(SomeClass.objectsCreated)
}

View file

@ -0,0 +1,15 @@
make "g 5 ; global
to proc :p
make "h 4 ; also global
local "l ; local, no initial value
localmake "m 3
sub 7
end
to sub :s
; can see :g, :h, and :s
; if called from proc, can also see :l and :m
localmake "h 5 ; hides global :h within this procedure and those it calls
end

View file

@ -0,0 +1,14 @@
:- public(foo/1). % predicate can be called from anywhere
:- protected(bar/2). % predicate can be called from the declaring entity and its descendants
:- private(baz/3). % predicate can only be called from the declaring entity
:- object(object, % predicates declared in the protocol become private for the object
implements(private::protocol)).
:- category(object, % predicates declared in the protocol become protected for the category
implements(protected::protocol)).
:- protocol(extended, % no change to the scope of the predicates inherited from the extended protocol
extends(public::minimal)).

View file

@ -0,0 +1,16 @@
foo = "global" -- global scope
print(foo)
local foo = "local module" -- local to the current block (which is the module)
print(foo) -- local obscures the global
print(_G.foo) -- but global still exists
do -- create a new block
print(foo) -- outer module-level scope still visible
local foo = "local block" -- local to the current block (which is this "do")
print(foo) -- obscures outer module-level local
for foo = 1,2 do -- create another more-inner scope
print("local for "..foo) -- obscures prior block-level local
end -- and close the scope
print(foo) -- prior block-level local still exists
end -- close the block (and thus its scope)
print(foo) -- module-level local still exists
print(_G.foo) -- global still exists

View file

@ -0,0 +1,77 @@
Module Checkit {
M=1000
Function Global xz {
=9999
}
Module TopModule {
\\ clear vars and static vars
Clear
M=500
Function Global xz {
=10000
}
Module Kappa {
Static N=1
Global M=1234
x=1
z=1
k=1
Group Alfa {
Private:
x=10, z=20, m=100
Function xz {
=.x*.z+M
}
Public:
k=50
Module AddOne {
.x++
.z++
.k++
Print .xz(), .m=100
}
Module ResetValues {
\\ use <= to change members, else using = we define local variables
.x<=10
.z<=20
}
}
' print 1465
Alfa.AddOne
Print x=1, z=1, k=1, xz()=10000
Print N ' 1 first time, 2 second time
N++
Push Alfa
}
Kappa
Drop ' drop one alfa
Kappa
Print M=500
' leave one alfa in stack of values
}
TopModule
Read AlfaNew
Try ok {
AlfaNew.AddOne
}
\\ we get an error because M global not exist now
\\ here M is Local.
If Error or Not Ok Then Print Error$ ' Uknown M in .xz() in AlfaNew.AddOne
Print M=1000, xz()=9999
For AlfaNew {
Global M=1234
.ResetValues
.AddOne ' now works because M exist as global, for this block
}
Print M=1000, xz()=9999
For This {
Local M=50
M++
Print M=51
}
Print M=1000
}
Checkit
List ' list of variables are empty
Modules ? ' list of modules show two: A and A.Checkit
Print Module$ ' print A

View file

@ -0,0 +1,54 @@
Module CheckIt {
Module CheckSub {
Read Z
M=5000
Module CheckThis {
Z=500
Hello("Bob")
}
Function CheckFun {
Z=50
Hello("Mary")
}
Call CheckFun()
CheckThis
Hello("George")
Gosub label1
\\ sub work as exit here
Sub Hello(a$)
\\ any new definition erased at exit of sub
Local M=100
Print "Hello ";a$, Z, M
End Sub
label1:
\\ this light subs have no "erased new definition mode"
\\ they are like code of module
Print Z, M
Return
}
CheckSub 10
Module CheckOther {
Z=1000
Hello("John")
}
\\ we can replace CheckThis with CheckOther
CheckSub 20; CheckThis as CheckOther
}
Call Checkit
Module Alfa {
x=1
Thread {
x++
} as K interval 20
Thread {
PrintMe()
} as J interval 20
Main.Task 20 {
if x>99 then exit
}
Wait 100
Sub PrintMe()
Print x
End Sub
}
Call Alfa

View file

@ -0,0 +1,15 @@
OUTER
SET OUT=1,IN=0
WRITE "OUT = ",OUT,!
WRITE "IN = ",IN,!
DO INNER
WRITE:$DATA(OUT)=0 "OUT was destroyed",!
QUIT
INNER
WRITE "OUT (inner scope) = ",OUT,!
WRITE "IN (outer scope) = ",IN,!
NEW IN
SET IN=3.14
WRITE "IN (inner scope) = ",IN,!
KILL OUT
QUIT

View file

@ -0,0 +1,14 @@
Module -> localize names of variables (lexical scoping)
Block -> localize values of variables (dynamic scoping)
Module creates new symbols:
Module[{x}, Print[x];
Module[{x}, Print[x]]
]
->x$119
->x$120
Block localizes values only; it does not create new symbols:
x = 7;
Block[{x=0}, Print[x]]
Print[x]
->0
->7

View file

@ -0,0 +1,6 @@
proc foo = echo "foo" # hidden
proc bar* = echo "bar" # acessible
type MyObject = object
name*: string # accessible
secretAge: int # hidden

View file

@ -0,0 +1,29 @@
procedure super;
var
f: boolean;
procedure nestedProcedure;
var
c: char;
begin
// here, `f`, `c`, `nestedProcedure` and `super` are available
end;
procedure commonTask;
var
f: boolean;
begin
// here, `super`, `commonTask` and _only_ the _local_ `f` is available
end;
var
c: char;
procedure fooBar;
begin
// here, `super`, `fooBar`, `f` and `c` are available
end;
var
x: integer;
begin
// here, `c`, `f`, and `x`, as well as,
// `nestedProcedure`, `commonTask`, `fooBar` and `super` are available
end;

View file

@ -0,0 +1,9 @@
use strict;
$x = 1; # Compilation error.
our $y = 2;
print "$y\n"; # Legal; refers to $main::y.
package Foo;
our $z = 3;
package Bar;
print "$z\n"; # Refers to $Foo::z.

View file

@ -0,0 +1,13 @@
package Foo;
my $fruit = 'apple';
package Bar;
print "$fruit\n"; # Prints "apple".
{
my $fruit = 'banana';
print "$fruit\n"; # Prints "banana".
}
print "$fruit\n"; # Prints "apple".
# The second $fruit has been destroyed.
our $fruit = 'orange';
print "$fruit\n"; # Prints "orange"; refers to $Bar::fruit.
# The first $fruit is inaccessible.

View file

@ -0,0 +1,10 @@
use 5.10.0;
sub count_up
{
state $foo = 13;
say $foo++;
}
count_up; # Prints "13".
count_up; # Prints "14".

View file

@ -0,0 +1,17 @@
our $camelid = 'llama';
sub phooey
{
print "$camelid\n";
}
phooey; # Prints "llama".
sub do_phooey
{
local $camelid = 'alpaca';
phooey;
}
do_phooey; # Prints "alpaca".
phooey; # Prints "llama".

View file

@ -0,0 +1,12 @@
-->
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">localf</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- not normally necesssary, but will not harm</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">globalf</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- ""</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">localf</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">globalf</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--

View file

@ -0,0 +1,7 @@
-->
<span style="color: #008080;">include</span> <span style="color: #000000;">somefile</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000000;">as</span> <span style="color: #000000;">xxx</span>
<span style="color: #000080;font-style:italic;">-- alternatively, within somefile.e:</span>
<span style="color: #7060A8;">namespace</span> <span style="color: #000000;">xxx</span> <span style="color: #000080;font-style:italic;">-- (only supported in Phix for compatibility with OpenEuphoria)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xxx</span><span style="color: #0000FF;">:</span><span style="color: #000000;">globalf</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- call a global function named globalf, specifically the one declared in somefile.e</span>
<!--

View file

@ -0,0 +1,6 @@
$a = "foo" # global scope
function test {
$a = "bar" # local scope
Write-Host Local: $a # "bar" - local variable
Write-Host Global: $global:a # "foo" - global variable
}

View file

@ -0,0 +1,32 @@
;define a local integer variable by simply using it
baseAge.i = 10
;explicitly define local strings
Define person.s = "Amy", friend.s = "Susan"
;define variables that are both accessible inside and outside procedures
Global ageDiff = 3
Global extraYears = 5
Procedure test()
;define a local integer variable by simply using it
baseAge.i = 30
;explicitly define a local string
Define person.s = "Bob"
;allow access to a local variable in the main body of code
Shared friend
;create a local variable distinct from a variable with global scope having the same name
Protected extraYears = 2
PrintN(person + " and " + friend + " are " + Str(baseAge) + " and " + Str(baseAge + ageDiff + extraYears) + " years old.")
EndProcedure
If OpenConsole()
test()
PrintN(person + " and " + friend + " are " + Str(baseAge) + " and " + Str(baseAge + ageDiff + extraYears) + " years old.")
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,30 @@
>>> x="From global scope"
>>> def outerfunc():
x = "From scope at outerfunc"
def scoped_local():
x = "scope local"
return "scoped_local scope gives x = " + x
print(scoped_local())
def scoped_nonlocal():
nonlocal x
return "scoped_nonlocal scope gives x = " + x
print(scoped_nonlocal())
def scoped_global():
global x
return "scoped_global scope gives x = " + x
print(scoped_global())
def scoped_notdefinedlocally():
return "scoped_notdefinedlocally scope gives x = " + x
print(scoped_notdefinedlocally())
>>> outerfunc()
scoped_local scope gives x = scope local
scoped_nonlocal scope gives x = From scope at outerfunc
scoped_global scope gives x = From global scope
scoped_notdefinedlocally scope gives x = From global scope
>>>

View file

@ -0,0 +1,7 @@
X <- "global x"
f <- function() {
x <- "local x"
print(x) #"local x"
}
f() #prints "local x"
print(x) #prints "global x"

View file

@ -0,0 +1,5 @@
d <- data.frame(a=c(2,4,6), b = c(5,7,9))
attach(d)
b - a #success
detach(d)
b - a #produces error

View file

@ -0,0 +1,37 @@
x <- "global x"
print(x) #"global x"
local({ ## local({...}) is a shortcut for evalq({...}, envir=new.env())
## and is also equivalent to (function() {...})()
x <- "outer local x"
print(x) #"outer local x"
x <<- "modified global x"
print(x) #"outer local x" still
y <<- "created global y"
print(y) #"created global y"
local({
## Note, <<- is _not_ a global assignment operator. If an
## enclosing scope defines the variable, that enclosing scope gets
## the assignment. This happens in the order of evalution; a local
## variable may be defined later on in the same scope.
x <- "inner local x"
print(x) #"inner local x"
x <<- "modified outer local x"
print(x) #"inner local x"
y <<- "modified global y"
print(y) #"modified global y"
y <- "local y"
print(y) #"local y"
##this is the only way to reliably do a global assignment:
assign("x", "twice modified global x", globalenv())
print(evalq(x, globalenv())) #"twice modified global x"
})
print(x) #"modified outer local x"
})
print(x) #"twice modified global x"
print(y) #"modified global y"

View file

@ -0,0 +1,11 @@
x <- "global x"
f <- function() {
cat("Lexically enclosed x: ", x,"\n")
cat("Lexically enclosed x: ", evalq(x, parent.env(sys.frame())),"\n")
cat("Dynamically enclosed x: ", evalq(x, parent.frame()),"\n")
}
local({
x <- "local x"
f()
})

View file

@ -0,0 +1,12 @@
d <- data.frame(a=c(2,4,6), b = c(5,7,9))
also <- c(1, 0, 2)
with(d, mean(b - a + also)) #returns 4
## with() is built in, but you might have implemented it like this:
with.impl <- function(env, expr) {
env <- as.environment(env)
parent.env(env) <- parent.frame()
eval(substitute(expr), envir=env)
}
with.impl(d, mean(b - a + also))

View file

@ -0,0 +1,23 @@
/*REXX program to display scope modifiers (for subroutines/functions). */
a=1/4
b=20
c=3
d=5
call SSN_571 d**4
/* at this point, A is defined and equal to .25 */
/* at this point, B is defined and equal to 40 */
/* at this point, C is defined and equal to 27 */
/* at this point, D is defined and equal to 5 */
/* at this point, FF isn't defined. */
/* at this point, EWE is defined and equal to 'female sheep' */
/* at this point, G is defined and equal to 625 */
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────SSN_571 submarine, er, subroutine*/
SSN_571: procedure expose b c ewe g; parse arg g
b = b*2
c = c**3
ff = b+c
ewe = 'female sheep'
d = 55555555
return /*compliments to Jules Verne's Captain Nemo? */

View file

@ -0,0 +1,17 @@
a=1
b=2
c=3
Call p /* a Procedure */
Say 'in m a b c x' a b c x
Call s /* a subroutine */
Say 'in m a b c x' a b c x
Exit
p: Procedure Expose sigl b
Say 'in p sigl a b c' sigl a b c
Call s
Return
s:
Say 'in s sigl a b c' sigl a b c
x=4
Return

View file

@ -0,0 +1,4 @@
my $lexical-variable;
our $package-variable;
state $persistent-lexical;
has $.public-attribute;

View file

@ -0,0 +1,13 @@
sub a {
my $*dyn = 'a';
c();
}
sub b {
my $*dyn = 'b';
c();
}
sub c {
say $*dyn;
}
a(); # says a
b(); # says b

View file

@ -0,0 +1,9 @@
class Demo
#public methods here
protected
#protect methods here
private
#private methods
end

View file

@ -0,0 +1,3 @@
Local x
2 → x
Return x^x

View file

@ -0,0 +1,6 @@
@(maybe)@# perhaps this subclause suceeds or not
@ (block foo)
@ (bind a "a")
@ (accept foo)
@(end)
@(bind b "b")

View file

@ -0,0 +1,6 @@
@(maybe)@# perhaps this subclause suceeds or not
@ (block foo)
@ (bind a "a")
@ (fail foo)
@(end)
@(bind b "b")

View file

@ -0,0 +1,18 @@
set globalVar "This is a global variable"
namespace eval nsA {
variable varInA "This is a variable in nsA"
}
namespace eval nsB {
variable varInB "This is a variable in nsB"
proc showOff {varname} {
set localVar "This is a local variable"
global globalVar
variable varInB
namespace upvar ::nsA varInA varInA
puts "variable $varname holds \"[set $varname]\""
}
}
nsB::showOff globalVar
nsB::showOff varInA
nsB::showOff varInB
nsB::showOff localVar

View file

@ -0,0 +1,11 @@
oo::class create example {
# Note that this is otherwise syntactically the same as a local variable
variable objVar
constructor {} {
set objVar "This is an object variable"
}
method showOff {} {
puts "variable objVar holds \"$objVar\""
}
}
[example new] showOff

View file

@ -0,0 +1,4 @@
proc decr {varName {decrement 1}} {
upvar 1 $varName var
incr var [expr {-$decrement}]
}

View file

@ -0,0 +1,3 @@
proc semival args {
uplevel 1 [join $args ";"]
}

View file

@ -0,0 +1,15 @@
proc loop {varName from to body} {
upvar 1 $varName var
for {set var $from} {$var <= $to} {incr var} {
uplevel 1 $body
}
}
loop x 1 10 {
puts "x is now $x"
if {$x == 5} {
puts "breaking out..."
break
}
}
puts "done"

View file

@ -0,0 +1,11 @@
local_shop = 0
hidden_variable = 3
#library+
this_public_constant = local_shop
a_visible_function = +
#library-
for_local_people = 7

View file

@ -0,0 +1,10 @@
foo = 1
#hide+
foo = 2
bar = 3
#hide-
x = foo

View file

@ -0,0 +1,13 @@
foo = 1
#hide+
#export+
foo = 2
#export-
bar = 3
#hide-
x = foo

View file

@ -0,0 +1,4 @@
#import std
cat = 3
a_string = std-cat('foo','bar')

View file

@ -0,0 +1,10 @@
class MyClass {
construct new(a) {
_a = a // creates an instance field _a automatically
}
a { _a } // allow public access to the field
}
var mc = MyClass.new(3)
System.print(mc.a) // fine
System.print(mc._a) // can't access _a directly as its private to the class