aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/common-lisp.lux
blob: 704dfe91eecb0be033550446cedb2b5d38ebf2f3 (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
(.module:
  lux
  (lux (control ["ex" exception #+ exception:]
                pipe
                [monad #+ do])
       (data [bit]
             [maybe]
             ["e" error #+ Error]
             [text "text/" Eq<Text>]
             text/format
             (coll [array]))
       [macro]
       [io #+ IO Process io]
       [host #+ class: interface: object]
       (world [file #+ File]))
  (luxc [lang]
        (lang [".L" variable #+ Register]
              (host ["_" common-lisp #+ Expression]))
        [".C" io]))

(do-template [<name>]
  [(exception: #export (<name> {message Text})
     message)]

  [No-Active-Module-Buffer]
  [Cannot-Execute]

  [No-Anchor]
  )

(host.import java/lang/String
  (getBytes [String] #try (Array byte)))

(host.import java/lang/CharSequence)

(host.import java/lang/Appendable
  (append [CharSequence] Appendable))

(host.import java/lang/StringBuilder
  (new [])
  (toString [] String))

(host.import org/armedbear/lisp/LispObject)

(host.import org/armedbear/lisp/Interpreter
  (#static getInstance [] Interpreter)
  (#static createInstance [] #? Interpreter)
  (eval [String] #try LispObject))

(type: #export Anchor [Text Register])

(type: #export Host
  {#context [Text Nat]
   #anchor (Maybe Anchor)
   #loader (-> Expression (Error Top))
   #interpreter (-> Expression (Error LispObject))
   #module-buffer (Maybe StringBuilder)
   #program-buffer StringBuilder})

(def: ____ (Interpreter::createInstance []))

(def: #export init
  (IO Host)
  (io (let [## interpreter ____
            _ (Interpreter::createInstance [])
            interpreter (Interpreter::getInstance [])]
        {#context ["" +0]
         #anchor #.None
         #loader (function (_ code)
                   (do e.Monad<Error>
                     [_ (Interpreter::eval [(_.expression code)] interpreter)]
                     (wrap [])))
         #interpreter (function (_ code)
                        (Interpreter::eval [(_.expression code)] interpreter))
         #module-buffer #.None
         #program-buffer (StringBuilder::new [])})))

(def: #export file-extension ".lisp")

(def: #export r-module-name Text (format "module" file-extension))

(def: #export init-module-buffer
  (Meta Top)
  (function (_ compiler)
    (#e.Success [(update@ #.host
                          (|>> (:! Host)
                               (set@ #module-buffer (#.Some (StringBuilder::new [])))
                               (:! Bottom))
                          compiler)
                 []])))

(def: #export (with-sub-context expr)
  (All [a] (-> (Meta a) (Meta [Text a])))
  (function (_ compiler)
    (let [old (:! Host (get@ #.host compiler))
          [old-name old-sub] (get@ #context old)
          new-name (format old-name "f___" (%i (nat-to-int old-sub)))]
      (case (expr (set@ #.host
                        (:! Bottom (set@ #context [new-name +0] old))
                        compiler))
        (#e.Success [compiler' output])
        (#e.Success [(update@ #.host
                              (|>> (:! Host)
                                   (set@ #context [old-name (n/inc old-sub)])
                                   (:! Bottom))
                              compiler')
                     [new-name output]])

        (#e.Error error)
        (#e.Error error)))))

(def: #export context
  (Meta Text)
  (function (_ compiler)
    (#e.Success [compiler
                 (|> (get@ #.host compiler)
                     (:! Host)
                     (get@ #context)
                     (let> [name sub]
                           name))])))

(def: #export (with-anchor anchor expr)
  (All [a] (-> Anchor (Meta a) (Meta a)))
  (function (_ compiler)
    (let [old (:! Host (get@ #.host compiler))]
      (case (expr (set@ #.host
                        (:! Bottom (set@ #anchor (#.Some anchor) old))
                        compiler))
        (#e.Success [compiler' output])
        (#e.Success [(update@ #.host
                              (|>> (:! Host)
                                   (set@ #anchor (get@ #anchor old))
                                   (:! Bottom))
                              compiler')
                     output])

        (#e.Error error)
        (#e.Error error)))))

(def: #export anchor
  (Meta Anchor)
  (function (_ compiler)
    (case (|> compiler (get@ #.host) (:! Host) (get@ #anchor))
      (#.Some anchor)
      (#e.Success [compiler anchor])

      #.None
      ((lang.throw No-Anchor "") compiler))))

(def: #export module-buffer
  (Meta StringBuilder)
  (function (_ compiler)
    (case (|> compiler (get@ #.host) (:! Host) (get@ #module-buffer))
      #.None
      ((lang.throw No-Active-Module-Buffer "") compiler)
      
      (#.Some module-buffer)
      (#e.Success [compiler module-buffer]))))

(def: #export program-buffer
  (Meta StringBuilder)
  (function (_ compiler)
    (#e.Success [compiler (|> compiler (get@ #.host) (:! Host) (get@ #program-buffer))])))

(do-template [<name> <field> <outputT>]
  [(def: (<name> code)
     (-> Expression (Meta <outputT>))
     (function (_ compiler)
       (let [runner (|> compiler (get@ #.host) (:! Host) (get@ <field>))]
         (case (runner code)
           (#e.Error error)
           ((lang.throw Cannot-Execute error) compiler)
           
           (#e.Success output)
           (#e.Success [compiler output])))))]

  [load!     #loader      Top]
  [interpret #interpreter LispObject]
  )

(def: #export variant-tag "LUX-VARIANT")

(def: #export unit Text "")

(def: #export (definition-name [module name])
  (-> Ident Text)
  (lang.normalize-name (format module "$" name)))

(def: #export (save code)
  (-> Expression (Meta Top))
  (do macro.Monad<Meta>
    [module-buffer module-buffer
     #let [_ (Appendable::append [(:! CharSequence (_.expression code))]
                                 module-buffer)]]
    (load! code)))

(def: #export run interpret)

(def: #export (save-module! target)
  (-> File (Meta (Process Top)))
  (do macro.Monad<Meta>
    [module macro.current-module-name
     module-buffer module-buffer
     program-buffer program-buffer
     #let [module-code (StringBuilder::toString [] module-buffer)
           _ (Appendable::append [(:! CharSequence (format module-code "\n"))]
                                 program-buffer)]]
    (wrap (ioC.write target
                     (format (lang.normalize-name module) "/" r-module-name)
                     (|> module-code
                         (String::getBytes ["UTF-8"])
                         e.assume)))))