-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPrograms.hs
More file actions
208 lines (179 loc) · 4.01 KB
/
Programs.hs
File metadata and controls
208 lines (179 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{-# language OverloadedStrings #-}
{-# language FlexibleContexts #-}
module Programs where
import Control.Lens.Getter ((^.))
import Control.Lens.Iso (from)
import Data.Function ((&))
import Data.List.NonEmpty (NonEmpty(..))
import Language.Python.DSL
import Language.Python.Syntax
-- |
-- @
-- def append_to(element, to=[]):
-- to.append(element)
-- return to
-- @
--
-- Written without the DSL (not recommended!)
append_to :: Raw Statement
append_to =
CompoundStatement $
Fundef (Ann ()) [] (Indents [] (Ann ()))
Nothing
(Space :| [])
"append_to"
[]
( CommaSepMany (PositionalParam (Ann ()) "element" Nothing) (MkComma [Space]) $
CommaSepOne (KeywordParam (Ann ()) "to" Nothing [] (List (Ann ()) [] Nothing []))
)
[]
Nothing
(SuiteMany (Ann ()) (MkColon []) Nothing LF $
Block []
( SmallStatement
(Indents [replicate 4 Space ^. from indentWhitespaces] (Ann ()))
(MkSmallStatement
(Expr (Ann ()) $
Call (Ann ())
(Deref (Ann ()) (Ident (Ann ()) "to") [] "append")
[]
(Just $ CommaSepOne1' (PositionalArg (Ann ()) (Ident (Ann ()) "element")) Nothing)
[])
[]
Nothing
Nothing
(Just LF))
)
[ Right $
SmallStatement
(Indents [replicate 4 Space ^. from indentWhitespaces] (Ann ()))
(MkSmallStatement
(Return (Ann ()) [Space] (Just $ Ident (Ann ()) "to"))
[]
Nothing
Nothing
(Just LF))
])
-- |
-- @
-- def append_to(element, to=[]):
-- to.append(element)
-- return to
-- @
--
-- Written with the DSL
append_to' :: Raw Fundef
append_to' =
def_ "append_to" [ p_ "element", k_ "to" (list_ []) ]
[ line_ $ call_ ("to" /> "append") [ "element" ]
, line_ $ return_ "to"
]
-- |
-- @
-- def fact(n)
-- def go(n, acc)
-- if n == 0:
-- return acc
-- else:
-- go(n-1, n*acc)
-- return go(n, 1)
-- @
fact_tr :: Raw Fundef
fact_tr =
def_ "fact" [p_ "n"]
[ line_ $
def_ "go" [p_ "n", p_ "acc"]
[ line_ $
if_ ("n" .== 0)
[line_ $ return_ (var_ "acc")] &
else_
[line_ . return_ $ call_ "go" [p_ $ "n" .- 1, p_ $ "n" .* "acc"]]
]
, line_ . return_ $ call_ "go" [p_ "n", p_ 1]
]
-- |
-- @
-- def spin():
-- spin()
-- @
spin :: Raw Fundef
spin = def_ "spin" [] [line_ $ call_ "spin" []]
-- |
-- @
-- def yes()
-- print("yes")
-- yes()
-- @
yes :: Raw Fundef
yes =
def_ "yes" []
[ line_ $ call_ "print" [p_ $ str_ "yes"]
, line_ $ call_ "yes" []
]
counter :: Raw ClassDef
counter =
class_ "Counter" []
[ line_ $
def_ "__init__" ["self"]
[line_ ("self" /> "x" .= 0)]
, blank_
, line_ $
def_ "incr" ["self"]
[line_ ("self" /> "x" .+= 1)]
, blank_
, line_ $
def_ "reset" ["self"]
[line_ ("self" /> "x" .= 0)]
, blank_
, line_ $
def_ "get" ["self"]
[line_ $ return_ ("self" /> "x")]
]
exceptions :: Raw Fundef
exceptions =
def_ "exceptions" []
[ line_ $
tryE_ [line_ pass_] &
except_ [line_ pass_]
, blank_
, line_ $
tryE_ [line_ pass_] &
exceptAs_ (var_ "a" `as_` id_ "b") [line_ pass_]
, blank_
, line_ $
tryE_ [line_ pass_] &
exceptAs_ (var_ "a" `as_` id_ "b") [line_ pass_] &
finally_ [line_ pass_]
, blank_
, line_ $
tryE_ [line_ pass_] &
exceptAs_ (var_ "a" `as_` id_ "b") [line_ pass_] &
else_ [line_ pass_] &
finally_ [line_ pass_]
, blank_
, line_ $ tryF_ [line_ pass_] [line_ pass_]
, blank_
, line_ $ tryF_ [line_ pass_] & finally_ [line_ pass_]
, blank_
, line_ $
tryF_ [line_ pass_] [line_ pass_] &
exceptAs_ (var_ "a" `as_` id_ "b") [line_ pass_] &
else_ [line_ pass_]
]
everything :: Raw Module
everything =
module_
[ line_ append_to
, blank_
, line_ append_to'
, blank_
, line_ fact_tr
, blank_
, line_ spin
, blank_
, line_ yes
, blank_
, line_ counter
, blank_
, line_ exceptions
]