summaryrefslogtreecommitdiff
path: root/tests/stdlib.lisp
blob: e23364c45b68edefb1fb932a7375dd9460988364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(define cadr (x) (car (cdr x)))
(define caddr (x) (car (cdr (cdr x))))
(define cadddr (x) (car (cdr (cdr (cdr x)))))

(define not (x) (if x 0 1))
(define or (x y) (if x 1 (if y 1 0)))
(define and (x y) (if x (if y 1 0) 0))

(define YY (recur) (lambda (f) (lambda (a) (f ((recur recur) f) a))))
(define Y (YY YY))

(define for (start end f)
    (if (<= start end)
        (do (f start) (for (+ start 1) end f))
        '()))

(define take-while (f l)
    (cond
        (null? l)
            l
        (f (car l))
            (cons (car l) (take-while f (cdr l)))
        '()))

(define drop-while (f l)
    (cond
        (null? l)
            l
        (f (car l))
            (drop-while f (cdr l))
        l))

(define stdin (sys-stdin))
(define stdout (sys-stdout))
(define stderr (sys-stderr))

(define with-open-file (path mode f)
    (let ((fid   (sys-open-file mode path))
          (value (f fid)))
        (do (sys-close-file fid) value)))

(define read-until (fid predicate)
    ((lambdarec rec (s)
        (let ((ch (sys-get-char fid)))
            (if (predicate ch) s (rec (concat s ch)))))
     ""))

(define read-until-eof (fid)
    (read-until fid null?))

(define read-file (path)
    (with-open-file path 0 read-until-eof))

(define read-line (fid)
    (read-until fid (lambda (ch) (or (= ch "\n") (null? ch)))))