haskell, r: add seq and string?.
[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
2a80d367 20 cond-true? make-anonymous-func
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)
2a80d367 28 (import (guile) (only (rnrs) define-record-type) (ice-9 regex) (ice-9 session)))
e658ffd2
NG
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 43(define (_keyword? k)
337c8031
JM
44 (and (string? k)
45 (> (string-length k) 0)
46 (char=? #\1236 (string-ref k 0))))
98cd78e4 47
0d9fb576 48(define-record-type mal-nil)
99fa8f0c 49
0d9fb576
NG
50(define nil (make-mal-nil))
51
52(define (_nil? obj) (mal-nil? obj))
99fa8f0c 53
b40c6262
NG
54(define (cond-true? obj)
55 (and (not (_nil? obj)) obj))
56
99fa8f0c 57(define-record-type atom (fields (mutable val)))
e658ffd2
NG
58
59(define-record-type callable
60 (fields
9f3c0995
NG
61 meta-info
62 (mutable unbox)
e658ffd2
NG
63 (mutable is_macro)
64 closure))
65
9f3c0995 66(define (make-func closure) (make-callable nil #t #f closure))
2a80d367 67(define (make-anonymous-func closure) (make-callable nil #f #f closure))
e658ffd2
NG
68
69(define (callable-apply c arglst)
10bc1bce 70 (apply (callable-closure c) (if (callable-unbox c) (map unbox arglst) arglst)))
e658ffd2
NG
71
72(define (callable-check c b)
73 (and (callable? c)
74 (eq? (callable-is_macro c) b)
75 c))
76
98cd78e4
NG
77(define (is-func? c) (callable-check c #f))
78(define (is-macro? c) (callable-check c #t))
79
80(define (hash-table-clone ht)
1c90c506 81 (list->hash-map (hash-fold (lambda (k v p) (cons k (cons v p))) '() ht)))
9f3c0995
NG
82
83(define-record-type box (fields val))
84
85(define (box o) (make-box o))
86(define (unbox o)
87 (if (box? o) (box-val o) o))
0d9fb576
NG
88
89(define* (list->hash-map lst #:optional (ht (make-hash-table)))
90 (cond
91 ((null? lst) ht)
92 (else
93 (let lp((next lst))
94 (cond
95 ((null? next) ht)
96 (else
97 (when (null? (cdr next))
98 (throw 'mal-error
99 (format #f "hash-map: '~a' lack of value" (car next))))
100 (let ((k (car next))
101 (v (cadr next)))
102 (hash-set! ht k v)
103 (lp (cddr next)))))))))