Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

542 строки
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. (define-syntax (generic-extend args . body)
  198. (let ((fun-name (first args))
  199. (params (rest args))
  200. (types ())
  201. (names ()))
  202. (define (process-params params)
  203. (when params
  204. (let ((_name (first params))
  205. (_type (first (rest params))))
  206. (assert (symbol? _name))
  207. (assert (keyword? _type))
  208. (set! types (append types _type))
  209. (set! names (append names _name))
  210. (process-params (rest (rest params))))))
  211. (process-params params)
  212. ;; we have the fun-name, the param names and the types, lets go:
  213. ;;
  214. ;; first check if there is already a generic-<name>-map
  215. (let ((generic-map-name (string->symbol
  216. (concat-strings "generic-" (symbol->string fun-name) "-map"))))
  217. (unless (bound? generic-map-name)
  218. (define generic-map-name (hash-map)))
  219. (hm/set! generic-map-name types (eval `(,lambda ,names ,@body)))
  220. ;; now check if the generic procedure already exists
  221. (if (bound? fun-name)
  222. (let ((exisiting-fun (eval fun-name)))
  223. (unless (type=? exisiting-fun :generic-procedure)
  224. (unless (procedure? exisiting-fun)
  225. (error :macro-expand-error "can only generic-extend procedures."))
  226. (define orig-proc exisiting-fun)
  227. (define fun-name (eval
  228. `(,lambda args (let ((fun (hm/get (map type args))))
  229. (if (procedure? fun)
  230. (fun . args)
  231. (,orig-proc . args))))
  232. ))
  233. )
  234. )
  235. )
  236. ))
  237. )
  238. (define (null? x)
  239. "Checks if the argument is =nil=."
  240. (= x ()))
  241. (define (type=? obj typ)
  242. "Checks if the argument =obj= is of type =typ="
  243. (= (type obj) typ))
  244. (define (types=? . objs)
  245. (define (inner objs)
  246. (if objs
  247. (let ((actual-type (type (first objs)))
  248. (desired-type (first (rest objs))))
  249. (if (= actual-type desired-type)
  250. (inner (rest (rest objs)))
  251. ()))
  252. t))
  253. (inner objs))
  254. (define (assert-types= . objs)
  255. (define (inner objs)
  256. (when objs
  257. (let ((actual-type (type (first objs)))
  258. (desired-type (first (rest objs))))
  259. (if (= actual-type desired-type)
  260. (inner (rest (rest objs)))
  261. (error :type-missmatch "type missmatch" actual-type desired-type)))))
  262. (inner objs))
  263. (define (number? x)
  264. "Checks if the argument is a number."
  265. (type=? x :number))
  266. (define (symbol? x)
  267. "Checks if the argument is a symbol."
  268. (type=? x :symbol))
  269. (define (keyword? x)
  270. "Checks if the argument is a keyword."
  271. (type=? x :keyword))
  272. (define (pair? x)
  273. "Checks if the argument is a pair."
  274. (type=? x :pair))
  275. (define (string? x)
  276. "Checks if the argument is a string."
  277. (type=? x :string))
  278. (define (lambda? x)
  279. "Checks if the argument is a function."
  280. (type=? x :lambda))
  281. (define (macro? x)
  282. "Checks if the argument is a macro."
  283. (type=? x :macro))
  284. (define (special-lambda? x)
  285. "Checks if the argument is a special-lambda."
  286. (type=? x :dynamic-macro))
  287. (define (built-in-function? x)
  288. "Checks if the argument is a built-in function."
  289. (type=? x :cfunction))
  290. (define (continuation? x)
  291. "Checks if the argument is a continuation."
  292. (type=? x :continuation))
  293. (define (procedure? x)
  294. (or (lambda? x)
  295. (special-lambda? x)
  296. (macro? x)
  297. (built-in-function? x)
  298. (continuation? x)))
  299. (define (end seq)
  300. "Returns the last pair in the sqeuence.
  301. {{{example_start}}}
  302. (define a (list 1 2 3 4))
  303. (print (end a))
  304. {{{example_end}}}
  305. "
  306. (if (or (null? seq) (not (pair? (rest seq))))
  307. seq
  308. (end (rest seq))))
  309. (define (last seq)
  310. "Returns the (first) of the last (pair) of the given sequence.
  311. {{{example_start}}}
  312. (define a (list 1 2 3 4))
  313. (print (last a))
  314. {{{example_end}}}
  315. "
  316. (first (end seq)))
  317. (define (extend seq elem)
  318. "Extends a list with the given element, by putting it in
  319. the (rest) of the last element of the sequence."
  320. (if (pair? seq)
  321. (begin
  322. (define e (end seq))
  323. (mutate e (pair (first e) elem))
  324. seq)
  325. elem))
  326. (define (extend2 seq elem)
  327. "Extends a list with the given element, by putting it in
  328. the (rest) of the last element of the sequence."
  329. (print "addr of (end seq)" (addr-of (end seq)))
  330. (if (pair? seq)
  331. (let ((e (end seq)))
  332. (print "addr if e inner" (addr-of e))
  333. (mutate e (pair (first e) elem))
  334. seq))
  335. elem)
  336. (define (append seq elem)
  337. "Appends an element to a sequence, by extendeing the list
  338. with (pair elem nil)."
  339. (extend seq (pair elem ())))
  340. (define (length seq)
  341. "Returns the length of the given sequence."
  342. (if (null? seq)
  343. 0
  344. (+ 1 (length (rest seq)))))
  345. (define (member? elem seq)
  346. (when (pair? seq)
  347. (if (= elem (first seq))
  348. t
  349. (member? elem (rest seq)))))
  350. (define (sublist-starting-at-index seq index)
  351. (cond ((< index 0)
  352. (error :index-out-of-range "sublist-starting-at-index: index must be positive"))
  353. ((null? seq) ())
  354. ((= 0 index) seq)
  355. (else (sublist-starting-at (rest seq) (- index 1)))))
  356. (define (list-without-index seq index)
  357. (cond ((or (< index 0) (null? seq))
  358. (error :index-out-of-range "list-remove-index!: index out of range"))
  359. ((= 0 index) (rest seq))
  360. (else (pair (first seq) (list-without-index (rest seq) (- index 1))))))
  361. (define (increment val)
  362. "Adds one to the argument."
  363. (+ val 1))
  364. (define (decrement val)
  365. "Subtracts one from the argument."
  366. (- val 1))
  367. (define (range (:from 0) :to)
  368. "Returns a sequence of numbers starting with the number defined
  369. by the key =from= and ends with the number defined in =to=."
  370. (when (< from to)
  371. (pair from (range :from (+ 1 from) :to to))))
  372. (define (range-while (:from 0) :to)
  373. "Returns a sequence of numbers starting with the number defined
  374. by the key 'from' and ends with the number defined in 'to'."
  375. (define result (list (copy from)))
  376. (define head result)
  377. (set! from (increment from))
  378. (while (< from to)
  379. (begin
  380. (mutate head (pair (first head) (pair (copy from) nil)))
  381. (define head (rest head))
  382. (set! from (increment from))))
  383. result)
  384. (define (map fun seq)
  385. "Takes a function and a sequence as arguments and returns a new
  386. sequence which contains the results of using the first sequences
  387. elemens as argument to that function."
  388. (if (null? seq)
  389. seq
  390. (pair (fun (first seq))
  391. (map fun (rest seq)))))
  392. (define (reduce fun seq)
  393. "Takes a function and a sequence as arguments and applies the
  394. function to the argument sequence. This only works correctly if the
  395. given function accepts a variable amount of parameters. If your
  396. funciton is limited to two arguments, use [[=reduce-binary=]]
  397. instead."
  398. (apply fun seq))
  399. (define (reduce-binary fun seq)
  400. "Takes a function and a sequence as arguments and applies the
  401. function to the argument sequence. reduce-binary applies the arguments
  402. *pair-wise* which means it works with binary functions as compared to
  403. [[=reduce=]]."
  404. (if (null? (rest seq))
  405. (first seq)
  406. (fun (first seq)
  407. (reduce-binary fun (rest seq)))))
  408. (define (filter fun seq)
  409. "Takes a function and a sequence as arguments and applies the
  410. function to every value in the sequence. If the result of that
  411. funciton application returns a truthy value, the original value is
  412. added to a list, which in the end is returned."
  413. (when seq
  414. (if (fun (first seq))
  415. (pair (first seq)
  416. (filter fun (rest seq)))
  417. (filter fun (rest seq)))))
  418. (define (zip l1 l2)
  419. (unless (and (null? l1) (null? l2))
  420. (pair (list (first l1) (first l2))
  421. (zip (rest l1) (rest l2)))))
  422. (define (unzip lists)
  423. (when lists
  424. (define (iter lists l1 l2)
  425. (define elem (first lists))
  426. (if elem
  427. (iter (rest lists)
  428. (pair (first elem) l1)
  429. (pair (first (rest elem)) l2))
  430. (list l1 l2)))
  431. (iter lists () ())))
  432. (define (enumerate seq)
  433. (define (enumerate-inner seq next-num)
  434. (when seq
  435. (pair (list (first seq) next-num)
  436. (enumerate-inner (rest seq) (+ 1 next-num)))))
  437. (enumerate-inner seq 0))
  438. ;; (generic-extend (+ v1 :vector v2 :vector)
  439. ;; (assert (= (vector-length v1)
  440. ;; (vector-length v2)))
  441. ;; (vector (+ (vector-ref v1 0)
  442. ;; (vector-ref v2 0))))
  443. ;; (unless (bound? generic-+-map)
  444. ;; (set! generic-+-map (create-hash-map)))
  445. ;; (hm/set! generic-+-map '(:vector :vector) (lambda (v1 v2)
  446. ;; (assert (= (vector-length v1)
  447. ;; (vector-length v2)))
  448. ;; (vector (+ (vector-ref v1 0)
  449. ;; (vector-ref v2 0)))))
  450. ;; (hm/set! generic-+-map '(:string :string) (lambda (v1 v2) (concat-strings v1 v2)))
  451. ;; (let ((define-it
  452. ;; (lambda (backup)
  453. ;; (set! + (set-type!
  454. ;; (lambda args (let ((fun (hm/get-or-nil generic-+-map (map type args))))
  455. ;; (if fun (apply fun args)
  456. ;; (backup args))))
  457. ;; :generic-procedure)))))
  458. ;; (if (bound? +)
  459. ;; (let ((exisiting-fun +))
  460. ;; (unless (type=? exisiting-fun :generic-procedure)
  461. ;; (unless (procedure? exisiting-fun)
  462. ;; (error :macro-expand-error "can only generic-extend procedures."))
  463. ;; (define orig-proc exisiting-fun)
  464. ;; (define-it (lambda (args) (apply orig-proc args)))))
  465. ;; (define-it (lambda (args) (error :generic-lookup "no overloads found")))))