Changeset 7035

Show
Ignore:
Timestamp:
09/17/05 22:55:58 (3 years ago)
Author:
gaal
Message:

Lexical pragmas (WIP). See Pugs.AST.Prag. Dominus++.
This doesn't work yet, my pragmas got lost somewhere between the parser
and the evaluator.
TODO after it's fixed:
- throw away the cons list in favor of a primitive [] - it's enough
- figure out how to install other kinds of pragma data, not just Int

And, the fun part:
- use fatal
- use sort 'mergesort'
- no strict :-)
- other ideas from http://perl.plover.com/TPF/Pragmas/PROPOSAL

Files:
4 added
6 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/AST.hs

    r6594 r7035  
    2121 
    2222    module Pugs.AST.Internals, 
     23    module Pugs.AST.Prag, 
    2324    module Pugs.AST.Pos, 
    2425    module Pugs.AST.Scope, 
     
    3132 
    3233import Pugs.AST.Internals 
     34import Pugs.AST.Prag 
    3335import Pugs.AST.Pos 
    3436import Pugs.AST.Scope 
  • src/Pugs/AST/Internals.hs

    r6934 r7035  
    99    Val(..),   -- uses V.* (which ones?) 
    1010    Value(..), -- uses Val, Eval 
     11    InitDat(..), 
    1112 
    1213    EvalT(..), ContT(..), 
     
    7980 
    8081import Pugs.Parser.Number 
     82import Pugs.AST.Prag 
    8183import Pugs.AST.Pos 
    8284import Pugs.AST.Scope 
     
    862864    | Cxt !Cxt !Exp                     -- ^ Context 
    863865    | Pos !Pos !Exp                     -- ^ Position 
     866    | Prag !Pragmas !Exp                -- ^ Lexical pragmas 
    864867    | Pad !Scope !Pad !Exp              -- ^ Lexical pad 
    865868    | Sym !Scope !Var !Exp              -- ^ Symbol declaration 
     
    904907    unwrap (Cxt _ exp)      = unwrap exp 
    905908    unwrap (Pos _ exp)      = unwrap exp 
     909    unwrap (Prag _ exp)     = unwrap exp 
    906910    unwrap (Pad _ _ exp)    = unwrap exp 
    907911    unwrap (Sym _ _ exp)    = unwrap exp 
     
    954958    | otherwise 
    955959    = (Var name, vs) 
     960extract (Prag prag ex) vs = ((Prag prag ex'), vs') 
     961    where 
     962    (ex', vs') = extract ex vs 
    956963extract (Pos pos ex) vs = ((Pos pos ex'), vs') 
    957964    where 
     
    10431050    , envDebug   :: !DebugInfo           -- ^ Debug info map 
    10441051    , envPos     :: !Pos                 -- ^ Source position range 
     1052    , envPragmas :: !Pragmas             -- ^ List of pragmas in effect 
     1053    , envInitDat :: !(TVar InitDat)      -- ^ BEGIN result information 
     1054    } deriving (Show, Eq, Ord, Typeable) 
     1055 
     1056{-| 
     1057Module initialization information. 
     1058 
     1059When a module is loaded and initialized (i.e., its &import routine is 
     1060called), it may need to communicate information back to the parser.  
     1061This information is held in a TVar to which the parser has access. 
     1062Currently we use this for keeping track of lexical pragma change 
     1063requests, but the possiblyExit mechanism may be refactored to use 
     1064this as well. 
     1065-} 
     1066data InitDat = MkInitDat 
     1067    { initPragmas :: [Pragma]            -- ^ Pragma values being installed 
    10451068    } deriving (Show, Eq, Ord, Typeable) 
    10461069 
  • src/Pugs/Eval.hs

    r6918 r7035  
    6161    syms <- initSyms 
    6262    glob <- newTVar (combine (pad ++ syms) $ mkPad []) 
     63    init <- newTVar $ MkInitDat { initPragmas=[] } 
    6364    return $ MkEnv 
    6465        { envContext = CxtVoid 
     
    7778        , envDebug   = Just ref -- Set to "Nothing" to disable debugging 
    7879        , envPos     = MkPos name 1 1 1 1 
     80        , envPragmas = PrNil 
     81        , envInitDat = init 
    7982        } 
    8083 
     
    229232reduce (Stmts this rest) = reduceStmts this rest 
    230233 
     234reduce (Prag prag exp) = reducePrag prag exp 
     235 
    231236reduce (Pos pos exp) = reducePos pos exp 
    232237 
     
    283288            return . VControl $ ControlEnv env 
    284289        _ -> reduce rest 
     290 
     291reducePrag :: Pragmas -> Exp -> Eval Val 
     292reducePrag prag exp = do 
     293    local (\e -> e{ envPragmas = prag }) $ do 
     294        evalExp exp 
    285295 
    286296{-| 
  • src/Pugs/Monads.hs

    r6672 r7035  
    7171 
    7272{-| 
    73 Evaluate the specified wxpression in the specified (Perl 6) context ('Cxt'). 
     73Evaluate the specified expression in the specified (Perl 6) context ('Cxt'). 
    7474 
    7575(Subsequent chained 'Eval's do /not/ see this new scope.) 
  • src/Pugs/Parser.hs

    r7033 r7035  
    898898        "BEGIN" -> do 
    899899            -- We've to exit if the user has written code like BEGIN { exit }. 
    900             possiblyExit =<< unsafeEvalExp (checkForIOLeak fun) 
     900            val <- possiblyExit =<< unsafeEvalExp (checkForIOLeak fun) 
     901            -- And install any pragmas they've requested. 
     902            env <- getRuleEnv 
     903            let idat = unsafePerformIO $ liftSTM $ readTVar $ envInitDat env 
     904            install $ initPragmas idat 
     905            return val 
    901906        "CHECK" -> vcode2checkBlock code 
    902907        "INIT"  -> vcode2initBlock code 
    903908        "FIRST" -> vcode2firstBlock code 
    904909        _       -> fail "" 
     910    where 
     911        install [] = return () 
     912        install (x:xs) = do 
     913            env' <- getRuleEnv 
     914            let env'' = envCaller env'  -- not sure about this. 
     915            case env'' of 
     916                Just target -> do 
     917                    putRuleEnv target { envPragmas = 
     918                        PrPrags{ pragma  = x 
     919                               , pragmas = envPragmas target} } 
     920                    install xs 
     921                _ -> fail "no caller env to install pragma in" 
    905922 
    906923{-| Wraps a call to @&Pugs::Internals::check_for_io_leak@ around the input 
  • src/Pugs/Prim.hs

    r6918 r7035  
    598598        VRule MkRulePCRE{rxAdverbs=hash} -> return hash 
    599599        _ -> fail $ "Not a rule: " ++ show v 
     600op1 "Pugs::Internals::current_pragma_value" = \v -> do 
     601    name <- fromVal v 
     602    prags <- asks envPragmas 
     603    return $ findPrag name prags 
     604    where 
     605        findPrag :: String -> Pragmas -> Val 
     606        findPrag _ PrNil = VUndef 
     607        findPrag n PrPrags {pragma=this, pragmas=rest} 
     608            | n == pragName this = VInt $ toInteger $ pragDat this 
     609            | otherwise          = findPrag n rest 
     610op1 "Pugs::Internals::caller_pragma_value" = \v -> do 
     611    caller <- asks envCaller 
     612    case caller of 
     613        Just env -> local (const env) (op1 "Pugs::Internals::current_pragma_value" v) 
     614        _        -> return $ VUndef 
    600615op1 other   = \_ -> fail ("Unimplemented unaryOp: " ++ other) 
    601616 
     
    927942op2 "IO::print" = op2Print hPutStr 
    928943op2 "BUILDALL" = op1WalkAll reverse "BUILD" 
     944op2 "Pugs::Internals::install_pragma_value" = \x y -> do 
     945    name <- fromVal x 
     946    val  <- fromVal y 
     947    idat <- asks envInitDat 
     948    idatval <- liftSTM $ readTVar idat 
     949    trace ("installing " ++ name ++ "/" ++ (show val)) $ return () 
     950    let prag = initPragmas idatval 
     951    liftSTM $ writeTVar idat idatval{initPragmas =  
     952        prag ++ [MkPrag{ pragName=name, pragDat=val }]} 
     953    return (VBool True) 
    929954op2 other = \_ _ -> fail ("Unimplemented binaryOp: " ++ other) 
    930955 
     
    16781703\\n   Str       pre     Pugs::Internals::rule_pattern safe (Pugs::Internals::VRule)\ 
    16791704\\n   Hash      pre     Pugs::Internals::rule_adverbs safe (Pugs::Internals::VRule)\ 
     1705\\n   Int       pre     Pugs::Internals::install_pragma_value safe (Str, Int)\ 
     1706\\n   Bool      pre     Pugs::Internals::current_pragma_value safe (Str)\ 
     1707\\n   Bool      pre     Pugs::Internals::caller_pragma_value safe (Str)\ 
    16801708\\n"