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.
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.
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/>.
17 (export string-sub *eof* non-list?
18 string->keyword _keyword? _string?
19 nil _nil? list->hash-map
20 cond-true? make-anonymous-func
21 make-atom atom? atom-val atom-val-set!
22 make-callable callable? callable-is_macro
23 callable-is_macro-set! callable-closure
24 is-func? is-macro? make-func callable-apply
25 callable-unbox-set! callable-unbox
26 callable-meta-info hash-table-clone
28 (import (guile) (only (rnrs) define-record-type) (ice-9 regex) (ice-9 session)))
30 (define (non-list? x) (not (list? x)))
33 (define (string-sub str p1 p2)
34 (regexp-substitute/global #f p1 str 'pre p2 'post))
36 (define *eof* (call-with-input-string "" read))
38 (define (string->keyword str)
39 (when (not (string? str))
40 (throw 'mal-error (format #f "string->keyword: '~a' is not a string" str)))
41 (string-append "\u029e" str))
45 (> (string-length k) 0)
46 (char=? #\1236 (string-ref k 0))))
49 (and (string? s) (not (_keyword? s))))
51 (define-record-type mal-nil)
53 (define nil (make-mal-nil))
55 (define (_nil? obj) (mal-nil? obj))
57 (define (cond-true? obj)
58 (and (not (_nil? obj)) obj))
60 (define-record-type atom (fields (mutable val)))
62 (define-record-type callable
69 (define (make-func closure) (make-callable nil #t #f closure))
70 (define (make-anonymous-func closure) (make-callable nil #f #f closure))
72 (define (callable-apply c arglst)
73 (apply (callable-closure c) (if (callable-unbox c) (map unbox arglst) arglst)))
75 (define (callable-check c b)
77 (eq? (callable-is_macro c) b)
80 (define (is-func? c) (callable-check c #f))
81 (define (is-macro? c) (callable-check c #t))
83 (define (hash-table-clone ht)
84 (list->hash-map (hash-fold (lambda (k v p) (cons k (cons v p))) '() ht)))
86 (define-record-type box (fields val))
88 (define (box o) (make-box o))
90 (if (box? o) (box-val o) o))
92 (define* (list->hash-map lst #:optional (ht (make-hash-table)))
100 (when (null? (cdr next))
102 (format #f "hash-map: '~a' lack of value" (car next))))
106 (lp (cddr next)))))))))