| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2023, 2025 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | 2022 Benjamin Vedder benjamin@vedder.se | ||
| 4 | 2025 Rasmus Söderhielm rasmus.soderhielm@gmail.com | ||
| 5 | |||
| 6 | This program is free software: you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 3 of the License, or | ||
| 9 | (at your option) any later version. | ||
| 10 | |||
| 11 | This program is distributed in the hope that it will be useful, | ||
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | GNU General Public License for more details. | ||
| 15 | |||
| 16 | You should have received a copy of the GNU General Public License | ||
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <extensions.h> | ||
| 21 | |||
| 22 | #if defined(LBM_USE_DYN_FUNS) || defined(LBM_USE_DYN_ARRAYS) | ||
| 23 | static const char* lbm_dyn_fun[] = { | ||
| 24 | #ifdef LBM_USE_DYN_FUNS | ||
| 25 | "(defun str-merge () (str-join (rest-args)))", | ||
| 26 | "(defun iota (n) (range n))", | ||
| 27 | |||
| 28 | "(defun foldl (f init lst)" | ||
| 29 | "(if (eq lst nil) init (foldl f (f init (car lst)) (cdr lst))))", | ||
| 30 | |||
| 31 | "(defun foldr (f init lst)" | ||
| 32 | "(if (eq lst nil) init (f (car lst) (foldr f init (cdr 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 unzip (xs) (cons (map car xs) (map cdr xs)))", | ||
| 45 | |||
| 46 | "(defun filter (f lst)" | ||
| 47 | "(let ((filter-rec (lambda (f lst ys)" | ||
| 48 | "(if (eq lst nil)" | ||
| 49 | "(reverse ys)" | ||
| 50 | "(if (f (car lst))" | ||
| 51 | "(filter-rec f (cdr lst) (cons (car lst) ys))" | ||
| 52 | "(filter-rec f (cdr lst) ys))))))" | ||
| 53 | "(filter-rec f lst nil)" | ||
| 54 | "))", | ||
| 55 | |||
| 56 | "(defun str-cmp-asc (a b) (< (str-cmp a b) 0))", | ||
| 57 | "(defun str-cmp-dsc (a b) (> (str-cmp a b) 0))", | ||
| 58 | |||
| 59 | "(defun second (x) (car (cdr x)))", | ||
| 60 | "(defun third (x) (car (cdr (cdr x))))", | ||
| 61 | |||
| 62 | "(defun abs (x) (if (< x 0) (- x) x))", | ||
| 63 | #ifdef LBM_USE_DYN_DEFSTRUCT | ||
| 64 | "(defun create-struct (name num-fields initials) { " | ||
| 65 | "(var arr (mkarray (+ 1 num-fields))) " | ||
| 66 | "(setix arr 0 name) " | ||
| 67 | "(var num_inits (length initials))" | ||
| 68 | "(if initials (loopfor i 0 (and (< i num-fields) (< i num_inits)) (+ i 1) (setix arr (+ i 1) (ix initials i))))" | ||
| 69 | "arr " | ||
| 70 | "})", | ||
| 71 | |||
| 72 | "(defun is-struct (struct name) " | ||
| 73 | "(and (eq (type-of struct) type-lisparray) " | ||
| 74 | "(eq (ix struct 0) name)))", | ||
| 75 | |||
| 76 | "(defun accessor-sym (name field) " | ||
| 77 | "(str2sym (str-merge name \"-\" (sym2str field))))", | ||
| 78 | |||
| 79 | "(defun access-set (i) " | ||
| 80 | "(lambda (struct) " | ||
| 81 | "(if (rest-args) " | ||
| 82 | "(setix struct i (rest-args 0)) " | ||
| 83 | "(ix struct i)))) ", | ||
| 84 | #endif | ||
| 85 | #endif //LBM_DYN_FUNS | ||
| 86 | #ifdef LBM_USE_DYN_ARRAYS | ||
| 87 | "(defun list-to-array (ls)" | ||
| 88 | "(let ((n (length ls)) (arr (mkarray n)) (i 0)) {" | ||
| 89 | "(loopforeach e ls { (setix arr i e) (setq i (+ i 1)) }) arr }))", | ||
| 90 | |||
| 91 | "(defun array-to-list (arr)" | ||
| 92 | "(let ((n (length arr)) (ls nil)) {" | ||
| 93 | "(loopfor i (- n 1) (>= i 0) (- i 1) { (setq ls (cons (ix arr i) ls)) }) ls }))", | ||
| 94 | |||
| 95 | "(defun array? (a) (eq (type-of a) type-lisparray))", | ||
| 96 | #endif | ||
| 97 | }; | ||
| 98 | #endif // defined(LBM_USE_DYN_FUNS) || defined(LBM_USE_DYN_ARRAYS) | ||
| 99 | |||
| 100 | |||
| 101 | #ifdef LBM_USE_DYN_MACROS | ||
| 102 | static const char* lbm_dyn_macros[] = { | ||
| 103 | "(define defun (macro (name args body) (me-defun name args body)))", | ||
| 104 | "(define defunret (macro (name args body) (me-defunret name args body)))", | ||
| 105 | "(define defmacro (macro (name args body) `(define ,name (macro ,args ,body))))", | ||
| 106 | #ifdef LBM_USE_DYN_LOOPS | ||
| 107 | "(define loopfor (macro (it start cnd update body) (me-loopfor it start cnd update body)))", | ||
| 108 | "(define loopwhile (macro (cnd body) (me-loopwhile cnd body)))", | ||
| 109 | "(define looprange (macro (it start end body) (me-looprange it start end body)))", | ||
| 110 | "(define loopforeach (macro (it lst body) (me-loopforeach it lst body)))", | ||
| 111 | "(define loopwhile-thd (macro (stk cnd body) `(spawn ,@(if (list? stk) stk (list stk)) (fn () (loopwhile ,cnd ,body)))))", | ||
| 112 | #endif | ||
| 113 | #ifdef LBM_USE_DYN_DEFSTRUCT | ||
| 114 | "(define defstruct (macro (name list-of-fields)" | ||
| 115 | "{" | ||
| 116 | "(var num-fields (length list-of-fields))" | ||
| 117 | "(var name-as-string (sym2str name))" | ||
| 118 | "(var new-create-sym (str2sym (str-merge \"make-\" name-as-string)))" | ||
| 119 | "(var new-pred-sym (str2sym (str-merge name-as-string \"?\")))" | ||
| 120 | "(var field-ix (zip list-of-fields (range 1 (+ num-fields 1))))" | ||
| 121 | "`(progn" | ||
| 122 | "(define ,new-create-sym (lambda () (create-struct ',name ,num-fields (rest-args))))" | ||
| 123 | "(define ,new-pred-sym (lambda (struct) (is-struct struct ',name)))" | ||
| 124 | ",@(map (lambda (x) (list define (accessor-sym name-as-string (car x))" | ||
| 125 | "(access-set (cdr x)))) field-ix)" | ||
| 126 | "'t" | ||
| 127 | ")" | ||
| 128 | "}))", | ||
| 129 | #endif | ||
| 130 | }; | ||
| 131 | |||
| 132 | static lbm_uint sym_return; | ||
| 133 | |||
| 134 | 266 | static lbm_value ext_me_defun(lbm_value *argsi, lbm_uint argn) { | |
| 135 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 265 times.
|
266 | if (argn != 3) { |
| 136 | 1 | return ENC_SYM_EERROR; | |
| 137 | } | ||
| 138 | |||
| 139 | 265 | lbm_value name = argsi[0]; | |
| 140 | 265 | lbm_value args = argsi[1]; | |
| 141 | 265 | lbm_value body = argsi[2]; | |
| 142 | |||
| 143 | // (define name (lambda args body)) | ||
| 144 | |||
| 145 | 265 | return make_list(3, | |
| 146 | ENC_SYM_DEFINE, | ||
| 147 | name, | ||
| 148 | mk_lam(args, body)); | ||
| 149 | } | ||
| 150 | |||
| 151 | 2 | static lbm_value ext_me_defunret(lbm_value *argsi, lbm_uint argn) { | |
| 152 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
|
2 | if (argn != 3) { |
| 153 | 1 | return ENC_SYM_EERROR; | |
| 154 | } | ||
| 155 | |||
| 156 | 1 | lbm_value name = argsi[0]; | |
| 157 | 1 | lbm_value args = argsi[1]; | |
| 158 | 1 | lbm_value body = argsi[2]; | |
| 159 | |||
| 160 | // (def name (lambda args (call-cc (lambda (return) body)))) | ||
| 161 | |||
| 162 | 1 | return make_list(3, | |
| 163 | ENC_SYM_DEFINE, | ||
| 164 | name, | ||
| 165 | mk_lam(args, | ||
| 166 | mk_call_cc(mk_lam(make_list(1, lbm_enc_sym(sym_return)), | ||
| 167 | body)))); | ||
| 168 | } | ||
| 169 | |||
| 170 | #endif | ||
| 171 | |||
| 172 | // DYN LOOPS //////////////////////////////////////////////////////////// | ||
| 173 | #ifdef LBM_USE_DYN_LOOPS | ||
| 174 | |||
| 175 | static lbm_uint sym_res; | ||
| 176 | static lbm_uint sym_loop; | ||
| 177 | static lbm_uint sym_break; | ||
| 178 | static lbm_uint sym_brk; | ||
| 179 | static lbm_uint sym_rst; | ||
| 180 | static lbm_uint sym_return; | ||
| 181 | |||
| 182 | 504 | static lbm_value ext_me_loopfor(lbm_value *args, lbm_uint argn) { | |
| 183 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 503 times.
|
504 | if (argn != 5) { |
| 184 | 1 | return ENC_SYM_EERROR; | |
| 185 | } | ||
| 186 | |||
| 187 | 503 | lbm_value it = args[0]; | |
| 188 | 503 | lbm_value start = args[1]; | |
| 189 | 503 | lbm_value cond = args[2]; | |
| 190 | 503 | lbm_value update = args[3]; | |
| 191 | 503 | lbm_value body = args[4]; | |
| 192 | |||
| 193 | // (call-cc-unsafe | ||
| 194 | // (lambda (break) | ||
| 195 | // (let ((loop (lambda (it res) | ||
| 196 | // (if cond (loop update body) res)))) | ||
| 197 | // (loop start nil)))) | ||
| 198 | |||
| 199 | 503 | lbm_value enc_sym_loop = lbm_enc_sym(sym_loop); // maybe do one time at init? | |
| 200 | 503 | lbm_value enc_sym_break = lbm_enc_sym(sym_break); | |
| 201 | 503 | lbm_value enc_sym_res = lbm_enc_sym(sym_res); | |
| 202 | |||
| 203 | 503 | return mk_call_cc(mk_lam(make_list(1, enc_sym_break), | |
| 204 | mk_let(make_list(1, | ||
| 205 | make_list(2, | ||
| 206 | enc_sym_loop, | ||
| 207 | mk_lam(make_list(2, it, enc_sym_res), | ||
| 208 | mk_if(cond, | ||
| 209 | make_list(3, enc_sym_loop, update, body), | ||
| 210 | enc_sym_res)))), | ||
| 211 | make_list(3, enc_sym_loop, start, ENC_SYM_NIL)))); | ||
| 212 | } | ||
| 213 | |||
| 214 | 181 | static lbm_value ext_me_loopwhile(lbm_value *args, lbm_uint argn) { | |
| 215 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 180 times.
|
181 | if (argn != 2) { |
| 216 | 1 | return ENC_SYM_EERROR; | |
| 217 | } | ||
| 218 | |||
| 219 | 180 | lbm_value cond = args[0]; | |
| 220 | 180 | lbm_value body = args[1]; | |
| 221 | |||
| 222 | //(call-cc-unsafe | ||
| 223 | // (lambda (break) | ||
| 224 | // (let ((loop (lambda (res) | ||
| 225 | // (if cond (loop body) res)))) | ||
| 226 | // (loop nil)))) | ||
| 227 | |||
| 228 | 180 | lbm_value enc_sym_loop = lbm_enc_sym(sym_loop); // maybe do one time at init? | |
| 229 | 180 | lbm_value enc_sym_break = lbm_enc_sym(sym_break); | |
| 230 | 180 | lbm_value enc_sym_res = lbm_enc_sym(sym_res); | |
| 231 | |||
| 232 | 180 | return mk_call_cc(mk_lam(make_list(1, enc_sym_break), | |
| 233 | mk_let(make_list(1, | ||
| 234 | make_list(2, | ||
| 235 | enc_sym_loop, | ||
| 236 | mk_lam(make_list(1, enc_sym_res), | ||
| 237 | mk_if(cond, | ||
| 238 | make_list(2,enc_sym_loop, body), | ||
| 239 | enc_sym_res)))), | ||
| 240 | (make_list(2, enc_sym_loop, ENC_SYM_NIL))))); | ||
| 241 | } | ||
| 242 | |||
| 243 | 186 | static lbm_value ext_me_looprange(lbm_value *args, lbm_uint argn) { | |
| 244 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 185 times.
|
186 | if (argn != 4) { |
| 245 | 1 | return ENC_SYM_EERROR; | |
| 246 | } | ||
| 247 | |||
| 248 | 185 | lbm_value it = args[0]; | |
| 249 | 185 | lbm_value start = args[1]; | |
| 250 | 185 | lbm_value end = args[2]; | |
| 251 | 185 | lbm_value body = args[3]; | |
| 252 | |||
| 253 | // (call-cc-unsafe | ||
| 254 | // (lambda (break) | ||
| 255 | // (let ((loop (lambda (it res) | ||
| 256 | // (if (< it end) | ||
| 257 | // (loop (+ it 1) body) | ||
| 258 | // res)))) | ||
| 259 | // (loop start nil)))) | ||
| 260 | |||
| 261 | 185 | lbm_value enc_sym_loop = lbm_enc_sym(sym_loop); // maybe do one time at init? | |
| 262 | 185 | lbm_value enc_sym_break = lbm_enc_sym(sym_break); | |
| 263 | 185 | lbm_value enc_sym_res = lbm_enc_sym(sym_res); | |
| 264 | |||
| 265 | 185 | return mk_call_cc(mk_lam(make_list(1, enc_sym_break), | |
| 266 | mk_let(make_list(1, | ||
| 267 | make_list(2, | ||
| 268 | enc_sym_loop, | ||
| 269 | mk_lam(make_list(2, it, enc_sym_res), | ||
| 270 | mk_if(mk_lt(it, end), | ||
| 271 | make_list(3, | ||
| 272 | enc_sym_loop, | ||
| 273 | mk_inc(it), | ||
| 274 | body), | ||
| 275 | enc_sym_res)))), | ||
| 276 | make_list(3, enc_sym_loop, start, ENC_SYM_NIL)))); | ||
| 277 | } | ||
| 278 | |||
| 279 | // TODO: Something that does not work as expected with this | ||
| 280 | // definition of loopforeach is (loopforeach e (list nil nil nil) ...). | ||
| 281 | |||
| 282 | 1345 | static lbm_value ext_me_loopforeach(lbm_value *args, lbm_uint argn) { | |
| 283 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1344 times.
|
1345 | if (argn != 3) { |
| 284 | 1 | return ENC_SYM_EERROR; | |
| 285 | } | ||
| 286 | |||
| 287 | 1344 | lbm_value it = args[0]; | |
| 288 | 1344 | lbm_value lst = args[1]; | |
| 289 | 1344 | lbm_value body = args[2]; | |
| 290 | |||
| 291 | //(call-cc-unsafe | ||
| 292 | // (lambda (break) | ||
| 293 | // (let ((loop (lambda (rst it res) | ||
| 294 | // (if (eq it nil) | ||
| 295 | // res | ||
| 296 | // (loop (car rst) (cdr rst) body))))) | ||
| 297 | // (loop (car lst) (cdr lst) nil)))) | ||
| 298 | |||
| 299 | 1344 | lbm_value enc_sym_loop = lbm_enc_sym(sym_loop); // maybe do one time at init? | |
| 300 | 1344 | lbm_value enc_sym_break = lbm_enc_sym(sym_break); | |
| 301 | 1344 | lbm_value enc_sym_res = lbm_enc_sym(sym_res); | |
| 302 | 1344 | lbm_value enc_sym_rst = lbm_enc_sym(sym_rst); | |
| 303 | |||
| 304 | 1344 | return mk_call_cc(mk_lam(make_list(1, enc_sym_break), | |
| 305 | mk_let(make_list(1, | ||
| 306 | make_list(2, | ||
| 307 | enc_sym_loop, | ||
| 308 | mk_lam(make_list(3, | ||
| 309 | it, | ||
| 310 | enc_sym_rst, | ||
| 311 | enc_sym_res), | ||
| 312 | mk_if(mk_eq(it, ENC_SYM_NIL), | ||
| 313 | enc_sym_res, | ||
| 314 | (make_list(4, | ||
| 315 | enc_sym_loop, | ||
| 316 | mk_car(enc_sym_rst), | ||
| 317 | mk_cdr(enc_sym_rst), | ||
| 318 | body)))))), | ||
| 319 | (make_list(4, | ||
| 320 | enc_sym_loop, | ||
| 321 | mk_car(lst), | ||
| 322 | mk_cdr(lst), | ||
| 323 | ENC_SYM_NIL))))); | ||
| 324 | } | ||
| 325 | #endif | ||
| 326 | |||
| 327 | |||
| 328 | // DYN_LIB_INIT //////////////////////////////////////////////////////////// | ||
| 329 | 66506 | void lbm_dyn_lib_init(void) { | |
| 330 | #ifdef LBM_USE_DYN_MACROS | ||
| 331 | 66506 | lbm_add_symbol_const("return", &sym_return); | |
| 332 | |||
| 333 | 66506 | lbm_add_extension("me-defun", ext_me_defun); | |
| 334 | 66506 | lbm_add_extension("me-defunret", ext_me_defunret); | |
| 335 | #ifdef LBM_USE_DYN_LOOPS | ||
| 336 | 66506 | lbm_add_symbol_const("a01", &sym_res); | |
| 337 | 66506 | lbm_add_symbol_const("a02", &sym_loop); | |
| 338 | 66506 | lbm_add_symbol_const("break", &sym_break); | |
| 339 | 66506 | lbm_add_symbol_const("a03", &sym_brk); | |
| 340 | 66506 | lbm_add_symbol_const("a04", &sym_rst); | |
| 341 | 66506 | lbm_add_symbol_const("return", &sym_return); | |
| 342 | |||
| 343 | 66506 | lbm_add_extension("me-loopfor", ext_me_loopfor); | |
| 344 | 66506 | lbm_add_extension("me-loopwhile", ext_me_loopwhile); | |
| 345 | 66506 | lbm_add_extension("me-looprange", ext_me_looprange); | |
| 346 | 66506 | lbm_add_extension("me-loopforeach", ext_me_loopforeach); | |
| 347 | #endif | ||
| 348 | #endif | ||
| 349 | 66506 | } | |
| 350 | |||
| 351 | 3415 | bool lbm_dyn_lib_find(const char *str, const char **code) { | |
| 352 | #ifndef LBM_USE_DYN_MACROS | ||
| 353 | #ifndef LBM_USE_DYN_FUNS | ||
| 354 | (void)str; | ||
| 355 | (void)code; | ||
| 356 | #endif | ||
| 357 | #endif | ||
| 358 | |||
| 359 | #ifdef LBM_USE_DYN_MACROS | ||
| 360 |
2/2✓ Branch 0 taken 21904 times.
✓ Branch 1 taken 1303 times.
|
23207 | for (unsigned int i = 0; i < (sizeof(lbm_dyn_macros) / sizeof(lbm_dyn_macros[0]));i++) { |
| 361 |
2/2✓ Branch 1 taken 2112 times.
✓ Branch 2 taken 19792 times.
|
21904 | if (strmatch(str, lbm_dyn_macros[i] + 8)) { // define is 6 char |
| 362 | 2112 | *code = lbm_dyn_macros[i]; | |
| 363 | 2112 | return true; | |
| 364 | } | ||
| 365 | } | ||
| 366 | #endif | ||
| 367 | |||
| 368 | #if defined(LBM_USE_DYN_FUNS) || defined(LBM_USE_DYN_ARRAYS) | ||
| 369 |
2/2✓ Branch 0 taken 18903 times.
✓ Branch 1 taken 276 times.
|
19179 | for (unsigned int i = 0; i < (sizeof(lbm_dyn_fun) / sizeof(lbm_dyn_fun[0]));i++) { |
| 370 |
2/2✓ Branch 1 taken 1027 times.
✓ Branch 2 taken 17876 times.
|
18903 | if (strmatch(str, lbm_dyn_fun[i] + 7)) { // defun is 5 |
| 371 | 1027 | *code = lbm_dyn_fun[i]; | |
| 372 | 1027 | return true; | |
| 373 | } | ||
| 374 | } | ||
| 375 | #endif | ||
| 376 | 276 | return false; | |
| 377 | } | ||
| 378 |