semantic_analyser module

class src.semantic_analyser.SemanticAnalyser(ast)

Bases: object

A semantic analyzer for the Ulto programming language.

The SemanticAnalyser class is responsible for checking the abstract syntax tree (AST) of an Ulto program for semantic correctness. It ensures that variables are declared before use, validates operations and control structures, and tracks the program’s symbol table. The class processes various nodes in the AST, including assignments, loops, conditionals, and expressions, to enforce the language’s semantic rules.

ast

The abstract syntax tree to be analyzed.

Type:

list

symbol_table

A symbol table to track variable declarations and their values during analysis.

Type:

dict

analyse()

Analyzes the AST for semantic correctness.

analyse_node(node)

Analyzes a single node in the AST.

Args: node (tuple): The node to be analyzed.

error(message)

Raises an error with the given message.

Args: message (str): The error message.

evaluate_expression(expr)

Evaluates an expression.

Args: expr (tuple): The expression to be evaluated.

evaluate_operation(op, left, right)

Evaluates a simple arithmetic operation.

Args: op (str): The operation to be performed. left (int): The left operand. right (int): The right operand.

Returns: int: The result of the operation.

inline_expression(expr)

Inlines an expression.

Args: expr (tuple or any): The expression to be inlined.

Returns: The inlined expression.

is_inside_loop()

Checks if the current context is inside a loop. This function would need to track the current context to ensure ‘break’ is within a loop.

Returns: bool: True if inside a loop, False otherwise.

process_assignment(node)

Processes an assignment node.

Args: node (tuple): The assignment node.

process_break(node)

Processes a break statement node.

Args: node (tuple): The break statement node.

process_for(node)

Processes a for loop node.

Args: node (tuple): The for loop node.

process_if(node)

Processes an if node.

Args: node (tuple): The if node.

process_len(node)

Processes a len function node.

Args: node (tuple): The len function node.

process_minus_assign(node)
process_over_assign(node)
process_plus_assign(node)
process_print(node)

Processes a print node.

Args: node (tuple): The print node.

process_reverse(node)

Processes a reverse node.

Args: node (tuple): The reverse node.

process_revtrace(node)

Processes a revtrace node.

Args: node (tuple): The revtrace node.

process_times_assign(node)
process_while(node)

Processes a while node.

Args: node (tuple): The while node.