aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/experiment/tool/interpreter.lux
blob: 2a3c3a1d4374504fec17c491dbaab1bc30215fff (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
218
219
220
221
222
223
224
225
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [control
    [monad (.only Monad do)]
    ["[0]" try (.only Try)]
    ["ex" exception (.only Exception)]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]]
   [type (.only sharing)
    ["[0]" check]]
   [compiler
    ["[0]" phase
     ["[0]" analysis
      ["[0]" module]
      ["[0]" type]]
     ["[0]" translation]
     ["[0]" declaration (.only State Operation)
      ["[0]" total]]]
    ["[0]" default
     ["[0]" syntax]
     ["[0]" platform (.only Platform)]
     ["[0]" init]]
    ["[0]" cli (.only Configuration)]]
   [world
    ["[0]" file (.only File)]
    ["[0]" console (.only Console)]]]]
 ["[0]" /type])

(exception.def .public (error message)
  (Exception Text)
  message)

(def .public module "<INTERPRETER>")

(def fresh_source
  Source
  [[..module 1 0] 0 ""])

(def (add_line line [where offset input])
  (-> Text Source Source)
  [where offset (format input text.new_line line)])

(def exit_command
  Text
  "exit")

(def welcome_message
  Text
  (format text.new_line
          "Welcome to the interpreter!" text.new_line
          "Type '" ..exit_command "' to leave." text.new_line
          text.new_line))

(def farewell_message
  Text
  "Till next time...")

(def enter_module
  (All (_ anchor expression declaration)
    (Operation anchor expression declaration Any))
  (declaration.of_analysis
   (do phase.monad
     [_ (module.create 0 ..module)]
     (analysis.set_current_module ..module))))

(def (initialize Monad<!> Console<!> platform configuration translation_bundle)
  (All (_ ! anchor expression declaration)
    (-> (Monad !)
        (Console !) (Platform ! anchor expression declaration)
        Configuration
        (translation.Bundle anchor expression declaration)
        (! (State anchor expression declaration))))
  (do Monad<!>
    [state (platform.initialize platform translation_bundle)
     state (platform.compile platform
                             (has cli.#module syntax.prelude configuration)
                             (has [declaration.#analysis declaration.#state
                                   .#info .#mode]
                                  {.#Interpreter}
                                  state))
     [state _] (of (the platform.#file_system platform)
                   lift (phase.result' state enter_module))
     _ (of Console<!> write ..welcome_message)]
    (in state)))

(with_expansions [<Interpretation> (these (Operation anchor expression declaration [Type Any]))]
  
  (def (interpret_declaration code)
    (All (_ anchor expression declaration)
      (-> Code <Interpretation>))
    (do phase.monad
      [_ (total.phase code)
       _ init.refresh]
      (in [Any []])))

  (def (interpret_expression code)
    (All (_ anchor expression declaration)
      (-> Code <Interpretation>))
    (do [! phase.monad]
      [state phase.state
       .let [analyse (the [declaration.#analysis declaration.#phase] state)
             synthesize (the [declaration.#synthesis declaration.#phase] state)
             translate (the [declaration.#translation declaration.#phase] state)]
       [_ codeT codeA] (declaration.of_analysis
                        (analysis.with_scope
                          (type.with_fresh_env
                            (do !
                              [[codeT codeA] (type.with_inference
                                               (analyse code))
                               codeT (type.with_env
                                       (check.clean codeT))]
                              (in [codeT codeA])))))
       codeS (declaration.of_synthesis
              (synthesize codeA))]
      (declaration.of_translation
       (translation.with_buffer
         (do !
           [codeH (translate codeS)
            count translation.next
            codeV (translation.evaluate! (format "interpretation_" (%.nat count)) codeH)]
           (in [codeT codeV]))))))

  (def (interpret configuration code)
    (All (_ anchor expression declaration)
      (-> Configuration Code <Interpretation>))
    (function (_ state)
      (when (<| (phase.result' state)
                (sharing [anchor expression declaration]
                  (is (State anchor expression declaration)
                      state)
                  (is <Interpretation>
                      (interpret_declaration code))))
        {try.#Success [state' output]}
        {try.#Success [state' output]}

        {try.#Failure error}
        (if (ex.match? total.not_a_declaration error)
          (<| (phase.result' state)
              (sharing [anchor expression declaration]
                (is (State anchor expression declaration)
                    state)
                (is <Interpretation>
                    (interpret_expression code))))
          {try.#Failure error}))))
  )

(def (execute configuration code)
  (All (_ anchor expression declaration)
    (-> Configuration Code (Operation anchor expression declaration Text)))
  (do phase.monad
    [[codeT codeV] (interpret configuration code)
     state phase.state]
    (in (/type.represent (the [declaration.#analysis declaration.#state] state)
                         codeT
                         codeV))))

(type (Context anchor expression declaration)
  (Record
   [#configuration Configuration
    #state (State anchor expression declaration)
    #source Source]))

(with_expansions [<Context> (these (Context anchor expression declaration))]
  (def (read_eval_print context)
    (All (_ anchor expression declaration)
      (-> <Context> (Try [<Context> Text])))
    (do try.monad
      [.let [[_where _offset _code] (the #source context)]
       [source' input] (syntax.parse ..module syntax.no_aliases (text.size _code) (the #source context))
       [state' representation] (let [... TODO: Simplify ASAP
                                     state (sharing [anchor expression declaration]
                                             (is <Context>
                                                 context)
                                             (is (State anchor expression declaration)
                                                 (the #state context)))]
                                 (<| (phase.result' state)
                                     ... TODO: Simplify ASAP
                                     (sharing [anchor expression declaration]
                                       (is <Context>
                                           context)
                                       (is (Operation anchor expression declaration Text)
                                           (execute (the #configuration context) input)))))]
      (in [(|> context
               (has #state state')
               (has #source source'))
           representation]))))

(def .public (run! Monad<!> Console<!> platform configuration translation_bundle)
  (All (_ ! anchor expression declaration)
    (-> (Monad !)
        (Console !) (Platform ! anchor expression declaration)
        Configuration
        (translation.Bundle anchor expression declaration)
        (! Any)))
  (do [! Monad<!>]
    [state (initialize Monad<!> Console<!> platform configuration)]
    (loop (again [context [#configuration configuration
                           #state state
                           #source ..fresh_source]
                  multi_line? false])
      (do !
        [_ (if multi_line?
             (of Console<!> write "  ")
             (of Console<!> write "> "))
         line (of Console<!> read_line)]
        (if (and (not multi_line?)
                 (text#= ..exit_command line))
          (of Console<!> write ..farewell_message)
          (when (read_eval_print (revised #source (add_line line) context))
            {try.#Success [context' representation]}
            (do !
              [_ (of Console<!> write representation)]
              (again context' false))

            {try.#Failure error}
            (if (ex.match? syntax.end_of_file error)
              (again context true)
              (exec (log! (ex.error ..error error))
                (again (has #source ..fresh_source context) false))))))
      )))