@@ -98,9 +98,9 @@ let inferTierFromInstructions = (instrs: array<string>): PuzzleFormat.tier => {
9898let extractRegisters = (stateJson : Js .Json .t ): array <(string , int )> => {
9999 switch Js .Json .classify (stateJson ) {
100100 | JSONObject (dict ) =>
101- let keys = Js . Dict .keys (dict )
101+ let keys = Dict .keysToArray (dict )
102102 Belt .Array .keepMap (keys , key => {
103- switch Js . Dict .get (dict , key ) {
103+ switch Dict .get (dict , key ) {
104104 | Some (v ) =>
105105 switch Js .Json .classify (v ) {
106106 | JSONNumber (n ) => Some ((key , Belt .Float .toInt (n )))
@@ -123,22 +123,22 @@ let parseHints = (json: Js.Json.t): array<PuzzleFormat.hint> => {
123123 | JSONString (s ) => Some ({PuzzleFormat .moveThreshold : 3 , text : s })
124124 // Old format: {moveNumber, text} objects
125125 | JSONObject (dict ) => {
126- let text = switch Js . Dict .get (dict , "text" ) {
126+ let text = switch Dict .get (dict , "text" ) {
127127 | Some (v ) =>
128128 switch Js .Json .classify (v ) {
129129 | JSONString (s ) => s
130130 | _ => ""
131131 }
132132 | None => ""
133133 }
134- let threshold = switch Js . Dict .get (dict , "moveNumber" ) {
134+ let threshold = switch Dict .get (dict , "moveNumber" ) {
135135 | Some (v ) =>
136136 switch Js .Json .classify (v ) {
137137 | JSONNumber (n ) => Belt .Float .toInt (n )
138138 | _ => 3
139139 }
140140 | None =>
141- switch Js . Dict .get (dict , "moveThreshold" ) {
141+ switch Dict .get (dict , "moveThreshold" ) {
142142 | Some (v ) =>
143143 switch Js .Json .classify (v ) {
144144 | JSONNumber (n ) => Belt .Float .toInt (n )
@@ -161,8 +161,8 @@ let parseHints = (json: Js.Json.t): array<PuzzleFormat.hint> => {
161161}
162162
163163// Get a string field from a JSON object
164- let getString = (dict : Js . Dict . t <Js .Json .t >, key : string , default : string ): string => {
165- switch Js . Dict .get (dict , key ) {
164+ let getString = (dict : dict <Js .Json .t >, key : string , default : string ): string => {
165+ switch Dict .get (dict , key ) {
166166 | Some (v ) =>
167167 switch Js .Json .classify (v ) {
168168 | JSONString (s ) => s
@@ -173,8 +173,8 @@ let getString = (dict: Js.Dict.t<Js.Json.t>, key: string, default: string): stri
173173}
174174
175175// Get an int field from a JSON object
176- let getInt = (dict : Js . Dict . t <Js .Json .t >, key : string , default : int ): int => {
177- switch Js . Dict .get (dict , key ) {
176+ let getInt = (dict : dict <Js .Json .t >, key : string , default : int ): int => {
177+ switch Dict .get (dict , key ) {
178178 | Some (v ) =>
179179 switch Js .Json .classify (v ) {
180180 | JSONNumber (n ) => Belt .Float .toInt (n )
@@ -185,8 +185,8 @@ let getInt = (dict: Js.Dict.t<Js.Json.t>, key: string, default: int): int => {
185185}
186186
187187// Get a string array from a JSON object
188- let getStringArray = (dict : Js . Dict . t <Js .Json .t >, key : string ): array <string > => {
189- switch Js . Dict .get (dict , key ) {
188+ let getStringArray = (dict : dict <Js .Json .t >, key : string ): array <string > => {
189+ switch Dict .get (dict , key ) {
190190 | Some (v ) =>
191191 switch Js .Json .classify (v ) {
192192 | JSONArray (arr ) =>
@@ -211,7 +211,7 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
211211 let description = getString (dict , "description" , "" )
212212 let difficulty = getString (dict , "difficulty" , "beginner" )-> parseDifficulty
213213 let maxMoves = getInt (dict , "maxMoves" , 50 )
214- let parMoves = switch Js . Dict .get (dict , "optimalMoves" ) {
214+ let parMoves = switch Dict .get (dict , "optimalMoves" ) {
215215 | Some (v ) =>
216216 switch Js .Json .classify (v ) {
217217 | JSONNumber (n ) => Belt .Float .toInt (n )
@@ -221,28 +221,28 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
221221 }
222222
223223 // Parse initial/goal state (supports both initialState and initialRegisters)
224- let initialRegisters = switch Js . Dict .get (dict , "initialState" ) {
224+ let initialRegisters = switch Dict .get (dict , "initialState" ) {
225225 | Some (v ) => extractRegisters (v )
226226 | None =>
227- switch Js . Dict .get (dict , "initialRegisters" ) {
227+ switch Dict .get (dict , "initialRegisters" ) {
228228 | Some (v ) => extractRegisters (v )
229229 | None => []
230230 }
231231 }
232- let goalRegisters = switch Js . Dict .get (dict , "goalState" ) {
232+ let goalRegisters = switch Dict .get (dict , "goalState" ) {
233233 | Some (v ) => extractRegisters (v )
234234 | None =>
235- switch Js . Dict .get (dict , "goalRegisters" ) {
235+ switch Dict .get (dict , "goalRegisters" ) {
236236 | Some (v ) => extractRegisters (v )
237237 | None => []
238238 }
239239 }
240240
241241 // Parse tier explicit, or inferred
242- let tier = switch Js . Dict .get (dict , "tier" ) {
242+ let tier = switch Dict .get (dict , "tier" ) {
243243 | Some (v ) => parseTier (v )
244244 | None => {
245- let fromInstr = switch Js . Dict .get (dict , "allowedInstructions" ) {
245+ let fromInstr = switch Dict .get (dict , "allowedInstructions" ) {
246246 | Some (_ ) => {
247247 let instrs = getStringArray (dict , "allowedInstructions" )
248248 if Belt .Array .length (instrs ) > 0 {
@@ -271,7 +271,7 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
271271 }
272272
273273 // Parse hints
274- let hints = switch Js . Dict .get (dict , "hints" ) {
274+ let hints = switch Dict .get (dict , "hints" ) {
275275 | Some (v ) => parseHints (v )
276276 | None => []
277277 }
@@ -282,7 +282,7 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
282282 if Belt .Array .length (topLevel ) > 0 {
283283 topLevel
284284 } else {
285- switch Js . Dict .get (dict , "metadata" ) {
285+ switch Dict .get (dict , "metadata" ) {
286286 | Some (v ) =>
287287 switch Js .Json .classify (v ) {
288288 | JSONObject (metaDict ) => getStringArray (metaDict , "tags" )
@@ -316,59 +316,59 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
316316
317317// Serialize a puzzle to JSON (for bundle generation)
318318let puzzleToJson = (p : PuzzleFormat .t ): Js .Json .t => {
319- let dict = Js . Dict .empty ()
320- Js . Dict .set (dict , "id" , Js .Json .string (p .id ))
321- Js . Dict .set (dict , "name" , Js .Json .string (p .name ))
322- Js . Dict .set (dict , "description" , Js .Json .string (p .description ))
323- Js . Dict .set (dict , "difficulty" , Js .Json .string (PuzzleFormat .difficultyToString (p .difficulty )))
324- Js . Dict .set (dict , "tier" , Js .Json .number (Belt .Int .toFloat (PuzzleFormat .tierToInt (p .tier ))))
325- Js . Dict .set (dict , "maxMoves" , Js .Json .number (Belt .Int .toFloat (p .maxMoves )))
326- Js . Dict .set (dict , "parMoves" , Js .Json .number (Belt .Int .toFloat (p .parMoves )))
319+ let dict = Dict .make ()
320+ Dict .set (dict , "id" , Js .Json .string (p .id ))
321+ Dict .set (dict , "name" , Js .Json .string (p .name ))
322+ Dict .set (dict , "description" , Js .Json .string (p .description ))
323+ Dict .set (dict , "difficulty" , Js .Json .string (PuzzleFormat .difficultyToString (p .difficulty )))
324+ Dict .set (dict , "tier" , Js .Json .number (Belt .Int .toFloat (PuzzleFormat .tierToInt (p .tier ))))
325+ Dict .set (dict , "maxMoves" , Js .Json .number (Belt .Int .toFloat (p .maxMoves )))
326+ Dict .set (dict , "parMoves" , Js .Json .number (Belt .Int .toFloat (p .parMoves )))
327327
328328 // Registers as objects
329329 let regsToJson = (regs : array <(string , int )>): Js .Json .t => {
330- let d = Js . Dict .empty ()
330+ let d = Dict .make ()
331331 Belt .Array .forEach (regs , ((k , v )) => {
332- Js . Dict .set (d , k , Js .Json .number (Belt .Int .toFloat (v )))
332+ Dict .set (d , k , Js .Json .number (Belt .Int .toFloat (v )))
333333 })
334334 Js .Json .object_ (d )
335335 }
336- Js . Dict .set (dict , "initialRegisters" , regsToJson (p .initialRegisters ))
337- Js . Dict .set (dict , "goalRegisters" , regsToJson (p .goalRegisters ))
336+ Dict .set (dict , "initialRegisters" , regsToJson (p .initialRegisters ))
337+ Dict .set (dict , "goalRegisters" , regsToJson (p .goalRegisters ))
338338
339339 // Memory pairs
340340 if Belt .Array .length (p .initialMemory ) > 0 {
341341 let memToJson = (pairs : array <(int , int )>): Js .Json .t => {
342342 Js .Json .array (
343343 Belt .Array .map (pairs , ((addr , val_ )) => {
344- let d = Js . Dict .empty ()
345- Js . Dict .set (d , "addr" , Js .Json .number (Belt .Int .toFloat (addr )))
346- Js . Dict .set (d , "value" , Js .Json .number (Belt .Int .toFloat (val_ )))
344+ let d = Dict .make ()
345+ Dict .set (d , "addr" , Js .Json .number (Belt .Int .toFloat (addr )))
346+ Dict .set (d , "value" , Js .Json .number (Belt .Int .toFloat (val_ )))
347347 Js .Json .object_ (d )
348348 }),
349349 )
350350 }
351- Js . Dict .set (dict , "initialMemory" , memToJson (p .initialMemory ))
352- Js . Dict .set (dict , "goalMemory" , memToJson (p .goalMemory ))
351+ Dict .set (dict , "initialMemory" , memToJson (p .initialMemory ))
352+ Dict .set (dict , "goalMemory" , memToJson (p .goalMemory ))
353353 }
354354
355355 // Allowed instructions
356356 switch p .allowedInstructions {
357357 | Some (instrs ) =>
358- Js . Dict .set (dict , "allowedInstructions" , Js .Json .array (Belt .Array .map (instrs , Js .Json .string )))
358+ Dict .set (dict , "allowedInstructions" , Js .Json .array (Belt .Array .map (instrs , Js .Json .string )))
359359 | None => ()
360360 }
361361
362362 // Hints
363363 if Belt .Array .length (p .hints ) > 0 {
364- Js . Dict .set (
364+ Dict .set (
365365 dict ,
366366 "hints" ,
367367 Js .Json .array (
368368 Belt .Array .map (p .hints , h => {
369- let d = Js . Dict .empty ()
370- Js . Dict .set (d , "moveThreshold" , Js .Json .number (Belt .Int .toFloat (h .moveThreshold )))
371- Js . Dict .set (d , "text" , Js .Json .string (h .text ))
369+ let d = Dict .make ()
370+ Dict .set (d , "moveThreshold" , Js .Json .number (Belt .Int .toFloat (h .moveThreshold )))
371+ Dict .set (d , "text" , Js .Json .string (h .text ))
372372 Js .Json .object_ (d )
373373 }),
374374 ),
@@ -377,18 +377,18 @@ let puzzleToJson = (p: PuzzleFormat.t): Js.Json.t => {
377377
378378 // Tags
379379 if Belt .Array .length (p .tags ) > 0 {
380- Js . Dict .set (dict , "tags" , Js .Json .array (Belt .Array .map (p .tags , Js .Json .string )))
380+ Dict .set (dict , "tags" , Js .Json .array (Belt .Array .map (p .tags , Js .Json .string )))
381381 }
382382
383383 Js .Json .object_ (dict )
384384}
385385
386386// Serialize an array of puzzles to a bundle JSON string
387387let bundleToJsonString = (puzzles : array <PuzzleFormat .t >): string => {
388- let bundle = Js . Dict .empty ()
389- Js . Dict .set (bundle , "version" , Js .Json .string ("1.0.0" ))
390- Js . Dict .set (bundle , "generatedAt" , Js .Json .string ("build-time" ))
391- Js . Dict .set (bundle , "count" , Js .Json .number (Belt .Int .toFloat (Belt .Array .length (puzzles ))))
392- Js . Dict .set (bundle , "puzzles" , Js .Json .array (Belt .Array .map (puzzles , puzzleToJson )))
388+ let bundle = Dict .make ()
389+ Dict .set (bundle , "version" , Js .Json .string ("1.0.0" ))
390+ Dict .set (bundle , "generatedAt" , Js .Json .string ("build-time" ))
391+ Dict .set (bundle , "count" , Js .Json .number (Belt .Int .toFloat (Belt .Array .length (puzzles ))))
392+ Dict .set (bundle , "puzzles" , Js .Json .array (Belt .Array .map (puzzles , puzzleToJson )))
393393 Js .Json .stringify (Js .Json .object_ (bundle ))
394394}
0 commit comments