Changeset 6229 for src/Pugs/Compile.hs
- Timestamp:
- 08/13/05 21:16:15 (3 years ago)
- svk:copy_cache_prev:
- 8452
- Files:
-
- 1 modified
-
src/Pugs/Compile.hs (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Compile.hs
r6195 r6229 12 12 13 13 module Pugs.Compile ( 14 PIL (..),14 PIL_Stmts(..), PIL_Stmt(..), PIL_Expr(..), PIL_Decl(..), PIL_Literal(..), PIL_LValue(..), 15 15 Compile(..), 16 16 TEnv(..), initTEnv, … … 35 35 -} 36 36 37 #ifndef HADDOCK 38 -- Type-indexed with GADT; it is a bit too baroque -- refactor toward ANF? 39 data (Typeable a) => PIL a where 40 PNil :: PIL [a] 41 PNoop :: PIL Stmt 42 43 PPos :: !Pos -> !Exp -> !(PIL a) -> PIL a 44 PRawName :: !VarName -> PIL Expression -- XXX HACK! 45 46 PVal :: !Val -> PIL Literal 47 PVar :: !VarName -> PIL LValue 48 49 PExp :: !(PIL LValue) -> PIL Expression 50 PLit :: !(PIL Literal) -> PIL Expression 51 PThunk :: !(PIL Expression) -> PIL Expression 52 PCode :: !SubType -> ![TParam] -> !(PIL [Stmt]) -> PIL Expression 53 54 PStmt :: !(PIL Expression) -> PIL Stmt 55 PStmts :: !(PIL Stmt) -> !(PIL [Stmt]) -> PIL [Stmt] 56 57 PApp :: !TCxt -> !(PIL Expression) -> !(Maybe (PIL Expression)) -> ![PIL Expression] -> PIL LValue 58 PAssign :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 59 PBind :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 60 61 -- The New Pad: Occurs at whenever a variable may occur 62 -- PPad :: !Scope -> !VarName -> PIL LValue 63 64 PPad :: !Scope -> ![(VarName, PIL Expression)] -> !(PIL [Stmt]) -> PIL [Stmt] 65 PSub :: !SubName -> !SubType -> ![TParam] -> !(PIL [Stmt]) -> PIL Decl 66 #endif 67 68 instance Typeable1 PIL where 69 typeOf1 _ = typeOf () 37 data PIL_Stmts = PNil 38 | PStmts 39 { pStmt :: !PIL_Stmt 40 , pStmts :: !PIL_Stmts 41 } 42 | PPad 43 { pScope :: !Scope 44 , pSyms :: ![(VarName, PIL_Expr)] 45 , pStmts :: !PIL_Stmts 46 } 47 deriving (Show, Eq, Ord, Typeable) 48 49 data PIL_Stmt = PNoop | PStmt { pExpr :: !PIL_Expr } | PPos 50 { pPos :: !Pos 51 , pExp :: !Exp 52 , pNode :: !PIL_Stmt 53 } 54 deriving (Show, Eq, Ord, Typeable) 55 56 data PIL_Expr 57 = PRawName { pRawName :: !VarName } 58 | PExp { pLV :: !PIL_LValue } 59 | PLit { pLit :: !PIL_Literal } 60 | PThunk { pThunk :: !PIL_Expr } 61 | PCode 62 { pType :: !SubType 63 , pParams :: ![TParam] 64 , pBody :: !PIL_Stmts 65 } 66 deriving (Show, Eq, Ord, Typeable) 67 68 data PIL_Decl = PSub 69 { pSubName :: !SubName 70 , pSubType :: !SubType 71 , pSubParams :: ![TParam] 72 , pSubBody :: !PIL_Stmts 73 } 74 deriving (Show, Eq, Ord, Typeable) 75 76 data PIL_Literal = PVal { pVal :: Val } 77 deriving (Show, Eq, Ord, Typeable) 78 79 data PIL_LValue = PVar { pVarName :: !VarName } 80 | PApp 81 { pCxt :: !TCxt 82 , pFun :: !PIL_Expr 83 , pInv :: !(Maybe PIL_Expr) 84 , pArgs :: ![PIL_Expr] 85 } 86 | PAssign 87 { pLHS :: ![PIL_LValue] 88 , pRHS :: !PIL_Expr 89 } 90 | PBind 91 { pLHS :: ![PIL_LValue] 92 , pRHS :: !PIL_Expr 93 } 94 deriving (Show, Eq, Ord, Typeable) 70 95 71 96 data TParam = MkTParam 72 97 { tpParam :: !Param 73 , tpDefault :: !(Maybe (PIL Expression))98 , tpDefault :: !(Maybe (PIL_Expr)) 74 99 } 75 deriving (Show, Typeable)100 deriving (Show, Eq, Ord, Typeable) 76 101 77 102 data TCxt 78 103 = TCxtVoid | TCxtLValue !Type | TCxtItem !Type | TCxtSlurpy !Type 79 104 | TTailCall !TCxt 80 deriving (Show, Eq, Typeable)105 deriving (Show, Eq, Ord, Typeable) 81 106 82 107 tcVoid, tcLValue :: TCxt … … 89 114 tcSlurpy = TCxtSlurpy anyType 90 115 -} 91 92 instance Show (PIL a) where93 show (PVal x) = "(PVal " ++ show x ++ ")"94 show (PVar x) = "(PVar " ++ show x ++ ")"95 show (PLit x) = "(PLit " ++ show x ++ ")"96 show (PStmts x y) = "(PStmts " ++ show x ++ " " ++ show y ++ ")"97 show PNil = "PNil"98 show PNoop = "PNoop"99 -- We don't show the raw Exp here to ease writing parsers for PIL (Exp100 -- contains things like MkEnv, etc.).101 show (PPos x _ z) = "(PPos " ++ show x ++ " Noop " ++ show z ++ ")"102 show (PApp x y i z) = "(PApp " ++ show x ++ " " ++ show y ++ " " ++ show i ++ " " ++ show z ++ ")"103 show (PExp x) = "(PExp " ++ show x ++ ")"104 show (PStmt x) = "(PStmt " ++ show x ++ ")"105 show (PAssign x y) = "(PAssign " ++ show x ++ " " ++ show y ++ ")"106 show (PBind x y) = "(PBind " ++ show x ++ " " ++ show y ++ ")"107 show (PThunk x) = "(PThunk " ++ show x ++ ")"108 show (PRawName x) = "(PRawName " ++ show x ++ ")"109 show (PPad x y z) = unwords ["(PPad", show x, show y, show z, ")"]110 show (PCode x y z) = unwords ["(PCode", show x, show y, show z, ")"]111 show (PSub x y z w) = unwords ["(PSub", show x, show y, show z, show w, ")"]112 116 113 117 data TEnv = MkTEnv … … 118 122 , tLabel :: !(TVar Int) -- ^ Label name supply 119 123 } 120 deriving (Show, Eq )124 deriving (Show, Eq, Ord, Typeable) 121 125 122 126 type Comp a = Eval a … … 129 133 130 134 -- Compile instances 131 instance Compile (Var, [(TVar Bool, TVar VRef)]) (PIL Decl) where135 instance Compile (Var, [(TVar Bool, TVar VRef)]) (PIL_Decl) where 132 136 compile = compError 133 137 … … 142 146 } 143 147 144 {-| Compiles a 'Pad' to a list of 'PIL Decl's. Currently, only subroutines and148 {-| Compiles a 'Pad' to a list of 'PIL_Decl's. Currently, only subroutines and 145 149 @\@*END@ are compiled. -} 146 instance Compile Pad [PIL Decl] where150 instance Compile Pad [PIL_Decl] where 147 151 compile pad = do 148 152 entries' <- mapM canCompile entries … … 164 168 cvList <- fromVals =<< readRef ref :: Comp [VCode] 165 169 decls <- eachM cvList $ \(i, cv) -> do 166 compile (("&*END_" ++ show i), cv) :: Comp [PIL Decl]170 compile (("&*END_" ++ show i), cv) :: Comp [PIL_Decl] 167 171 compile ("&*END", concat decls) 168 172 canCompile ((_:twigil:_), _) | not (isAlphaNum twigil) = return [] … … 186 190 eachM = forM . ([0..] `zip`) 187 191 188 instance Compile (SubName, [PIL Decl]) [PILDecl] where192 instance Compile (SubName, [PIL_Decl]) [PIL_Decl] where 189 193 compile (name, decls) = do 190 194 let bodyC = [ PStmts . PStmt . PExp $ PApp tcVoid (PExp (PVar sub)) Nothing [] … … 193 197 return (PSub name SubPrim [] (combine bodyC PNil):decls) 194 198 195 instance Compile (SubName, VCode) [PIL Decl] where199 instance Compile (SubName, VCode) [PIL_Decl] where 196 200 compile (name, vsub) | packageOf name /= packageOf (subName vsub) = do 197 201 let storeC = PBind [PVar $ qualify name] (PExp . PVar . qualify $ subName vsub) … … 206 210 return [PSub name (subType vsub) paramsC bodyC] 207 211 208 instance Compile (String, [(TVar Bool, TVar VRef)]) (PIL Expression) where212 instance Compile (String, [(TVar Bool, TVar VRef)]) (PIL_Expr) where 209 213 compile (name, _) = return $ PRawName name 210 214 211 instance Compile Exp (PIL [Stmt]) where212 compile (Pos pos rest) =fmap (PPos pos rest) $ compile rest215 instance Compile Exp (PIL_Stmts) where 216 compile (Pos _ rest) = compile rest -- fmap (PPos pos rest) $ compile rest 213 217 compile (Cxt cxt rest) = enter cxt $ compile rest 214 218 compile (Stmts (Pad SOur _ exp) rest) = do … … 229 233 enter cxt = local (\e -> e{ envContext = cxt }) 230 234 231 compileStmts :: Exp -> Comp (PIL [Stmt])235 compileStmts :: Exp -> Comp (PIL_Stmts) 232 236 compileStmts exp = case exp of 233 237 Stmts this Noop -> do … … 250 254 _ -> compile (Stmts exp Noop) 251 255 252 instance Compile Val (PIL Stmt) where256 instance Compile Val (PIL_Stmt) where 253 257 compile = fmap PStmt . compile . Val 254 258 255 instance Compile Val (PIL Expression) where259 instance Compile Val (PIL_Expr) where 256 260 compile = compile . Val 257 261 258 instance Compile Exp (PIL Stmt) where262 instance Compile Exp (PIL_Stmt) where 259 263 compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 260 264 compile (Cxt cxt rest) = enter cxt $ compile rest … … 299 303 compile exp = fmap PStmt $ compile exp 300 304 301 pBlock :: PIL [Stmt] -> PIL Expression305 pBlock :: PIL_Stmts -> PIL_Expr 302 306 pBlock = PCode SubBlock [] 303 307 … … 330 334 compile x = compError x 331 335 332 instance Compile Exp (PIL LValue) where333 compile (Pos pos rest) =fmap (PPos pos rest) $ compile rest336 instance Compile Exp (PIL_LValue) where 337 compile (Pos _ rest) = compile rest -- fmap (PPos pos rest) $ compile rest 334 338 compile (Cxt cxt rest) = enter cxt $ compile rest 335 339 compile (Var name) = return $ PVar name … … 391 395 compile exp = compError exp 392 396 393 compLoop :: Exp -> Comp (PIL Stmt)397 compLoop :: Exp -> Comp (PIL_Stmt) 394 398 compLoop (Syn name [cond, body]) = do 395 399 cxt <- askTCxt … … 403 407 appropriate function call (@&statement_control:if@ or 404 408 @&statement_control:unless@). -} 405 compConditional :: Exp -> Comp (PIL LValue)409 compConditional :: Exp -> Comp (PIL_LValue) 406 410 compConditional (Syn name exps) = do 407 411 [condC, trueC, falseC] <- compile exps … … 411 415 compConditional exp = compError exp 412 416 413 {-| Compiles various 'Exp's to 'PIL Expression's. -}414 instance Compile Exp (PIL Expression) where415 compile (Pos pos rest) =fmap (PPos pos rest) $ compile rest417 {-| Compiles various 'Exp's to 'PIL_Expr's. -} 418 instance Compile Exp (PIL_Expr) where 419 compile (Pos _ rest) = compile rest -- fmap (PPos pos rest) $ compile rest 416 420 compile (Cxt cxt rest) = enter cxt $ compile rest 417 421 compile (Var name) = return . PExp $ PVar name … … 442 446 ++ (show $ typeOf (undefined :: b)) 443 447 444 {-| Compiles a 'Val' to a 'PIL Literal'. -}445 instance Compile Val (PIL Literal) where448 {-| Compiles a 'Val' to a 'PIL_Literal'. -} 449 instance Compile Val (PIL_Literal) where 446 450 compile val = return $ PVal val 447 451
