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.
 
 
 
 
 
 

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