-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforth-5.lsp
101 lines (101 loc) · 4.15 KB
/
forth-5.lsp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
;;
;; ËØÞÏ-2024
;;
;; àâò: ßçîâ Ð.
;;
(defun forth nil
(let ((stack nil) (dict nil) (stt nil) (tmp nil) (tmp1 nil) (tmp2 nil) (tmp3 nil))
(defun forget (word)
(setq dict (remove-if (lambda (x) (eq (car x) word)) dict))
)
(defun adddict (newword)
(forget (car newword))
(push newword dict)
)
(defun lookdict (word)
(let ((a (remove-if-not (lambda (x) (eq (car x) word)) dict)))
(if (null a)
(doword word)
(dolst (cdar a))
)
)
)
(defun doword (wrd)
(try
(cond
((eq wrd "quit") (printsline "=> Bye!") (prints "=> Stack: ") (printline stack) (prints "=> Dictionary: ") (printline dict) (setq stt "quit") (return nil))
((eq wrd "!stack") (prints "=> ") (printline stack))
((eq wrd "!dict") (prints "=> ") (printline dict))
((eq wrd "!get") (prints "=> Stack: ") (printline stack) (prints "=> Dictionary: ") (printline dict))
((eq wrd ".") (prints "=> ") (printline (pop stack)))
((eq wrd ",") (prints "=> ") (printline (car stack)))
((eq wrd "+") (push (+ (pop stack) (pop stack)) stack))
((eq wrd "*") (push (* (pop stack) (pop stack)) stack))
((eq wrd "^") (push (^ (cadr stack) (car stack)) stack) (pop stack) (pop stack))
((eq wrd "-") (push (* -1 (- (pop stack) (pop stack))) stack))
((eq wrd "/") (setq tmp1 (\ (cadr stack) (pop stack))) (pop stack) (push tmp1 stack))
((eq wrd "mod") (setq tmp1 (% (cadr stack) (car stack))) (pop stack) (pop stack) (push tmp1 stack))
((eq wrd "abs") (push (abs (pop stack)) stack))
((eq wrd "swap") (setq tmp1 (pop stack)) (setq tmp2 (pop stack)) (push tmp1 stack) (push tmp2 stack))
((eq wrd "dup") (push (car stack) stack))
((eq wrd "over") (push (cadr stack) stack))
((eq wrd "rot") (setq tmp1 (pop stack)) (setq tmp2 (pop stack)) (setq tmp3 (pop stack)) (push tmp2 stack) (push tmp1 stack) (push tmp3 stack))
((eq wrd "drop") (pop stack))
((eq wrd "<") (push (if (< (pop stack) (pop stack)) 0 1) stack))
((eq wrd "<=") (push (if (<= (pop stack) (pop stack)) 0 1) stack))
((eq wrd "=") (push (if (= (pop stack) (pop stack)) 0 1) stack))
((eq wrd ">=") (push (if (>= (pop stack) (pop stack)) 0 1) stack))
((eq wrd ">") (push (if (> (pop stack) (pop stack)) 0 1) stack))
((eq wrd "not") (push (if (= (pop stack) 0) 1 0) stack))
((eq wrd "and") (push (if (or (= (pop stack) 0) (= (pop stack) 0)) 0 1) stack))
((eq wrd "or") (push (if (and (= (pop stack) 0) (= (pop stack) 0)) 0 1) stack))
((eq wrd "dupe") (setq tmp1 (cadr stack)) (iter (for i from 2 to (pop stack)) (push tmp1 stack)))
((isNumber wrd) (push (input wrd) stack))
((null wrd) nil)
(t (print '?) (printsline wrd))
)
except (printsline (errormessage))
)
)
(defun dolst (txt)
(cond
((and (eq (car txt) "do") (position "loop" txt))
(let ((cnt (pop stack)) (lop (subseq txt 1 (position "loop" txt))))
(iter (for i from 1 to cnt) (dolst lop))
(dolst (subseq txt (position "loop" txt)))
)
)
((and (position "else" txt) (eq (car txt) "if"))
(let*
(
(cnd (pop stack)) (els (position "else" txt))
(thn (if (position "then" txt) (if (position "if" (cdr txt)) (if (< (position "then" (cdr txt)) (position "if" (cdr txt))) (position "then" txt) nil) (position "then" txt)) nil))
(ift (subseq txt 1 els)) (iff (if thn (subseq txt (+ 1 els) thn) nil))
)
(if (= 0 cnd) (if thn (progn (dolst iff) (dolst (subseq txt (+ 1 thn)))) (dolst (+ 1 els))) (progn (dolst ift) (if thn (dolst (subseq txt (+ 1 thn))) (dolst (subseq txt (+ 1 els))))))
)
)
((and (position ";" txt) (and (eq (car txt) ":") (> (length txt) 3)))
(setq tmp1 (position ";" txt))
(adddict (subseq txt 1 tmp1))
(when (< tmp1 (length txt)) (dolst (subseq txt (+ 1 tmp1))))
)
((and (eq (car txt) "forget") (> (length txt) 1))
(forget (cadr txt))
(when (cddr txt) (dolst (cddr txt)))
)
(t
(lookdict (car txt))
(when (cdr txt) (dolst (cdr txt)))
)
)
)
(printsline "Fortdoslav v3")
(loop
(let ((txt (read t)))
(dolst (strwords txt))
)
(when (eq stt "quit") (return nil))
)
)
)