1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
mal/guile/types.scm

109 lines
3.3 KiB
Scheme
Raw Normal View History

2015-03-24 13:37:31 +03:00
;; Copyright (C) 2015
;; "Mu Lei" known as "NalaGinrut" <NalaGinrut@gmail.com>
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(library (types)
2015-04-02 21:39:02 +03:00
(export string-sub *eof* non-list?
2016-02-15 21:41:39 +03:00
string->keyword _keyword? _string?
nil _nil? list->hash-map
2015-04-06 21:39:04 +03:00
cond-true? make-anonymous-func
2015-04-03 21:27:19 +03:00
make-atom atom? atom-val atom-val-set!
2015-04-02 21:39:02 +03:00
make-callable callable? callable-is_macro
callable-is_macro-set! callable-closure
is-func is-func? is-macro is-macro? make-func callable-apply
2015-04-03 21:27:19 +03:00
callable-unbox-set! callable-unbox
callable-meta-info hash-table-clone
box? box unbox)
2015-04-06 21:39:04 +03:00
(import (guile) (only (rnrs) define-record-type) (ice-9 regex) (ice-9 session)))
2015-04-02 21:39:02 +03:00
(define (non-list? x) (not (list? x)))
2015-03-30 21:32:56 +03:00
2015-04-03 13:45:15 +03:00
2015-03-30 21:32:56 +03:00
(define (string-sub str p1 p2)
(regexp-substitute/global #f p1 str 'pre p2 'post))
2015-03-24 13:37:31 +03:00
(define *eof* (call-with-input-string "" read))
2015-04-03 13:45:15 +03:00
(define (string->keyword str)
(when (not (string? str))
(throw 'mal-error (format #f "string->keyword: '~a' is not a string" str)))
2015-03-24 13:37:31 +03:00
(string-append "\u029e" str))
2015-04-03 13:45:15 +03:00
(define (_keyword? k)
2015-10-09 19:14:51 +03:00
(and (string? k)
(> (string-length k) 0)
(char=? #\1236 (string-ref k 0))))
2015-04-03 13:45:15 +03:00
2016-02-15 21:41:39 +03:00
(define (_string? s)
(and (string? s) (not (_keyword? s))))
(define-record-type mal-nil)
2015-03-24 13:37:31 +03:00
(define nil (make-mal-nil))
(define (_nil? obj) (mal-nil? obj))
2015-03-24 13:37:31 +03:00
2015-03-30 21:32:56 +03:00
(define (cond-true? obj)
(and (not (_nil? obj)) obj))
2015-03-24 13:37:31 +03:00
(define-record-type atom (fields (mutable val)))
2015-04-02 21:39:02 +03:00
(define-record-type callable
(fields
2015-04-03 21:27:19 +03:00
meta-info
(mutable unbox)
2015-04-02 21:39:02 +03:00
(mutable is_macro)
closure))
2015-04-03 21:27:19 +03:00
(define (make-func closure) (make-callable nil #t #f closure))
2015-04-06 21:39:04 +03:00
(define (make-anonymous-func closure) (make-callable nil #f #f closure))
2015-04-02 21:39:02 +03:00
(define (callable-apply c arglst)
2015-04-06 11:20:00 +03:00
(apply (callable-closure c) (if (callable-unbox c) (map unbox arglst) arglst)))
2015-04-02 21:39:02 +03:00
(define (callable-check c b)
(and (callable? c)
(eq? (callable-is_macro c) b)
c))
(define (is-func c) (callable-check c #f))
(define (is-func? c) (and (is-func c) #t))
(define (is-macro c) (callable-check c #t))
(define (is-macro? c) (and (is-macro c) #t))
2015-04-03 13:45:15 +03:00
(define (hash-table-clone ht)
2015-04-06 00:08:48 +03:00
(list->hash-map (hash-fold (lambda (k v p) (cons k (cons v p))) '() ht)))
2015-04-03 21:27:19 +03:00
(define-record-type box (fields val))
(define (box o) (make-box o))
(define (unbox o)
(if (box? o) (box-val o) o))
(define* (list->hash-map lst #:optional (ht (make-hash-table)))
(cond
((null? lst) ht)
(else
(let lp((next lst))
(cond
((null? next) ht)
(else
(when (null? (cdr next))
(throw 'mal-error
(format #f "hash-map: '~a' lack of value" (car next))))
(let ((k (car next))
(v (cadr next)))
(hash-set! ht k v)
(lp (cddr next)))))))))