module Futhark.CodeGen.Backends.GenericPython.AST
( PyExp (..),
PyIdx (..),
PyArg (..),
PyStmt (..),
module Language.Futhark.Core,
PyProg (..),
PyExcept (..),
PyFunDef (..),
PyClassDef (..),
)
where
import Futhark.Util.Pretty
import Language.Futhark.Core
data UnOp
=
Not
|
Complement
|
Negate
|
Abs
deriving (UnOp -> UnOp -> Bool
(UnOp -> UnOp -> Bool) -> (UnOp -> UnOp -> Bool) -> Eq UnOp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UnOp -> UnOp -> Bool
$c/= :: UnOp -> UnOp -> Bool
== :: UnOp -> UnOp -> Bool
$c== :: UnOp -> UnOp -> Bool
Eq, Int -> UnOp -> ShowS
[UnOp] -> ShowS
UnOp -> String
(Int -> UnOp -> ShowS)
-> (UnOp -> String) -> ([UnOp] -> ShowS) -> Show UnOp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UnOp] -> ShowS
$cshowList :: [UnOp] -> ShowS
show :: UnOp -> String
$cshow :: UnOp -> String
showsPrec :: Int -> UnOp -> ShowS
$cshowsPrec :: Int -> UnOp -> ShowS
Show)
data PyExp
= Integer Integer
| Bool Bool
| Float Double
| String String
| RawStringLiteral String
| Var String
| BinOp String PyExp PyExp
| UnOp String PyExp
| Cond PyExp PyExp PyExp
| Index PyExp PyIdx
| Call PyExp [PyArg]
| Cast PyExp String
| Tuple [PyExp]
| List [PyExp]
| Field PyExp String
| Dict [(PyExp, PyExp)]
| Lambda String PyExp
| None
deriving (PyExp -> PyExp -> Bool
(PyExp -> PyExp -> Bool) -> (PyExp -> PyExp -> Bool) -> Eq PyExp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyExp -> PyExp -> Bool
$c/= :: PyExp -> PyExp -> Bool
== :: PyExp -> PyExp -> Bool
$c== :: PyExp -> PyExp -> Bool
Eq, Int -> PyExp -> ShowS
[PyExp] -> ShowS
PyExp -> String
(Int -> PyExp -> ShowS)
-> (PyExp -> String) -> ([PyExp] -> ShowS) -> Show PyExp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyExp] -> ShowS
$cshowList :: [PyExp] -> ShowS
show :: PyExp -> String
$cshow :: PyExp -> String
showsPrec :: Int -> PyExp -> ShowS
$cshowsPrec :: Int -> PyExp -> ShowS
Show)
data PyIdx
= IdxRange PyExp PyExp
| IdxExp PyExp
deriving (PyIdx -> PyIdx -> Bool
(PyIdx -> PyIdx -> Bool) -> (PyIdx -> PyIdx -> Bool) -> Eq PyIdx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyIdx -> PyIdx -> Bool
$c/= :: PyIdx -> PyIdx -> Bool
== :: PyIdx -> PyIdx -> Bool
$c== :: PyIdx -> PyIdx -> Bool
Eq, Int -> PyIdx -> ShowS
[PyIdx] -> ShowS
PyIdx -> String
(Int -> PyIdx -> ShowS)
-> (PyIdx -> String) -> ([PyIdx] -> ShowS) -> Show PyIdx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyIdx] -> ShowS
$cshowList :: [PyIdx] -> ShowS
show :: PyIdx -> String
$cshow :: PyIdx -> String
showsPrec :: Int -> PyIdx -> ShowS
$cshowsPrec :: Int -> PyIdx -> ShowS
Show)
data PyArg
= ArgKeyword String PyExp
| Arg PyExp
deriving (PyArg -> PyArg -> Bool
(PyArg -> PyArg -> Bool) -> (PyArg -> PyArg -> Bool) -> Eq PyArg
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyArg -> PyArg -> Bool
$c/= :: PyArg -> PyArg -> Bool
== :: PyArg -> PyArg -> Bool
$c== :: PyArg -> PyArg -> Bool
Eq, Int -> PyArg -> ShowS
[PyArg] -> ShowS
PyArg -> String
(Int -> PyArg -> ShowS)
-> (PyArg -> String) -> ([PyArg] -> ShowS) -> Show PyArg
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyArg] -> ShowS
$cshowList :: [PyArg] -> ShowS
show :: PyArg -> String
$cshow :: PyArg -> String
showsPrec :: Int -> PyArg -> ShowS
$cshowsPrec :: Int -> PyArg -> ShowS
Show)
data PyStmt
= If PyExp [PyStmt] [PyStmt]
| Try [PyStmt] [PyExcept]
| While PyExp [PyStmt]
| For String PyExp [PyStmt]
| With PyExp [PyStmt]
| Assign PyExp PyExp
| AssignOp String PyExp PyExp
| String [PyStmt]
| Assert PyExp PyExp
| Raise PyExp
| Exp PyExp
| Return PyExp
| Pass
|
Import String (Maybe String)
| FunDef PyFunDef
| ClassDef PyClassDef
|
Escape String
deriving (PyStmt -> PyStmt -> Bool
(PyStmt -> PyStmt -> Bool)
-> (PyStmt -> PyStmt -> Bool) -> Eq PyStmt
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyStmt -> PyStmt -> Bool
$c/= :: PyStmt -> PyStmt -> Bool
== :: PyStmt -> PyStmt -> Bool
$c== :: PyStmt -> PyStmt -> Bool
Eq, Int -> PyStmt -> ShowS
[PyStmt] -> ShowS
PyStmt -> String
(Int -> PyStmt -> ShowS)
-> (PyStmt -> String) -> ([PyStmt] -> ShowS) -> Show PyStmt
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyStmt] -> ShowS
$cshowList :: [PyStmt] -> ShowS
show :: PyStmt -> String
$cshow :: PyStmt -> String
showsPrec :: Int -> PyStmt -> ShowS
$cshowsPrec :: Int -> PyStmt -> ShowS
Show)
data PyExcept = Catch PyExp [PyStmt]
deriving (PyExcept -> PyExcept -> Bool
(PyExcept -> PyExcept -> Bool)
-> (PyExcept -> PyExcept -> Bool) -> Eq PyExcept
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyExcept -> PyExcept -> Bool
$c/= :: PyExcept -> PyExcept -> Bool
== :: PyExcept -> PyExcept -> Bool
$c== :: PyExcept -> PyExcept -> Bool
Eq, Int -> PyExcept -> ShowS
[PyExcept] -> ShowS
PyExcept -> String
(Int -> PyExcept -> ShowS)
-> (PyExcept -> String) -> ([PyExcept] -> ShowS) -> Show PyExcept
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyExcept] -> ShowS
$cshowList :: [PyExcept] -> ShowS
show :: PyExcept -> String
$cshow :: PyExcept -> String
showsPrec :: Int -> PyExcept -> ShowS
$cshowsPrec :: Int -> PyExcept -> ShowS
Show)
data PyFunDef = Def String [String] [PyStmt]
deriving (PyFunDef -> PyFunDef -> Bool
(PyFunDef -> PyFunDef -> Bool)
-> (PyFunDef -> PyFunDef -> Bool) -> Eq PyFunDef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyFunDef -> PyFunDef -> Bool
$c/= :: PyFunDef -> PyFunDef -> Bool
== :: PyFunDef -> PyFunDef -> Bool
$c== :: PyFunDef -> PyFunDef -> Bool
Eq, Int -> PyFunDef -> ShowS
[PyFunDef] -> ShowS
PyFunDef -> String
(Int -> PyFunDef -> ShowS)
-> (PyFunDef -> String) -> ([PyFunDef] -> ShowS) -> Show PyFunDef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyFunDef] -> ShowS
$cshowList :: [PyFunDef] -> ShowS
show :: PyFunDef -> String
$cshow :: PyFunDef -> String
showsPrec :: Int -> PyFunDef -> ShowS
$cshowsPrec :: Int -> PyFunDef -> ShowS
Show)
data PyClassDef = Class String [PyStmt]
deriving (PyClassDef -> PyClassDef -> Bool
(PyClassDef -> PyClassDef -> Bool)
-> (PyClassDef -> PyClassDef -> Bool) -> Eq PyClassDef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyClassDef -> PyClassDef -> Bool
$c/= :: PyClassDef -> PyClassDef -> Bool
== :: PyClassDef -> PyClassDef -> Bool
$c== :: PyClassDef -> PyClassDef -> Bool
Eq, Int -> PyClassDef -> ShowS
[PyClassDef] -> ShowS
PyClassDef -> String
(Int -> PyClassDef -> ShowS)
-> (PyClassDef -> String)
-> ([PyClassDef] -> ShowS)
-> Show PyClassDef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyClassDef] -> ShowS
$cshowList :: [PyClassDef] -> ShowS
show :: PyClassDef -> String
$cshow :: PyClassDef -> String
showsPrec :: Int -> PyClassDef -> ShowS
$cshowsPrec :: Int -> PyClassDef -> ShowS
Show)
newtype PyProg = PyProg [PyStmt]
deriving (PyProg -> PyProg -> Bool
(PyProg -> PyProg -> Bool)
-> (PyProg -> PyProg -> Bool) -> Eq PyProg
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PyProg -> PyProg -> Bool
$c/= :: PyProg -> PyProg -> Bool
== :: PyProg -> PyProg -> Bool
$c== :: PyProg -> PyProg -> Bool
Eq, Int -> PyProg -> ShowS
[PyProg] -> ShowS
PyProg -> String
(Int -> PyProg -> ShowS)
-> (PyProg -> String) -> ([PyProg] -> ShowS) -> Show PyProg
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PyProg] -> ShowS
$cshowList :: [PyProg] -> ShowS
show :: PyProg -> String
$cshow :: PyProg -> String
showsPrec :: Int -> PyProg -> ShowS
$cshowsPrec :: Int -> PyProg -> ShowS
Show)
instance Pretty PyIdx where
ppr :: PyIdx -> Doc
ppr (IdxExp PyExp
e) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e
ppr (IdxRange PyExp
from PyExp
to) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
from Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
to
instance Pretty PyArg where
ppr :: PyArg -> Doc
ppr (ArgKeyword String
k PyExp
e) = String -> Doc
text String
k Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
equals Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e
ppr (Arg PyExp
e) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e
instance Pretty PyExp where
ppr :: PyExp -> Doc
ppr (Integer Integer
x) = Integer -> Doc
forall a. Pretty a => a -> Doc
ppr Integer
x
ppr (Bool Bool
x) = Bool -> Doc
forall a. Pretty a => a -> Doc
ppr Bool
x
ppr (Float Double
x)
| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
x = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ if Double
x Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0 then String
"float('inf')" else String
"float('-inf')"
| Bool
otherwise = Double -> Doc
forall a. Pretty a => a -> Doc
ppr Double
x
ppr (String String
x) = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ ShowS
forall a. Show a => a -> String
show String
x
ppr (RawStringLiteral String
s) = String -> Doc
text String
"\"\"\"" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
s Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
"\"\"\""
ppr (Var String
n) = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map (\Char
x -> if Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\'' then Char
'm' else Char
x) String
n
ppr (Field PyExp
e String
s) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
"." Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
s
ppr (BinOp String
s PyExp
e1 PyExp
e2) = Doc -> Doc
parens (PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e1 Doc -> Doc -> Doc
<+> String -> Doc
text String
s Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e2)
ppr (UnOp String
s PyExp
e) = String -> Doc
text String
s Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc -> Doc
parens (PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e)
ppr (Cond PyExp
e1 PyExp
e2 PyExp
e3) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e2 Doc -> Doc -> Doc
<+> String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e1 Doc -> Doc -> Doc
<+> String -> Doc
text String
"else" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e3
ppr (Cast PyExp
src String
bt) =
String -> Doc
text String
"ct.cast"
Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc -> Doc
parens
( PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
src Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
","
Doc -> Doc -> Doc
<+> String -> Doc
text String
"ct.POINTER" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc -> Doc
parens (String -> Doc
text String
bt)
)
ppr (Index PyExp
src PyIdx
idx) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
src Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc -> Doc
brackets (PyIdx -> Doc
forall a. Pretty a => a -> Doc
ppr PyIdx
idx)
ppr (Call PyExp
fun [PyArg]
exps) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
fun Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc -> Doc
parens ([Doc] -> Doc
commasep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyArg -> Doc) -> [PyArg] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyArg -> Doc
forall a. Pretty a => a -> Doc
ppr [PyArg]
exps)
ppr (Tuple [PyExp
dim]) = Doc -> Doc
parens (PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
dim Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
",")
ppr (Tuple [PyExp]
dims) = Doc -> Doc
parens ([Doc] -> Doc
commasep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyExp -> Doc) -> [PyExp] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr [PyExp]
dims)
ppr (List [PyExp]
es) = Doc -> Doc
brackets (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
commasep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyExp -> Doc) -> [PyExp] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr [PyExp]
es
ppr (Dict [(PyExp, PyExp)]
kvs) = Doc -> Doc
braces (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
commasep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((PyExp, PyExp) -> Doc) -> [(PyExp, PyExp)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (PyExp, PyExp) -> Doc
forall {a} {a}. (Pretty a, Pretty a) => (a, a) -> Doc
ppElem [(PyExp, PyExp)]
kvs
where
ppElem :: (a, a) -> Doc
ppElem (a
k, a
v) = a -> Doc
forall a. Pretty a => a -> Doc
ppr a
k Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
colon Doc -> Doc -> Doc
<+> a -> Doc
forall a. Pretty a => a -> Doc
ppr a
v
ppr (Lambda String
p PyExp
e) = String -> Doc
text String
"lambda" Doc -> Doc -> Doc
<+> String -> Doc
text String
p Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e
ppr PyExp
None = String -> Doc
text String
"None"
instance Pretty PyStmt where
ppr :: PyStmt -> Doc
ppr (If PyExp
cond [] []) =
String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
cond Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 (String -> Doc
text String
"pass")
ppr (If PyExp
cond [] [PyStmt]
fbranch) =
String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
cond Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 (String -> Doc
text String
"pass")
Doc -> Doc -> Doc
</> String -> Doc
text String
"else:"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
fbranch)
ppr (If PyExp
cond [PyStmt]
tbranch []) =
String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
cond Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
tbranch)
ppr (If PyExp
cond [PyStmt]
tbranch [PyStmt]
fbranch) =
String -> Doc
text String
"if" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
cond Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
tbranch)
Doc -> Doc -> Doc
</> String -> Doc
text String
"else:"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
fbranch)
ppr (Try [PyStmt]
pystms [PyExcept]
pyexcepts) =
String -> Doc
text String
"try:"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
pystms)
Doc -> Doc -> Doc
</> [Doc] -> Doc
stack ((PyExcept -> Doc) -> [PyExcept] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyExcept -> Doc
forall a. Pretty a => a -> Doc
ppr [PyExcept]
pyexcepts)
ppr (While PyExp
cond [PyStmt]
body) =
String -> Doc
text String
"while" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
cond Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
body)
ppr (For String
i PyExp
what [PyStmt]
body) =
String -> Doc
text String
"for" Doc -> Doc -> Doc
<+> String -> Doc
forall a. Pretty a => a -> Doc
ppr String
i Doc -> Doc -> Doc
<+> String -> Doc
text String
"in" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
what Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
body)
ppr (With PyExp
what [PyStmt]
body) =
String -> Doc
text String
"with" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
what Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
body)
ppr (Assign PyExp
e1 PyExp
e2) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e1 Doc -> Doc -> Doc
<+> String -> Doc
text String
"=" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e2
ppr (AssignOp String
op PyExp
e1 PyExp
e2) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e1 Doc -> Doc -> Doc
<+> String -> Doc
text (String
op String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"=") Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e2
ppr (Comment String
s [PyStmt]
body) = String -> Doc
text String
"#" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
s Doc -> Doc -> Doc
</> [Doc] -> Doc
stack ((PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
body)
ppr (Assert PyExp
e1 PyExp
e2) = String -> Doc
text String
"assert" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e1 Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
"," Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e2
ppr (Raise PyExp
e) = String -> Doc
text String
"raise" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e
ppr (Exp PyExp
c) = PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
c
ppr (Return PyExp
e) = String -> Doc
text String
"return" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
e
ppr PyStmt
Pass = String -> Doc
text String
"pass"
ppr (Import String
from (Just String
as)) =
String -> Doc
text String
"import" Doc -> Doc -> Doc
<+> String -> Doc
text String
from Doc -> Doc -> Doc
<+> String -> Doc
text String
"as" Doc -> Doc -> Doc
<+> String -> Doc
text String
as
ppr (Import String
from Maybe String
Nothing) =
String -> Doc
text String
"import" Doc -> Doc -> Doc
<+> String -> Doc
text String
from
ppr (FunDef PyFunDef
d) = PyFunDef -> Doc
forall a. Pretty a => a -> Doc
ppr PyFunDef
d
ppr (ClassDef PyClassDef
d) = PyClassDef -> Doc
forall a. Pretty a => a -> Doc
ppr PyClassDef
d
ppr (Escape String
s) = [Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
text ([String] -> [Doc]) -> [String] -> [Doc]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
s
instance Pretty PyFunDef where
ppr :: PyFunDef -> Doc
ppr (Def String
fname [String]
params [PyStmt]
body) =
String -> Doc
text String
"def" Doc -> Doc -> Doc
<+> String -> Doc
text String
fname Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc -> Doc
parens ([Doc] -> Doc
commasep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
forall a. Pretty a => a -> Doc
ppr [String]
params) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ((PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
body))
instance Pretty PyClassDef where
ppr :: PyClassDef -> Doc
ppr (Class String
cname [PyStmt]
body) =
String -> Doc
text String
"class" Doc -> Doc -> Doc
<+> String -> Doc
text String
cname Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
text String
":"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ((PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
body))
instance Pretty PyExcept where
ppr :: PyExcept -> Doc
ppr (Catch PyExp
pyexp [PyStmt]
stms) =
String -> Doc
text String
"except" Doc -> Doc -> Doc
<+> PyExp -> Doc
forall a. Pretty a => a -> Doc
ppr PyExp
pyexp Doc -> Doc -> Doc
<+> String -> Doc
text String
"as e:"
Doc -> Doc -> Doc
</> Int -> Doc -> Doc
indent Int
2 ([Doc] -> Doc
stack ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
stms)
instance Pretty PyProg where
ppr :: PyProg -> Doc
ppr (PyProg [PyStmt]
stms) = [Doc] -> Doc
stack ((PyStmt -> Doc) -> [PyStmt] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map PyStmt -> Doc
forall a. Pretty a => a -> Doc
ppr [PyStmt]
stms)