Thanks for the help, and I don't use LISP, at the moment I only know python.
I asked, because LISP sort of looks like that. An endless sea of open braces, with dynamic scoping.
Aye, Python doesn;t use curly spaces, so soping certainly looks different.
In ZScript, as in C, scope is purely defined:
In instruction blocks, by open and closed curly braces, '{' and '}',
In statements, or function calls/declarations, by open and closed parens '(' and ')' and
In array index nests, by open and closed braces '[', and ']'.
Thus...
//! file scope: global values and global functions are at this scope.
int GlobalValueN; //This variable is available to all scripts, and functions.
int ptrs[4096]; //global array. Always available. use these, and not simple vars:
//ZScript may only have 255 vars in use, and this cap is reduced by function calls
//and other things, but you may have 4095 arrays in use, each with a maximum
//size of 214747, which gives you an effective 879,603,712 variables, instead of 255.
//! file scope breaks here, and we enter the script scope of ffc script foo:
ffc script foo{ //! left curly brace (LBRACE) opens the scope
void run() { //! function declaration, with left and right parens for its params, and a LBRACE to open the scope
//Array declarations:
int arr[16]; //declare an array with a size of 16 indices
int arrB[10]; //declare a second array, size of 10.
int arrC[2]; //third array, size of 2.
//These are available to this script, and any scope at the level of run(), or lower...
// **AND** they can be made available to other scripts, because they have array pointers.
bool run = false; //Only available to this script.
arr[0] = arrbB; //store the pointer for arrB in arrC[0];
arr[1] = 100;
arrC[0] = arr; //store the pointer for arr in arrC[0].
arrC[1] = arrB; //and store the pointer to arrB.
arrC[0[0]] = 10; //! Nested braces for array usage.
ptrs[0] = arrC; //Now we can also reference any of these arrays globally, until they become invalid, because we stored their pointers globally.
//Note that this does not follow 2D array syntax from C.
while ( --arrC[0[0]] >= 0 && ( arr[1] > 0 || run == true ) ) { //! new scope
//A statement with its condition
//in a nested statement series, with nests handled by parens.
//Priority with parens works as with standard maths.
arr[1] = DoSomething(arrC[0]); //! Call a function, and put the param inside the parens.
if ( arr[1] <= 0 ) { //! This is another scope, this time under while()
run = true;
} //! end scope of the if statement
if ( run === true ) DoOtherThing(); //! Another scope, under while, without
// curly braces. The parens define the scope here.
Waitframe();
}
}
// This function is at the SCRIPT scope, and thus local to the script.
// This is similar to how a class function works in C, and you can call
// it externally with a dereference to the script.
//it needs scope resolution if you call it externally.
int DoSomething(int v){ //declare a function with one input.
return v + 100;
} //! end this the while loop scope
}//! end script scope
//! resume file scope
//! This function is at a file (global) scope, and available to everything without needing scope resolution.
void DoSomethingElse(int val){
//use the dot for scope resolution to call a function that is at the scope of a script.
GlobalValueN = foo.DoSomething(val); //set the global value by first referencing the
//function local to the script foo 'DoSomething()'
int aptr = ptrs[0]; //Grab the pointer to make referencing it easier, and to avoid the parser complaining.
if ( aptr != 0 ) { //new scope level inside this function to ensure that the pointer is valid
aptr[1[5]] val - 100; //This stores the value into the array arrB[5] in the script 'foo'.
}
}
I hope that is a useful demonstration, and not merely a confusing mess. I intend to give this sort of thing a good few pages in the revised ZScript docs, with yet more pages of working samples of this sort of thing, in use.
Edited by ZoriaRPG, 08 January 2017 - 11:20 AM.