aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/interpreter.lux
blob: a6e88941eacf6eaa8cefce9487475f60a2d043ae (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
226
227
228
229
230
231
(.using
 [library
  [lux (.full)
   [control
    [monad (.only Monad do)]
    ["[0]" try (.only Try)]
    ["ex" exception (.only exception:)]]
   [data
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format (.only format)]]]
   [type (.only sharing)
    ["[0]" check]]
   [compiler
    ["[0]" phase
     ["[0]" analysis
      ["[0]" module]
      ["[0]" type]]
     ["[0]" generation]
     ["[0]" directive (.only State+ Operation)
      ["[0]" total]]
     ["[0]" extension]]
    ["[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: .public (error [message 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 directive)
    (Operation anchor expression directive Any))
  (directive.lifted_analysis
   (do phase.monad
     [_ (module.create 0 ..module)]
     (analysis.set_current_module ..module))))

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

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

  (def: (interpret_expression code)
    (All (_ anchor expression directive)
      (-> Code <Interpretation>))
    (do [! phase.monad]
      [state (extension.lifted phase.state)
       .let [analyse (the [directive.#analysis directive.#phase] state)
             synthesize (the [directive.#synthesis directive.#phase] state)
             generate (the [directive.#generation directive.#phase] state)]
       [_ codeT codeA] (directive.lifted_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 (directive.lifted_synthesis
              (synthesize codeA))]
      (directive.lifted_generation
       (generation.with_buffer
         (do !
           [codeH (generate codeS)
            count generation.next
            codeV (generation.evaluate! (format "interpretation_" (%.nat count)) codeH)]
           (in [codeT codeV]))))))

  (def: (interpret configuration code)
    (All (_ anchor expression directive)
      (-> Configuration Code <Interpretation>))
    (function (_ state)
      (case (<| (phase.result' state)
                (sharing [anchor expression directive]
                         (State+ anchor expression directive)
                         state

                         <Interpretation>
                         (interpret_directive code)))
        {try.#Success [state' output]}
        {try.#Success [state' output]}

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

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

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

(with_expansions [<Context> (these (Context anchor expression directive))]
  (def: (read_eval_print context)
    (All (_ anchor expression directive)
      (-> <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 directive]
                                                    <Context>
                                                    context
                                                    
                                                    (State+ anchor expression directive)
                                                    (the #state context))]
                                 (<| (phase.result' state)
                                     ... TODO: Simplify ASAP
                                     (sharing [anchor expression directive]
                                              <Context>
                                              context
                                              
                                              (Operation anchor expression directive Text)
                                              (execute (the #configuration context) input))))]
      (in [(|> context
               (has #state state')
               (has #source source'))
           representation]))))

(def: .public (run! Monad<!> Console<!> platform configuration generation_bundle)
  (All (_ ! anchor expression directive)
    (-> (Monad !)
        (Console !) (Platform ! anchor expression directive)
        Configuration
        (generation.Bundle anchor expression directive)
        (! Any)))
  (do [! Monad<!>]
    [state (initialize Monad<!> Console<!> platform configuration)]
    (loop (again [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 (revised #source (add_line line) context))
            {try.#Success [context' representation]}
            (do !
              [_ (# Console<!> write representation)]
              (again context' #0))

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