Skip to content

$$ Variables and Patterns

kleer001 edited this page Mar 17, 2025 · 3 revisions

The Parm class implements a pattern-matching system that allows for dynamic variable substitution and evaluation. The system is built around several specialized patterns that use $ and $$ prefixes to indicate different types of variable access and operations.

Pattern Types

The class defines four main pattern types:

  1. Global Variables ($VARNAME):

    • Pattern: \$[A-Z]{2,}([-+*/%]\d+)?
    • Examples: $FOO, $BAR+4, $COUNT-2
    • Purpose: Access global variables stored in GlobalStore, with optional math operations
  2. Loop Number ($$L):

    • Pattern: \$\$L
    • Purpose: Returns the current loop iteration number (0-indexed)
  3. List Access ($$N and $$num):

    • Pattern: \$\$(?:N([-+*/%]\d+)?|(\d+))
    • Examples:
      • $$N - Access element at current loop index
      • $$N+2 - Access element at loop index + 2
      • $$N*3 - Access element at loop index * 3
      • $$1 - Access first element
      • $$5 - Access fifth element
    • Purpose: Retrieves items from an input list based on loop number or explicit index
  4. Python Code (`code`):

    • Pattern: `([^`]+)`
    • Examples: `len("test")`, `math.sin(0.5)`
    • Purpose: Evaluates arbitrary Python expressions within a safe execution environment

How It Works

  1. The system processes expressions in a specific order:

    • First: Global variables
    • Second: Loop numbers
    • Third: List access
    • Fourth: Python code evaluation
  2. When eval() is called on a parameter, the system:

    • Checks if the parameter contains patterns
    • For each pattern match, it applies the corresponding transformation
    • Returns the fully resolved value
  3. List access requires an input node that returns a list

    • The system calculates the appropriate index (with overflow protection)
    • Returns the corresponding list item as a string
  4. The Python code evaluation runs in a sandboxed environment with limited built-ins and modules for security

This pattern system enables powerful template-based string manipulation and data access while maintaining a controlled execution environment.

Clone this wiki locally