-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcst-interpreter.lisp
More file actions
68 lines (54 loc) · 1.96 KB
/
cst-interpreter.lisp
File metadata and controls
68 lines (54 loc) · 1.96 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#|
This file is a part of cst-interpreter.
(c) 2019 Finn Völkel
Author: Finn Völkel (first.lastname@gmail.com)
|#
(in-package :cst-interpreter)
(defun repl (&optional (stream *standard-input*) (env (make-null-environment)))
(format *standard-output* ">")
(multiple-value-bind (res env)
(-> (eclector.concrete-syntax-tree:read stream) (cst-evaluate env))
(cst-print res)
(repl stream env)))
(define-condition not-implemented (error) ())
(define-condition illegal-function-call (error) ())
(define-condition unknown-symbol (error) ())
(defun cst-evaluate (cst &optional (env (make-null-environment)))
"Evaluates a CST in the given ENV."
(if (cst:atom cst)
(cst-eval-atom cst env)
(evaluate-compound-form cst env)))
(defun cst-eval-atom (cst env)
;;FIXME add symbols
(if (find (type-of (cst:raw cst)) '(string integer character)
:test #'subtypep)
(values cst env)
(values (lookup-var (symbol-name (cst:raw cst)) env) env)))
(defun evaluate-csts (csts env)
"Evaluates a list of csts."
(mapcar #'(lambda (cst) (cst-evaluate cst env)) csts))
(defun evaluate-compound-form (cst env)
(let ((first-raw (-> cst cst:first cst:raw)))
(when (eq first-raw 'set)
(return-from evaluate-compound-form (eval-set cst env)))
(unless (fun-in-env? (symbol-name first-raw) env)
(error 'undefined-function))))
(defun evaluate-function-form (cst)
(error 'not-implemented))
(defun evaluate-macro-form (cst)
(error 'not-implemented))
(defun evaluate-special-form (cst)
(error 'not-implemented))
(defun def-lambda (args body)
(cst:cons (cst:cst-from-expression 'lambda) (cst:cons args (cst:cstify (list body)))))
(defun cst-print (cst)
(format *standard-output* "~a~%" cst))
;; helper stuff
(defun cst-to-list (cst)
"Transforms a cst into a list of csts of its subexpressions."
(cond
((cst:null cst)
'())
((cst:consp cst)
(cons (cst:first cst) (cst-to-list (cst:rest cst))))
(T (list cst))))