-
Notifications
You must be signed in to change notification settings - Fork 2
/
pantagruel.janet
352 lines (278 loc) · 10.4 KB
/
pantagruel.janet
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
(use spork/argparse)
(import spork/path)
(import /pantagruel/lexer)
(import /pantagruel/parser)
(import /pantagruel/eval/engine)
(import /pantagruel/types/type-checking)
(import /pantagruel/print-src)
(def version "0.9.1")
(def default-path "pantagruel")
(def params
[```
A program specification notation.
Usage:
pant <file1 file2 ...> | evaluate Pantagruel document files
pant | read Pantagruel from stdin
```
"version" {:kind :flag
:short "v"
:help "Show version and exit"}
"path" {:kind :option
:short "p"
:help "The module path"}
"config" {:kind :option
:short "c"
:help "The config file to consult, in JDN format"
:default ".pantagruel.jdn"}
:default {:kind :accumulate}])
(defn handle-syntax-error
[err file src]
(def start-line (print-src/line-starter file src))
(defn- in-bounds
[n mx]
(if (< n 0)
(max n (- mx))
(min n mx)))
(let [form (or (err :form)
{:span [(dec (length src)) (length src)]
:text ""})
from (if-let [from (get-in form [:span 0])]
(- from 10)
-20)
to (if-let [to (get-in form [:span 1])]
(+ to 10)
-1)
prefix (if (or (= from (- (length src)))
(= from 0))
""
"…")
suffix (if (or (= to (length src))
(= to -1))
""
"…")]
(if (os/getenv "PANT_DEBUG") (print (dyn :yydebug)))
(start-line form)
(eprintf
```
syntax error: `%s` (%q)
in
%s%s%s
```
(string (form :text))
(form :kind)
prefix
(string/slice src (in-bounds from (length src)) (in-bounds to (length src)))
suffix))
(when (dyn :exit-on-error)
(os/exit 1)))
(defn- print-types
[str & args]
(defn- render-type
[t]
(defn- join
[ts]
(string/format "(%s)" (-> (map render-type ts) (string/join ", "))))
(defn- render-procedure
[args yields]
(string/format "%s => %s" (render-type args) (render-type yields)))
(match t
(ts (indexed? ts)) (join ts)
{:literal literal} (string literal)
{:name t-name} t-name
# Special case the empty set.
{:set-of ()} "{}"
{:set-of t} (string/format "{%s}" (render-type t))
{:list-of t} (string/format "[%s]" (render-type t))
{:tuple-of ts} (join ts)
{:kind :sum :inner ts} (-> (map render-type ts) (string/join " + "))
{:decl-name name :args args :yields yields} (string/format
"%s %s"
name
(render-procedure args yields))
{:args args :yields yields} (render-procedure args yields)
{:thunk thunk} (render-type thunk)
{:kind :sym :text text} text
t (string/format "%q" t)))
(eprintf (string "type error. " str) ;(map render-type args)))
(defn handle-evaluation-error
[err]
(let [{:evaluator {:src src :file file}} err]
(def start-line (print-src/line-starter file src))
(case (err :err)
:evaluation
(each sym (keys (err :symbols))
(start-line sym)
(eprintf "unglossed symbol error: %s"
(sym :text)))
:single-binding
(do
(start-line (err :sym))
(print-types
"can't bind %s to `%s`, already bound to `%s`"
(get-in err [:sym :text])
(get-in err [:t :type])
(get-in err [:already :type])))
:import
(do
(start-line (err :to-import))
(eprintf "import error: module `%s` not found. Available modules: %s"
(get-in err [:to-import :text])
(-> (err :available-modules) (keys) (string/join ", "))))
:import-cycle
(do
(start-line (err :to-import))
(eprintf "import cycle error: encountered cycle: %s"
(string/join (err :currently-importing-modules) " -> ")))
(errorf "Got unknown evaluation error: %q" err)))
(when (dyn :exit-on-error) (os/exit 1)))
(defn handle-version
[]
(print (string "Pantagruel " version)))
(defn lex-and-parse
[file src]
(let [lexed (lexer/lex src)
tree (try
(parser/parse-tokens lexed)
([err fib]
(when (struct? err) (handle-syntax-error err file src))
(propagate err fib)))]
tree))
(defn- handle-resolution-error
[err]
(case (err :type)
:list-application-multiple-args
(print-types "attempted to apply to multiple arguments: `%s`"
(err :xs))
:list-application-bad-arg
(print-types "attempted to apply type: %s to an argument of type: %s"
(err :f)
(err :x))
:application
(print-types "attempted to apply type: %s to type: %s"
(err :f)
(err :x))
:container
(print-types "attempted to check for membership or cardinality in non-container type: %s"
(err :t))
:arg-length
(print-types "received invalid arguments: %s to procedure %s"
(err :args)
(err :f))
:gcd-app
(print-types "couldn't bind value of type %s to argument of type %s in procedure: %s" (err :right) (err :left) (get-in err [:extra :f]))
:gcd-comp
(print-types "couldn't unify types for comparison: %s and %s" (err :left) (err :right))
:gcd-arith
(print-types "couldn't unify types for arithmetic: %s and %s" (err :left) (err :right))
:gcd-in
(print-types "couldn't unify set element type %s when checking membership of %s" (err :left) (err :right))
:gcd-case-test
(print-types "couldn't unify test expression of `case` %s with branch %s" (err :left) (err :right))
:gcd-case-branches
(print-types "couldn't unify branch expressions of `case` %s and %s" (err :left) (err :right))
:gcd-update-procedure-args
(print-types "couldn't unify procedure argument type of `update` %s with left side of mapping %s" (err :left) (err :right))
:gcd-update-procedure-yields
(print-types "couldn't unify procedure yields type of `update` %s with right side of mapping %s" (err :left) (err :right))
:gcd-set-update
(print-types "couldn't unify set of %s when updating with element type %s" (err :left) (err :right))
:gcd-list-update
(print-types "couldn't unify list of %s when updating with element type %s" (err :left) (err :right))
:gcd-set-extension
(print-types "couldn't unify set of %s when extending with element type %s" (err :left) (err :right))
:gcd-list-extension
(print-types "couldn't unify list of %s when extending with element type %s" (err :left) (err :right))
:gcd
(print-types "couldn't unify types: %s and %s" (err :left) (err :right))
(print-types "unknown type resolution error: %s" err)))
(defn handle-src
```
Given some input file, fully evaluate it as a Pantagruel document.
```
[file src available-modules]
(defn evaluator-callback
```
The logic to slurp and parse a file, encapsulated and passed into the :eval
method. This way, the evaluator logic knows how to call this callback but
it doesn't know how to lex and parse by itself.
```
[file]
(let [src (slurp file)
tree (lex-and-parse file src)]
[tree (engine/Evaluator file src)]))
(array/clear engine/currently-importing-modules)
(let [start-line (print-src/line-starter file src)
tree (lex-and-parse file src)
evaluator (engine/Evaluator file src)
env (try (:eval evaluator tree available-modules evaluator-callback)
([err fib] (when (table? err) (handle-evaluation-error err))
(propagate err fib)))
type-errors (type-checking/get-type-errors tree env file src)]
(each [body-expr type-error] type-errors
(start-line body-expr)
(handle-resolution-error type-error)
(eprintf "\nin expression:\n\n%s\n" (print-src/print-src body-expr src)))
(when (and (not (empty? type-errors))
(dyn :exit-on-error))
(os/exit 1))
type-errors))
(defn- maybe-read
[filename]
(when (os/stat filename) (slurp filename)))
(defn populate-available-modules
```
Read the module path and parse all the Pantagruel files that are there. Build
a map of module names (for any file that declares one) to file paths.
```
[args]
(defn pantagruel-files
[path]
(if (os/stat path)
(->> (os/dir path)
(filter |(= (path/ext $) ".pant"))
(map |(path/join path $)))
@[]))
(def available-modules @{})
(let [config (or (-?> (args "config") (maybe-read) (parse)) {})
path (-> (or (args "path") (config "path") default-path))
cur-path (pantagruel-files ".")
module-path (pantagruel-files path)]
(each file (array ;module-path ;cur-path)
(let [src (slurp file)
start-line (print-src/line-starter file src)
{:directives directives} (lex-and-parse file src)]
(var module-name nil)
(each directive directives
(match directive
{:statement "module" :args {:text directive-name}}
(do
(when module-name
(start-line directive)
(eprintf "module name `%s` already declared; found module declaration `%s`"
module-name
directive-name)
(when (dyn :exit-on-error) (os/exit 1))
(error :available-modules-error))
(set module-name directive-name))))
(when module-name
(put available-modules module-name file)))))
available-modules)
(defn main
```
Main application logic.
Load file(s) or read a document from stdin.
Evaluate it and check; if all checks pass, return 0.
```
[& _args]
(def args (argparse ;params))
(unless args (os/exit 1))
(setdyn :exit-on-error true)
(cond
(args "version") (handle-version)
(let [available-modules (populate-available-modules args)]
(if-let [filenames (args :default)]
(each file filenames
(let [src (slurp file)]
(handle-src file src available-modules)))
(let [src (file/read stdin :all)]
(handle-src "<stdin>" src available-modules))))))