interpreter module
- exception src.interpreter.BreakException
Bases:
Exception
- class src.interpreter.Interpreter(ast)
Bases:
object
- class Interpreter
Bases:
object
An interpreter for the Ulto programming language.
The Interpreter class is responsible for executing the abstract syntax tree (AST) of an Ulto program. It manages variable assignments, control flow (e.g., loops, conditionals), arithmetic operations, and reversible operations. The interpreter also handles memory management, profiling, and logging of execution details. Additionally, it interfaces with a C library for optimized arithmetic and compound assignment operations through foreign function interface (FFI) using ctypes.
- ast
The abstract syntax tree representing the program.
- Type:
list
- symbol_table
A sorted dictionary used to store variable names and their associated values.
- Type:
SortedDict
- history
A list to keep track of execution history.
- Type:
list
- detailed_history
A list to store detailed execution history.
- Type:
list
- assignments
A counter for the number of assignments performed.
- Type:
int
- evaluations
A counter for the number of expressions evaluated.
- Type:
int
- reversals
A counter for the number of reversals executed.
- Type:
int
- current_step
The current step number in the execution.
- Type:
int
- memory_manager
An instance of the MemoryManager class for managing memory allocation.
- Type:
- eager_vars
A set of variables identified for eager evaluation.
- Type:
set
- profiling_data
A dictionary to store profiling data for optimizing execution.
- Type:
dict
- profile_batch_size
The batch size for profiling updates.
- Type:
int
- profile_counter
A counter to manage profiling updates.
- Type:
int
- lib
A C library loaded for performing arithmetic and compound assignments.
- Type:
ctypes.CDLL
- apply_operator(op, left, right)
Applies an operator to two operands.
Args: op (str): The operator. left (int): The left operand. right (int): The right operand.
Returns: The result of the operation.
- collect_profiling_data(ast)
Collects profiling data from the AST.
Args: ast (list): The abstract syntax tree.
- detect_eager_vars()
Detects eager variables based on the profiling data.
- error(message)
Raises an error with the given message.
Args: message (str): The error message.
- evaluate_expression(expr)
Evaluates an expression.
Args: expr (any): The expression to be evaluated.
Returns: The evaluated result.
- execute()
Executes the AST.
- execute_assignment(node)
Executes an assignment node.
Args: node (tuple): The assignment node.
- execute_for(node)
Executes a for loop node.
Args: node (tuple): The for loop node.
- execute_function(node)
Executes a function node.
Args: node (tuple): The function node.
- execute_if(node)
Executes an if node.
Args: node (tuple): The if node.
- execute_minus_assign(node)
Executes a minus assignment operation (-=) on a variable.
- Parameters:
node (tuple) – A tuple containing the operation and the variable name, and the value to be subtracted. Expected format: (_, var_name, value).
- Raises:
KeyError – If var_name is not found in the symbol table.
- execute_node(node)
Executes a single node in the AST.
Args: node (tuple): The node to be executed.
- execute_over_assign(node)
Executes a division assignment operation (/=) on a variable.
- Parameters:
node (tuple) – A tuple containing the operation and the variable name, and the value to be divided. Expected format: (_, var_name, value).
- Raises:
KeyError – If var_name is not found in the symbol table.
- execute_plus_assign(node)
Executes a plus assignment operation (+=) on a variable.
- Parameters:
node (tuple) – A tuple containing the operation and the variable name, and the value to be added. Expected format: (_, var_name, value).
- Raises:
KeyError – If var_name is not found in the symbol table.
- execute_print(node)
Executes a print node.
Args: node (tuple): The print node.
- execute_reverse(node)
Executes a reverse node.
Args: node (tuple): The reverse node.
- execute_revtrace(node)
Executes a revtrace node.
Args: node (tuple): The revtrace node.
- execute_times_assign(node)
Executes a times assignment operation (*=) on a variable.
- Parameters:
node (tuple) – A tuple containing the operation and the variable name, and the value to be multiplied. Expected format: (_, var_name, value).
- Raises:
KeyError – If var_name is not found in the symbol table.
- execute_while(node)
Executes a while node.
Args: node (tuple): The while node.
- get_memory_usage()
Gets the current memory usage.
Returns: float: The memory usage in megabytes.
- get_previous_value(var_name, index)
Retrieves the previous value of a variable from the log stack by index.
Args: var_name (str): The name of the variable. index (int): The index of the state to retrieve.
Returns: The previous value of the variable or None if not found.
- log_execution_details(start_time, end_time)
Logs the execution details to a file.
Args: start_time (float): The start time of the execution. end_time (float): The end time of the execution.
- print_computation_cost()
Prints the computation cost of the execution.
- profile_assignment(node)
Profiles an assignment node.
Args: node (tuple): The assignment node.
- profile_if(node)
Profiles an if node.
Args: node (tuple): The if node.
- profile_node(node)
Profiles a single node in the AST. For now, handling conditionals, assignments, reversals and prints.
Args: node (tuple): The node to be profiled.
- profile_print(node)
Profiles a print node.
Args: node (tuple): The print node.
- profile_reverse(node)
Profiles a reverse node.
Args: node (tuple): The reverse node.
- profile_revtrace(node)
Profiles a revtrace node.
Args: node (tuple): The revtrace node.
- profile_while(node)
Profiles a while node.
Args: node (tuple): The while node.
- prune_logstack()
- update_profiling_data(expr)
Updates the profiling data with the given expression.
Args: expr (any): The expression to be profiled.