interpreter.cse_machine package¶
Submodules¶
interpreter.cse_machine.control module¶
- class interpreter.cse_machine.control.Control(controlStruct)¶
Bases:
object
The control of the CSE machine.
- Methods:
removeRightMost() -> Symbol: Removes the rightmost element of the control. insertControlStruct(controlStruct: List[Symbol]): Inserts a control structure to the control. insertEnvMarker(env_index: int): Inserts an environment marker to the control.
- addGamma()¶
Inserts a gamma symbol to the control.
- insertControlStruct(controlStruct)¶
Inserts a control structure to the control.
- Args:
controlStruct: List[Symbol]
- insertEnvMarker(env_index: int)¶
Inserts an environment marker to the control.
- Args:
env_index: int
- removeRightMost()¶
Removes the rightmost element of the control.
Returns: Symbol | None
interpreter.cse_machine.control_structures module¶
- class interpreter.cse_machine.control_structures.CSInitializer(st: STNode)¶
Bases:
object
- init() ControlStructures ¶
Generates the control structures for the CSE machine from the Standardized Tree.
- Returns:
ControlStructures object containing the control structures.
- class interpreter.cse_machine.control_structures.ControlStruct(index)¶
Bases:
Iterable
Represents a control structure in the CSE machine.
Implemented as a List[Symbol]
- getIndex()¶
Returns the index of the control structure.
- class interpreter.cse_machine.control_structures.ControlStructures(control_structure_map: dict)¶
Bases:
object
A class which represents the control structures as an object.
- Attributes:
control_structure_map (dict): A dictionary of control structures.
- get(delta_index) ControlStruct ¶
Returns the control structure for the given delta index.
interpreter.cse_machine.environment module¶
- class interpreter.cse_machine.environment.Environment(envIndex, parent=None)¶
Bases:
object
Represents the environments of the CSE machine as a tree structure.
- Attributes:
envMarker (EnvMarkerSymbol): The environment marker symbol. parent (Environment): The parent environment. envData (dict): The data in the environment.
- Methods:
insertEnvData(name: str, value: Symbol): Inserts the values for the variables in the environment. lookUpEnv(name: str) -> Symbol: Looks up the relevant value for a given variable.
- getIndex()¶
- insertEnvData(name, value)¶
- lookUpEnv(name: str)¶
interpreter.cse_machine.exceptions module¶
- exception interpreter.cse_machine.exceptions.MachineException(message)¶
Bases:
Exception
interpreter.cse_machine.functions module¶
- class interpreter.cse_machine.functions.ConcFn¶
Bases:
DefinedFunction
The Concatenate function in the Primitive Environment. Concatenates the strings in the tuple
- run(args)¶
- class interpreter.cse_machine.functions.DefinedFunction(_DefinedFunction__name)¶
Bases:
object
A parent class for the defined functions in the Primitive Environment.
- getName()¶
- run(arg)¶
- class interpreter.cse_machine.functions.DefinedFunctions¶
Bases:
object
The functions defined in the Primitive Environment are represented here.
- CONC = 'Conc'¶
- ISDUMMY = 'Isdummy'¶
- ISFUNCTION = 'Isfunction'¶
- ISINTEGER = 'Isinteger'¶
- ISSTRING = 'Isstring'¶
- ISTRUTHVALUE = 'Istruthvalue'¶
- ISTUPLE = 'Istuple'¶
- ITOS = 'ItoS'¶
- ORDER = 'Order'¶
- PRINT = 'Print'¶
- STEM = 'Stem'¶
- STERN = 'Stern'¶
- static get_functions()¶
- isdefined()¶
- class interpreter.cse_machine.functions.FunctionFactory¶
Bases:
object
A factory class to create objects to define the functions of each predefined function.
- static create(name)¶
- class interpreter.cse_machine.functions.IsDummyFn¶
Bases:
DefinedFunction
The IsDummy function in the Primitive Environment. Checks if the argument is a dummy value.
- run(arg)¶
- class interpreter.cse_machine.functions.IsFunctionFn¶
Bases:
DefinedFunction
The IsFunction function in the Primitive Environment. Checks if the argument is a function.
- run(arg)¶
- class interpreter.cse_machine.functions.IsIntegerFn¶
Bases:
DefinedFunction
The IsInteger function in the Primitive Environment. Checks if the argument is an integer.
- run(arg)¶
- class interpreter.cse_machine.functions.IsStringFn¶
Bases:
DefinedFunction
The IsString function in the Primitive Environment. Checks if the argument is a string.
- run(arg)¶
- class interpreter.cse_machine.functions.IsTruthValueFn¶
Bases:
DefinedFunction
The IsTruthValue function in the Primitive Environment. Checks if the argument is a boolean value.
- run(arg)¶
- class interpreter.cse_machine.functions.IsTupleFn¶
Bases:
DefinedFunction
The IsTuple function in the Primitive Environment. Checks if the argument is a tuple.
- run(arg)¶
- class interpreter.cse_machine.functions.ItoSFn¶
Bases:
DefinedFunction
The ItoS function in the Primitive Environment. Converts the integer to string.
- run(arg)¶
- class interpreter.cse_machine.functions.OrderFn¶
Bases:
DefinedFunction
The Order function in the Primitive Environment. Returns the length of tuple
- run(arg)¶
- class interpreter.cse_machine.functions.PrintFn¶
Bases:
DefinedFunction
The Print function in the Primitive Environment.
- run(arg)¶
- class interpreter.cse_machine.functions.StemFn¶
Bases:
DefinedFunction
The Stem function in the Primitive Environment. Returns the first character of the string.
- run(arg)¶
- class interpreter.cse_machine.functions.SternFn¶
Bases:
DefinedFunction
The Stern function in the Primitive Environment. Returns the string without the first character.
- run(arg)¶
interpreter.cse_machine.st module¶
- class interpreter.cse_machine.st.STNode(value, left=None, right=None)¶
Bases:
ASTNode
A class representing a node in a Standardized Tree (ST).
The STNode class uses the left child right sibling representation to store the tree structure. Each node contains a reference to its node value, left child, and right sibling.
- static assign_node(left=None, right=None)¶
Creates a new assign node in the form of a FCRS node.
- static comma_node(left, right=None)¶
Creates a new comma node in the form of a FCRS node.
- static createFCRSNode(value, left: BinaryTreeNode = None, right: BinaryTreeNode = None)¶
Create node with left as first child and right as sibling of first child
- static gamma_node(left=None, right=None)¶
Creates a new gamma node in the form of a FCRS node.
- getChildrenCount()¶
- getSibilingCount()¶
- is_conditional()¶
- is_gamma()¶
- is_lambda()¶
- is_tau()¶
- static lambda_node(left=None, right=None)¶
Creates a new lambda node in the form of a FCRS node.
- parseValueInToken()¶
- static siblings(sibling_list)¶
Set the right sibling of each node in the list to the next node in the list
- Args:
sibling_list (List[STNode]): A list of nodes to be set as siblings.
- Returns:
The first node.
- static tau_node(left, right=None)¶
Creates a new tau node in the form of a FCRS node.
- static ystar_node()¶
Creates a new ystar node in the form of a FCRS node.
interpreter.cse_machine.stack module¶
- class interpreter.cse_machine.stack.Stack¶
Bases:
object
The stack to evaluate the CONTROL.
- Methods:
popStack() -> Symbol: Pops the top element from the stack. top() -> Symbol: Returns the top element of the stack. pushStack(symbol: Symbol): Pushes the symbol to the stack. removeEnvironment(envMarker: EnvMarkerSymbol): Removes the environment marker from the stack.
- removeEnvironment(envMarker: EnvMarkerSymbol)¶
interpreter.cse_machine.symbol module¶
- class interpreter.cse_machine.symbol.BetaSymbol¶
Bases:
Symbol
Represents a beta symbol.
Used when representing a conditonal operator in the control without standardizing.
- class interpreter.cse_machine.symbol.BinaryOperatorSymbol(operator)¶
Bases:
OperatorSymbol
Represents a binary operator symbol.
- class interpreter.cse_machine.symbol.DeltaSymbol(index)¶
Bases:
Symbol
Represents a control structure as a Symbol in the control.
- Attributes:
index (int): Points to relevant control structure in the control structure array.
- class interpreter.cse_machine.symbol.EnvMarkerSymbol(envIndex)¶
Bases:
Symbol
Represents an environment marker in the control and the stack.
- Attributes:
envIndex (int): The index of the environment marker.
- Methods:
__eq__(other) -> bool: Checks whether an instance of the EnvMarkerSymbol and then checks whether equal.
- class interpreter.cse_machine.symbol.EtaClosureSymbol(variables, index, envIndex)¶
Bases:
LambdaClosureSymbol
Represents an eta closure in the CSE machine. Extends the LambdaClosure class.
- Methods:
fromLambdaClosure(lambdaClosure:LambdaClosureSymbol) -> EtaClosureSymbol: Creates an eta closure from a lambda closure. toLambdaClosure(etaClosure) -> LambdaClosureSymbol: Converts an eta closure to a lambda closure.
- static fromLambdaClosure(lambdaClosure: LambdaClosureSymbol)¶
- static toLambdaClosure(etaClosure)¶
- class interpreter.cse_machine.symbol.FunctionSymbol(func)¶
Bases:
Symbol
Represents a function symbol.
- class interpreter.cse_machine.symbol.LambdaClosureSymbol(variables, index, envIndex)¶
Bases:
LambdaSymbol
Represents a lambda closure (lambda in the stack) in the CSE machine. Extends the Lambda class.
- Attributes:
envMarker (EnvMarkerSymbol): The environment marker the lambda closure.
- getEnvMarkerIndex()¶
- class interpreter.cse_machine.symbol.LambdaSymbol(index, variables: Iterable)¶
Bases:
Symbol
Represents a lambda Symbol,.
- Attributes:
index (int): The index of the lambda in the control structure array. variables (Iterable): The variables of the lambda.
- class interpreter.cse_machine.symbol.NameSymbol(name, is_id=False)¶
Bases:
Symbol
Represents variables and numerics as a symbol.
- Attributes:
name (str): The name of the symbol. nameType (type): The type of the symbol. is_id (bool): True if the symbol is an identifier.
- isFunction()¶
Returns True if the symbol is a function.
- isId()¶
Returns if the symbol is an identifier.
- static isPrimitive(nameType)¶
Returns True if the symbol is a string, integer or a bool.
- isString()¶
Returns True if the symbol is a string.
- static isTupleSymbol(nameType)¶
Returns True if the symbol is a TupleSymbol.
- static isValidType(nameType)¶
Returns True if the symbol is a valid type.
- class interpreter.cse_machine.symbol.OperatorSymbol(operator)¶
Bases:
Symbol
“Represents an operator symbol.
- class interpreter.cse_machine.symbol.Symbol¶
Bases:
object
Represents symbols in the CSE machine.
- isType(t)¶
- class interpreter.cse_machine.symbol.SymbolFactory¶
Bases:
object
A factory class to create symbols from the ST nodes.
- class interpreter.cse_machine.symbol.TauSymbol(n)¶
Bases:
Symbol
Represents a tau symbol.
Used when representing a tau node in the control.
- class interpreter.cse_machine.symbol.TupleSymbol(n, tupleList)¶
Bases:
TauSymbol
Represents a Tuple in the stack as an object.
Used in standardizing the tau node in the st.
- class interpreter.cse_machine.symbol.UnaryOperatorSymbol(operator)¶
Bases:
OperatorSymbol
Represents a unary operator symbol.
Module contents¶
- class interpreter.cse_machine.CSEMachine(st: STNode)¶
Bases:
object
The CSEMachine class is responsible for simulating the Control Structure Environment (CSE) machine used in the RPAL language interpreter. It manages the control structures, environment, and stack to intepret the RPAL code. This class provides methods to evaluate expressions based on the cse machine rules.
- Attributes:
st (STNode): The standardized tree which is used to generate control structures. csMap (dict): The map of control structures. ie. delta-0, delta-1, etc. control (Control): The control of the cse machine. envIndexCounter (int): The environment index counter use to create new environments - eg e0, e1, etc. envMap (dict): The map of environments in the cse machine. __envStack (Stack): The stack of environments. stack (Stack): The stack of the cse machine. logger (Logger): The logger object.
- applyFP(top: EtaClosureSymbol)¶
CSE Rule 13
This function handles the eta closure in the stack.
- applyFunction(top: FunctionSymbol)¶
CSE Rule 14 - Apply Predefined Function
- applyLambda(top: LambdaClosureSymbol)¶
CSE Rule 4 and CSE Rule 11
This function evaluates n-ary functions as well. Creates a new environment and make it the current environment. Also Inserts environment data for env_variables with the respective env_values.
- applyYStar()¶
CSE Rule 12
This function evaluates the Y* symbol.
- binop(operator)¶
CSE Rule 6
Evaluates Binary Operators and pushes the computed result into the stack.
- conditional()¶
CSE Rule 8
This evaluates the conditional expression. Conditional functions are defined in the control structure in the form of delta_then, delta_else, beta, B.
- currentEnv() Environment ¶
Returns the current environment.
- evaluate()¶
Evaluates the Control.
- exitEnv(env_marker)¶
CSE Rule 5
Exits from the current environment.
- get_arg(rand_symbol)¶
- stackLambda(_lambda: LambdaSymbol)¶
CSE Rule 2
Pushes a lambda closure into the stack.
- tupleSelection(tuple_: TupleSymbol)¶
CSE Rule 10
- unop(operator)¶
” CSE Rule 7
Evaluates Unary Operators and pushes the computed result into the stack.