blob: 85be602dae295e5bedeff393682f4fd0bdd7e665 (
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
(ns lang.interpreter
(:refer-clojure :exclude [eval resolve -' *'])
(:require [clojure.core.match :refer [match]]
(lang [util :as &util :refer [exec return* return fail fail*
repeat-m try-m try-all-m map-m
apply-m]]
[parser :as &parser]
[lexer :as &lexer]
[compiler :as &compiler])
:reload)
)
(declare eval-form)
;; (defonce _init_
;; (do (alter-var-root #'clojure.core/prn
;; (constantly #(.println System/out (apply pr-str %&))))))
(def <=' (fn [x] (fn [y] (<= x y))))
(def -' (fn [x] (fn [y] (- x y))))
(def +' (fn [x] (fn [y] (+ x y))))
(def *' (fn [x] (fn [y] (* x y))))
;; [Utils]
(def ^:private +state+
{:globals {"*" (fn [x] (fn [y] (* x y)))}
:stack {}
:forms '()})
(defn wrap [x]
(update-in +state+ [:forms] conj x))
(defn wrap-in [state x]
(assoc state :forms (list x)))
(defn resolve [ident]
(fn [state]
;; (prn 'resolve ident (get-in state [:globals ident]) (get-in state [:globals]))
(if-let [value (get-in state [:globals ident])]
(return* state value)
(fail* (str "Unrecognized identifier: " ident)))))
(defn define [name value]
(fn [state]
;; (prn 'define name value (assoc-in state [:globals name] value))
(return* (assoc-in state [:globals name] value) nil)))
(defn fn-call [f args]
;; (prn 'fn-call/call f args (first args) (second args))
;; (prn 'fn-call/output* (f (first args)))
;; (prn 'fn-call/output* ((f (first args)) (second args)))
(let [output (reduce #(%1 %2) f args)]
;; (prn 'fn-call/output output)
(return output)))
(defmacro ^:private defeval [name match return]
`(def ~name
(fn [state#]
(let [~'*token* (first (:forms state#))]
;; (prn '~name ~'*token*)
;; (prn '~name state#)
(match ~'*token*
~match
(let [output# (~return (update-in state# [:forms] rest))]
;; (prn "output#" output#)
output#)
_#
(do ;; (println "Unknown syntax: " (pr-str ~'*token*))
(fail* (str "Unknown syntax: " (pr-str ~'*token*)))))))))
(defeval eval-ident
[::&parser/ident ?ident]
(resolve ?ident))
(defeval eval-int
[::&parser/int ?int]
(return ?int))
(defeval eval-float
[::&parser/float ?float]
(return ?float))
(defeval eval-def
[::&parser/def ?form ?body]
(exec [;; :let [_ (prn 'eval-defdata ?form ?cases)]
=value (apply-m &compiler/compile-form (wrap *token*))
;; :let [_ (prn 'eval-def 'DONE =value)]
:let [=name (match ?form
[::&parser/fn-call [::&parser/ident ?name] ?args]
?name
[::&parser/ident ?name]
?name)
=value* (clojure.core/eval =value)
;; _ (prn '=value* =value*)
]
]
(define =name =value*)))
(defeval eval-defdata
[::&parser/defdata ?form & ?cases]
(exec [;; :let [_ (prn 'eval-defdata ?form ?cases)]
_ (apply-m &compiler/compile-form (wrap `[::&parser/defdata ~?form ~@?cases]))
;; :let [_ (prn 'eval-defdata 'DONE)]
]
(return nil)))
(defeval eval-get
[::&parser/get ?tag [::&parser/ident ?record]]
(exec [=record (resolve ?record)]
(return (get =record ?tag))))
(defeval eval-set
[::&parser/set ?tag ?value [::&parser/ident ?record]]
(exec [state &util/get-state
=value (apply-m eval-form (wrap-in state ?value))
=record (resolve ?record)]
(return (assoc =record ?tag =value))))
(defeval eval-remove
[::&parser/remove ?tag [::&parser/ident ?record]]
(exec [=record (resolve ?record)]
(return (dissoc =record ?tag))))
(defeval eval-fn-call
[::&parser/fn-call ?fn ?args]
(exec [state &util/get-state
=fn (apply-m eval-form (wrap-in state ?fn))
;; :let [_ (prn '=fn ?fn =fn)]
=args (map-m (fn [arg] (apply-m eval-form (wrap-in state arg)))
?args)
;; :let [_ (prn '=args =args)]
]
(fn-call =fn =args)))
(def eval-form
(try-all-m [eval-int
eval-float
eval-ident
eval-def
eval-defdata
eval-get
eval-set
eval-remove
eval-fn-call]))
(defn eval [text]
(match ((repeat-m eval-form) text)
[::&util/ok [?state ?forms]]
(if (empty? (:forms ?state))
?forms
(assert false (str "Unconsumed input: " ?state)))
[::&util/failure ?message]
(assert false ?message)))
(comment
(let [source-code (slurp "src/example/test1.lang")
tokens (&lexer/lex source-code)
;; _ (prn 'tokens tokens)
syntax (&parser/parse tokens)
;; _ (prn 'syntax syntax)
]
(eval (update-in +state+ [:forms] concat syntax)))
;; (defdata (List x)
;; (#Nil [])
;; (#Cons [x] (List x)))
;; (def (** base exp)
;; (fold * 1 (repeat exp base)))
;; Syntax for chars: #"a"
;; (set@ {#z 30} bar) (set@ {#z 30 #w "YOLO"} bar)
;; (remove@ [#x #y] bar)
;; (get@ [#x #y] bar)
)
|