Class: Environment

Environment(parent)

The key to correct execution is to properly maintain the environment — a structure holding variable bindings. It will be passed as an argument to our evaluate function. Each time we enter a "lambda" node we must extend the environment with new variables (function's arguments) and initialize them with values passed at run time. If an argument shadows a variable from the outer scope (I'll use words scope and environment interchangeably here) we must be careful to restore the previous value when we leave the function.

Constructor

new Environment(parent)

Creates an instance of Environment.
Parameters:
Name Type Description
parent Object | Environment extended env
Source:

Methods

def(name, value) → {any}

this function creates (or shadows, or overwrites) a variable in the current scope
Parameters:
Name Type Description
name String variable
value any variable's value
Source:
Returns:
value
Type
any

extend() → {Environment}

to create a subscope
Source:
Returns:
extended env
Type
Environment

get(name) → {any}

to get the current value of a variable
Parameters:
Name Type Description
name String variable
Source:
Returns:
value
Type
any

lookup(name) → {Environment}

to find the scope where the variable with the given name is defined
Parameters:
Name Type Description
name String variable
Source:
Returns:
scope
Type
Environment

set(name, value) → {any}

to set the value of a variable. This needs to lookup the actual scope where the variable is defined.
Parameters:
Name Type Description
name String varaible
value any vraible's value
Source:
Returns:
value
Type
any