| 141 | | |
| 142 | | catchT :: ((Val -> Eval b) -> Eval Val) -> Eval Val |
| 143 | | catchT action = tryT (action retShift) |
| | 129 | -- Data Definitions |
| | 130 | |
| | 131 | {-| |
| | 132 | Represents a value. |
| | 133 | |
| | 134 | Note that 'Val' is also a constructor for 'Exp' (i.e. an expression containing |
| | 135 | a value), so don't confuse the two. Similarly, all the constructors for |
| | 136 | @data 'Val'@ are themselves puns on the types of values they contain. |
| | 137 | -} |
| | 138 | data Val |
| | 139 | = VUndef -- ^ Undefined value |
| | 140 | | VBool !VBool -- ^ Boolean value |
| | 141 | | VInt !VInt -- ^ Integer value |
| | 142 | | VRat !VRat -- ^ Rational number value |
| | 143 | | VNum !VNum -- ^ Number (i.e. a double) |
| | 144 | | VComplex !VComplex -- ^ Complex number value |
| | 145 | | VStr !VStr -- ^ String value |
| | 146 | | VList !VList -- ^ List value |
| | 147 | | VType !VType -- ^ Type value (e.g. @Int@ or @Type@) |
| | 148 | | VJunc !VJunc -- ^ Junction value |
| | 149 | | VError !Val ![Pos] -- ^ Error |
| | 150 | | VControl !VControl |
| | 151 | ------------------------------------------------------------------- |
| | 152 | -- The following are runtime-only values (VRef is negotiable) |
| | 153 | | VRef !VRef -- ^ Reference value |
| | 154 | | VCode !VCode -- ^ A code object |
| | 155 | | VBlock !VBlock |
| | 156 | | VHandle !VHandle -- ^ File handle |
| | 157 | | VSocket !VSocket -- ^ Socket handle |
| | 158 | | VThread !VThread |
| | 159 | | VProcess !VProcess -- ^ PID value |
| | 160 | | VRule !VRule -- ^ Rule\/regex value |
| | 161 | | VSubst !VSubst -- ^ Substitution value (correct?) |
| | 162 | | VMatch !VMatch -- ^ Match value |
| | 163 | | VObject !VObject -- ^ Object |
| | 164 | | VOpaque !VOpaque |
| | 165 | | PerlSV !PerlSV |
| | 166 | | VV !Val.Val |
| | 167 | deriving (Show, Eq, Ord, Typeable) |
| | 168 | |
| | 169 | {-| |
| | 170 | Evaluation environment. |
| | 171 | |
| | 172 | The current environment is stored in the @Reader@ monad inside the current |
| | 173 | 'Eval' monad, and can be retrieved using @ask@ for the whole 'Env', or @asks@ |
| | 174 | if you just want a single field. |
| | 175 | -} |
| | 176 | data Env = MkEnv |
| | 177 | { envContext :: !Cxt -- ^ Current context |
| | 178 | -- ('CxtVoid', 'CxtItem' or 'CxtSlurpy') |
| | 179 | , envLValue :: !Bool -- ^ Are we in an LValue context? |
| | 180 | , envLexical :: !Pad -- ^ Cached lexical pad for variable lookup |
| | 181 | , envLexPads :: !LexPads -- ^ Current lexical pads; MY is leftmost, OUTER is next, etc |
| | 182 | , envCaller :: !(Maybe Env) -- ^ CALLER pads |
| | 183 | , envCompPad :: !(Maybe MPad) -- ^ Current COMPILING pad |
| | 184 | , envGlobal :: !MPad -- ^ Global pad for variable lookup |
| | 185 | , envPackage :: !Pkg -- ^ Current package |
| | 186 | , envEval :: !(Exp -> Eval Val) -- ^ Active evaluator |
| | 187 | , envBody :: !Exp -- ^ Current AST expression |
| | 188 | , envFrames :: !(Set Frame) -- ^ Special-markers in the dynamic path |
| | 189 | , envDebug :: !DebugInfo -- ^ Debug info map |
| | 190 | , envPos :: !Pos -- ^ Source position range |
| | 191 | , envPragmas :: ![Pragma] -- ^ List of pragmas in effect |
| | 192 | , envInitDat :: !(TVar InitDat) -- ^ BEGIN result information |
| | 193 | , envMaxId :: !(TVar ObjectId) -- ^ Current max object id |
| | 194 | , envAtomic :: !Bool -- ^ Are we in an atomic transaction? |
| | 195 | } |
| | 196 | deriving (Show, Eq, Ord, Typeable) -- don't derive YAML for now |
| | 197 | |
| | 198 | data IVar v where |
| | 199 | IScalar :: ScalarClass a => !a -> IVar VScalar |
| | 200 | IArray :: ArrayClass a => !a -> IVar VArray |
| | 201 | IHash :: HashClass a => !a -> IVar VHash |
| | 202 | ICode :: CodeClass a => !a -> IVar VCode |
| | 203 | IHandle :: HandleClass a => !a -> IVar VHandle |
| | 204 | IRule :: RuleClass a => !a -> IVar VRule |
| | 205 | IThunk :: ThunkClass a => !a -> IVar VThunk |
| | 206 | IPair :: PairClass a => !a -> IVar VPair |
| | 207 | IVal :: !Val -> IVar Val |
| | 208 | |
| | 209 | data VOpaque where |
| | 210 | MkOpaque :: Value a => !a -> VOpaque |
| | 211 | |
| | 212 | -- GADTs, here we come! |
| | 213 | data VRef where |
| | 214 | MkRef :: (Typeable a) => !(IVar a) -> VRef |
| | 215 | |
| | 216 | data VObject = MkObject |
| | 217 | { objType :: !VType |
| | 218 | , objAttrs :: !IHash |
| | 219 | , objOpaque :: !(Maybe Dynamic) |
| | 220 | , objId :: !ObjectId |
| | 221 | } |
| | 222 | deriving (Show, Eq, Ord, Typeable) {-!derive: YAML_Pos!-} |
| | 223 | |
| | 224 | -- | Represents an expression tree. |
| | 225 | data Exp |
| | 226 | = Noop -- ^ No-op |
| | 227 | | App !Exp !(Maybe Exp) ![Exp] -- ^ Function application |
| | 228 | -- e.g. myfun($invocant: $arg) |
| | 229 | | Syn !String ![Exp] -- ^ Syntactic construct that cannot |
| | 230 | -- be represented by 'App'. |
| | 231 | | Ann !Ann !Exp -- ^ Annotation (see @Ann@) |
| | 232 | -- | Pad !Scope !Pad !Exp -- ^ Lexical pad |
| | 233 | | Sym !Scope !Var !EntryFlags !Exp !Exp -- ^ Symbol declaration |
| | 234 | | Stmts !Exp !Exp -- ^ Multiple statements |
| | 235 | | Prim !([Val] -> Eval Val) -- ^ Primitive |
| | 236 | | Val !Val -- ^ Value |
| | 237 | | Var !Var -- ^ Variable |
| | 238 | | NonTerm !Pos -- ^ Parse error |
| | 239 | deriving (Show, Eq, Ord, Typeable) {-!derive: YAML_Pos!-} |
| | 240 | |
| | 241 | newtype ObjectId = MkObjectId { unObjectId :: Int } |
| | 242 | deriving (Show, Eq, Ord, Typeable) {-!derive: YAML_Pos!-} |
| | 243 | |
| | 244 | -- Type Synonyms |
| | 245 | |
| | 246 | type VType = Type |
| | 247 | type VArray = [Val] |
| | 248 | type VHash = Map VStr Val |
| | 249 | type VList = [Val] |
| | 250 | |
| | 251 | -- Functions |
| | 388 | |
| | 389 | runInvokePerl5 :: PerlSV -> PerlSV -> [PerlSV] -> Eval Val |
| | 390 | runInvokePerl5 sub inv args = do |
| | 391 | env <- ask |
| | 392 | rv <- io $ do |
| | 393 | envSV <- mkEnv env |
| | 394 | invokePerl5 sub inv args envSV (enumCxt $ envContext env) |
| | 395 | case rv of |
| | 396 | Perl5ReturnValues [x] -> io $ svToVal x |
| | 397 | Perl5ReturnValues xs -> io $ fmap VList (mapM svToVal xs) |
| | 398 | Perl5ErrorString str -> fail str |
| | 399 | Perl5ErrorObject err -> throwError (PerlSV err) |
| | 400 | |
| | 401 | anyToVal :: (Show a, Typeable a) => a -> Val |
| | 402 | anyToVal x |
| | 403 | | Just v <- fromTypeable x = v |
| | 404 | | Just v <- fromTypeable x = PerlSV v |
| | 405 | | Just v <- fromTypeable x = VStr v |
| | 406 | | Just v <- fromTypeable x = VInt v |
| | 407 | | Just v <- fromTypeable x = VNum v |
| | 408 | | Just () <- fromTypeable x = VUndef |
| | 409 | | otherwise = error (show x) |
| | 410 | |
| | 411 | newSVval :: Val -> IO PerlSV |
| | 412 | newSVval val = case val of |
| | 413 | PerlSV sv -> return sv |
| | 414 | VStr str -> vstrToSV str |
| | 415 | VType typ -> vstrToSV (showType typ) |
| | 416 | VBool bool -> vintToSV (fromEnum bool) |
| | 417 | VInt int -> vintToSV int |
| | 418 | VRat rat -> vnumToSV rat |
| | 419 | VNum num -> vnumToSV num |
| | 420 | VRef ref -> vrefToSV ref |
| | 421 | VCode{} -> mkValRef val "Code" |
| | 422 | VBlock{} -> mkValRef val "Code" |
| | 423 | VHandle{} -> mkValRef val "Handle" |
| | 424 | VSocket{} -> mkValRef val "Socket" |
| | 425 | VList{} -> mkValRef val "Array" |
| | 426 | VUndef -> svUndef |
| | 427 | VError{} -> svUndef |
| | 428 | _ -> mkValRef val "" |
| | 429 | |
| | 430 | vrefToSV :: VRef -> IO PerlSV |
| | 431 | vrefToSV ref = mkValRef (VRef ref) $ case ref of |
| | 432 | MkRef IScalar{} -> "Scalar" |
| | 433 | MkRef IArray{} -> "Array" |
| | 434 | MkRef IHash{} -> "Hash" |
| | 435 | MkRef ICode{} -> "Code" |
| | 436 | MkRef IHandle{} -> "Handle" |
| | 437 | MkRef IRule{} -> "Rule" |
| | 438 | MkRef IThunk{} -> "Thunk" |
| | 439 | MkRef IPair{} -> "Pair" |
| | 440 | MkRef (IVal v) -> show (valType v) |
| | 441 | |
| | 442 | valToStr :: Val -> Eval VStr |
| | 443 | valToStr = fromVal |
| | 444 | |
| | 445 | |
| | 446 | errStr :: VStr -> Val |
| | 447 | errStr str = VError (VStr str) [] |
| | 448 | |
| | 449 | errStrPos :: VStr -> Pos -> Val |
| | 450 | errStrPos str pos = VError (VStr str) [pos] |
| | 451 | |
| | 452 | errValPos :: Val -> Pos -> Val |
| | 453 | errValPos val pos = VError val [pos] |
| | 454 | |
| | 455 | enterAtomicEnv :: Env -> Env |
| | 456 | enterAtomicEnv env = env{ envAtomic = True } |
| | 457 | |
| | 458 | {-| |
| | 459 | Find the 'Type' of the value contained by a 'Val'. |
| | 460 | |
| | 461 | See "Pugs.Types" for info on types. |
| | 462 | -} |
| | 463 | valType :: Val -> Type |
| | 464 | valType VUndef = mkType "Scalar" |
| | 465 | valType (VRef v) = refType v |
| | 466 | valType (VBool _) = mkType "Bool" |
| | 467 | valType (VInt _) = mkType "Int" |
| | 468 | valType (VRat _) = mkType "Rat" |
| | 469 | valType (VNum _) = mkType "Num" |
| | 470 | valType (VComplex _) = mkType "Complex" |
| | 471 | valType (VStr _) = mkType "Str" |
| | 472 | -- valType (VList _) = mkType "List" |
| | 473 | valType (VList _) = mkType "Array" |
| | 474 | valType (VCode c) = code_iType c |
| | 475 | valType (VBlock _) = mkType "Block" |
| | 476 | valType (VJunc _) = mkType "Junction" |
| | 477 | valType (VError _ _) = mkType "Error" |
| | 478 | valType (VHandle _) = mkType "IO" |
| | 479 | valType (VSocket _) = mkType "Socket" |
| | 480 | valType (VThread _) = mkType "Thread" |
| | 481 | valType (VProcess _) = mkType "Process" |
| | 482 | valType (VControl _) = mkType "Control" |
| | 483 | valType (VRule _) = mkType "Regex" |
| | 484 | valType (VSubst _) = mkType "Subst" |
| | 485 | valType (VMatch _) = mkType "Match" |
| | 486 | valType (VType t) = t |
| | 487 | valType (VObject o) = objType o |
| | 488 | valType (VOpaque _) = mkType "Object" |
| | 489 | valType (PerlSV _) = mkType "Scalar::Perl5" |
| | 490 | valType (VV _) = mkType "Scalar::Perl5" -- (cast $ Val.valMeta v) |
| | 491 | |
| | 492 | valToBool :: Val -> Eval VBool |
| | 493 | valToBool = fromVal |
| | 494 | |
| | 495 | _Sym :: Scope -> String -> EntryFlags -> Exp -> Exp -> Exp |
| | 496 | _Sym scope str flags init rest = Sym scope (cast str) flags init rest |
| | 497 | |
| | 498 | _Var :: String -> Exp |
| | 499 | _Var str = Var (possiblyFixOperatorName (cast str)) |
| | 500 | |
| | 501 | -- Recursively apply a transformation to an Exp structure |
| | 502 | transformExp :: (Monad m) => (Exp -> m Exp) -> Exp -> m Exp |
| | 503 | transformExp f (App a b cs) = do |
| | 504 | a' <- transformExp f a |
| | 505 | b' <- case b of |
| | 506 | Just e -> liftM Just $ transformExp f e |
| | 507 | Nothing -> return Nothing |
| | 508 | cs' <- mapM (transformExp f) cs |
| | 509 | f $ App a' b' cs' |
| | 510 | transformExp f (Syn t es) = f =<< liftM (Syn t) (mapM (transformExp f) es) |
| | 511 | transformExp f (Ann a e) = f =<< liftM (Ann a) (transformExp f e) |
| | 512 | -- transformExp f (Pad s p e) = f =<< liftM (Pad s p) (transformExp f e) |
| | 513 | transformExp f (Sym s v c i e) = f =<< liftM (Sym s v c i) (transformExp f e) |
| | 514 | transformExp f (Stmts e1 e2) = do |
| | 515 | e1' <- transformExp f e1 |
| | 516 | e2' <- transformExp f e2 |
| | 517 | f $ Stmts e1' e2' |
| | 518 | transformExp f e = f e |
| | 519 | |
| | 520 | {- FIXME: Figure out how to get this working without a monad, and make it castV -} |
| | 521 | expToEvalVal :: Exp -> Eval Val |
| | 522 | expToEvalVal exp = do |
| | 523 | obj <- createObject (mkType "Code::Exp") [] |
| | 524 | return $ VObject obj{ objOpaque = Just $ toDyn exp } |
| | 525 | |
| | 526 | fromVals :: (Value n) => Val -> Eval [n] |
| | 527 | fromVals v = mapM fromVal =<< fromVal v |
| | 528 | |
| | 529 | extractPlaceholderVarsExp :: Exp -> ([Exp], Set Var) -> ([Exp], Set Var) |
| | 530 | extractPlaceholderVarsExp ex (exps, vs) = (ex':exps, vs') |
| | 531 | where |
| | 532 | (ex', vs') = extractPlaceholderVars ex vs |
| | 533 | |
| | 534 | {-| Deduce the placeholder vars ($^a, $^x etc.) used by a block). -} |
| | 535 | extractPlaceholderVars :: Exp -> Set Var -> (Exp, Set Var) |
| | 536 | extractPlaceholderVars (App n invs args) vs = (App n' invs' args', vs''') |
| | 537 | where |
| | 538 | (n', vs') = extractPlaceholderVars n vs |
| | 539 | (invs', vs'') = maybe (invs, vs') (\inv -> let (x, y) = extractPlaceholderVars inv vs' in (Just x, y)) invs |
| | 540 | (args', vs''') = foldr extractPlaceholderVarsExp ([], vs'') args |
| | 541 | extractPlaceholderVars (Stmts exp1 exp2) vs = (Stmts exp1' exp2', vs'') |
| | 542 | where |
| | 543 | (exp1', vs') = extractPlaceholderVars exp1 vs |
| | 544 | (exp2', vs'') = extractPlaceholderVars exp2 vs' |
| | 545 | extractPlaceholderVars (Syn n exps) vs = (Syn n exps', vs'') |
| | 546 | where |
| | 547 | (exps', vs') = foldr extractPlaceholderVarsExp ([], vs) exps |
| | 548 | vs'' = case n of |
| | 549 | "when" -> Set.insert (cast "$_") vs' |
| | 550 | "given" -> Set.delete (cast "$_") vs' |
| | 551 | _ -> vs' |
| | 552 | extractPlaceholderVars (Var var) vs |
| | 553 | | TImplicit <- v_twigil var |
| | 554 | , var' <- var{ v_twigil = TNil } |
| | 555 | = (Var var', Set.insert var' vs) |
| | 556 | | var == cast "$_" |
| | 557 | = (Var var, Set.insert var vs) |
| | 558 | | otherwise |
| | 559 | = (Var var, vs) |
| | 560 | extractPlaceholderVars (Ann ann ex) vs = ((Ann ann ex'), vs') |
| | 561 | where |
| | 562 | (ex', vs') = extractPlaceholderVars ex vs |
| | 563 | -- extractPlaceholderVars (Pad scope pad ex) vs = ((Pad scope pad ex'), vs') |
| | 564 | -- where |
| | 565 | -- (ex', vs') = extractPlaceholderVars ex vs |
| | 566 | extractPlaceholderVars (Sym scope var flags ini ex) vs = ((Sym scope var flags ini ex'), vs') |
| | 567 | where |
| | 568 | (ex', vs') = extractPlaceholderVars ex vs |
| | 569 | extractPlaceholderVars exp vs = (exp, vs) |
| | 570 | |
| | 571 | envPos' :: Env -> Pos |
| | 572 | envPos' = envPos |
| | 573 | |
| | 574 | envWant :: Env -> String |
| | 575 | envWant env = |
| | 576 | showCxt (envContext env) ++ (if envLValue env then ", LValue" else "") |
| | 577 | where |
| | 578 | showCxt CxtVoid = "Void" |
| | 579 | showCxt (CxtItem typ) = "Scalar (" ++ showType typ ++ ")" |
| | 580 | showCxt (CxtSlurpy typ) = "List (" ++ showType typ ++ ")" |
| | 581 | |
| | 582 | refreshPad :: Pad -> Eval Pad |
| | 583 | refreshPad pad = do |
| | 584 | fmap listToPad $ forM (padToList pad) $ \(name, entry) -> do |
| | 585 | -- warn "Refreshing pad entry" (name, entry) |
| | 586 | entry' <- case entry of |
| | 587 | PELexical{ pe_proto = proto } -> stm $ do |
| | 588 | ref <- cloneRef proto |
| | 589 | tvar' <- newTVar ref |
| | 590 | return entry{ pe_store = tvar' } |
| | 591 | _ -> return entry |
| | 592 | return (name, entry') |
| | 593 | |
| | 594 | {-| |
| | 595 | Retrieve the global 'Pad' from the current evaluation environment. |
| | 596 | |
| | 597 | 'Env' stores the global 'Pad' in an STM variable, so we have to @asks@ |
| | 598 | 'Eval'\'s @ReaderT@ for the variable, then extract the pad itself from the |
| | 599 | STM var. |
| | 600 | -} |
| | 601 | askGlobal :: Eval Pad |
| | 602 | askGlobal = do |
| | 603 | glob <- asks (mp_pad . envGlobal) |
| | 604 | stm $ readTVar glob |
| | 605 | |
| | 606 | writeVar :: Var -> Val -> Eval () |
| | 607 | writeVar var val |
| | 608 | | isLexicalVar var = doWriteVar (asks envLexical) |
| | 609 | | otherwise = doWriteVar askGlobal |
| | 610 | where |
| | 611 | doWriteVar askPad = do |
| | 612 | pad <- askPad |
| | 613 | case lookupPad var pad of |
| | 614 | Just PEConstant{} -> fail $ "Cannot rebind constant: " ++ show var |
| | 615 | Just c -> do |
| | 616 | ref <- stm $ readTVar (pe_store c) |
| | 617 | writeRef ref val |
| | 618 | _ -> fail $ "Cannot bind to non-existing variable: " ++ show var |
| | 619 | |
| | 620 | readVar :: Var -> Eval Val |
| | 621 | readVar var |
| | 622 | | isLexicalVar var = do |
| | 623 | lex <- asks envLexical |
| | 624 | case findSym var lex of |
| | 625 | Just action -> stm action >>= readRef |
| | 626 | _ -> return undef |
| | 627 | | otherwise = do |
| | 628 | glob <- askGlobal |
| | 629 | case findSym var glob of |
| | 630 | Just action -> stm action >>= readRef |
| | 631 | _ -> return undef |
| | 632 | |
| | 633 | {-| |
| | 634 | The \'empty expression\' is just a no-op ('Noop'). |
| | 635 | -} |
| | 636 | emptyExp :: Exp |
| | 637 | emptyExp = Noop |
| | 638 | |
| | 639 | retControl :: VControl -> Eval a |
| | 640 | retControl = retShift . VControl |
| | 641 | |
| | 642 | defined :: VScalar -> Bool |
| | 643 | defined VUndef = False |
| | 644 | defined VType{} = False |
| | 645 | defined _ = True |
| | 646 | -- | Produce an undefined Perl 6 value (i.e. 'VUndef'). |
| | 647 | undef :: VScalar |
| | 648 | undef = VUndef |
| | 649 | |
| | 650 | forceRef :: VRef -> Eval Val |
| | 651 | forceRef (MkRef (IScalar sv)) = forceRef =<< fromVal =<< scalar_fetch sv |
| | 652 | forceRef (MkRef (IThunk tv)) = thunk_force tv |
| | 653 | forceRef r = die "Cannot forceRef" r |
| | 654 | |
| | 655 | dumpRef :: VRef -> Eval Val |
| | 656 | dumpRef (MkRef (ICode cv)) = do |
| | 657 | vsub <- code_fetch cv |
| | 658 | return (VStr $ "(MkRef (ICode $ " ++ show vsub ++ "))") |
| | 659 | dumpRef (MkRef (IScalar sv)) | scalar_iType sv == mkType "Scalar::Const" = do |
| | 660 | sv <- scalar_fetch sv |
| | 661 | return (VStr $ "(MkRef (IScalar $ " ++ show sv ++ "))") |
| | 662 | dumpRef ref = return (VStr $ "(unsafePerformIO . newObject $ mkType \"" ++ showType (refType ref) ++ "\")") |
| | 663 | |
| | 664 | -- Reduce a VRef in rvalue context. |
| | 665 | readRef :: VRef -> Eval Val |
| | 666 | readRef (MkRef (IScalar sv)) = scalar_fetch sv |
| | 667 | readRef (MkRef (ICode cv)) = do |
| | 668 | vsub <- code_fetch cv |
| | 669 | return $ VCode vsub |
| | 670 | readRef (MkRef (IHash hv)) = do |
| | 671 | pairs <- hash_fetch hv |
| | 672 | return $ VList $ map (\(k, v) -> castV (castV k, v)) (Map.assocs pairs) |
| | 673 | readRef (MkRef (IArray av)) = do |
| | 674 | vals <- array_fetch av |
| | 675 | return $ VList vals |
| | 676 | |
| | 677 | -- XXX - This case is entirely bogus; but no time to fix it now. |
| | 678 | readRef (MkRef (IPair pv)) = do |
| | 679 | (k, v) <- pair_fetch pv |
| | 680 | return $ VList [k, v] |
| | 681 | |
| | 682 | readRef (MkRef (IHandle io)) = return . VHandle =<< handle_fetch io |
| | 683 | readRef (MkRef (IRule rx)) = return . VRule =<< rule_fetch rx |
| | 684 | readRef (MkRef (IThunk tv)) = readRef =<< fromVal =<< thunk_force tv |
| | 685 | readRef (MkRef (IVal v)) = do |
| | 686 | cxt <- asks envContext |
| | 687 | v ./ cxt |
| | 688 | |
| | 689 | retIVar :: (Typeable a) => IVar a -> Eval Val |
| | 690 | retIVar = return . VRef . MkRef |
| | 691 | |
| | 692 | fromVList :: Val -> Eval VArray |
| | 693 | fromVList (VList v) = return v |
| | 694 | fromVList x = return [x] |
| | 695 | |
| | 696 | fromVHash :: Val -> Eval VHash |
| | 697 | fromVHash = fromVal |
| | 698 | |
| | 699 | writeRef :: VRef -> Val -> Eval () |
| | 700 | writeRef (MkRef (IScalar s)) (VList vals) = do |
| | 701 | av <- newArray vals |
| | 702 | scalar_store s (VRef $ MkRef av) |
| | 703 | writeRef (MkRef (IScalar s)) val = scalar_store s val |
| | 704 | writeRef (MkRef (IArray s)) val = array_store s =<< fromVList val |
| | 705 | writeRef (MkRef (IHash s)) val = hash_store s =<< fromVHash val |
| | 706 | writeRef (MkRef (ICode s)) val = code_store s =<< fromVal val |
| | 707 | writeRef (MkRef (IPair s)) val = pair_storeVal s val |
| | 708 | writeRef (MkRef (IThunk tv)) val = (`writeRef` val) =<< fromVal =<< thunk_force tv |
| | 709 | writeRef r _ = die "Cannot writeRef" r |
| | 710 | |
| | 711 | cloneRef :: VRef -> STM VRef |
| | 712 | cloneRef (MkRef x) = fmap MkRef (cloneIVar x) |
| | 713 | |
| | 714 | clearRef :: VRef -> Eval () |
| | 715 | clearRef (MkRef (IScalar s)) = scalar_store s undef |
| | 716 | clearRef (MkRef (IArray s)) = array_clear s |
| | 717 | clearRef (MkRef (IHash s)) = hash_clear s |
| | 718 | clearRef (MkRef (IPair s)) = pair_storeVal s undef |
| | 719 | clearRef (MkRef (IThunk tv)) = clearRef =<< fromVal =<< thunk_force tv |
| | 720 | clearRef r = die "Cannot clearRef" r |
| | 721 | |
| | 722 | {-# SPECIALISE newObject :: Type -> Eval VRef #-} |
| | 723 | {-# SPECIALISE newObject :: Type -> IO VRef #-} |
| | 724 | newObject :: (MonadSTM m, MonadIO m) => Type -> m VRef |
| | 725 | newObject typ = case showType typ of |
| | 726 | "Any" -> io $ fmap scalarRef $ newTVarIO undef |
| | 727 | "Item" -> io $ fmap scalarRef $ newTVarIO undef |
| | 728 | "Scalar" -> io $ fmap scalarRef $ newTVarIO undef |
| | 729 | "Array" -> io $ do |
| | 730 | iv <- newTVarIO [::] |
| | 731 | return $ arrayRef (MkIArray iv) |
| | 732 | "Hash" -> do |
| | 733 | h <- io (H.new (==) H.hashString) |
| | 734 | return $ hashRef (h :: IHash) |
| | 735 | "Sub" -> newObject $ mkType "Code" |
| | 736 | "Routine" -> newObject $ mkType "Code" |
| | 737 | "Method" -> newObject $ mkType "Code" |
| | 738 | "Submethod" -> newObject $ mkType "Code" |
| | 739 | "Code" -> return $! codeRef $ mkPrim |
| | 740 | { subAssoc = AIrrelevantToParsing |
| | 741 | , subBody = Prim . const $ fail "Cannot use Undef as a Code object" |
| | 742 | } |
| | 743 | "Type" -> io $ fmap scalarRef $ newTVarIO undef |
| | 744 | "Pair" -> do |
| | 745 | key <- newObject (mkType "Scalar") |
| | 746 | val <- newObject (mkType "Scalar") |
| | 747 | return $ MkRef (IPair (VRef key, VRef val)) |
| | 748 | "Regex" -> io $ fmap scalarRef $ newTVarIO undef -- XXX Wrong |
| | 749 | "Capture" -> io $ fmap scalarRef $ newTVarIO undef -- XXX Wrong |
| | 750 | _ -> fail ("Class prototype occured where its instance object expected: " ++ showType typ) |
| | 751 | |
| | 752 | doPair :: Val -> (forall a. PairClass a => a -> b) -> Eval b |
| | 753 | doPair (VRef (MkRef (IPair pv))) f = return $ f pv |
| | 754 | doPair (VRef (MkRef (IHash hv))) f = do |
| | 755 | vals <- hash_fetch hv |
| | 756 | let [(k, v)] = Map.toList vals |
| | 757 | return $ f (VStr k, v) |
| | 758 | doPair (VRef (MkRef (IArray av))) f = do |
| | 759 | vals <- array_fetch av |
| | 760 | let [k, v] = take 2 (vals ++ repeat undef) |
| | 761 | return $ f (k, v) |
| | 762 | doPair (VRef (MkRef (IScalar sv))) f = do |
| | 763 | val <- scalar_fetch sv |
| | 764 | case val of |
| | 765 | VUndef -> do |
| | 766 | ref@(MkRef (IPair pv)) <- newObject (mkType "Pair") |
| | 767 | scalar_store sv (VRef ref) |
| | 768 | return $ f pv |
| | 769 | _ -> doPair val f |
| | 770 | doPair (VRef x) _ = die "Cannot cast into Pair" x |
| | 771 | doPair val f = do |
| | 772 | vs <- fromVal val |
| | 773 | case (vs :: VList) of |
| | 774 | [x, y] -> return $ f (x, y) |
| | 775 | _ -> do |
| | 776 | pv <- castFailM val "Confusing pair?" |
| | 777 | return $ f (pv :: VPair) |
| | 778 | |
| | 779 | -- XXX: Refactor doHash and doArray into one -- also see Eval's [] and {} |
| | 780 | doHash :: Val -> (forall a. HashClass a => a -> b) -> Eval b |
| | 781 | doHash (PerlSV sv) f = return $ f sv |
| | 782 | doHash (VRef (MkRef (IHash hv))) f = return $ f hv |
| | 783 | doHash (VRef (MkRef (IScalar sv))) f = do |
| | 784 | val <- scalar_fetch sv |
| | 785 | case val of |
| | 786 | VUndef -> do |
| | 787 | ref@(MkRef (IHash hv)) <- newObject (mkType "Hash") |
| | 788 | scalar_store sv (VRef ref) |
| | 789 | return $ f hv |
| | 790 | _ -> doHash val f |
| | 791 | doHash (VRef (MkRef p@(IPair _))) f = return $ f p |
| | 792 | doHash (VObject o) f = return $ f (objAttrs o) |
| | 793 | doHash (VMatch m) f = do |
| | 794 | return $ f (matchSubNamed m) |
| | 795 | doHash val@(VRef _) _ = die "Cannot cast into Hash" val |
| | 796 | doHash val f = do |
| | 797 | hv <- fromVal val |
| | 798 | return $ f (hv :: VHash) |
| | 799 | |
| | 800 | -- can be factored out |
| | 801 | doArray :: Val -> (forall a. ArrayClass a => a -> b) -> Eval b |
| | 802 | doArray (PerlSV sv) f = return $ f sv |
| | 803 | doArray (VRef (MkRef (IArray av))) f = return $ f av |
| | 804 | doArray (VRef (MkRef (IScalar sv))) f = do |
| | 805 | val <- scalar_fetch sv |
| | 806 | if defined val |
| | 807 | then doArray val f |
| | 808 | else do |
| | 809 | ref@(MkRef (IArray hv)) <- newObject (mkType "Array") |
| | 810 | scalar_store sv (VRef ref) |
| | 811 | return $ f hv |
| | 812 | doArray (VRef (MkRef p@(IPair _))) f = return $ f p |
| | 813 | doArray val@(VRef (MkRef IHash{})) f = do |
| | 814 | av <- fromVal val |
| | 815 | return $ f (av :: VArray) |
| | 816 | doArray val@(VRef _) _ = die "Cannot cast into Array" val |
| | 817 | doArray (VMatch m) f = do |
| | 818 | return $ f (matchSubPos m) |
| | 819 | doArray val f = do |
| | 820 | av <- fromVal val |
| | 821 | return $ f (av :: VArray) |
| | 822 | |
| | 823 | readIVar :: IVar v -> Eval v |
| | 824 | readIVar (IScalar x) = scalar_fetch x |
| | 825 | readIVar (IPair x) = pair_fetch x |
| | 826 | readIVar (IArray x) = array_fetch x |
| | 827 | readIVar (IHash x) = hash_fetch x |
| | 828 | readIVar _ = fail "readIVar" |
| | 829 | |
| | 830 | cloneIVar :: IVar v -> STM (IVar v) |
| | 831 | cloneIVar (IScalar x) = fmap IScalar $ scalar_clone x |
| | 832 | cloneIVar (IArray x) = fmap IArray $ array_clone x |
| | 833 | cloneIVar (IHash x) = fmap IHash $ hash_clone x |
| | 834 | cloneIVar (ICode x) = fmap ICode $ code_clone x |
| | 835 | cloneIVar x = return x |
| | 836 | |
| | 837 | writeIVar :: IVar v -> v -> Eval () |
| | 838 | writeIVar (IScalar x) = scalar_store x |
| | 839 | writeIVar (IArray x) = array_store x |
| | 840 | writeIVar (IHash x) = hash_store x |
| | 841 | writeIVar _ = fail "writeIVar" |
| | 842 | |
| | 843 | refType :: VRef -> Type |
| | 844 | refType (MkRef x) = object_iType x |
| | 845 | |
| | 846 | scalarRef :: ScalarClass a=> a -> VRef |
| | 847 | scalarRef x = MkRef (IScalar x) |
| | 848 | codeRef :: CodeClass a => a -> VRef |
| | 849 | codeRef x = MkRef (ICode x) |
| | 850 | arrayRef :: ArrayClass a => a -> VRef |
| | 851 | arrayRef x = MkRef (IArray x) |
| | 852 | hashRef :: HashClass a => a -> VRef |
| | 853 | hashRef x = MkRef (IHash x) |
| | 854 | thunkRef :: ThunkClass a => a -> VRef |
| | 855 | thunkRef x = MkRef (IThunk x) |
| | 856 | pairRef :: PairClass a => a -> VRef |
| | 857 | pairRef x = MkRef (IPair x) |
| | 858 | |
| | 859 | newScalar :: (MonadSTM m) => VScalar -> m (IVar VScalar) |
| | 860 | newScalar = stm . (fmap IScalar) . newTVar |
| | 861 | |
| | 862 | newArray :: (MonadSTM m) => VArray -> m (IVar VArray) |
| | 863 | newArray vals = stm $ do |
| | 864 | tvs <- mapM newScalar vals |
| | 865 | iv <- newTVar (toP tvs) |
| | 866 | return $ IArray (MkIArray iv) |
| | 867 | |
| | 868 | newHash :: (MonadSTM m) => VHash -> m (IVar VHash) |
| | 869 | newHash hash = do |
| | 870 | --stm $ unsafeIOToSTM $ putStrLn "new hash" |
| | 871 | ihash <- stm . unsafeIOToSTM $ H.fromList H.hashString (map (\(a,b) -> (a, lazyScalar b)) (Map.toList hash)) |
| | 872 | return $ IHash ihash |
| | 873 | |
| | 874 | newHandle :: (MonadSTM m) => VHandle -> m (IVar VHandle) |
| | 875 | newHandle = return . IHandle |
| | 876 | |
| | 877 | proxyScalar :: Eval VScalar -> (VScalar -> Eval ()) -> IVar VScalar |
| | 878 | proxyScalar fetch store = IScalar (fetch, store) |
| | 879 | |
| | 880 | constScalar :: VScalar -> IVar VScalar |
| | 881 | constScalar = IScalar |
| | 882 | |
| | 883 | lazyScalar :: VScalar -> IVar VScalar |
| | 884 | lazyScalar = IScalar . Just |
| | 885 | |
| | 886 | lazyUndef :: IVar VScalar |
| | 887 | lazyUndef = IScalar (Nothing :: IScalarLazy) |
| | 888 | |
| | 889 | constArray :: VArray -> IVar VArray |
| | 890 | constArray = IArray |
| | 891 | |
| | 892 | ------------------------------------------------------------------------ |
| | 893 | anyFromVal :: forall a. Typeable a => Val -> a |
| | 894 | anyFromVal v = case fromTypeable (fromVal v :: Eval PerlSV) of |
| | 895 | Just f -> f :: a |
| | 896 | _ -> error "anyFromVal failed!" |
| | 897 | |
| | 898 | intCast :: Num b => Val -> Eval b |
| | 899 | intCast x = fmap fromIntegral (fromVal x :: Eval VInt) |
| | 900 | |
| | 901 | |
| | 902 | showVal :: Val -> String |
| | 903 | showVal = show |
| | 904 | |
| | 905 | defaultArrayParam :: Param |
| | 906 | defaultHashParam :: Param |
| | 907 | defaultScalarParam :: Param |
| | 908 | |
| | 909 | defaultArrayParam = buildParam "" "*" "@_" (Val VUndef) |
| | 910 | defaultHashParam = buildParam "" "*" "%_" (Val VUndef) |
| | 911 | defaultScalarParam = buildParam "" "?" "$_" (Var $ cast "$OUTER::_") |
| | 912 | |
| | 913 | |
| | 914 | -- Class: Value |
| | 915 | |