guile: refix _apply
[jackhill/mal.git] / guile / types.scm
CommitLineData
99fa8f0c
NG
1;; Copyright (C) 2015
2;; "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
3;; This file is free software: you can redistribute it and/or modify
4;; it under the terms of the GNU General Public License as published by
5;; the Free Software Foundation, either version 3 of the License, or
6;; (at your option) any later version.
7
8;; This file is distributed in the hope that it will be useful,
9;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11;; GNU General Public License for more details.
12
13;; You should have received a copy of the GNU General Public License
14;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16(library (types)
e658ffd2 17 (export string-sub *eof* non-list?
98cd78e4 18 string->keyword _keyword?
0d9fb576 19 nil _nil? list->hash-map
b40c6262 20 cond-true?
9f3c0995 21 make-atom atom? atom-val atom-val-set!
e658ffd2
NG
22 make-callable callable? callable-is_macro
23 callable-is_macro-set! callable-closure
98cd78e4 24 is-func? is-macro? make-func callable-apply
9f3c0995
NG
25 callable-unbox-set! callable-unbox
26 callable-meta-info hash-table-clone
27 box? box unbox)
e658ffd2
NG
28 (import (guile) (rnrs) (ice-9 regex) (ice-9 session)))
29
30(define (non-list? x) (not (list? x)))
b40c6262 31
98cd78e4 32
b40c6262
NG
33(define (string-sub str p1 p2)
34 (regexp-substitute/global #f p1 str 'pre p2 'post))
99fa8f0c
NG
35
36(define *eof* (call-with-input-string "" read))
37
98cd78e4
NG
38(define (string->keyword str)
39 (when (not (string? str))
40 (throw 'mal-error (format #f "string->keyword: '~a' is not a string" str)))
99fa8f0c
NG
41 (string-append "\u029e" str))
42
98cd78e4
NG
43(define (_keyword? k)
44 (and (string? k) (if (string-match "^\u029e" k) #t #f)))
45
0d9fb576 46(define-record-type mal-nil)
99fa8f0c 47
0d9fb576
NG
48(define nil (make-mal-nil))
49
50(define (_nil? obj) (mal-nil? obj))
99fa8f0c 51
b40c6262
NG
52(define (cond-true? obj)
53 (and (not (_nil? obj)) obj))
54
99fa8f0c 55(define-record-type atom (fields (mutable val)))
e658ffd2
NG
56
57(define-record-type callable
58 (fields
9f3c0995
NG
59 meta-info
60 (mutable unbox)
e658ffd2
NG
61 (mutable is_macro)
62 closure))
63
9f3c0995 64(define (make-func closure) (make-callable nil #t #f closure))
e658ffd2
NG
65
66(define (callable-apply c arglst)
10bc1bce 67 (apply (callable-closure c) (if (callable-unbox c) (map unbox arglst) arglst)))
e658ffd2
NG
68
69(define (callable-check c b)
70 (and (callable? c)
71 (eq? (callable-is_macro c) b)
72 c))
73
98cd78e4
NG
74(define (is-func? c) (callable-check c #f))
75(define (is-macro? c) (callable-check c #t))
76
77(define (hash-table-clone ht)
1c90c506 78 (list->hash-map (hash-fold (lambda (k v p) (cons k (cons v p))) '() ht)))
9f3c0995
NG
79
80(define-record-type box (fields val))
81
82(define (box o) (make-box o))
83(define (unbox o)
84 (if (box? o) (box-val o) o))
0d9fb576
NG
85
86(define* (list->hash-map lst #:optional (ht (make-hash-table)))
87 (cond
88 ((null? lst) ht)
89 (else
90 (let lp((next lst))
91 (cond
92 ((null? next) ht)
93 (else
94 (when (null? (cdr next))
95 (throw 'mal-error
96 (format #f "hash-map: '~a' lack of value" (car next))))
97 (let ((k (car next))
98 (v (cadr next)))
99 (hash-set! ht k v)
100 (lp (cddr next)))))))))