Commit e98f7fc
committed
feat!: redesign for-loop syntax with explicit context access
BREAKING CHANGE: The for-loop syntax no longer implicitly treats
the first variable as an index when two variables are present.
Old syntax (no longer works):
for idx, item in items:
print(idx, item)
New syntax:
for item in items with loop:
print(loop.idx, item)
The `with <identifier>` clause is optional and provides a context
object containing:
- loop.idx: current iteration index (0-based)
- loop.src: the original collection being iterated
The recommended convention for for-loops is `loop` (e.g., `with loop`),
making the syntax self-documenting: `loop.idx` clearly indicates
"the loop's current index".
This change unifies the mental model: all variables on the left
side of `in` now correspond to values being unpacked from each
element. Two variables means unpacking pairs, not index + item.
Benefits:
- Clearer semantics: variables always map to values
- Explicit over implicit: context access is opt-in
- Consistent: same pattern for lists, maps, strings, comprehensions
- Flexible: context variable name is user-chosen
A migration hint appears when the interpreter detects the old
pattern (first variable named 'idx', 'index', or 'i' with a
non-unpackable collection). This hint triggers for 2 or more
left-hand variables, not just exactly 2.
Note: The migration hint can be removed from 2027 onwards, once
users have had sufficient time to update their scripts.
Bug fix: `break` now works correctly in map iteration. Previously,
`break` inside a `for key in map:` loop would only exit the switch
statement, not the for loop itself. Fixed by adding a labeled loop
(`Loop:`) and using `break Loop`. A regression test with switch
inside a map loop ensures this won't recur.
Implementation notes:
- Context creation extracted to `setLoopContext()` helper to avoid
duplication between list and map iteration
- go.mod contains a temporary replace directive pointing to a local
tree-sitter-rad with the grammar changes. This will be removed
once tree-sitter-rad publishes a new release.1 parent 9e4e595 commit e98f7fc
File tree
11 files changed
+591
-78
lines changed- core
- testing
- rts
- rl
- test/dumps/cases
11 files changed
+591
-78
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
963 | 963 | | |
964 | 964 | | |
965 | 965 | | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
966 | 979 | | |
967 | 980 | | |
968 | 981 | | |
| 982 | + | |
969 | 983 | | |
970 | 984 | | |
971 | 985 | | |
972 | 986 | | |
973 | | - | |
| 987 | + | |
974 | 988 | | |
975 | | - | |
| 989 | + | |
976 | 990 | | |
977 | | - | |
| 991 | + | |
978 | 992 | | |
979 | 993 | | |
980 | 994 | | |
| |||
983 | 997 | | |
984 | 998 | | |
985 | 999 | | |
986 | | - | |
| 1000 | + | |
987 | 1001 | | |
| 1002 | + | |
988 | 1003 | | |
989 | 1004 | | |
990 | | - | |
991 | | - | |
992 | | - | |
993 | 1005 | | |
994 | 1006 | | |
995 | 1007 | | |
996 | 1008 | | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | 1009 | | |
1005 | 1010 | | |
1006 | 1011 | | |
1007 | 1012 | | |
1008 | | - | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
| 1013 | + | |
1012 | 1014 | | |
1013 | | - | |
1014 | | - | |
| 1015 | + | |
| 1016 | + | |
1015 | 1017 | | |
1016 | 1018 | | |
1017 | | - | |
1018 | | - | |
| 1019 | + | |
| 1020 | + | |
1019 | 1021 | | |
1020 | 1022 | | |
1021 | | - | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
1022 | 1034 | | |
1023 | 1035 | | |
1024 | | - | |
| 1036 | + | |
1025 | 1037 | | |
1026 | | - | |
| 1038 | + | |
1027 | 1039 | | |
1028 | 1040 | | |
1029 | | - | |
1030 | | - | |
1031 | | - | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
1032 | 1044 | | |
1033 | 1045 | | |
1034 | 1046 | | |
| |||
1043 | 1055 | | |
1044 | 1056 | | |
1045 | 1057 | | |
1046 | | - | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
1047 | 1065 | | |
1048 | 1066 | | |
1049 | 1067 | | |
| |||
1059 | 1077 | | |
1060 | 1078 | | |
1061 | 1079 | | |
| 1080 | + | |
| 1081 | + | |
1062 | 1082 | | |
| 1083 | + | |
| 1084 | + | |
1063 | 1085 | | |
1064 | 1086 | | |
1065 | 1087 | | |
| |||
1072 | 1094 | | |
1073 | 1095 | | |
1074 | 1096 | | |
1075 | | - | |
| 1097 | + | |
1076 | 1098 | | |
1077 | 1099 | | |
1078 | 1100 | | |
| 1101 | + | |
1079 | 1102 | | |
1080 | 1103 | | |
1081 | 1104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
| 31 | + | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
0 commit comments