knit_script.knit_script_interpreter.knit_script_actions module
Actions for converting parglare elements into useful code.
This module contains parser actions that convert parglare parsing elements into executable KnitScript code elements. These actions are called during the parsing process to transform the parsed syntax tree into appropriate expression, statement, and value objects that can be executed by the KnitScript interpreter. The module provides comprehensive support for all KnitScript language constructs including expressions, statements, control flow, functions, and machine operations.
- program(_parser_node, __, statements)[source]
Create a program from a list of statements.
- Parameters:
- Returns:
The list of statements representing the complete program.
- Return type:
- identifier(parser_node, node)[source]
Convert a string identifier into an appropriate expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
node (str) – The string recognized as an identifier.
- Returns:
Variable expression or specialized keyword expression based on the identifier.
- Return type:
- declare_variable(parser_node, __, assign)[source]
Create a variable declaration statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.assign (Assignment) – Assignment operation for the variable declaration.
__
- Returns:
Variable Declaration Statement that assigns the variable on execution.
- Return type:
- declare_global(parser_node, __, assign)[source]
Create a global variable declaration statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.assign (Assignment) – Assignment operation for the global variable declaration.
__
- Returns:
Variable Declaration Statement that assigns the global variable on execution.
- Return type:
- assertion(parser_node, __, exp, error=None)[source]
Create an assertion statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.exp (Expression) – Expression to evaluate assertion condition.
error (Expression | None, optional) – Error message expression to report if assertion fails. Defaults to None.
__
- Returns:
Assertion Statement that validates the condition during execution.
- Return type:
- print_statement(parser_node, __, exp)[source]
Create a print statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.exp (Expression) – Expression to print to output.
__
- Returns:
Print Statement that outputs the expression value.
- Return type:
- try_catch(parser_node, __, try_block, catch_block, errors)[source]
Create a try-catch statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.try_block (Statement) – Statements to execute in try branch.
catch_block (Statement) – Statements to execute in catch branch.
errors (list[Expression]) – List of exception expressions to catch.
__
- Returns:
Try Catch statement for exception handling.
- Return type:
- exception_assignment(parser_node, __, except_val, var_name)[source]
Create assignment with reversed syntax for catch statements.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.except_val (Expression) – The exception expression to allow.
var_name (Variable_Expression) – The name of the variable for storing the caught exception.
__
- Returns:
An assignment operation for binding the exception to a variable.
- Return type:
- pause_statement(parser_node, __)[source]
Create a pause statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.__
- Returns:
Pause statement that halts execution temporarily.
- Return type:
- assignment(parser_node, __, var_name, exp)[source]
Create an assignment expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.var_name (Variable_Expression) – Variable identifier to assign to.
exp (Expression) – Expression to assign as the variable value.
__
- Returns:
Assignment expression which evaluates to the assigned expression value.
- Return type:
- float_exp(parser_node, node)[source]
Create a float value expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
node (str) – The number string to parse as a float.
- Returns:
The floating point number expression.
- Return type:
- direction_exp(parser_node, nodes)[source]
Create a direction expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
nodes (list) – Single node list with direction keyword.
- Returns:
Pass direction expression for carriage movement.
- Return type:
- string(parser_node, node)[source]
Create a string value expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
node (str) – String value including quotes.
- Returns:
Expression storing the decoded string value.
- Return type:
- f_string_section(parser_node, __, exp=None, string_value=None)[source]
Create an expression from a section of a formatted string.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exp (Expression | None, optional) – Expression in formatting section. Defaults to None.
string_value (str | None, optional) – String literal in formatting section. Defaults to None.
__
- Returns:
Expression representing the formatted string section value.
- Return type:
- formatted_string(parser_node, __, sections)[source]
Create a formatted string expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.sections (list[Expression]) – F string sections parsed as expressions.
__
- Returns:
Formatted string expression that combines sections.
- Return type:
- call_list(_parser_node, __, params=None, kwargs=None)[source]
Create a call list with parameters and keyword arguments.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.params (list[Expression] | None, optional) – The positional parameters in the call list. Defaults to None.
kwargs (list[Assignment] | None, optional) – The keyword parameters in the call list. Defaults to None.
__
- Returns:
Tuple of positional parameters and keyword arguments.
- Return type:
- function_call(parser_node, __, func_name, args)[source]
Create a function call expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.func_name (Variable_Expression) – Name of the function being called.
args (tuple[list[Expression], list[Assignment]] | None) – The arguments passed to the function as tuple of positional and keyword arguments.
__
- Returns:
The function call expression.
- Return type:
- list_expression(parser_node, __, exps)[source]
Create a list expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exps (list[Expression]) – Expressions in the list.
__
- Returns:
The list expression containing the specified elements.
- Return type:
- list_comp(parser_node, __, fill_exp, variables, iter_exp, comp_cond=None)[source]
Create a list comprehension expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.fill_exp (Expression) – Expression that fills the list for each iteration.
variables (list[Variable_Expression]) – Variables to fill from the iterable.
iter_exp (Expression) – The iterable expression to iterate over.
comp_cond (Expression, optional) – Condition expression to evaluate for including a value. Defaults to None.
__
- Returns:
List comprehension expression that generates a list from iteration.
- Return type:
- indexed_value(parser_node, __, item, key, assign)[source]
Create an indexed value expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.item (Expression) – Item expression to index into.
key (Slice_Index | Knit_Script_List) – Key expression to index by.
assign (Expression) – Optional assignment expression for indexed assignment.
__
- Returns:
Expression that evaluates indexed access or assignment.
- Return type:
- slice_index(parser_node, __, start, end)[source]
Create a slice index expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.start (Expression | None) – Start index expression for the slice.
end (Expression | list[Expression | None]) – End index expression or list of expressions for the slice.
__
- Returns:
Slice index expression for list/string slicing operations.
- Return type:
- dict_assign(_parser_node, __, key, exp)[source]
Collect key value pair for dictionary construction.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.key (Expression) – Key expression for the dictionary entry.
exp (Expression) – Value expression for the dictionary entry.
__
- Returns:
Tuple of key and value expressions.
- Return type:
- dict_expression(parser_node, __, kwargs)[source]
Create a dictionary expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.kwargs (list[tuple[Expression, Expression]]) – Key-value pairs for the dictionary.
__
- Returns:
The dictionary expression containing the specified key-value pairs.
- Return type:
- dict_comp(parser_node, __, key, value, variables, iter_exp, comp_cond=None)[source]
Create a dictionary comprehension expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.key (Expression) – Key expression for each dictionary entry.
value (Expression) – Value expression for each dictionary entry.
variables (list[Variable_Expression]) – Variables to assign from the iterable.
iter_exp (Expression) – The iterable expression to iterate over.
comp_cond (Expression | None, optional) – Conditional expression to filter entries. Defaults to None.
__
- Returns:
Dictionary comprehension expression that generates a dictionary from iteration.
- Return type:
- unpack(parser_node, __, exp)[source]
Create an unpack expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exp (Expression) – Expression to unpack into individual elements.
__
- Returns:
Unpacking expression for spreading iterable elements.
- Return type:
- code_block(parser_node, __, statements)[source]
Create a code block statement.
- Parameters:
- Returns:
Scoped code block that executes statements in a sub-scope.
- Return type:
- elif_statement(_parser_node, __, exp, stmnt)[source]
Create components of an elif statement.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Ignored parser nodes.exp (Expression) – Condition expression to test for the elif branch.
stmnt (Statement) – Statement to execute when the condition is true.
__
- Returns:
Tuple of condition expression and statement to execute when true.
- Return type:
- else_statement(_parser_node, __, false_statement)[source]
Create an else statement.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.false_statement (Code_Block) – Code block to execute when all conditions are false.
__
- Returns:
The code block to execute in the else branch.
- Return type:
- if_statement(parser_node, __, condition, true_statement, elifs, else_stmt)[source]
Create an if statement with optional elif and else branches.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.condition (Expression) – Primary branching condition for the if statement.
true_statement (Code_Block) – Statement to execute when the primary condition is true.
elifs (list[tuple[Expression, Statement]]) – List of elif conditions and their corresponding statements.
else_stmt (Code_Block | None) – Statement to execute when all conditions are false.
__
- Returns:
Complete if statement with all conditional branches.
- Return type:
- while_statement(parser_node, __, condition, while_block)[source]
Create a while-statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.condition (Expression) – Condition expression to evaluate for each iteration.
while_block (Code_Block) – The statement block to execute with each iteration.
__
- Returns:
While loop statement for conditional iteration.
- Return type:
- for_each_statement(parser_node, __, variables, iters, block)[source]
Create a for each statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.variables (list[Variable_Expression]) – Variables to assign on each iteration of the iterable.
iters (list[Expression]) – Iterable expressions to iterate over.
block (Code_Block) – Statement block to execute with each iteration.
__
- Returns:
For each loop statement for iterating over collections.
- Return type:
- as_assignment(parser_node, __, variable, exp)[source]
Create an assignment using ‘as’ syntax.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.variable (Variable_Expression) – Variable to assign the expression result to.
exp (Expression) – Expression to evaluate and assign.
__
- Returns:
Assignment operation using ‘as’ syntax.
- Return type:
- with_statement(parser_node, __, assigns, block)[source]
Create a with statement for scoped resource management.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.assigns (list[Assignment]) – Assignment operations to perform in the with scope.
block (Code_Block) – Block to execute within the with context.
__
- Returns:
With statement for managing scoped resources and assignments.
- Return type:
- instruction_assignment(parser_node, __, inst, needles)[source]
Create a needle instruction expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.inst (Expression) – Instruction expression to apply to needles.
needles (list[Expression]) – Needle expressions to apply the instruction to.
__
- Returns:
Needle instruction expression for applying operations to needles.
- Return type:
- carriage_pass(parser_node, __, pass_dir, instructions)[source]
Create a carriage pass statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.pass_dir (Expression) – Direction expression for the carriage pass.
instructions (list[Needle_Instruction_Exp]) – List of needle instructions to apply during the pass.
__
- Returns:
Carriage pass statement that executes instructions in a specific direction.
- Return type:
- needle_id(parser_node, needle_node)[source]
Create a needle expression from identifier.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
needle_node (str) – String representing the needle identifier.
- Returns:
Needle expression for referencing specific needles.
- Return type:
- sheet_id(parser_node, sheet_node)[source]
Create a sheet expression from identifier.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
sheet_node (str) – String representing the sheet identifier.
- Returns:
Sheet expression for referencing specific sheets.
- Return type:
- carrier(parser_node, carrier_node)[source]
Create a carrier expression from identifier.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
carrier_node (str) – String describing the carrier identifier.
- Returns:
Carrier expression for referencing yarn carriers.
- Return type:
- return_statement(parser_node, __, exp)[source]
Create a return statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exp (Expression) – Expression to return as the function result.
__
- Returns:
Return statement for exiting functions with a value.
- Return type:
- param_list(_parser_node, __, args=None, kwargs=None)[source]
Create a parameter list for function definitions.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.args (list[Variable_Expression] | None, optional) – List of positional parameter identifiers. Defaults to None.
kwargs (list[Assignment] | None, optional) – List of keyword parameter assignments with default values. Defaults to None.
__
- Returns:
Tuple of positional parameters and keyword parameter assignments.
- Return type:
- function_declaration(parser_node, __, func_name, params, block)[source]
Create a function declaration.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.func_name (Variable_Expression) – Name of the function being declared.
params (tuple[list[Variable_Expression], list[Assignment]] | None) – Parameter list as tuple of positional and keyword parameters.
block (Statement) – Function body statement to execute when called.
__
- Returns:
The function declaration that defines a callable function.
- Return type:
- expression(parser_node, nodes)[source]
Create an expression from parser nodes.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
nodes (list[str | KS_Element | Expression]) – Parser nodes to combine into an expression.
- Returns:
Combined expression based on the parser nodes and operators.
- Return type:
- negation(parser_node, __, exp)[source]
Create a negation expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exp (Expression) – Expression to negate.
__
- Returns:
Negation expression that inverts the boolean value.
- Return type:
- xfer_rack(parser_node, __, is_across=None, dist_exp=None, side_id=None)[source]
Create a transfer racking specification.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.is_across (str | None, optional) – Indicator if transfer is directly across beds. Defaults to None.
dist_exp (Expression | None, optional) – The needle offset expression for transfer. Defaults to None.
side_id (Expression | None, optional) – Offset direction expression. Defaults to None.
__
- Returns:
Transfer pass racking specification for needle transfers.
- Return type:
- xfer_pass(parser_node, __, needles, rack_val, bed=None, slider=None)[source]
Create a transfer pass statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.needles (list[Expression]) – Needle expressions to start transfer from.
rack_val (Xfer_Pass_Racking) – Racking specification for the transfers.
bed (Expression | None, optional) – Target bed expression for transfers. Defaults to None.
slider (str | None, optional) – Indicator if transferring to slider needles. Defaults to None.
__
- Returns:
Transfer pass statement for moving loops between needles.
- Return type:
- accessor(parser_node, __, exp, attribute)[source]
Create an attribute accessor expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exp (Expression) – Expression to access attributes from.
attribute (Expression) – Attribute expression to access.
__
- Returns:
Accessor expression for getting object attributes.
- Return type:
- exp_statement(parser_node, __, exp)[source]
Create an expression statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exp (Expression) – Expression to execute as a statement.
__
- Returns:
Statement that executes an expression for its side effects.
- Return type:
- cut_statement(parser_node, __, exps)[source]
Create a cut statement for yarn carriers.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exps (list[Expression]) – Carrier expressions to cut.
__
- Returns:
Cut statement for severing yarn carriers.
- Return type:
- release_statement(parser_node, __)[source]
Create a release statement for the current carrier.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.__
- Returns:
Release statement for releasing the current active carrier.
- Return type:
- remove_statement(parser_node, __, exps)[source]
Create a remove statement for yarn carriers.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.exps (list[Expression]) – Carrier expressions to remove/out.
__
- Returns:
Remove statement for moving carriers out of working area.
- Return type:
- gauge_exp(parser_node, __, sheet_exp, gauge)[source]
Create a gauge expression.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.sheet_exp (Expression) – Sheet expression for the gauge.
gauge (Expression) – Gauge value expression.
__
- Returns:
Gauge expression for sheet configuration.
- Return type:
- drop_pass(parser_node, __, needles)[source]
Create a drop pass statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.needles (list[Expression]) – Needle expressions to drop loops from.
__
- Returns:
Drop pass statement for releasing loops from needles.
- Return type:
- push_to(_parser_node, __, push_val)[source]
Create a push target specification.
- Parameters:
- Returns:
Identifying string or expression for the layer value.
- Return type:
- push_dir(_parser_node, __, amount, direction)[source]
Create a push direction specification.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.amount (Expression) – Amount expression to push.
direction (str) – Direction string to push in.
__
- Returns:
Tuple of push amount and direction.
- Return type:
- push_statement(parser_node, __, needles, push_val)[source]
Create a push statement for layer management.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.needles (list[Expression]) – Needle expressions to push layer values for.
push_val (str | Expression | tuple[Expression, str]) – Push value specification.
__
- Returns:
Push statement for managing needle layer positions.
- Return type:
- swap_statement(parser_node, __, needles, swap_type, value)[source]
Create a swap statement for layer management.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.needles (list[Expression]) – The needle expressions to perform swaps with.
swap_type (str) – Type of value to swap with.
value (Expression) – The value expression to swap with.
__
- Returns:
Swap statement for exchanging layer positions.
- Return type:
- pass_second(_parser_node, nodes)[source]
Return the second node in a list.
- Parameters:
_parser_node (LRStackNode) – The parser element that created this value.
nodes (list[Any]) – Parser nodes.
- Returns:
The second node in the list.
- Return type:
Any
- import_statement(parser_node, __, src, alias)[source]
Create an import statement.
- Parameters:
parser_node (LRStackNode) – The parser element that created this value.
__ (
list
) – Unused parameter.src (Expression) – Source module expression to import.
alias (Expression | None) – Alias expression to assign in variable scope.
__
- Returns:
Import statement for loading external modules.
- Return type: