aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/generator.lux
blob: f64ca333ee4d88025caff5666b14afaf52979eb0 (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
(;module:
  lux
  (lux (control [monad #+ do])
       (concurrency ["T" task])
       (data ["e" error]
             [text "text/" Hash<Text>]
             text/format
             (coll [dict]))
       [meta]
       [host]
       [io]
       (world [file #+ File]))
  (luxc ["&" base]
        ["&;" io]
        ["&;" module]
        ["&;" parser]
        ["&;" host]
        ["&;" analyser]
        ["&;" analyser/common]
        ["&;" synthesizer]
        ["&;" eval]
        (generator ["&&;" runtime]
                   ["&&;" statement]
                   ["&&;" common]
                   ["&&;" expr]
                   ["&&;" eval])
        ))

(def: analyse
  (&;Analyser)
  (&analyser;analyser &eval;eval))

(def: (generate code)
  (-> Code (Meta Unit))
  (case code
    (^ [_ (#;Form (list [_ (#;Text "lux def")]
                        [_ (#;Symbol ["" def-name])]
                        valueC
                        metaC))])
    (do meta;Monad<Meta>
      [[_ metaA] (&;with-scope
                   (&;with-expected-type Code
                     (analyse metaC)))
       metaI (&&expr;generate (&synthesizer;synthesize metaA))
       metaV (&&eval;eval metaI)
       [_ valueT valueA] (&;with-scope
                           (if (meta;type? (:! Code metaV))
                             (&;with-expected-type Type
                               (do @
                                 [valueA (analyse valueC)]
                                 (wrap [Type valueA])))
                             (&analyser/common;with-unknown-type
                               (analyse valueC))))
       valueI (&&expr;generate (&synthesizer;synthesize valueA))
       _ (&;with-scope
           (&&statement;generate-def def-name valueT valueI metaI (:! Code metaV)))]
      (wrap []))

    (^ [_ (#;Form (list [_ (#;Text "lux program")]
                        [_ (#;Symbol ["" program-args])]
                        programC))])
    (do meta;Monad<Meta>
      [[_ programA] (&;with-scope
                      (&;with-expected-type (type (io;IO Unit))
                        (analyse programC)))
       programI (&&expr;generate (&synthesizer;synthesize programA))]
      (&&statement;generate-program program-args programI))

    _
    (&;fail (format "Unrecognized statement: " (%code code)))))

(def: (exhaust action)
  (All [a] (-> (Meta a) (Meta Unit)))
  (do meta;Monad<Meta>
    [result action]
    (exhaust action)))

(def: (ensure-new-module! file-hash module-name)
  (-> Nat Text (Meta Unit))
  (do meta;Monad<Meta>
    [module-exists? (meta;module-exists? module-name)
     _ (: (Meta Unit)
          (if module-exists?
            (&;fail (format "Cannot re-define a module: " module-name))
            (wrap [])))
     _ (&module;create file-hash module-name)]
    (wrap [])))

(def: prelude Text "lux")

(def: (with-active-compilation [module-name file-name source-code] action)
  (All [a] (-> [Text Text Text] (Meta a) (Meta a)))
  (do meta;Monad<Meta>
    [_ (ensure-new-module! (text/hash source-code) module-name)
     #let [init-cursor [file-name +0 +0]]
     output (&;with-source-code [init-cursor +0 source-code]
              action)
     _ (&module;flag-compiled! module-name)]
    (wrap output)))

(def: parse
  (Meta Code)
  (function [compiler]
    (case (&parser;parse (get@ #;source compiler))
      (#e;Error error)
      (#e;Error error)

      (#e;Success [source' output])
      (#e;Success [(set@ #;source source' compiler)
                   output]))))

(def: (generate-module source-dirs module-name target-dir compiler)
  (-> (List File) Text File Compiler (T;Task Compiler))
  (do T;Monad<Task>
    [_ (&io;prepare-module target-dir module-name)
     [file-name file-content] (&io;read-module source-dirs module-name)]
    (case (meta;run' compiler
                     (do meta;Monad<Meta>
                       [[artifacts _] (&&common;with-artifacts
                                        (with-active-compilation [module-name
                                                                  file-name
                                                                  file-content]
                                          (exhaust
                                           (do @
                                             [code parse]
                                             (generate code)))))]
                       (wrap artifacts)
                       ## (&module;generate-descriptor module-name)
                       ))
      (#e;Success [compiler artifacts ## module-descriptor
                   ])
      (do @
        [## _ (&io;write-module module-name module-descriptor)
         _ (monad;map @ (function [[class-name class-bytecode]]
                          (&io;write-file target-dir class-name class-bytecode))
                      (dict;entries artifacts))]
        (wrap compiler))
      
      (#e;Error error)
      (T;fail error))))

(def: init-cursor Cursor ["" +0 +0])

(def: #export init-type-context
  Type-Context
  {#;ex-counter +0
   #;var-counter +0
   #;var-bindings (list)})

(def: #export init-info
  Info
  {#;target  "JVM"
   #;version &;version
   #;mode    #;Build})

(def: #export (init-compiler host)
  (-> &&common;Host Compiler)
  {#;info            init-info
   #;source          [init-cursor +0 ""]
   #;cursor          init-cursor
   #;modules         (list)
   #;scopes          (list)
   #;type-context    init-type-context
   #;expected        #;None
   #;seed            +0
   #;scope-type-vars (list)
   #;host            (:! Void host)})

(def: #export (generate-program program target sources)
  (-> Text File (List File) (T;Task Unit))
  (do T;Monad<Task>
    [compiler (|> (case (&&runtime;generate (init-compiler (io;run &host;init-host)))
                    (#e;Error error)
                    (T;fail error)

                    (#e;Success [compiler [runtime-bc function-bc]])
                    (do @
                      [_ (&io;prepare-target target)
                       _ (&io;write-file target &&runtime;runtime-class runtime-bc)
                       _ (&io;write-file target &&runtime;function-class function-bc)]
                      (wrap compiler)))
                  (: (T;Task Compiler))
                  (:: @ map (generate-module sources prelude target)) (:: @ join)
                  (:: @ map (generate-module sources program target)) (:: @ join))
     #let [_ (log! "Compilation complete!")]]
    (wrap [])))