Changeset 8600 for src/Pugs/Prim/Yaml.hs

Show
Ignore:
Timestamp:
01/07/06 09:46:00 (3 years ago)
Author:
gaal
Message:

Yaml emitting:

  • some readability improvements, need more work
  • towards proper tagging of emitted arbitrary objects (probably need to add a tag to all YamlNode? types, not just YamlMap?... we'll see)
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim/Yaml.hs

    r8593 r8600  
    88import Pugs.AST 
    99import Pugs.Pretty 
     10import Pugs.Types 
    1011import Data.Yaml.Syck 
    1112import qualified Data.Map as Map 
     
    2223 
    2324fromYaml :: YamlNode -> Eval Val 
     25fromYaml YamlNil = return VUndef 
    2426fromYaml (YamlStr str) = return $ VStr (decodeUTF8 str) 
    2527fromYaml (YamlSeq nodes) = do 
     
    2830        IntMap.fromAscList ([0..] `zip` map lazyScalar vals) 
    2931    return $ VRef (arrayRef av) 
    30 fromYaml (YamlMap nodes) = do 
     32fromYaml (YamlMap _ nodes) = do 
    3133    vals    <- forM nodes $ \(keyNode, valNode) -> do 
    3234        key <- fromVal =<< fromYaml keyNode 
     
    3436        return (key, val) 
    3537    hv      <- liftSTM $ (newTVar (Map.fromList vals) :: STM IHash) 
     38    -- XXX: if YamlMap (Just "!perl/":type) nodes then mkObject etc. 
    3639    return $ VRef (hashRef hv) 
    3740 
     
    5457    trace ("toYaml VRef: " ++ (show v) ++ " type=" ++ (show t)) $ return () 
    5558    (ifValTypeIsa v "Hash" 
    56         (do  
    57             case r of 
    58                 MkRef (IHash hv) -> do 
    59                     h <- hash_fetch hv 
    60                     let assocs = Map.toList h 
    61                     yamlmap <- mapM ( \(k, v) -> do 
    62                         k' <- toYaml d (VStr k) 
    63                         v' <- toYaml d v 
    64                         return (k', v')) assocs 
    65                     return $ YamlMap yamlmap 
    66                 _ -> error ("can't process hash: " ++ show v') -- XXX 
     59        (case r of 
     60            -- "My brain just exploded. I can't handle pattern bindings for existentially-quantified constructors." 
     61            -- let (MkRef (IHash hv)) = r 
     62            -- XXX golfme for readability! 
     63            MkRef (IHash hv) -> do 
     64                h <- hash_fetch hv 
     65                let assocs = Map.toList h 
     66                yamlmap <- flip mapM assocs (\(ka, va) -> do 
     67                   ka' <- toYaml d (VStr ka) 
     68                   va' <- toYaml d va 
     69                   return (ka', va')) 
     70                return $ YamlMap Nothing yamlmap 
     71            _ -> error ("unexpected node: " ++ show v) 
    6772        ) 
    6873        (do nodes <- toYaml d v' 
    69             ifValTypeIsa v "Array" 
    70                 (return $ nodes) 
    71                 (return $ YamlMap [(YamlStr "<ref>", nodes)])) -- XXX 
    72         ) 
     74            (ifValTypeIsa v "Array" 
     75                (return $ nodes) --(return $ YamlMap Nothing [(YamlStr "<ref>", nodes)])) -- XXX 
     76                (return $ case v' of 
     77                    VObject _ -> nodes 
     78                    _ -> YamlMap Nothing [(YamlStr "<ref>", nodes)] -- XXX 
     79                )))) 
    7380toYaml (d+1) (VList nodes) = do 
    7481    trace ("toYaml VList: " ++ (show nodes)) $ return () 
    7582    fmap YamlSeq $ mapM (toYaml d) nodes 
     83toYaml (d+1) v@(VObject obj) = do 
     84    -- ... dump the objAttrs 
     85    -- XXX this needs fixing WRT demagicalized pairs: 
     86    -- currently, this'll return Foo.new((attr => "value)), with the inner 
     87    -- parens, which is, of course, wrong. 
     88    hash    <- fromVal v :: Eval VHash 
     89    attrs   <- toYaml d (VRef (hashRef hash)) 
     90    return $ addTag (Just $ "!pugs:object/" ++ showType (objType obj)) attrs 
     91    where 
     92        addTag _ (YamlMap (Just x) _) = error ("can't add tag: already tagged with" ++ x) 
     93        addTag tag (YamlMap _ m) = YamlMap tag m 
    7694toYaml _ v = return $ YamlStr $ encodeUTF8 $ pretty v 
    7795