Commit ecd50eb
authored
Merge pull request #17 from ECP-Solutions/spread_and_rest_operators_support
## Summary
ASF v2.0.8 introduces modern JavaScript-style spread/rest operators (`...`) and array destructuring assignments, significantly enhancing array manipulation capabilities and function parameter handling.
---
## Highlights
- **Added**
- Spread/rest operator (`...`) support:
```javascript
// Spread in array literals
arr1 = [1, 2, 3];
arr2 = [0, ...arr1, 4, 5]; // => [0, 1, 2, 3, 4, 5]
// Spread in function calls
fun add(a, b, c) { return a + b + c; };
numbers = [1, 2, 3];
result = add(...numbers); // => 6
// Rest parameters in functions
fun sum(first, ...rest) {
total = first;
rest.forEach(fun(n) { total = total + n });
return total;
};
sum(1, 2, 3, 4, 5); // => 15
// Spread strings into character arrays
chars = [...'hello']; // => ['h', 'e', 'l', 'l', 'o']
```
- Array destructuring assignments:
```javascript
// Basic destructuring
[a, b, c] = [1, 2, 3]; // a=1, b=2, c=3
// With rest element
[first, ...rest] = [1, 2, 3, 4, 5];
// first=1, rest=[2, 3, 4, 5]
// Fewer targets than elements
[x, y] = [10, 20, 30, 40]; // x=10, y=20
// More targets than elements (assigns Empty)
[p, q, r] = [100, 200]; // p=100, q=200, r=Empty
// Practical use cases
[a, b] = [b, a]; // Swap variables
[head, ...tail] = myArray; // Extract head and tail
// Combine spread and destructuring
arr1 = [1, 2];
arr2 = [3, 4];
[first, ...combined] = [...arr1, ...arr2];
// first=1, combined=[2, 3, 4]
```
- **Internal core changes**:
- **Parser** (`ASF_Parser.cls`):
- Added tokenization for `...` spread/rest operator
- New token type: `"SPREAD"` with value `"..."`
- **Compiler** (`ASF_Compiler.cls`):
- Added `IsSpreadToken()` helper function for spread operator detection
- Rest parameter support in function and method definitions
- Spread operator handling in array literal compilation (`ParseArrayLiteral`)
- Spread operator support in function call argument processing
- Array destructuring pattern detection in `ParseStatementTokensToAST`
- New AST node type: `"ArrayDestructuring"` with targets collection and optional rest target
- Compile-time validation:
- Rest parameter must be last in function signatures
- Only one rest parameter allowed per function
- Rest element must be last in destructuring patterns
- Multiple rest elements not allowed in destructuring patterns
- **VM** (`ASF_VM.cls`):
- Spread operator evaluation in `EvalArrayNode` with type handling
- Array expansion for arrays, strings, and scalar values
- Spread operator support in function call argument expansion
- Rest parameter handling in function calls with automatic array creation
- New `"ArrayDestructuring"` case handler in `ExecuteStmtNode`
- **Technical Details**:
- **Spread/Rest Token Structure**:
```
Token: ["SPREAD", "..."]
```
- **Rest Parameter AST**:
```
Function/Method node {
params: Collection of parameter names
restParam: String (optional)
...
}
```
- **Array Destructuring AST**:
```
ArrayDestructuring {
targets: Collection of Variable nodes
restTarget: String (optional)
source: Expression node
}
```
- **Error Messages**:
- `"Rest parameter must be last parameter"` - Rest parameter not in final position
- `"Multiple rest parameters not allowed"` - More than one rest parameter in function
- `"Rest element must be last in destructuring"` - Rest element not in final position
- `"Multiple rest elements in destructuring"` - More than one rest element in pattern
- `"Expected identifier after ... in destructuring"` - Invalid rest syntax
- `"Cannot destructure non-array value"` - Runtime type validation failure
---
**Full Changelog**: v2.0.7...v2.0.8File tree
11 files changed
+1301
-72
lines changed- docs
- src
- test
11 files changed
+1301
-72
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
5 | 126 | | |
6 | | - | |
| 127 | + | |
7 | 128 | | |
8 | 129 | | |
9 | 130 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
422 | 422 | | |
423 | 423 | | |
424 | 424 | | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
425 | 457 | | |
426 | 458 | | |
427 | 459 | | |
| |||
812 | 844 | | |
813 | 845 | | |
814 | 846 | | |
815 | | - | |
| 847 | + | |
816 | 848 | | |
817 | 849 | | |
818 | 850 | | |
| |||
824 | 856 | | |
825 | 857 | | |
826 | 858 | | |
| 859 | + | |
827 | 860 | | |
828 | 861 | | |
829 | 862 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
395 | 395 | | |
396 | 396 | | |
397 | 397 | | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
398 | 423 | | |
399 | 424 | | |
400 | 425 | | |
401 | 426 | | |
402 | 427 | | |
403 | | - | |
| 428 | + | |
404 | 429 | | |
405 | 430 | | |
406 | 431 | | |
| |||
2907 | 2932 | | |
2908 | 2933 | | |
2909 | 2934 | | |
2910 | | - | |
2911 | | - | |
2912 | 2935 | | |
2913 | 2936 | | |
2914 | 2937 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
0 commit comments