knit_script.knit_script_interpreter.statements.Statement module
Basic statement structures.
This module provides the base Statement class and the Expression_Statement class, which form the foundation of the knit script statement system. These classes define the basic contract for executable code elements and provide mechanisms for using expressions as statements.
- class Statement(parser_node)[source]
Bases:
KS_Element
Superclass for all operations that do not produce a value.
This is the base class for all executable statements in the knit script language. Statements perform actions or side effects but do not return values (unlike expressions). They represent the fundamental building blocks of knit script programs and define the contract that all executable code elements must follow.
The Statement class extends KS_Element to provide execution capabilities while maintaining access to parser node information for error reporting and debugging. All specific statement types inherit from this class and implement the execute method to define their behavior.
- Parameters:
parser_node (
LRStackNode
)
- __init__(parser_node)[source]
Initialize a statement.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
- execute(context)[source]
Execute the statement at the current machine context.
This is the main method that subclasses override to implement their specific behavior. The base implementation does nothing and should be overridden by all concrete statement types.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Return type:
- property line_number: int
Get the line number of the symbol that generated this statement.
- Returns:
The line number where this element appears in the source file.
- Return type:
- property location: Location
Get the location of this symbol in KnitScript file.
- Returns:
The location of this symbol in the source file, including file name, line number, and position information.
- Return type:
Location
- class Expression_Statement(parser_node, expression)[source]
Bases:
Statement
Statement that evaluates an expression without using its result.
This statement type is used when an expression is written as a standalone statement. The expression is evaluated for its side effects, but the result is discarded. This is common for function calls that are executed for their actions rather than their return values.
Expression statements bridge the gap between expressions and statements, allowing any expression to be used as a statement by simply discarding its return value. This enables the use of function calls, assignments, and other expressions as standalone operations.
- _expression
The expression to evaluate when the statement executes.
- Type:
- Parameters:
parser_node (
LRStackNode
)expression (
Expression
)
- __init__(parser_node, expression)[source]
Initialize an expression statement.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
expression (Expression) – The expression to evaluate when the statement executes.
- property expression: Expression
Get the expression contained in this statement.
- Returns:
The expression that will be evaluated when this statement executes.
- Return type:
- __str__()[source]
Return string representation of the expression statement.
- Returns:
String representation of the contained expression.
- Return type:
- __repr__()[source]
Return detailed string representation of the expression statement.
- Returns:
Same as __str__ for this class.
- Return type:
- execute(context)[source]
Evaluate the expression and discard the result.
- This allows expressions with side effects (like function calls) to be used as statements, even though their return values are not needed.
The expression is evaluated in the current context but the result is ignored.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Return type:
- property line_number: int
Get the line number of the symbol that generated this statement.
- Returns:
The line number where this element appears in the source file.
- Return type:
- property location: Location
Get the location of this symbol in KnitScript file.
- Returns:
The location of this symbol in the source file, including file name, line number, and position information.
- Return type:
Location