Skip to content

Commit f4a9b0b

Browse files
authored
Merge pull request #5706 from unisonweb/topic/json-replacement
Add builtin replacements for some Json functions
2 parents db315ad + 447a338 commit f4a9b0b

File tree

21 files changed

+931
-493
lines changed

21 files changed

+931
-493
lines changed

parser-typechecker/src/Unison/Builtin/Decls.hs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ mapBin = Maybe.fromJust $ constructorId mapRef "Map.Bin"
125125
setWrap :: ConstructorId
126126
setWrap = Maybe.fromJust $ constructorId setRef "Set.Set"
127127

128+
jsonNull, jsonBool, jsonObj, jsonNum, jsonText, jsonArr :: ConstructorId
129+
jsonNull = Maybe.fromJust $ constructorId jsonRef "Json.Null"
130+
jsonBool = Maybe.fromJust $ constructorId jsonRef "Json.Boolean"
131+
jsonObj = Maybe.fromJust $ constructorId jsonRef "Json.Object"
132+
jsonNum = Maybe.fromJust $ constructorId jsonRef "Json.Number.Unparsed"
133+
jsonText = Maybe.fromJust $ constructorId jsonRef "Json.Text"
134+
jsonArr = Maybe.fromJust $ constructorId jsonRef "Json.Array"
135+
136+
jsonParseError :: ConstructorId
137+
jsonParseError =
138+
Maybe.fromJust $
139+
constructorId parseErrorRef "Json.ParseError.ParseError"
140+
128141
isPropagatedConstructorId = Maybe.fromJust $ constructorId isPropagatedRef "IsPropagated.IsPropagated"
129142

130143
isTestConstructorId = Maybe.fromJust $ constructorId isTestRef "IsTest.IsTest"
@@ -260,6 +273,12 @@ mapRef = lookupDeclRef "Map"
260273
setRef :: Reference
261274
setRef = lookupDeclRef "Set"
262275

276+
jsonRef :: Reference
277+
jsonRef = lookupDeclRef "Json"
278+
279+
parseErrorRef :: Reference
280+
parseErrorRef = lookupDeclRef "Json.ParseError"
281+
263282
pattern Rewrites' :: [Term2 vt at ap v a] -> Term2 vt at ap v a
264283
pattern Rewrites' ts <- (unRewrites -> Just ts)
265284

@@ -316,7 +335,9 @@ builtinDataDecls = rs1 ++ rs
316335
(v "RewriteCase", rewriteCase),
317336
(v "Rewrites", rewrites),
318337
(v "Map", map),
319-
(v "Set", set)
338+
(v "Set", set),
339+
(v "Json", json),
340+
(v "Json.ParseError", jsonParseError)
320341
] of
321342
Right a -> a
322343
Left e -> error $ "builtinDataDecls: " <> show e
@@ -649,6 +670,40 @@ builtinDataDecls = rs1 ++ rs
649670
)
650671
]
651672

673+
json =
674+
DataDeclaration
675+
(Unique "oml0j9g6bb2tij2s75k4v7n1nftj199i")
676+
()
677+
[]
678+
let json = var "Json"
679+
tup x y = Type.apps' (var "Tuple") [x, y]
680+
pair x y = tup x (tup y (var "Unit"))
681+
in [ ((), v "Json.Null", var "Json"),
682+
((), v "Json.Boolean", Type.boolean () `arr` json),
683+
( (),
684+
v "Json.Object",
685+
Type.app () (Type.list ()) (pair (Type.text ()) json)
686+
`arr` json
687+
),
688+
((), v "Json.Number.Unparsed", Type.text () `arr` json),
689+
((), v "Json.Text", Type.text () `arr` json),
690+
( (),
691+
v "Json.Array",
692+
Type.app () (Type.list ()) json `arr` json
693+
)
694+
]
695+
jsonParseError =
696+
DataDeclaration
697+
(Unique "u3j6g9j6daejijc5e0rcujjj3sd6j3gq")
698+
()
699+
[]
700+
let jpe = var "Json.ParseError"
701+
in [ ( (),
702+
v "Json.ParseError.ParseError",
703+
Type.text () `arr` Type.nat () `arr` Type.text () `arr` jpe
704+
)
705+
]
706+
652707
builtinEffectDecls :: [(Symbol, Reference.Id, DD.EffectDeclaration Symbol ())]
653708
builtinEffectDecls =
654709
case hashDataDecls $ Map.fromList [(v "Exception", exception)] of

parser-typechecker/src/Unison/Util/Text.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ replicate n t =
4848
toLazyText :: Text -> TL.Text
4949
toLazyText (Text t) = TL.fromChunks (chunkToText <$> toList t)
5050

51+
fromLazyText :: TL.Text -> Text
52+
fromLazyText =
53+
Text . foldl' (\t -> R.snoc t . chunk) mempty . TL.toChunks
54+
5155
chunkToText :: Chunk -> T.Text
5256
chunkToText (Chunk _ t) = t
5357

unison-runtime/src/Unison/Runtime/Builtin.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,9 @@ declareForeigns = do
12961296
declareForeign Untracked 2 Set_union
12971297
declareForeign Untracked 2 Set_intersect
12981298
declareForeign Untracked 1 Set_toList
1299+
declareForeign Untracked 1 Json_toText
1300+
declareForeign Untracked 1 Json_unconsText
1301+
declareForeign Untracked 1 Json_tryUnconsText
12991302

13001303
foreignDeclResults :: (Map ForeignFunc (Sandbox, SuperNormal Symbol))
13011304
foreignDeclResults =

0 commit comments

Comments
 (0)