| 46 | | toYaml :: Val -> Eval YamlNode |
| 47 | | toYaml VUndef = return YamlNil |
| 48 | | --toYaml (VNum num) = return $ YamlStr -- better handled by pretty |
| 49 | | toYaml (VStr str) = return $ YamlStr (encodeUTF8 str) |
| 50 | | toYaml (VList nodes) = do |
| 51 | | fmap YamlSeq $ mapM toYaml nodes |
| 52 | | toYaml x = return $ YamlStr $ encodeUTF8 $ pretty x |
| 53 | | --toYaml (VHash hash) = do |
| 54 | | -- fmap YamlMap $ Map.toList hash |
| | 46 | toYaml :: Int -> Val -> Eval YamlNode |
| | 47 | toYaml 0 _ = return $ YamlStr "<deep recursion>" -- fail? make this configurable? |
| | 48 | toYaml _ VUndef = return YamlNil |
| | 49 | --toYaml (VNum num) = return $ YamlStr -- better handled by pretty? |
| | 50 | toYaml _ (VStr str) = return $ YamlStr (encodeUTF8 str) |
| | 51 | toYaml (d+1) (VList nodes) = do |
| | 52 | fmap YamlSeq $ mapM (toYaml d) nodes |
| | 53 | toYaml (d+1) v@(VRef r) = do -- stolen from Pugs.Prim prettyVal. Can these be refactored together? |
| | 54 | v' <- readRef r |
| | 55 | ifValTypeIsa v "Pair" |
| | 56 | (case v' of |
| | 57 | VList [ks, vs] -> do |
| | 58 | kStr <- toYaml d ks |
| | 59 | vStr <- toYaml d vs |
| | 60 | return $ YamlMap [(kStr, vStr)] -- assume a pair is a one-element hash |
| | 61 | _ -> toYaml d v' -- XXX: probably broken to blithingly ignore ref levels here |
| | 62 | ) |
| | 63 | (do nodes <- toYaml d v' |
| | 64 | ifValTypeIsa v "Array" |
| | 65 | (return $ nodes) |
| | 66 | (ifValTypeIsa v "Hash" |
| | 67 | --(return $ YamlMap('{':(init (tail str))) ++ "}") |
| | 68 | (return nodes) |
| | 69 | (return $ YamlMap [(YamlStr "<ref>", nodes)])) -- XXX |
| | 70 | ) |
| | 71 | toYaml _ v = return $ YamlStr $ encodeUTF8 $ pretty v |