-
Notifications
You must be signed in to change notification settings - Fork 4
Variables
Variables not only serve as storage containers for values and references, they are indicators of how and when their respective values are swept by the garbage collector.
A standard variable, declared by placing a value and set token following a identifier token (id = 100), creates a variable that will:
-
Not be destroyed until it's parent call frame is finished. Note that in REPL mode, FastCode maintains the bottom-most call frame for the duration of the interpreter session.
a = 10In the above example,a's value10will not be destroyed until the program ends.proc myfunc() { b = 7In the above example
b's value7will not be destroyed untilmyfunc()has returned and concluded. Note thatacannot be accessed withinmyfunc()due to access protection. -
Cannot be accessed outside of it's call frame.
a = 10 proc myfunc() { print(a) }In the above example, calling
myfunc()will invoke aVARIABLE_NOT_DEFINEDerror because of access protection.acannot be accessed outside of it's call frame.
A static variable, can be created by inserting a static token before an identifier, set, and value token (static myvar = 10). Static variables...
-
Lack access protection
static a = 10 proc myfunc() { print(a) }In the above example,
acan be referenced inmyfuncbecause static variables do not have access protection. -
Maintain an "infinite" lifetime. The garbage collector does not destroy it's value until the end of the program.
Constant variables are special because they are primarily handled by the preprocessor, not the interpreter. The preprocessor substitutes every proceeding reference after it's initial declaration with the value it's assigned. This way, constant variables do not interact nor place additional computational burden upon the garbage collector and interpreter. Note that they cannot be assigned to. They can be declared by inserting a constant token before an identifier, set, and value token (const myconst = 100).
const pi = 3.14159
area = 3^2 * pi
Note that the preceding example becomes the following example after preprocessing.
area = 3^2 * 3.14159
const pi = 3.14159
pi = 3.14
Note that the preceding example invokes an unexpected token error because it translates into the following example after preprocessing.
3.14159 = 3.14
Note that you cannot assign a value to another value.
For further reading, please go to this wiki page about garbage collection which further elaborates on specific behavior and garbage collection sweeps.