2015-03-30 21:34:40 +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 (core)
|
2015-03-31 20:41:58 +03:00
|
|
|
(export core.ns ->list)
|
|
|
|
(import (guile) (rnrs) (types) (reader) (printer) (ice-9 match)))
|
|
|
|
|
|
|
|
(define (->list o) ((if (vector? o) vector->list identity) o))
|
2015-03-30 21:34:40 +03:00
|
|
|
|
|
|
|
(define (_count obj)
|
|
|
|
(cond
|
|
|
|
((_nil? obj) 0)
|
|
|
|
((vector? obj) (vector-length obj))
|
|
|
|
(else (length obj))))
|
|
|
|
|
|
|
|
(define (_empty? obj) (zero? (_count obj)))
|
|
|
|
|
2015-03-31 06:53:37 +03:00
|
|
|
;; Well, strange spec...
|
|
|
|
(define (_equal? o1 o2)
|
2015-03-31 20:41:58 +03:00
|
|
|
(equal? (->list o1) (->list o2)))
|
2015-03-31 06:53:37 +03:00
|
|
|
|
2015-03-30 21:34:40 +03:00
|
|
|
(define (pr-str . args)
|
|
|
|
(define (pr x) (pr_str x #t))
|
|
|
|
(string-join (map pr args) " "))
|
|
|
|
|
2015-03-31 06:53:37 +03:00
|
|
|
(define (str . args)
|
|
|
|
(define (pr x) (pr_str x #f))
|
|
|
|
(string-join (map pr args) ""))
|
|
|
|
|
|
|
|
(define (prn . args)
|
|
|
|
(format #t "~a~%" (apply pr-str args))
|
|
|
|
nil)
|
|
|
|
|
|
|
|
(define (println . args)
|
|
|
|
(define (pr x) (pr_str x #f))
|
|
|
|
(format #t "~{~a~^ ~}~%" (map pr args) " ")
|
|
|
|
nil)
|
|
|
|
|
2015-03-31 18:39:59 +03:00
|
|
|
(define (slurp filename)
|
|
|
|
(when (not (file-exists? filename))
|
|
|
|
(throw 'mal-error "File/dir doesn't exist" filename))
|
|
|
|
(call-with-input-file filename get-string-all))
|
|
|
|
|
2015-03-31 20:41:58 +03:00
|
|
|
(define (_cons x y)
|
|
|
|
(cons x (->list y)))
|
|
|
|
|
|
|
|
(define (concat . args)
|
|
|
|
(apply append (map ->list args)))
|
|
|
|
|
2015-04-02 21:39:02 +03:00
|
|
|
(define (_nth lst n)
|
|
|
|
(define ll (->list lst))
|
|
|
|
(when (>= n (length ll))
|
|
|
|
(throw 'mal-error "nth: index out of range"))
|
|
|
|
(list-ref ll n))
|
|
|
|
|
|
|
|
(define (_first lst)
|
|
|
|
(define ll (->list lst))
|
|
|
|
(if (null? ll)
|
|
|
|
nil
|
|
|
|
(car ll)))
|
|
|
|
|
|
|
|
(define (_rest lst)
|
|
|
|
(define ll (->list lst))
|
|
|
|
(if (null? ll)
|
|
|
|
'()
|
|
|
|
(cdr ll)))
|
|
|
|
|
2015-03-30 21:34:40 +03:00
|
|
|
(define *primitives*
|
2015-03-31 18:39:59 +03:00
|
|
|
`((list ,list)
|
|
|
|
(list? ,list?)
|
|
|
|
(empty? ,_empty?)
|
|
|
|
(count ,_count)
|
|
|
|
(= ,_equal?)
|
|
|
|
(< ,<)
|
|
|
|
(<= ,<=)
|
|
|
|
(> ,>)
|
|
|
|
(>= ,>=)
|
|
|
|
(+ ,+)
|
|
|
|
(- ,-)
|
|
|
|
(* ,*)
|
|
|
|
(/ ,/)
|
|
|
|
(not ,not)
|
|
|
|
(pr-str ,pr-str)
|
|
|
|
(str ,str)
|
|
|
|
(prn ,prn)
|
|
|
|
(println ,println)
|
|
|
|
(read-string ,read_str)
|
|
|
|
(slurp ,slurp)
|
2015-03-31 20:41:58 +03:00
|
|
|
(cons ,_cons)
|
|
|
|
(concat ,concat)
|
2015-04-02 21:39:02 +03:00
|
|
|
(nth ,_nth)
|
|
|
|
(first ,_first)
|
|
|
|
(rest ,_rest)
|
2015-03-30 21:34:40 +03:00
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
;; Well, we have to rename it to this strange name...
|
|
|
|
(define core.ns *primitives*)
|