No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

546 líneas
16 KiB

  1. ;; (remove_when_double_free_is_fixed)
  2. ;; (remove_when_double_free_is_fixed_2)
  3. ;; (define (kk (:key ()))
  4. ;; ())
  5. ;; (kk)
  6. (define pair cons)
  7. (define first car)
  8. (define rest cdr)
  9. (define hm/set! hash-map-set!)
  10. (define hm/get hash-map-get)
  11. (define (hm/get-or-nil hm key)
  12. (attempt (hm/get hm key) ()))
  13. (define-macro (pe expr)
  14. `(begin
  15. (print :end " " ',expr "evaluates to")
  16. ((lambda (e)
  17. (print e)
  18. e) ,expr))
  19. )
  20. (define the-empty-stream ())
  21. (define (stream-null? s) (when s t))
  22. (define-macro (delay expr)
  23. `(,lambda () ,expr))
  24. (define (force promise)
  25. (promise))
  26. (define-macro (mac a) (list + 1 1))
  27. (define-macro (add . args) (cons '+ args))
  28. (define-macro (and . args)
  29. ;; (and cond1 cond2 (cond3 args))
  30. ;; ->
  31. ;; (if cond1
  32. ;; (if cond2
  33. ;; (let ((g (cond3 args)))
  34. ;; (if g
  35. ;; g
  36. ;; ()))
  37. ;; ())
  38. ;; ())
  39. (if args
  40. `(,if ,(car args)
  41. ,(apply and (cdr args))
  42. ())
  43. t))
  44. (define-macro (or . args)
  45. ;; (or cond1 cond2 (cond3 args))
  46. ;; ->
  47. ;; (if cond1
  48. ;; t
  49. ;; (if cond2
  50. ;; t
  51. ;; (if (cond3 args)
  52. ;; t
  53. ;; ())))
  54. (if args
  55. `(,if ,(car args)
  56. t
  57. ,(apply or (cdr args)))
  58. ()))
  59. (define-macro (when condition . body)
  60. "Special form for when multiple actions should be done if a
  61. condition is true.
  62. {{{example_start}}}
  63. (when (not ())
  64. (print \"Hello \")
  65. (print \"from \")
  66. (print \"when!\"))
  67. (when ()
  68. (print \"Goodbye \")
  69. (print \"World!\"))
  70. {{{example_end}}}
  71. "
  72. (if (= (cdr body) ())
  73. `(if ,condition ,@body nil)
  74. `(if ,condition (begin ,@body) nil)))
  75. (define-macro (unless condition . body)
  76. "Special form for when multiple actions should be done if a
  77. condition is false."
  78. (if (= (cdr body) ())
  79. `(if ,condition nil ,@body)
  80. `(if ,condition nil (begin ,@body))))
  81. (define-macro (n-times times action)
  82. "Executes action times times."
  83. (define (repeat times elem)
  84. (unless (> 1 times)
  85. (cons elem (repeat (- times 1) elem))))
  86. `(begin ,@(repeat times action)))
  87. (define-macro (let bindings . body)
  88. (define (unzip lists)
  89. (when lists
  90. (define (iter lists l1 l2)
  91. (define elem (car lists))
  92. (if elem
  93. (iter (cdr lists)
  94. (cons (car elem) l1)
  95. (cons (car (cdr elem)) l2))
  96. (list l1 l2)))
  97. (iter lists () ())))
  98. (define unzipped (unzip bindings))
  99. `((,lambda ,(car unzipped) ,@body) ,@(car (cdr unzipped))))
  100. (define-macro (cond . clauses)
  101. (define (rec clauses)
  102. (if (= () clauses)
  103. ()
  104. (if (= (car (car clauses)) 'else)
  105. (begin
  106. (if (not (= (cdr clauses) ()))
  107. (error :syntax-error "There are additional clauses after the else clause!")
  108. (cons 'begin (cdr (car clauses)))))
  109. `(if ,(car (car clauses))
  110. (begin ,@(cdr (car clauses)))
  111. ,(rec (cdr clauses))))))
  112. (rec clauses))
  113. (define-macro (case var . clauses)
  114. (define (rec clauses)
  115. (if (= nil clauses)
  116. nil
  117. (if (= (car (car clauses)) 'else)
  118. (begin
  119. (if (not (= (cdr clauses) ()))
  120. (error :syntax-error "There are additional clauses after the else clause!")
  121. (cons 'begin (cdr (car clauses)))))
  122. `(if (member? ,var ',(car (car clauses)))
  123. (begin ,@(cdr (car clauses)))
  124. ,(rec (cdr clauses))))))
  125. (rec clauses))
  126. (define-macro (construct-list . body)
  127. "
  128. {{{example_start}}}
  129. (construct-list
  130. i <- '(1 2 3 4 5)
  131. yield (* i i))
  132. {{{example_end}}}
  133. (construct-list
  134. i <- '(1 2 3 4)
  135. j <- '(A B)
  136. yield (cons i j))
  137. (construct-list
  138. i <- '(1 2 3 4 5 6 7 8)
  139. if (= 0 (% i 2))
  140. yield i)
  141. "
  142. (define (append-map f ll)
  143. (unless (= ll ())
  144. (define val (f (car ll)))
  145. (if (= (car val) ())
  146. (append-map f (cdr ll))
  147. (extend
  148. val
  149. (append-map f (cdr ll))))))
  150. (define (rec body)
  151. (cond
  152. ((= () body) ())
  153. ((= () (cdr body)) (car body))
  154. ((= (car (cdr body)) '<-)
  155. `(,append-map (lambda (,(car body)) (list ,(rec (cdr (cdr (cdr body)))))) ,(car (cdr (cdr body)))))
  156. ((= (car body) 'if)
  157. `(when ,(car (cdr body)) ,(rec (cdr (cdr body)))))
  158. ((= (car (cdr body)) 'yield)
  159. (car (cdr body)))
  160. (else (error :syntax-error "Not a do-able expression"))))
  161. (rec body))
  162. (define-macro (define-typed args . body)
  163. (define (get-arg-names args)
  164. (when args
  165. (cons (car args)
  166. (get-arg-names (cdr (cdr args))))))
  167. (let ((name (car args))
  168. (lambda-list (cdr args))
  169. (arg-names (get-arg-names (cdr args))))
  170. `(define (,name ,@arg-names)
  171. (assert-types= ,@lambda-list)
  172. ,@body)))
  173. (define-macro (define-module module-name (:imports ()) (:exports ()) . body)
  174. (let ((module-prefix (concat-strings (symbol->string module-name) "::")))
  175. (eval `(,begin ,@(map (lambda (x) `(,import ,x)) imports) ,@body))
  176. (cons 'begin
  177. (map (lambda (orig-export-name)
  178. ((lambda (export-name)
  179. `(define ,export-name
  180. ,(eval orig-export-name)))
  181. (string->symbol
  182. (concat-strings module-prefix
  183. (symbol->string orig-export-name)))))
  184. exports))))
  185. (define (tdefine-module module-name (:imports ()) (:exports ()) . body)
  186. (let ((module-prefix (concat-strings (symbol->string module-name) "::"))
  187. (exec `(begin ,@(map (lambda (x) `(,import ,x)) imports) ,@body)))
  188. (eval exec)
  189. (enable-debug-log)
  190. (cons begin
  191. (map (lambda (orig-export-name)
  192. ((lambda (export-name)
  193. `(define ,export-name
  194. ,(eval orig-export-name)))
  195. (string->symbol
  196. (concat-strings module-prefix
  197. (symbol->string orig-export-name)))))
  198. exports))
  199. (disable-debug-log)))
  200. (define-macro (generic-extend args . body)
  201. (let ((fun-name (car args))
  202. (params (cdr args))
  203. (types ())
  204. (names ()))
  205. (define (process-params params)
  206. (when params
  207. (let ((_name (car params))
  208. (_type (car (cdr params))))
  209. (assert (symbol? _name))
  210. (assert (keyword? _type))
  211. (set! types (append types _type))
  212. (set! names (append names _name))
  213. (process-params (cdr (cdr params))))))
  214. (process-params params)
  215. ;; we have the fun-name, the param names and the types, lets go:
  216. ;;
  217. ;; car check if there is already a generic-<name>-map
  218. (let ((generic-map-name (string->symbol
  219. (concat-strings "generic-" (symbol->string fun-name) "-map"))))
  220. (unless (bound? generic-map-name)
  221. (define generic-map-name (hash-map)))
  222. (hm/set! generic-map-name types (eval `(,lambda ,names ,@body)))
  223. ;; now check if the generic procedure already exists
  224. (if (bound? fun-name)
  225. (let ((exisiting-fun (eval fun-name)))
  226. (unless (type=? exisiting-fun :generic-procedure)
  227. (unless (procedure? exisiting-fun)
  228. (error :macro-expand-error "can only generic-extend procedures."))
  229. (define orig-proc exisiting-fun)
  230. (define fun-name (eval
  231. `(,lambda args (let ((fun (hm/get (map type args))))
  232. (if (procedure? fun)
  233. (fun . args)
  234. (,orig-proc . args))))
  235. ))
  236. )
  237. )
  238. )
  239. ))
  240. )
  241. (define (null? x)
  242. "Checks if the argument is =nil=."
  243. (= x ()))
  244. (define (type=? obj typ)
  245. "Checks if the argument =obj= is of type =typ="
  246. (= (type obj) typ))
  247. (define (types=? . objs)
  248. (define (inner objs)
  249. (if objs
  250. (let ((actual-type (type (car objs)))
  251. (desired-type (car (cdr objs))))
  252. (if (= actual-type desired-type)
  253. (inner (cdr (cdr objs)))
  254. ()))
  255. t))
  256. (inner objs))
  257. (define (assert-types= . objs)
  258. (define (inner objs)
  259. (when objs
  260. (let ((actual-type (type (car objs)))
  261. (desired-type (car (cdr objs))))
  262. (if (= actual-type desired-type)
  263. (inner (cdr (cdr objs)))
  264. (error :type-missmatch "type missmatch" actual-type desired-type)))))
  265. (inner objs))
  266. (define (number? x)
  267. "Checks if the argument is a number."
  268. (type=? x :number))
  269. (define (symbol? x)
  270. "Checks if the argument is a symbol."
  271. (type=? x :symbol))
  272. (define (keyword? x)
  273. "Checks if the argument is a keyword."
  274. (type=? x :keyword))
  275. (define (cons? x)
  276. "Checks if the argument is a pair."
  277. (type=? x :pair))
  278. (define (string? x)
  279. "Checks if the argument is a string."
  280. (type=? x :string))
  281. (define (lambda? x)
  282. "Checks if the argument is a function."
  283. (type=? x :lambda))
  284. (define (macro? x)
  285. "Checks if the argument is a macro."
  286. (type=? x :macro))
  287. (define (special-lambda? x)
  288. "Checks if the argument is a special-lambda."
  289. (type=? x :dynamic-macro))
  290. (define (built-in-function? x)
  291. "Checks if the argument is a built-in function."
  292. (type=? x :cfunction))
  293. (define (continuation? x)
  294. "Checks if the argument is a continuation."
  295. (type=? x :continuation))
  296. (define (procedure? x)
  297. (or (lambda? x)
  298. (special-lambda? x)
  299. (macro? x)
  300. (built-in-function? x)
  301. (continuation? x)))
  302. (define (end seq)
  303. "Returns the last pair in the sqeuence.
  304. {{{example_start}}}
  305. (define a (list 1 2 3 4))
  306. (print (end a))
  307. {{{example_end}}}
  308. "
  309. (if (or (null? seq) (not (cons? (cdr seq))))
  310. seq
  311. (end (cdr seq))))
  312. (define (last seq)
  313. "Returns the (car) of the last (cons) of the given sequence.
  314. {{{example_start}}}
  315. (define a (list 1 2 3 4))
  316. (print (last a))
  317. {{{example_end}}}
  318. "
  319. (car (end seq)))
  320. (define (extend seq elem)
  321. "Extends a list with the given element, by putting it in
  322. the (cdr ) of the last element of the sequence."
  323. (if (cons? seq)
  324. (begin
  325. (define e (end seq))
  326. (mutate! e (cons (car e) elem))
  327. seq)
  328. elem))
  329. (define (extend2 seq elem)
  330. "Extends a list with the given element, by putting it in
  331. the (cdr ) of the last element of the sequence."
  332. (print "addr of (end seq)" (addr-of (end seq)))
  333. (if (cons? seq)
  334. (let ((e (end seq)))
  335. (print "addr if e inner" (addr-of e))
  336. (mutate! e (cons (car e) elem))
  337. seq))
  338. elem)
  339. (define (append seq elem)
  340. "Appends an element to a sequence, by extendeing the list
  341. with (cons elem nil)."
  342. (extend seq (cons elem ())))
  343. (define (length seq)
  344. "Returns the length of the given sequence."
  345. (if (null? seq)
  346. 0
  347. (+ 1 (length (cdr seq)))))
  348. (define (member? elem seq)
  349. (when (cons? seq)
  350. (if (= elem (car seq))
  351. t
  352. (member? elem (cdr seq)))))
  353. (define (sublist-starting-at-index seq index)
  354. (cond ((< index 0)
  355. (error :index-out-of-range "sublist-starting-at-index: index must be positive"))
  356. ((null? seq) ())
  357. ((= 0 index) seq)
  358. (else (sublist-starting-at (cdr seq) (- index 1)))))
  359. (define (list-without-index seq index)
  360. (cond ((or (< index 0) (null? seq))
  361. (error :index-out-of-range "list-remove-index!: index out of range"))
  362. ((= 0 index) (cdr seq))
  363. (else (cons (car seq) (list-without-index (cdr seq) (- index 1))))))
  364. (define (increment val)
  365. "Adds one to the argument."
  366. (+ val 1))
  367. (define (decrement val)
  368. "Subtracts one from the argument."
  369. (- val 1))
  370. (define (range (:from 0) :to)
  371. "Returns a sequence of numbers starting with the number defined
  372. by the key =from= and ends with the number defined in =to=."
  373. (when (< from to)
  374. (cons from (range :from (+ 1 from) :to to))))
  375. (define (range-while (:from 0) :to)
  376. "Returns a sequence of numbers starting with the number defined
  377. by the key 'from' and ends with the number defined in 'to'."
  378. (define result (list (copy from)))
  379. (define head result)
  380. (set! from (increment from))
  381. (while (< from to)
  382. (begin
  383. (mutate! head (cons (car head) (cons (copy from) nil)))
  384. (define head (cdr head))
  385. (set! from (increment from))))
  386. result)
  387. (define (map fun seq)
  388. "Takes a function and a sequence as arguments and returns a new
  389. sequence which contains the results of using the car sequences
  390. elemens as argument to that function."
  391. (if (null? seq)
  392. seq
  393. (cons (fun (car seq))
  394. (map fun (cdr seq)))))
  395. (define (reduce fun seq)
  396. "Takes a function and a sequence as arguments and applies the
  397. function to the argument sequence. This only works correctly if the
  398. given function accepts a variable amount of parameters. If your
  399. funciton is limited to two arguments, use [[=reduce-binary=]]
  400. instead."
  401. (apply fun seq))
  402. (define (reduce-binary fun seq)
  403. "Takes a function and a sequence as arguments and applies the
  404. function to the argument sequence. reduce-binary applies the arguments
  405. *pair-wise* which means it works with binary functions as compared to
  406. [[=reduce=]]."
  407. (if (null? (cdr seq))
  408. (car seq)
  409. (fun (car seq)
  410. (reduce-binary fun (cdr seq)))))
  411. (define (filter fun seq)
  412. "Takes a function and a sequence as arguments and applies the
  413. function to every value in the sequence. If the result of that
  414. funciton application returns a truthy value, the original value is
  415. added to a list, which in the end is returned."
  416. (when seq
  417. (if (fun (car seq))
  418. (cons (car seq)
  419. (filter fun (cdr seq)))
  420. (filter fun (cdr seq)))))
  421. (define (zip l1 l2)
  422. (unless (and (null? l1) (null? l2))
  423. (cons (list (car l1) (car l2))
  424. (zip (cdr l1) (cdr l2)))))
  425. (define (unzip lists)
  426. (when lists
  427. (define (iter lists l1 l2)
  428. (define elem (car lists))
  429. (if elem
  430. (iter (cdr lists)
  431. (cons (car elem) l1)
  432. (cons (car (cdr elem)) l2))
  433. (list l1 l2)))
  434. (iter lists () ())))
  435. (define (enumerate seq)
  436. (define (enumerate-inner seq next-num)
  437. (when seq
  438. (cons (list (car seq) next-num)
  439. (enumerate-inner (cdr seq) (+ 1 next-num)))))
  440. (enumerate-inner seq 0))
  441. ;; (generic-extend (+ v1 :vector v2 :vector)
  442. ;; (assert (= (vector-length v1)
  443. ;; (vector-length v2)))
  444. ;; (vector (+ (vector-ref v1 0)
  445. ;; (vector-ref v2 0))))
  446. ;; (unless (bound? generic-+-map)
  447. ;; (set! generic-+-map (create-hash-map)))
  448. ;; (hm/set! generic-+-map '(:vector :vector) (lambda (v1 v2)
  449. ;; (assert (= (vector-length v1)
  450. ;; (vector-length v2)))
  451. ;; (vector (+ (vector-ref v1 0)
  452. ;; (vector-ref v2 0)))))
  453. ;; (hm/set! generic-+-map '(:string :string) (lambda (v1 v2) (concat-strings v1 v2)))
  454. ;; (let ((define-it
  455. ;; (lambda (backup)
  456. ;; (set! + (set-type!
  457. ;; (lambda args (let ((fun (hm/get-or-nil generic-+-map (map type args))))
  458. ;; (if fun (apply fun args)
  459. ;; (backup args))))
  460. ;; :generic-procedure)))))
  461. ;; (if (bound? +)
  462. ;; (let ((exisiting-fun +))
  463. ;; (unless (type=? exisiting-fun :generic-procedure)
  464. ;; (unless (procedure? exisiting-fun)
  465. ;; (error :macro-expand-error "can only generic-extend procedures."))
  466. ;; (define orig-proc exisiting-fun)
  467. ;; (define-it (lambda (args) (apply orig-proc args)))))
  468. ;; (define-it (lambda (args) (error :generic-lookup "no overloads found")))))