Changeset 4869
- Timestamp:
- 06/20/05 13:45:16 (4 years ago)
- svk:copy_cache_prev:
- 6641
- Files:
-
- 1 modified
-
src/Pugs/Compile/PIR.hs (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Compile/PIR.hs
r4861 r4869 16 16 17 17 #ifndef HADDOCK 18 data P ASTa where19 PNil :: P AST[a]20 PNoop :: P ASTStmt21 22 PRaw :: !Exp -> P ASTStmt -- XXX HACK!23 PRawName :: !VarName -> P ASTExpression -- XXX HACK!24 25 PExp :: !(P AST LValue) -> PASTExpression26 PLit :: !(P AST Literal) -> PASTExpression27 PPos :: !Pos -> !Exp -> P AST a -> PASTa28 PStmt :: !(P AST Expression) -> PASTStmt29 PThunk :: !(P AST Expression) -> PASTExpression30 PBlock :: !(P AST [Stmt]) -> PASTExpression31 32 PVal :: !Val -> P ASTLiteral33 PVar :: !VarName -> P ASTLValue34 35 PStmts :: !(P AST Stmt) -> PAST [Stmt] -> PAST[Stmt]36 PApp :: !TCxt -> !(P AST Expression) -> ![PAST Expression] -> PASTLValue37 PAssign :: ![P AST LValue] -> !(PAST Expression) -> PASTLValue38 PBind :: ![P AST LValue] -> !(PAST Expression) -> PASTLValue39 PPad :: ![(VarName, P AST Expression)] -> !(PAST [Stmt]) -> PAST[Stmt]40 41 PSub :: !SubName -> ![TParam] -> !(P AST [Stmt]) -> PASTDecl18 data PIL a where 19 PNil :: PIL [a] 20 PNoop :: PIL Stmt 21 22 PRaw :: !Exp -> PIL Stmt -- XXX HACK! 23 PRawName :: !VarName -> PIL Expression -- XXX HACK! 24 25 PExp :: !(PIL LValue) -> PIL Expression 26 PLit :: !(PIL Literal) -> PIL Expression 27 PPos :: !Pos -> !Exp -> PIL a -> PIL a 28 PStmt :: !(PIL Expression) -> PIL Stmt 29 PThunk :: !(PIL Expression) -> PIL Expression 30 PBlock :: !(PIL [Stmt]) -> PIL Expression 31 32 PVal :: !Val -> PIL Literal 33 PVar :: !VarName -> PIL LValue 34 35 PStmts :: !(PIL Stmt) -> PIL [Stmt] -> PIL [Stmt] 36 PApp :: !TCxt -> !(PIL Expression) -> ![PIL Expression] -> PIL LValue 37 PAssign :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 38 PBind :: ![PIL LValue] -> !(PIL Expression) -> PIL LValue 39 PPad :: ![(VarName, PIL Expression)] -> !(PIL [Stmt]) -> PIL [Stmt] 40 41 PSub :: !SubName -> ![TParam] -> !(PIL [Stmt]) -> PIL Decl 42 42 #endif 43 43 44 44 data TParam = MkTParam 45 45 { tpParam :: !Param 46 , tpDefault :: !(Maybe (P ASTExpression))46 , tpDefault :: !(Maybe (PIL Expression)) 47 47 } 48 48 deriving (Show, Typeable) … … 63 63 -} 64 64 65 instance Show (P ASTa) where65 instance Show (PIL a) where 66 66 show (PVal x) = "(PVal " ++ show x ++ ")" 67 67 show (PVar x) = "(PVar " ++ show x ++ ")" … … 98 98 type TransMonad = WriterT [Stmt] (ReaderT TEnv IO) 99 99 100 {-| Currently only 'Exp' → 'P AST' -}100 {-| Currently only 'Exp' → 'PIL' -} 101 101 class (Show a, Typeable b) => Compile a b where 102 102 compile :: a -> Comp b 103 103 compile x = fail ("Unrecognized construct: " ++ show x) 104 104 105 {-| Currently only 'P AST' → 'PIR' -}105 {-| Currently only 'PIL' → 'PIR' -} 106 106 class (Show a, Typeable b) => Translate a b | a -> b where 107 107 trans :: a -> Trans b 108 108 trans _ = fail "Untranslatable construct!" 109 109 110 instance Compile (Var, [(TVar Bool, TVar VRef)]) (P ASTDecl) where110 instance Compile (Var, [(TVar Bool, TVar VRef)]) (PIL Decl) where 111 111 compile = compError 112 112 … … 121 121 } 122 122 123 instance Compile Pad (PAST PIR) where 124 compile = compError 125 126 {-| Compiles a 'Pad' to a list of 'PAST Decl's. Currently, only subroutines and 123 {-| Compiles a 'Pad' to a list of 'PIL Decl's. Currently, only subroutines and 127 124 @\@*END@ are compiled. -} 128 instance Compile Pad [P ASTDecl] where125 instance Compile Pad [PIL Decl] where 129 126 compile pad = do 130 127 entries' <- mapM canCompile entries … … 144 141 cvList <- fromVals =<< readRef ref :: Comp [VCode] 145 142 decls <- forM ([0..] `zip` cvList) $ \(i :: Int, cv) -> do 146 compile (("&*END_" ++ show i), cv) :: Comp [P ASTDecl]143 compile (("&*END_" ++ show i), cv) :: Comp [PIL Decl] 147 144 compile ("&*END", concat decls) 148 145 canCompile _ = return [] … … 151 148 else compile (name, vsub) 152 149 153 instance Compile ([Char], [P AST Decl]) [PASTDecl] where150 instance Compile ([Char], [PIL Decl]) [PIL Decl] where 154 151 compile (name, decls) = do 155 152 let bodyC = [ PStmts . PStmt . PExp $ PApp tcVoid (PExp (PVar sub)) [] … … 158 155 return (PSub name [] (combine bodyC PNil):decls) 159 156 160 instance Compile ([Char], VCode) [P ASTDecl] where157 instance Compile ([Char], VCode) [PIL Decl] where 161 158 compile (name, MkCode{ subBody = Syn "block" [body], subParams = params }) = do 162 159 bodyC <- enter cxtItemAny $ compile body … … 166 163 167 164 {- 168 instance Compile [(TVar Bool, TVar VRef)] (P ASTExpression) where165 instance Compile [(TVar Bool, TVar VRef)] (PIL Expression) where 169 166 compile _ = return (PLit $ PVal undef) 170 167 -} 171 168 172 instance Compile (String, [(TVar Bool, TVar VRef)]) (P ASTExpression) where169 instance Compile (String, [(TVar Bool, TVar VRef)]) (PIL Expression) where 173 170 compile (name, _) = return $ PRawName name 174 171 175 instance Compile Exp (P AST[Stmt]) where172 instance Compile Exp (PIL [Stmt]) where 176 173 compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 177 174 compile (Cxt cxt rest) = enter cxt $ compile rest … … 191 188 enter cxt = local (\e -> e{ tCxt = cxt }) 192 189 193 compileStmts :: Exp -> Comp (P AST[Stmt])190 compileStmts :: Exp -> Comp (PIL [Stmt]) 194 191 compileStmts exp = case exp of 195 192 Stmts this Noop -> do … … 208 205 _ -> compile (Stmts exp Noop) 209 206 210 instance Compile Val (P ASTStmt) where207 instance Compile Val (PIL Stmt) where 211 208 compile = fmap PStmt . compile . Val 212 209 213 instance Compile Exp (P ASTStmt) where210 instance Compile Exp (PIL Stmt) where 214 211 compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 215 212 compile (Cxt cxt rest) = enter cxt $ compile rest … … 257 254 compile x = compError x 258 255 259 instance Compile Exp (P ASTLValue) where256 instance Compile Exp (PIL LValue) where 260 257 compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 261 258 compile (Cxt cxt rest) = enter cxt $ compile rest … … 301 298 compile exp = compError exp 302 299 303 compLoop :: Exp -> Comp (P ASTLValue)300 compLoop :: Exp -> Comp (PIL LValue) 304 301 compLoop (Syn name [cond, body]) = do 305 302 cxt <- askTCxt … … 313 310 appropriate function call (@&statement_control:if@ or 314 311 @&statement_control:unless@). -} 315 compConditional :: Exp -> Comp (P ASTLValue)312 compConditional :: Exp -> Comp (PIL LValue) 316 313 compConditional (Syn name exps) = do 317 314 [condC, trueC, falseC] <- compile exps … … 321 318 compConditional exp = compError exp 322 319 323 {-| Compiles various 'Exp's to 'P ASTExpression's. -}324 instance Compile Exp (P ASTExpression) where320 {-| Compiles various 'Exp's to 'PIL Expression's. -} 321 instance Compile Exp (PIL Expression) where 325 322 compile (Pos pos rest) = fmap (PPos pos rest) $ compile rest 326 323 compile (Cxt cxt rest) = enter cxt $ compile rest … … 348 345 349 346 compError :: forall a b. Compile a b => a -> Comp b 350 compError = die $ "Compile error -- invalid P AST"347 compError = die $ "Compile error -- invalid PIL " 351 348 ++ (drop 12 . show $ typeOf (undefined :: b)) 352 349 … … 355 352 ++ (show $ typeOf (undefined :: b)) 356 353 357 {-| Compiles a 'Val' to a 'P ASTLiteral'. -}358 instance Compile Val (P ASTLiteral) where354 {-| Compiles a 'Val' to a 'PIL Literal'. -} 355 instance Compile Val (PIL Literal) where 359 356 compile val = return $ PVal val 360 357 … … 368 365 hPutStrLn stderr $ "*** " ++ str ++ ":\n " ++ show val 369 366 370 instance Typeable1 P ASTwhere367 instance Typeable1 PIL where 371 368 typeOf1 _ = typeOf () 372 369 373 instance (Typeable a) => Translate (P ASTa) a where370 instance (Typeable a) => Translate (PIL a) a where 374 371 trans PNil = return [] 375 372 trans PNoop = return (StmtComment "") … … 622 619 glob <- askGlobal 623 620 main <- asks envBody 624 globP AST<- compile glob625 mainP AST<- compile main626 globPIR <- runTransGlob tenv globP AST:: Eval [Decl]627 mainPIR <- runTransMain tenv mainP AST:: Eval [Stmt]621 globPIL <- compile glob 622 mainPIL <- compile main 623 globPIR <- runTransGlob tenv globPIL :: Eval [Decl] 624 mainPIR <- runTransMain tenv mainPIL :: Eval [Stmt] 628 625 return . VStr . unlines $ 629 626 [ "#!/usr/bin/env parrot" … … 656 653 ,evalError =EvalErrorFatal} 657 654 658 runTransGlob :: TEnv -> [P ASTDecl] -> Eval [Decl]655 runTransGlob :: TEnv -> [PIL Decl] -> Eval [Decl] 659 656 runTransGlob tenv = mapM $ fmap fst . liftIO . (`runReaderT` tenv) . runWriterT . trans 660 657 661 runTransMain :: TEnv -> P AST[Stmt] -> Eval [Stmt]658 runTransMain :: TEnv -> PIL [Stmt] -> Eval [Stmt] 662 659 runTransMain tenv = fmap snd . liftIO . (`runReaderT` tenv) . runWriterT . trans 663 660
