Skip to content

feat: assignment#366

Merged
stefan-gorules merged 2 commits into
masterfrom
feat/assignment
Jun 18, 2025
Merged

feat: assignment#366
stefan-gorules merged 2 commits into
masterfrom
feat/assignment

Conversation

@stefan-gorules

@stefan-gorules stefan-gorules commented Jun 17, 2025

Copy link
Copy Markdown
Contributor

Assignment Support

This PR adds assignment capabilities to the expression engine, enabling variable assignment and modification during expression evaluation. The assignment feature supports both simple variable assignment and nested property assignment with dot notation.

Features

  • Variable Assignment: Support for assigning values to variables using = operator
  • 🔧 Property Assignment: Nested property assignment with dot notation (e.g., user.name = 'John')
  • 📝 Multiple Assignments: Chain multiple assignments using semicolon separator
  • 🔄 Assignment with Return: Optional return value after assignment operations
  • 🏗️ Deep Property Creation: Automatically creates nested object structures during assignment

Technical Changes

  • Added new AST node type Assignments for handling assignment operations
  • Implemented new lexer tokens for assignment (=) and semicolon (;) operators
  • Added VM opcodes for assignment object lifecycle management:
    • AssignedObjectBegin - Initialize assignment scope
    • AssignedObjectStep - Process individual assignments
    • AssignedObjectEnd - Finalize assignment and handle return values
  • Enhanced parser to handle assignment syntax and property access validation
  • Added dot_insert_detached methods for immutable environment updates
  • Updated intellisense to support assignment operations and type inference
  • Added comprehensive test coverage for various assignment patterns

Example Usage

Basic Assignment

a = 5                           // {"a": 5}
name = 'John'                   // {"name": "John"}

Property Assignment

user.name = 'Alice'             // {"user": {"name": "Alice"}}
app.config.debug = true         // {"app": {"config": {"debug": true}}}

Multiple Assignments

a = 1; b = 2                    // {"a": 1, "b": 2}
user.name = 'Charlie'; user.age = 35  // {"user": {"name": "Charlie", "age": 35}}

Assignment with Return Value

user.name = 'Eve'; user.name    // Returns: 'Eve'
config.debug = true; $root      // Returns entire root object

Assignment with Expressions

counter = counter + 1           // Increment existing counter
total = price * quantity        // Calculate from existing variables
fullName = firstName + ' ' + lastName  // String concatenation

Validation

The implementation includes validation to ensure only property access (dot notation) is allowed on the left-hand side of assignments, preventing invalid assignment targets like computed property access with brackets.

Testing

Added 60+ test cases covering:

  • Basic variable assignment
  • Nested property assignment
  • Multiple assignment chains
  • Assignment with expressions and function calls
  • Assignment with conditional expressions
  • Assignment with return values
  • Error cases and validation

@stefan-gorules stefan-gorules merged commit f2fcb95 into master Jun 18, 2025
43 checks passed
@stefan-gorules stefan-gorules deleted the feat/assignment branch June 18, 2025 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants