-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.lisp
77 lines (68 loc) · 1.9 KB
/
eval.lisp
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
69
70
71
72
73
74
75
76
77
; These are stacks that hold the last program and instruction pointer position.
(defvar code-string-q (list 'remove))
(defvar i-q (list 'remove))
; This function saves the program and instruction pointer position.
(defun save-code ()
(nconc code-string-q (list code-string))
(nconc i-q (list i))
)
; Function for popping value from stacks
(defun pop-top (l)
(reverse (cdr (reverse l)))
)
; Function that loads the last program and instruction pointer position.
(defun load-code ()
(setf code-string (first (last code-string-q)))
(setf i (first (last i-q)))
(setf code-string-q (pop-top code-string-q))
(setf i-q (pop-top i-q))
)
(defvar code-string "")
; Function for evaluating string as OCanada code.
(defun evaluate-no-errors (foo)
(declaim (sb-ext:muffle-conditions cl:warning))
(save-code)
(setf i 0)
(setf code-string (split-string foo))
(let ((x nil))
(loop
(when (>= i (length code-string)) (return))
(setf x (eval (interpret-commands i)))
(setf i (+ i 1))
)
(load-code)
x
)
)
; Evaluate but with error handling.
(defun evaluate (foo)
(handler-case (evaluate-no-errors foo)
(error (c)
(format t "Sorry buddy, something went wrong with your code!" c)
(terpri)
(format t "~a~%" c)
(exit)
)
)
)
; Function that interprets an OCanada program.
(defun interpret-no-errors (foo)
(declaim (sb-ext:muffle-conditions cl:warning))
(setf code-string (split-string (get-file-contents foo)))
(loop
(when (>= i (length code-string)) (return))
(eval (interpret-commands i))
(setf i (+ i 1))
)
)
; Interpret but with error handling.
(defun interpret (foo)
(handler-case (interpret-no-errors foo)
(error (c)
(format t "Sorry buddy, something went wrong with your code!" c)
(terpri)
(format t "~a~%" c)
(exit)
)
)
)