Skip to content

Variables

TheRealMichaelWang edited this page May 9, 2021 · 1 revision

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.

Standard Variable

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 = 10 In the above example, a's value 10 will not be destroyed until the program ends.

    proc myfunc() {
      b = 7
    

    In the above example b's value 7 will not be destroyed until myfunc() has returned and concluded. Note that a cannot be accessed within myfunc() 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 a VARIABLE_NOT_DEFINED error because of access protection. a cannot be accessed outside of it's call frame.

Static Variables

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, a can be referenced in myfunc because 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"

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.

Clone this wiki locally