aboutsummaryrefslogtreecommitdiff
path: root/lux-c++/source/program.lux
blob: 4d669f5d50c50e0839c647d5525e30dd995b6a0c (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
... 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/.

... https://github.com/wlav/cppyy
(.require
 [library
  [lux (.except)
   [program (.only program)]
   ["[0]" ffi (.only import)]
   ["[0]" debug]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" maybe (.use "[1]#[0]" monad)]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]
    ["[0]" io (.only IO io)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" product]
    ["[0]" text (.only)
     ["%" \\format (.only format)]
     [encoding
      ["[0]" utf8]]]
    [collection
     ["[0]" array (.only Array)]]]
   [math
    [number
     ["n" nat]
     ["i" int]
     ["[0]" i64]]]
   ["[0]" world
    ["[0]" file]
    ["[0]" environment]]
   [meta
    [macro
     ["^" pattern]
     ["[0]" template]]
    [compiler
     [target
      ["_" c++ (.only)
       [":" type]]]
     [reference
      [variable (.only Register)]]
     [language
      [lux
       [program (.only Program)]
       [translation (.only Host)]
       [analysis
        [macro (.only Expander)]]
       ["[0]" phase (.only Operation Phase)
        ["[0]" extension (.only Extender Handler)
         ["[0]" analysis
          ["[1]" js]]
         ["[0]" translation
          ["[1]" js]]]
        [translation
         ["[0]" reference]
         ["[0]" c++ (.only)
          ["[0]" runtime]
          ["[0]" type]
          ["[0]" primitive]
          ["[0]" complex]]]]]]
     [default
      ["[0]" platform (.only Platform)]]
     [meta
      ["[0]" cli]
      ["[0]" context]
      [archive (.only Archive)
       ["[0]" unit]]
      ["[0]" packager
       ["[1]" script]]]]]]]
 [program
  ["[0]" compositor]])

(import cppyy
  "[1]::[0]"
  ("static" cppdef [Text] "io" Bit)
  ("static" gbl (ffi.Object Any)))

... (def standard_out (_.global [_.standard "cout"] (list)))
... (def \n (_.global [_.standard "endl"] (list)))

... (def (print it)
...   (-> _.Expression
...       _.Statement)
...   (|> standard_out
...       (_.<< it)
...       (_.<< \n)
...       _.;))

... (with_expansions [<say_hello> "say_hello"]
...   (def _
...     (program []
...       (do io.monad
...         [? (cppyy::cppdef [(_.code (all _.then
...                                         runtime.declaration

...                                         (_.include "iostream")
...                                         (_.function (_.local <say_hello>)
...                                           (list)
...                                           (list)
...                                           :.void
...                                           (all _.then
...                                                (print (_.deref (runtime.host_value type.bit (runtime.simple type.bit (primitive.bit true)))))
...                                                (print (_.deref (runtime.host_value type.i64 (runtime.simple type.i64 (primitive.i64 +123)))))
...                                                (print (_.deref (runtime.host_value type.f64 (runtime.simple type.f64 (primitive.f64 -456.789)))))
...                                                ... (print (_.deref (runtime.host_value type.text (runtime.simple type.text (primitive.text "YOLO")))))
...                                                ))))])
...          .let [_ (debug.log! (%.format "BEFORE " (%.bit ?)))]
...          global (cppyy::gbl)
...          .let [say_hello (as ffi.Function
...                              (.python_object_get# <say_hello> global))
...                _ (debug.log! "AFTER")
...                _ (.python_apply# say_hello [])]]
...         (in (debug.log! "Hello, C++"))))))

(ffi.import (getattr [(ffi.Object Any) Text] Any))

(def host
  (IO (Host runtime.Value runtime.Declaration))
  (io (let [\n\t (%.format text.\n text.\t)
            evaluate! (is (-> unit.ID [(Maybe unit.ID) runtime.Value] (Try Any))
                          (function (evaluate! context [_ input])
                            (let [global (reference.artifact context)
                                  definition (_.constant (_.local global) type.value input)]
                              (if (io.run! (cppyy::cppdef (_.code definition)))
                                {try.#Success (getattr (io.run! (cppyy::gbl)) global)}
                                {try.#Failure "Cannot evaluate!"}))))
            execute! (is (-> runtime.Declaration
                             (Try Any))
                         (function (execute! input)
                           (if (io.run! (cppyy::cppdef (_.code input)))
                             {try.#Success []}
                             {try.#Failure "Cannot execute!"})))]
        (is (Host runtime.Value runtime.Declaration)
            (implementation
             (def evaluate evaluate!)
             (def execute execute!)
             (def (define context custom [@def input])
               (let [global (maybe.else (reference.artifact context)
                                        custom)
                     @global (_.local global)]
                 (do try.monad
                   [.let [definition (_.constant @global type.value input)]
                    _ (execute! definition)
                    value (evaluate! context [@def @global])]
                   (in [global value definition]))))

             (def (ingest context content)
               (|> content
                   (of utf8.codec decoded)
                   try.trusted
                   (as runtime.Declaration)))

             (def (re_learn context custom content)
               (execute! content))
             
             (def (re_load context custom content)
               (do try.monad
                 [_ (execute! content)]
                 (evaluate! context [{.#None} (_.local (reference.artifact context))]))))))))

(def (phase_wrapper _)
  phase.Wrapper
  (undefined))

(def .public platform
  (IO [runtime.Host
       (Platform runtime.Anchor runtime.Value runtime.Declaration)])
  (do io.monad
    [host ..host]
    (in [host
         [platform.#file_system (file.async file.default)
          platform.#host host

          platform.#phase c++.translation
          platform.#runtime runtime.translation

          platform.#phase_wrapper ..phase_wrapper
          platform.#write (|>> _.code (of utf8.codec encoded))]])))

(def (extender _)
  Extender
  (undefined))

(def reference_system
  (reference.System runtime.Value)
  (implementation
   (def constant' _.local)
   (def variable' _.local)))

(def (expander macro inputs lux)
  Expander
  (undefined))

(def (c++_program name it)
  (Program runtime.Value runtime.Declaration)
  (undefined))

(def (declare_success! _)
  (-> Any (Async Any))
  (async.future (of environment.default exit +0)))

(def _
  (program [service cli.service]
    (let [context (context.jvm (cli.target service))]
      (exec
        (do async.monad
          [[host platform] (async.future ..platform)
           _ (compositor.compiler (|>> (as platform.Custom))
                                  context
                                  ..expander
                                  extension.empty
                                  (io.io platform)
                                  extension.empty
                                  extension.empty
                                  ..c++_program
                                  (reference.constant reference_system)
                                  ..extender
                                  service
                                  [(packager.package (is runtime.Declaration (_.manual ""))
                                                     _.code
                                                     _.also
                                                     (_.namespace "lux_program"))
                                   "program.cpp"])]
          (..declare_success! []))
        (io.io [])))))