Here's an example of how to create dynamic local variables:
Code:
locvars:={};
locname:={};
NEWLOC();
LSTO();
LRCL();
LSTON();
LRCLN();
EXPORT DYNLOC()
BEGIN
// an example
local mylocal;
mylocal:=1;
NEWLOC("cool",0);
LSTO("cool",-5);
NEWLOC("alist",1);
LSTO("alist",{1,2,3});
mylocal:=LRCL("cool")*3;
LRCL("alist")*mylocal+LRCLN("alist",1);
END;
// name is a string
// vtype = 0 --> atomic;
NEWLOC(name,vtype)
BEGIN
local j;
j:=POS(locname,name);
if vtype then
locvars(j):={0};
else
locvars(j):=0;
end;
locname(j):=name;
END;
// name is a string
// val is the value to store
// n is the index (0 if atomic)
LSTON(name,val,n)
BEGIN
local j;
j:=POS(locname,name);
if j then
if n then
if n>size(locvars(j)) then
MSGBOX("Invalid index: " + name + "(" + n + ")");
kill;
end;
locvars(j,n):=val;
else
locvars(j):=val;
end;
else
MSGBOX("Error: no such local var (" + name + ")");
kill;
end;
END;
// name is a string
// n is the index (0 if atomic)
LRCLN(name,n)
BEGIN
local j;
j:=POS(locname,name);
if j then
if n then
if n>size(locvars(j)) then
MSGBOX("Invalid index " + name + "(" + n + ")");
kill;
end;
return(locvars(j,n));
else
return(locvars(j));
end;
else
MSGBOX("Error: no such local var (" + name + ")");
kill;
end;
END;
LSTO(name,val)
BEGIN
local j;
j:=POS(locname,name);
if j then
locvars(j):=val;
else
MSGBOX("Error: no such local var (" + name + ")");
kill;
end;
END;
LRCL(name)
BEGIN
local j;
j:=POS(locname,name);
if j then
return(locvars(j));
else
MSGBOX("Error: no such local var (" + name + ")");
kill;
end;
END;
Explanation to come a bit later; heading home from work.