Commit | Line | Data |
---|---|---|
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? |
4e6ae3b7 | 18 | string->keyword _keyword? _string? |
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 | |
85657c96 | 24 | is-func is-func? is-macro 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 | |
4e6ae3b7 DM |
48 | (define (_string? s) |
49 | (and (string? s) (not (_keyword? s)))) | |
50 | ||
0d9fb576 | 51 | (define-record-type mal-nil) |
99fa8f0c | 52 | |
0d9fb576 NG |
53 | (define nil (make-mal-nil)) |
54 | ||
55 | (define (_nil? obj) (mal-nil? obj)) | |
99fa8f0c | 56 | |
b40c6262 NG |
57 | (define (cond-true? obj) |
58 | (and (not (_nil? obj)) obj)) | |
59 | ||
99fa8f0c | 60 | (define-record-type atom (fields (mutable val))) |
e658ffd2 NG |
61 | |
62 | (define-record-type callable | |
63 | (fields | |
9f3c0995 NG |
64 | meta-info |
65 | (mutable unbox) | |
e658ffd2 NG |
66 | (mutable is_macro) |
67 | closure)) | |
68 | ||
9f3c0995 | 69 | (define (make-func closure) (make-callable nil #t #f closure)) |
2a80d367 | 70 | (define (make-anonymous-func closure) (make-callable nil #f #f closure)) |
e658ffd2 NG |
71 | |
72 | (define (callable-apply c arglst) | |
10bc1bce | 73 | (apply (callable-closure c) (if (callable-unbox c) (map unbox arglst) arglst))) |
e658ffd2 NG |
74 | |
75 | (define (callable-check c b) | |
76 | (and (callable? c) | |
77 | (eq? (callable-is_macro c) b) | |
78 | c)) | |
79 | ||
85657c96 VS |
80 | (define (is-func c) (callable-check c #f)) |
81 | (define (is-func? c) (and (is-func c) #t)) | |
82 | (define (is-macro c) (callable-check c #t)) | |
83 | (define (is-macro? c) (and (is-macro c) #t)) | |
98cd78e4 NG |
84 | |
85 | (define (hash-table-clone ht) | |
1c90c506 | 86 | (list->hash-map (hash-fold (lambda (k v p) (cons k (cons v p))) '() ht))) |
9f3c0995 NG |
87 | |
88 | (define-record-type box (fields val)) | |
89 | ||
90 | (define (box o) (make-box o)) | |
91 | (define (unbox o) | |
92 | (if (box? o) (box-val o) o)) | |
0d9fb576 NG |
93 | |
94 | (define* (list->hash-map lst #:optional (ht (make-hash-table))) | |
95 | (cond | |
96 | ((null? lst) ht) | |
97 | (else | |
98 | (let lp((next lst)) | |
99 | (cond | |
100 | ((null? next) ht) | |
101 | (else | |
102 | (when (null? (cdr next)) | |
103 | (throw 'mal-error | |
104 | (format #f "hash-map: '~a' lack of value" (car next)))) | |
105 | (let ((k (car next)) | |
106 | (v (cadr next))) | |
107 | (hash-set! ht k v) | |
108 | (lp (cddr next))))))))) |