| |
- builtins.object
-
- Environment
class Environment(builtins.object) |
|
Environment(enclosing=None)
Environment class
Attributes:
_enclosing (Environment or NoneType): The parent environment.
None is given when this
instance is root.
_variables (dict): Variable name and value are stored.
UNDEFINED (tuple): It is used to represent undefined variable.
RESULT_NAME (str): It is used to assign particular value as
result by ':='.
FUNCTION_RETURN_NAME (str): It is used for "return" statement.
Firstly, when anonymous/pure function
starts, it creates this variable whose
value is UNDEFINED.
Secondly, "return" statement sets value
and evaluation goes back until finishing
function. When the returned value is
given like "return(xxxx)", its value is
set. Otherwise, this name is set as
value.
For example, the function "test" creates
this variable. Then "return 'zero'" sets
"zero" into this variable and evaluation
goes back to assignment of "x".
function test(a) {
if (a == 0) {
return 'zero'
}
:
:
}
x = test(0)
BREAK_NAME (str): It is used for "break" statement.
Firstly, when loop starts, it creates this
variable whose value is UNDEFINED.
Secondly, "break" statement sets any value
and evaluation goes back until finishing
loop.
CONTINUE_NAME (str): It is used for "continue" statement.
Firstly, when loop starts, it creates this
variable whose value is UNDEFINED.
Secondly, "continue" statement sets any
value and evaluation goes back until
starting loop. |
|
Methods defined here:
- __init__(self, enclosing=None)
- Initialize class.
Args:
enclosing (Environment or NoneType, optional): The parent
environment. None is given when this
instance is root.
The default is None.
- get(self, name=None, only_this_scope=False)
- Get variable's value.
Get variable's value.
When it is not existed in this scope, it is tried in the parent
scope.
None is returned if it is not existed in the whole scope.
When name is None, the whole values are returned as dict.
However it is excluded when the prefix of variable is "_" or
value is the definition of function (CalleeRegistry).
Note that a result value of ":=" statement had been registered,
it is returned.
Args:
name (str, optional): Variable name.
When this is None, the whole values
are returned as dict. However it is
excluded when the prefix of variable
is "_" or value is the function
(CalleeRegistry).
Note that a result value of ":="
statement had been registered, it is
returned.
The default is None.
only_this_scope (bool, optional): When it is True, getting
scope is limited only here.
The default is False.
Returns:
Any: Its value.
Environment.UNDEFINED is returned if the given name's
variable is not existed.
- get_enclosing(self)
- Get enclosing.
Returns:
Environment or NoneType: enclosing that is the parent
Environment.
None if enclosing is not
available. In other words,
this is root Environment
if None is returned.
- modify(self, name, value)
- Modify a variable.
Modify a variable.
When the given variable is not existed in this scope, it is
tried in the parent scope.
Args:
name (str): Variable name
value (Any): Its value
Returns:
bool: True if modifying is completed,
False otherwise.
- remove(self, name, only_this_scope=False)
- Remove a variable.
Args:
name (str): Variable name.
only_this_scope (bool, optional): When it is True, removing
scope is limited only here.
The default is False.
Returns:
bool: True if removing is completed,
False otherwise.
- set(self, name, value, only_this_scope=False)
- Set a variable.
Modify/Assign a variable.
Firstly, modifying is tried. When the given variable is not
existed in this scope, it is tried in the parent scope.
When it is not existed in the whole scope, it is assigned in
this scope newly.
Args:
name (str or NoneType): Variable name.
When None is given, value is set
as the returned value.
value (Any): Its value
only_this_scope (bool, optional): When it is True, setting
scope is limited only here.
The default is False.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes defined here:
- BREAK_NAME = '___#BREAK#___'
- CONTINUE_NAME = '___#CONTINUE#___'
- FUNCTION_RETURN_NAME = '___#RETURN#___'
- RESULT_NAME = '___#RESULT#___'
- UNDEFINED = ()
| |