generated from 8dcc/c-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.lisp
165 lines (142 loc) · 5.31 KB
/
stdlib.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
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
;; Copyright 2024 8dcc
;;
;; This file is part of SL.
;;
;; This program is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation, either version 3 of the License, or any later version.
;;
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;;
;; You should have received a copy of the GNU General Public License along with
;; SL. If not, see <https://www.gnu.org/licenses/>.
;;
;;==============================================================================
;;
;; Standard Lisp library for SL. <https://github.com/8dcc/sl>
;;
;; TODO:
;; - `let*'
;; - `nth'
;; - `assoc'
;; - `find'
;; - `filter'
;; - `reduce'
;; - `member' (return arg0 when car is arg1)
;; - `member?' (is arg1 in arg0?)
;; - `string-split' using regex
;;------------------------------------------------------------------------------
;; Meta-definition macros
;;------------------------------------------------------------------------------
;; (defmacro 1+ (var) > (define 1+
;; (list '+ 1 var)) > (macro (var)
;; > (list '+ 1 var)))
(define defmacro
(macro (name &rest macro-args)
`(define ,name (macro ,@macro-args))))
;; (defun my-function (n) > (define my-function
;; (+ 1 n)) > (lambda (n)
;; > (+ 1 n)))
(defmacro defun (name &rest lambda-args)
`(define ,name (lambda ,@lambda-args)))
;;------------------------------------------------------------------------------
;; List-accessing functions
;;------------------------------------------------------------------------------
(defun caar (lst) (car (car lst)))
(defun cadr (lst) (car (cdr lst)))
(defun cdar (lst) (cdr (car lst)))
(defun cddr (lst) (cdr (cdr lst)))
(defun caaar (lst) (car (car (car lst))))
(defun caadr (lst) (car (car (cdr lst))))
(defun cadar (lst) (car (cdr (car lst))))
(defun caddr (lst) (car (cdr (cdr lst))))
(defun cdaar (lst) (cdr (car (car lst))))
(defun cdadr (lst) (cdr (car (cdr lst))))
(defun cddar (lst) (cdr (cdr (car lst))))
(defun cdddr (lst) (cdr (cdr (cdr lst))))
;;------------------------------------------------------------------------------
;; List-building functions
;;------------------------------------------------------------------------------
(defun cons* (&rest args)
(if (null? (cdr args))
(car args)
(cons (car args)
(apply cons* (cdr args)))))
;;------------------------------------------------------------------------------
;; Conditional macros
;;------------------------------------------------------------------------------
;; TODO: Convert to primitive?
;;
;; (cond (pred1 expr1) > (if pred1 (begin expr1)
;; (pred2 expr2) > (if pred2 (begin expr2)
;; (pred3 expr3a expr3b)) > (if pred3 (begin expr3a expr3b)
;; > nil)))
(defmacro cond (&rest clauses)
(if (null? clauses)
nil
(list 'if (caar clauses)
(cons 'begin (cdar clauses))
(cons 'cond (cdr clauses)))))
;;------------------------------------------------------------------------------
;; Local variables
;;------------------------------------------------------------------------------
;; (let ((s1 e1) > ((lambda (s1 s2 s3)
;; (s2 e2) > body1
;; (s3 e3)) > body2
;; body1 > body3)
;; body2 > e1 e2 e3)
;; body3) >
(defmacro let (definitions &rest body)
`((lambda ,(mapcar car definitions)
,@body)
,@(mapcar cadr definitions)))
;;------------------------------------------------------------------------------
;; General predicates
;;------------------------------------------------------------------------------
(defun not (predicate)
(if predicate nil tru))
(defun null? (expr)
(not expr))
(defun every (f lst)
(cond ((null? lst) tru)
((not (f (car lst))) nil)
(tru (every f (cdr lst)))))
(defun some (f lst)
(if (null? lst)
nil
(let ((result (f (car lst))))
(if result result
(some f (cdr lst))))))
;; NOTE: Should match C's `EXPRP_NUMBER'
(defun number? (expr)
(or (int? expr)
(flt? expr)))
;; NOTE: Should match C's `EXPRP_APPLICABLE'
(defun applicable? (expr)
(or (primitive? expr)
(lambda? expr)
(macro? expr)))
;;------------------------------------------------------------------------------
;; Debugging
;;------------------------------------------------------------------------------
(defmacro assert (predicate)
`(if ,predicate ,predicate
(error (append "Assertion `"
(write-to-str (quote ,predicate))
"' failed."))))
;; TODO: Toggle by removing `func' if it's already in `*debug-trace*'
(defun trace (func)
(assert (applicable? func))
(define-global *debug-trace* (cons func *debug-trace*))
"Trace enabled.")
;;------------------------------------------------------------------------------
;; Mapping
;;------------------------------------------------------------------------------
(defun mapcar (f lst)
(if (null? lst)
nil
(cons (f (car lst))
(mapcar f (cdr lst)))))