-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.lisp
71 lines (57 loc) · 1.78 KB
/
helpers.lisp
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
(in-package :vanilla-lc)
(defpattern lower-case (c)
(let ((it (gensym)))
`(guard1 (,it :type character)
(lower-case-p ,it)
,it ,c)))
(defpattern upper-case (c)
(let ((it (gensym)))
`(guard1 (,it :type character)
(upper-case-p ,it)
,it ,c)))
(defpattern whitespace (c)
(let ((it (gensym)))
`(guard1 (,it :type character)
(member ,it '(#\Newline #\Tab #\Return #\ ))
,it ,c)))
(defpattern ^ (var)
(let ((it (gensym)))
`(guard1 ,it (equalp ,it ,var))))
(defun mklambda (bind term)
(list :lambda bind term))
(defpattern lambda (bind term)
`(list* :lambda ,bind ,term _))
(defstruct (type-lambda1
(:constructor mklambda1 (t1 &optional dirty depth)))
t1
(dirty t :type boolean)
(depth most-positive-fixnum :type fixnum))
(defpattern lambda1 (t1 &optional dirty depth)
`(type-lambda1 :t1 ,t1
,@(if dirty (list :dirty dirty))
,@(if depth (list :depth depth))))
(defstruct (type-app
(:constructor mkapp (t1 t2 &optional dirty depth)))
t1 t2
(dirty t :type boolean)
(depth most-positive-fixnum :type fixnum))
(defpattern app (t1 t2 &optional dirty depth)
`(type-app :t1 ,t1 :t2 ,t2
,@(if dirty (list :dirty dirty))
,@(if depth (list :depth depth))))
(defun string-chars (str)
(loop for c across str collecting c))
(defun take-word (chars &optional acc)
(labels ((rconcat (chars)
(concatenate 'string (nreverse chars))))
(cond
((null chars)
(values (rconcat acc) nil))
((alphanumericp (car chars))
(take-word (cdr chars)
(cons (car chars) acc)))
(t (values (rconcat acc) chars)))))
(defun file-text (filename)
(iterate (for c in-file filename using 'read-char)
(collect c into chars)
(finally (return-from file-text (concatenate 'string chars)))))