From 3f1baf2747993fec57b3d441c0e9264184f4e4e7 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 10 Aug 2017 20:19:18 -0400 Subject: - Small refactorings and fixes. --- stdlib/source/lux/data/format/json/codec.lux | 78 ++++++++++++++++++---- stdlib/source/lux/macro/poly/eq.lux | 13 ++-- stdlib/source/lux/type/check.lux | 98 ++++++++++++++-------------- 3 files changed, 124 insertions(+), 65 deletions(-) (limited to 'stdlib/source') diff --git a/stdlib/source/lux/data/format/json/codec.lux b/stdlib/source/lux/data/format/json/codec.lux index 073d3636b..8a50757bf 100644 --- a/stdlib/source/lux/data/format/json/codec.lux +++ b/stdlib/source/lux/data/format/json/codec.lux @@ -9,6 +9,7 @@ codec ["p" parser "p/" Monad]) (data [bool] + [bit] [text "text/" Eq Monoid] (text ["l" lexer]) [number "real/" Codec "nat/" Codec] @@ -19,6 +20,9 @@ (coll [list "L/" Fold Monad] [vector #+ Vector vector "Vector/" Monad] ["d" dict])) + (time ["i" instant] + ["du" duration] + ["da" date]) [macro #+ Monad with-gensyms] (macro ["s" syntax #+ syntax:] [code] @@ -131,16 +135,16 @@ (def: string~ (l;Lexer ..;String) (<| (l;enclosed ["\"" "\""]) - (loop [_ []] - (do p;Monad - [chars (l;some (l;none-of "\\\"")) - stop l;peek] - (if (text/= "\\" stop) - (do @ - [escaped escaped~ - next-chars (recur [])] - (wrap ($_ text/append chars escaped next-chars))) - (wrap chars)))))) + (loop [_ []]) + (do p;Monad + [chars (l;some (l;none-of "\\\"")) + stop l;peek]) + (if (text/= "\\" stop) + (do @ + [escaped escaped~ + next-chars (recur [])] + (wrap ($_ text/append chars escaped next-chars))) + (wrap chars)))) (def: (kv~ json~) (-> (-> Unit (l;Lexer JSON)) (l;Lexer [..;String JSON])) @@ -191,6 +195,28 @@ (function [input] (non-rec (rec-encode non-rec) input))) +(def: low-mask Nat (|> +1 (bit;shift-left +32) n.dec)) +(def: high-mask Nat (|> low-mask (bit;shift-left +32))) + +(struct: #hidden _ (Codec JSON Nat) + (def: (encode input) + (let [high (|> input (bit;and high-mask) (bit;unsigned-shift-right +32)) + low (bit;and low-mask input)] + (..;array (vector (|> high nat-to-int int-to-real #..;Number) + (|> low nat-to-int int-to-real #..;Number))))) + (def: (decode input) + (<| (../reader;run input) + (do p;Monad + [high ../reader;number + low ../reader;number]) + (wrap (n.+ (|> high real-to-int int-to-nat (bit;shift-left +32)) + (|> low real-to-int int-to-nat)))))) + +(struct: #hidden _ (Codec JSON Int) + (def: encode (|>. int-to-nat (:: Codec encode))) + (def: decode + (|>. (:: Codec decode) (:: R;Functor map nat-to-int)))) + (poly: #hidden Codec//encode (with-expansions [ (do-template [ ] @@ -201,8 +227,21 @@ [Unit poly;unit (function [(~ (code;symbol ["" "0"]))] #..;Null)] [Bool poly;bool ..;boolean] + [Nat poly;nat (:: ;;Codec (~' encode))] + [Int poly;int (:: ;;Codec (~' encode))] [Real poly;real ..;number] - [Text poly;text ..;string])] + [Text poly;text ..;string]) +