Merge pull request #5 from zmower/literal-empty-list
[jackhill/mal.git] / guile / step5_tco.scm
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 (import (readline) (reader) (printer) (ice-9 match) (srfi srfi-43)
17 (srfi srfi-1) (ice-9 receive) (env) (core) (types))
18
19 (define *toplevel*
20 (receive (b e) (unzip2 core.ns)
21 (make-Env #:binds b #:exprs e)))
22
23 (define (READ)
24 (read_str (_readline "user> ")))
25
26 (define (eval_ast ast env)
27 (define (_eval x) (EVAL x env))
28 (match ast
29 ((? _nil? obj) obj)
30 ((? symbol? sym) (env-has sym env))
31 ((? list? lst) (map _eval lst))
32 ((? vector? vec) (vector-map (lambda (i x) (_eval x)) vec))
33 ((? hash-table? ht)
34 (hash-for-each (lambda (k v) (hash-set! ht k (_eval v))) ht)
35 ht)
36 (else ast)))
37
38 (define (eval_func ast env)
39 (define expr (eval_ast ast env))
40 (match expr
41 (((? procedure? proc) args ...)
42 (apply proc args))
43 (else (throw 'mal-error (format #f "'~a' not found" (car expr))))))
44
45 (define (eval_seq ast env)
46 (cond
47 ((null? ast) nil)
48 ((null? (cdr ast)) (EVAL (car ast) env))
49 (else
50 (EVAL (car ast) env)
51 (eval_seq (cdr ast) env))))
52
53 (define (EVAL ast env)
54 (define (->list kvs) ((if (vector? kvs) vector->list identity) kvs))
55 (define (%unzip2 kvs)
56 (let lp((next kvs) (k '()) (v '()))
57 (cond
58 ;; NOTE: reverse is very important here!
59 ((null? next) (values (reverse k) (reverse v)))
60 ((null? (cdr next)) (throw 'mal-error "let*: Invalid binding form" kvs))
61 (else (lp (cddr next) (cons (car next) k) (cons (cadr next) v))))))
62 ;; NOTE: I wish I can use (while #t ...) for that, but this is not Lispy, which means
63 ;; it'll bring some trouble in control flow. We have to use continuations to return
64 ;; and use non-standard `break' feature. In a word, not elegant at all.
65 ;; The named let loop is natural for Scheme, but it looks a bit cheating. But NO!
66 ;; Such kind of loop is actually `while loop' in Scheme, I don't take advantage of
67 ;; TCO in Scheme to implement TCO, but it's the same principle with normal loop.
68 ;; If you're Lispy enough, there's no recursive at all while you saw named let loop.
69 (let tco-loop((ast ast) (env env))
70 (match ast
71 (() ast)
72 (('def! k v) ((env 'set) k (EVAL v env)))
73 (('let* kvs body)
74 (let* ((new-env (make-Env #:outer env))
75 (setter (lambda (k v) ((new-env 'set) k (EVAL v new-env)))))
76 (receive (keys vals) (%unzip2 (->list kvs))
77 (for-each setter keys vals))
78 (tco-loop body new-env)))
79 (('do rest ...)
80 (cond
81 ((null? rest) (throw 'mal-error "do: Invalid form!" rest))
82 ((= 1 (length rest)) (tco-loop (car rest) env))
83 (else
84 (let ((mexpr (take rest (1- (length rest))))
85 (tail-call (car (take-right rest 1))))
86 (eval_seq mexpr env)
87 (tco-loop tail-call env)))))
88 (('if cnd thn els ...)
89 (cond
90 ((and (not (null? els)) (not (null? (cdr els))))
91 ;; Invalid `if' form
92 (throw 'mal-error "if: failed to match any pattern in form " ast))
93 ((cond-true? (EVAL cnd env)) (tco-loop thn env))
94 (else (if (null? els) nil (tco-loop (car els) env)))))
95 (('fn* params body ...) ; function definition
96 (lambda args
97 (let ((nenv (make-Env #:outer env #:binds (->list params) #:exprs args)))
98 (cond
99 ((null? body) (throw 'mal-error "fn*: bad lambda in form " ast))
100 ((= 1 (length body)) (tco-loop (car body) nenv))
101 (else
102 (let ((mexpr (take body (1- (length body))))
103 (tail-call (car (take-right body 1))))
104 (eval_seq mexpr nenv)
105 (tco-loop tail-call nenv)))))))
106 ((? list?) (eval_func ast env)) ; function calling
107 (else (eval_ast ast env)))))
108
109 (define (PRINT exp)
110 (and (not (eof-object? exp))
111 (format #t "~a~%" (pr_str exp #t))))
112
113 (define (LOOP continue?)
114 (and continue? (REPL)))
115
116 (define (REPL)
117 (LOOP
118 (catch 'mal-error
119 (lambda () (PRINT (EVAL (READ) *toplevel*)))
120 (lambda (k . e)
121 (if (string=? (car e) "blank line")
122 (display "")
123 (format #t "Error: ~a~%" (car e)))))))
124
125 ;; NOTE: we have to reduce stack size to pass step5 test
126 ((@ (system vm vm) call-with-stack-overflow-handler)
127 1024
128 (lambda () (REPL))
129 (lambda k (throw 'mal-error "stack overflow")))
130