forked from keisen/minilisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.lisp
132 lines (108 loc) · 2.68 KB
/
cond.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
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
(defun not (x)
(if x () t))
(defun and (lhs rhs)
(if lhs (if rhs t)))
(defun or (lhs rhs)
(if lhs t (if rhs t)))
(defun lt (lhs rhs)
(prim-lt lhs rhs))
(defun gt (lhs rhs)
(lt rhs lhs))
(defun eq (lhs rhs)
(and (not (lt lhs rhs))
(not (gt lhs rhs))))
(defun ne (lhs rhs)
(not (eq lhs rhs)))
(defun le (lhs rhs)
(or (lt lhs rhs)
(eq lhs rhs)))
(defun ge (lhs rhs)
(or (gt lhs rhs)
(eq lhs rhs)))
(defun length (lst)
(if lst
(+ 1 (length (cdr lst)))
0))
(defun nthcdr (n lst)
(if (eq n 0)
lst
(nthcdr (+ n (negate 1)) (cdr lst))))
(defun nth (n lst)
(car (nthcdr n lst)))
(defun last (lst)
(nth (+ (length lst) (negate 1)) lst))
(defmacro progn (lst)
(if (eq 1 (length lst))
(car lst)
(list 'last
(list 'list
(car lst)
(list 'progn (cdr lst))))))
(println 'progn-macro-test-1)
(progn ((println 'hello)
(println 'world)))
(println 'progn-macro-test-2)
(println (progn ((println 'hello)
(println 'world)
(+ 1 1))))
(defmacro when (x lst)
(list 'if x (list 'progn lst)))
(println 'when-macro-test-1)
(when (eq 1 1)
((println 'hello)
(println 'world)))
(println 'when-macro-test-2)
(println (when (eq 1 1)
((println 'hello)
(println 'world)
(eq 1 1))))
(defun cadr (lst)
(car (cdr lst)))
(defun cddr (lst)
(cdr (cdr lst)))
(defmacro cond (lst)
(list 'if
(list 'and (car lst) (ge (length lst) 2))
(list 'progn (cadr lst))
(list 'if
(gt (length lst) 2)
(list 'cond (cddr lst)))))
(println 'cond-macro-test-1)
(cond ((eq 1 0)
((println 'case-1)
(println 'hello)
(println 'world))
(and (eq 2 (+ 1 1)) (gt (+ 1 1) 0))
((println 'case-2)
(println 'hello)
(println 'world))
(eq 1 1)
((println 'case-3)
(println 'hello)
(println 'world))
t
((println 'default)
(println 'hello)
(println 'world))))
(println 'cond-macro-test-2)
(println (cond ((eq 1 0)
((println 'case-1)
(println 'hello)
(println 'world)
1)
(and (eq 2 (+ 1 1)) (gt (+ 1 1) 0))
((println 'case-2)
(println 'hello)
(println 'world)
(+ 1 2))
(eq 1 1)
((println 'case-3)
(println 'hello)
(println 'world)
(+ 1 3))
t
((println 'default)
(println 'hello)
(println 'world)
(+ 1 4)))))
(exit)