knit_script.knit_script_interpreter.statements.function_dec_statement module

Structures for declaring functions.

This module provides classes for function declaration and execution in knit script programs. It includes the Function_Signature class for managing function objects and the Function_Declaration statement for creating and registering user-defined functions.

class Function_Signature(name, parameter_names, body, defaults, module_scope, source_statement)[source]

Bases: object

Function object which processes parameter values and executes function calls.

Handles parameter binding, scope management, and execution of user-defined functions in the knit script language. The Function_Signature class manages the complete lifecycle of function calls including parameter validation, argument binding, scope creation, body execution, and return value handling.

This class provides the runtime representation of user-defined functions, encapsulating their parameters, body, default values, and execution context. It ensures proper scope isolation while allowing access to the module scope where the function was defined.

_name

The name of the function.

Type:

str

_parameter_names

List of parameter names in declaration order.

Type:

list[str]

_body

The statement body to execute when the function is called.

Type:

Statement

_defaults

Dictionary mapping parameter names to their default values.

Type:

dict[str, Any]

_module_scope

The scope in which the function was defined.

Type:

Knit_Script_Scope

__init__(name, parameter_names, body, defaults, module_scope, source_statement)[source]

Initialize a function signature.

Parameters:
  • source_statement (Function_Declaration) – The statement that declared this function signature.

  • name (str) – The name of the function.

  • parameter_names (list[str]) – List of parameter names in declaration order.

  • body (Statement) – The statement body to execute when the function is called.

  • defaults (dict[str, Any]) – Dictionary mapping parameter names to their default values.

  • module_scope (Knit_Script_Scope) – The scope in which the function was defined, used for lexical scoping.

execute(context, args, kwargs)[source]

Execute the function with the given arguments.

Creates a new function scope, binds parameters to arguments, executes the function body, and returns the result. Handles parameter validation, default value assignment, and proper scope management throughout the function call.

Parameters:
  • context (Knit_Script_Context) – The current execution context of the knit script interpreter.

  • args (list[Expression]) – Positional arguments passed to the function, evaluated in order.

  • kwargs (list[Assignment]) – Keyword arguments passed as assignment objects with parameter names and values.

Returns:

The return value of the function, or None if no return statement was executed.

Return type:

Any

Raises:
  • NameError – If an unexpected keyword argument is provided that doesn’t match any parameter name.

  • TypeError – If required parameters are missing values after processing all arguments and defaults.

class Function_Declaration(parser_node, func_name, args, kwargs, body)[source]

Bases: Statement

Statement structure for declaring functions.

Creates a function signature and adds it to the current variable scope, making it available for later function calls. The Function_Declaration statement processes function definitions and creates executable Function_Signature objects that can be called from other parts of the knit script program.

This statement handles parameter processing, default value evaluation, and function signature creation while providing proper warnings for variable shadowing situations.

_func_name

The name of the function being declared.

Type:

str

_args

List of variable expressions representing positional parameters.

Type:

list[Variable_Expression]

_kwargs

List of assignment objects representing keyword parameters with defaults.

Type:

list[Assignment]

_body

The statement body to execute when the function is called.

Type:

Statement

__init__(parser_node, func_name, args, kwargs, body)[source]

Initialize a function declaration.

Parameters:
  • parser_node (LRStackNode) – The parser node from the abstract syntax tree.

  • func_name (str) – The name of the function being declared.

  • args (list[Variable_Expression]) – List of variable expressions representing positional parameters.

  • kwargs (list[Assignment]) – List of assignment objects representing keyword parameters with default values.

  • body (Statement) – The statement body to execute when the function is called.

execute(context)[source]

Execute the function declaration by creating and storing the function.

Creates a Function_Signature object with the specified parameters and body, evaluates default values, and adds the function to the current variable scope. Issues warnings for parameter names that shadow existing variables.

Parameters:

context (Knit_Script_Context) – The current execution context of the knit script interpreter.

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

classmethod __init_subclass__(**kwargs)

Automatically wrap execute methods in subclasses with appropriate error handling and debugging decorators.

This method is called whenever a class inherits from Statement. It checks if the subclass defines its own execute method and wraps it with the appropriate decorators

Parameters:

**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__

property file_name: str | None

Returns: str | None: The file name of the knitscript program this was parsed from or None if the program was passed as a string.

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:

int

property local_path: str | None

Returns: str | None: The path to the directory containing the file from which this element was parsed or None if the value was parsed from a python string.

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

property location_str: str

Returns: str: The string referencing the line number and possible file name information about this element.

property position_context: str

The position context string is the string from the knitscript program from which this element was parsed. The context string will begin at the start of this element and continue to the end of the line of knitscript or a semicolon on new line are reached.

Returns:

The string used to contextualize this element in the knitscript program.

Return type:

str