1 |
|
|
/* |
2 |
|
|
Copyright 2023, 2025 Joel Svensson svenssonjoel@yahoo.se |
3 |
|
|
2022 Benjamin Vedder benjamin@vedder.se |
4 |
|
|
|
5 |
|
|
This program is free software: you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation, either version 3 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <extensions.h> |
20 |
|
|
|
21 |
|
|
#ifdef LBM_USE_DYN_FUNS |
22 |
|
|
static const char* lbm_dyn_fun[] = { |
23 |
|
|
"(defun str-merge () (str-join (rest-args)))", |
24 |
|
|
"(defun iota (n) (range n))", |
25 |
|
|
|
26 |
|
|
"(defun foldl (f init lst)" |
27 |
|
|
"(if (eq lst nil) init (foldl f (f init (car lst)) (cdr lst))))", |
28 |
|
|
|
29 |
|
|
"(defun foldr (f init lst)" |
30 |
|
|
"(if (eq lst nil) init (f (car lst) (foldr f init (cdr lst)))))", |
31 |
|
|
|
32 |
|
|
"(defun apply (f lst) (eval (cons f lst)))", |
33 |
|
|
|
34 |
|
|
"(defun zipwith (f xs ys) " |
35 |
|
|
"(let (( zip-acc (lambda (acc xs ys) " |
36 |
|
|
"(if (and xs ys) " |
37 |
|
|
"(zip-acc (cons (f (car xs) (car ys)) acc) (cdr xs) (cdr ys)) " |
38 |
|
|
"acc)))) " |
39 |
|
|
"(reverse (zip-acc nil xs ys))))", |
40 |
|
|
|
41 |
|
|
"(defun zip (xs ys) " |
42 |
|
|
"(zipwith cons xs ys))", |
43 |
|
|
|
44 |
|
|
"(defun filter (f lst)" |
45 |
|
|
"(let ((filter-rec (lambda (f lst ys)" |
46 |
|
|
"(if (eq lst nil)" |
47 |
|
|
"(reverse ys)" |
48 |
|
|
"(if (f (car lst))" |
49 |
|
|
"(filter-rec f (cdr lst) (cons (car lst) ys))" |
50 |
|
|
"(filter-rec f (cdr lst) ys))))))" |
51 |
|
|
"(filter-rec f lst nil)" |
52 |
|
|
"))", |
53 |
|
|
|
54 |
|
|
"(defun str-cmp-asc (a b) (< (str-cmp a b) 0))", |
55 |
|
|
"(defun str-cmp-dsc (a b) (> (str-cmp a b) 0))", |
56 |
|
|
|
57 |
|
|
"(defun second (x) (car (cdr x)))", |
58 |
|
|
"(defun third (x) (car (cdr (cdr x))))", |
59 |
|
|
|
60 |
|
|
"(defun abs (x) (if (< x 0) (- x) x))", |
61 |
|
|
#ifdef LBM_USE_DYN_DEFSTRUCT |
62 |
|
|
"(defun create-struct (name num-fields) { " |
63 |
|
|
"(var arr (mkarray (+ 1 num-fields))) " |
64 |
|
|
"(setix arr 0 name) " |
65 |
|
|
"arr " |
66 |
|
|
"})", |
67 |
|
|
|
68 |
|
|
"(defun is-struct (struct name) " |
69 |
|
|
"(and (eq (type-of struct) type-lisparray) " |
70 |
|
|
"(eq (ix struct 0) name)))", |
71 |
|
|
|
72 |
|
|
"(defun accessor-sym (name field) " |
73 |
|
|
"(str2sym (str-merge name \"-\" (sym2str field))))", |
74 |
|
|
|
75 |
|
|
"(defun access-set (i) " |
76 |
|
|
"(lambda (struct) " |
77 |
|
|
"(if (rest-args) " |
78 |
|
|
"(setix struct i (rest-args 0)) " |
79 |
|
|
"(ix struct i)))) ", |
80 |
|
|
#endif |
81 |
|
|
}; |
82 |
|
|
#endif |
83 |
|
|
|
84 |
|
|
#ifdef LBM_USE_DYN_MACROS |
85 |
|
|
static const char* lbm_dyn_macros[] = { |
86 |
|
|
"(define defun (macro (name args body) (me-defun name args body)))", |
87 |
|
|
"(define defunret (macro (name args body) (me-defunret name args body)))", |
88 |
|
|
"(define defmacro (macro (name args body) `(define ,name (macro ,args ,body))))", |
89 |
|
|
#ifdef LBM_USE_DYN_DEFSTRUCT |
90 |
|
|
"(define defstruct (macro (name list-of-fields)" |
91 |
|
|
"{" |
92 |
|
|
"(var num-fields (length list-of-fields))" |
93 |
|
|
"(var name-as-string (sym2str name))" |
94 |
|
|
"(var new-create-sym (str2sym (str-merge \"make-\" name-as-string)))" |
95 |
|
|
"(var new-pred-sym (str2sym (str-merge name-as-string \"?\")))" |
96 |
|
|
"(var field-ix (zip list-of-fields (range 1 (+ num-fields 1))))" |
97 |
|
|
"`(progn" |
98 |
|
|
"(define ,new-create-sym (lambda () (create-struct ',name ,num-fields)))" |
99 |
|
|
"(define ,new-pred-sym (lambda (struct) (is-struct struct ',name)))" |
100 |
|
|
",@(map (lambda (x) (list define (accessor-sym name-as-string (car x))" |
101 |
|
|
"(access-set (cdr x)))) field-ix)" |
102 |
|
|
"'t" |
103 |
|
|
")" |
104 |
|
|
"}))", |
105 |
|
|
|
106 |
|
|
#endif |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
static lbm_uint sym_return; |
110 |
|
|
|
111 |
|
|
static lbm_value ext_me_defun(lbm_value *argsi, lbm_uint argn) { |
112 |
|
|
if (argn != 3) { |
113 |
|
|
return ENC_SYM_EERROR; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
lbm_value name = argsi[0]; |
117 |
|
|
lbm_value args = argsi[1]; |
118 |
|
|
lbm_value body = argsi[2]; |
119 |
|
|
|
120 |
|
|
// (define name (lambda args body)) |
121 |
|
|
|
122 |
|
|
return make_list(3, |
123 |
|
|
ENC_SYM_DEFINE, |
124 |
|
|
name, |
125 |
|
|
mk_lam(args, body)); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
static lbm_value ext_me_defunret(lbm_value *argsi, lbm_uint argn) { |
129 |
|
|
if (argn != 3) { |
130 |
|
|
return ENC_SYM_EERROR; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
lbm_value name = argsi[0]; |
134 |
|
|
lbm_value args = argsi[1]; |
135 |
|
|
lbm_value body = argsi[2]; |
136 |
|
|
|
137 |
|
|
// (def name (lambda args (call-cc (lambda (return) body)))) |
138 |
|
|
|
139 |
|
|
return make_list(3, |
140 |
|
|
ENC_SYM_DEFINE, |
141 |
|
|
name, |
142 |
|
|
mk_lam(args, |
143 |
|
|
mk_call_cc(mk_lam(make_list(1, lbm_enc_sym(sym_return)), |
144 |
|
|
body)))); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
#endif |
148 |
|
|
|
149 |
|
|
void lbm_dyn_lib_init(void) { |
150 |
|
|
#ifdef LBM_USE_DYN_MACROS |
151 |
|
|
lbm_add_symbol_const("return", &sym_return); |
152 |
|
|
|
153 |
|
|
lbm_add_extension("me-defun", ext_me_defun); |
154 |
|
|
lbm_add_extension("me-defunret", ext_me_defunret); |
155 |
|
|
#endif |
156 |
|
|
} |
157 |
|
|
|
158 |
|
84 |
bool lbm_dyn_lib_find(const char *str, const char **code) { |
159 |
|
|
#ifndef LBM_USE_DYN_MACROS |
160 |
|
|
#ifndef LBM_USE_DYN_FUNS |
161 |
|
|
(void)str; |
162 |
|
|
(void)code; |
163 |
|
|
#endif |
164 |
|
|
#endif |
165 |
|
|
|
166 |
|
|
#ifdef LBM_USE_DYN_MACROS |
167 |
|
|
for (unsigned int i = 0; i < (sizeof(lbm_dyn_macros) / sizeof(lbm_dyn_macros[0]));i++) { |
168 |
|
|
if (strmatch(str, lbm_dyn_macros[i] + 8)) { // define is 6 char |
169 |
|
|
*code = lbm_dyn_macros[i]; |
170 |
|
|
return true; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
#endif |
174 |
|
|
|
175 |
|
|
#ifdef LBM_USE_DYN_FUNS |
176 |
|
|
for (unsigned int i = 0; i < (sizeof(lbm_dyn_fun) / sizeof(lbm_dyn_fun[0]));i++) { |
177 |
|
|
if (strmatch(str, lbm_dyn_fun[i] + 7)) { // defun is 5 |
178 |
|
|
*code = lbm_dyn_fun[i]; |
179 |
|
|
return true; |
180 |
|
|
} |
181 |
|
|
} |
182 |
|
|
#endif |
183 |
|
84 |
return false; |
184 |
|
|
} |