feat: implement map literal syntax with curly braces and DUP bytecode

Add support for map literals using `{}` syntax in the compiler, replacing the previous `new Map` constructor pattern. This introduces a new `map()` grammar function that loads the Map class, instantiates it, and compiles key-value pairs using subscript setter calls. A new `CODE_DUP` bytecode is added to duplicate the map reference on the stack for each element insertion. The change includes updated test files to use literal syntax and new error tests for edge cases like EOF after colon, comma, key, or value.
This commit is contained in:
Bob Nystrom
2015-01-25 07:21:50 +00:00
parent 5c33ecc04f
commit 2ec0a8aef9
12 changed files with 83 additions and 20 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
var map = new Map
var map = {}
IO.print(map.count) // expect: 0
map["one"] = "value"
IO.print(map.count) // expect: 1
+1
View File
@@ -0,0 +1 @@
var map = {1: // expect error
+2
View File
@@ -0,0 +1,2 @@
var map = {1: 2,
// expect error
+1
View File
@@ -0,0 +1 @@
var map = {1 // expect error
+2
View File
@@ -0,0 +1,2 @@
var map = {1: 2
// expect error
+1 -1
View File
@@ -58,7 +58,7 @@ var fishes = [
"Cutlassfish", "Cutthroat eel", "Cutthroat trout"
]
var map = new Map
var map = {}
for (fish in fishes) {
map[fish] = fish.count
}
+10 -10
View File
@@ -1,13 +1,13 @@
var map = new Map
map[null] = "null value"
map[true] = "true value"
map[false] = "false value"
map[0] = "zero"
map[1.2] = "1 point 2"
map[List] = "list class"
map["null"] = "string value"
map[1..3] = "1 to 3"
var map = {
null: "null value",
true: "true value",
false: "false value",
0: "zero",
1.2: "1 point 2",
List: "list class",
"null": "string value",
(1..3): "1 to 3"
}
IO.print(map[null]) // expect: null value
IO.print(map[true]) // expect: true value
+4 -6
View File
@@ -1,7 +1,5 @@
// TODO: Use map literal.
IO.print(new Map is Map) // expect: true
IO.print({} is Map) // expect: true
// TODO: Abstract base class for associations.
IO.print(new Map is Object) // expect: true
IO.print(new Map is Bool) // expect: false
IO.print((new Map).type == Map) // expect: true
IO.print({} is Object) // expect: true
IO.print({} is Bool) // expect: false
IO.print({}.type == Map) // expect: true