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

Parameters:
__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

Parameters:
__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.

__str__()[source]

Return string representation of the function declaration.

Returns:

A string showing the function name, parameters, and body.

Return type:

str

__repr__()[source]

Return detailed string representation of the function declaration.

Returns:

Same as __str__ for this class.

Return type:

str

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.

Return type:

None

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 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