blob: 2feb4b81c838b966f971d6f7f50b4d577b4f13b4 (
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
(.module:
[lux #*
[control
[monad (#+ Monad do)]
["ex" exception (#+ exception:)]]
[data
["." error (#+ Error)]
[text ("text/." Equivalence<Text>)
format]]
[type (#+ :share)
["." check]]
[compiler
["." cli (#+ Configuration)]
["." default (#+ Platform)
["." syntax]
["." init]
["." phase
["." analysis
["." module]
["." type]]
["." translation]
["." statement (#+ State+ Operation)
["." total]]
["." extension]]]]
[world
["." file (#+ File)]
["." console (#+ Console)]]]
["." /type])
(exception: #export (error {message Text})
message)
(def: #export module "<INTERPRETER>")
(def: fresh-source Source [[..module 1 0] 0 ""])
(def: (add-line line [where offset input])
(-> Text Source Source)
[where offset (format input "\n" line)])
(def: exit-command Text "exit")
(def: welcome-message
Text
(format "\n"
"Welcome to the interpreter!" "\n"
"Type \"exit\" to leave." "\n"
"\n"))
(def: farewell-message
Text
"Till next time...")
(def: enter-module
(All [anchor expression statement]
(Operation anchor expression statement Any))
(statement.lift-analysis
(do phase.Monad<Operation>
[_ (module.create 0 ..module)]
(analysis.set-current-module ..module))))
(def: (initialize Monad<!> Console<!> platform configuration)
(All [! anchor expression statement]
(-> (Monad !)
(Console !) (Platform ! anchor expression statement)
Configuration
(! (State+ anchor expression statement))))
(do Monad<!>
[state (default.initialize platform configuration)
state (default.compile-module platform
(set@ #cli.module default.prelude configuration)
(set@ [#extension.state
#statement.analysis #statement.state
#extension.state
#.info #.mode]
#.Interpreter
state))
[state _] (:: (get@ #default.file-system platform)
lift (phase.run' state enter-module))
_ (:: Console<!> write ..welcome-message)]
(wrap state)))
(with-expansions [<Interpretation> (as-is (Operation anchor expression statement [Type Any]))]
(def: (interpret-statement code)
(All [anchor expression statement]
(-> Code <Interpretation>))
(do phase.Monad<Operation>
[_ (total.phase code)
_ init.refresh]
(wrap [Any []])))
(def: (interpret-expression code)
(All [anchor expression statement]
(-> Code <Interpretation>))
(do phase.Monad<Operation>
[state (extension.lift phase.get-state)
#let [analyse (get@ [#statement.analysis #statement.phase] state)
synthesize (get@ [#statement.synthesis #statement.phase] state)
translate (get@ [#statement.translation #statement.phase] state)]
[_ codeT codeA] (statement.lift-analysis
(analysis.with-scope
(type.with-fresh-env
(do @
[[codeT codeA] (type.with-inference
(analyse code))
codeT (type.with-env
(check.clean codeT))]
(wrap [codeT codeA])))))
codeS (statement.lift-synthesis
(synthesize codeA))]
(statement.lift-translation
(translation.with-buffer
(do @
[codeH (translate codeS)
count translation.next
codeV (translation.evaluate! (format "interpretation_" (%n count)) codeH)]
(wrap [codeT codeV]))))))
(def: (interpret configuration code)
(All [anchor expression statement]
(-> Configuration Code <Interpretation>))
(function (_ state)
(case (<| (phase.run' state)
(:share [anchor expression statement]
{(State+ anchor expression statement)
state}
{<Interpretation>
(interpret-statement code)}))
(#error.Success [state' output])
(#error.Success [state' output])
(#error.Error error)
(if (ex.match? total.unrecognized-statement error)
(<| (phase.run' state)
(:share [anchor expression statement]
{(State+ anchor expression statement)
state}
{<Interpretation>
(interpret-expression code)}))
(#error.Error error)))))
)
(def: (execute configuration code)
(All [anchor expression statement]
(-> Configuration Code (Operation anchor expression statement Text)))
(do phase.Monad<Operation>
[[codeT codeV] (interpret configuration code)
state phase.get-state]
(wrap (/type.represent (get@ [#extension.state
#statement.analysis #statement.state
#extension.state]
state)
codeT
codeV))))
(type: (Context anchor expression statement)
{#configuration Configuration
#state (State+ anchor expression statement)
#source Source})
(with-expansions [<Context> (as-is (Context anchor expression statement))]
(def: (read-eval-print context)
(All [anchor expression statement]
(-> <Context> (Error [<Context> Text])))
(do error.Monad<Error>
[[source' input] (syntax.read ..module syntax.no-aliases (get@ #source context))
[state' representation] (let [## TODO: Simplify ASAP
state (:share [anchor expression statement]
{<Context>
context}
{(State+ anchor expression statement)
(get@ #state context)})]
(<| (phase.run' state)
## TODO: Simplify ASAP
(:share [anchor expression statement]
{<Context>
context}
{(Operation anchor expression statement Text)
(execute (get@ #configuration context) input)})))]
(wrap [(|> context
(set@ #state state')
(set@ #source source'))
representation]))))
(def: #export (run Monad<!> Console<!> platform configuration)
(All [! anchor expression statement]
(-> (Monad !)
(Console !) (Platform ! anchor expression statement)
Configuration
(! Any)))
(do Monad<!>
[state (initialize Monad<!> Console<!> platform configuration)]
(loop [context {#configuration configuration
#state state
#source ..fresh-source}
multi-line? #0]
(do @
[_ (if multi-line?
(:: Console<!> write " ")
(:: Console<!> write "> "))
line (:: Console<!> read-line)]
(if (and (not multi-line?)
(text/= ..exit-command line))
(:: Console<!> write ..farewell-message)
(case (read-eval-print (update@ #source (add-line line) context))
(#error.Success [context' representation])
(do @
[_ (:: Console<!> write representation)]
(recur context' #0))
(#error.Error error)
(if (ex.match? syntax.end-of-file error)
(recur context #1)
(exec (log! (ex.construct ..error error))
(recur (set@ #source ..fresh-source context) #0))))))
)))
|