Repository navigation
Expand file tree
/
Copy pathlmx.cpp
More file actions
224 lines (201 loc) · 7.17 KB
/
Copy pathlmx.cpp
File metadata and controls
224 lines (201 loc) · 7.17 KB
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
#include "include/lmx.h"
#include "include/lmx_expr.h"
#include "compiler/compiler.hpp"
#include "compiler/ast/ast_printer.hpp"
#include "compiler/hir/type_checker.hpp"
#include "compiler/parser.hpp"
#include "compiler/lexer.hpp"
#include "runtime/vm.hpp"
#include "runtime/object/expr_obj.hpp"
#include "runtime/object/StringObj.hpp"
#include "runtime/object/adt.hpp"
#include "runtime/object/complex.hpp"
#include "runtime/object/array.hpp"
#include "runtime/object/vector.hpp"
#include "runtime/object/matrix.hpp"
#include "runtime/object/table.hpp"
#include "runtime/object/random.hpp"
#include "runtime/object/quantity.hpp"
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <fstream>
#include <limits>
#include <map>
#include <optional>
#include <utility>
#include <cctype>
#include "compiler/mir/mir_printer.hpp"
#include "lmmc/numeric.h"
#include "lmmc/complex.h"
#include "lmmc/init.h"
#include "lmmc/dense.h"
#include "lmmc/stats.h"
#include "lmmc/random.h"
#include "lmmc/stdlib.h"
#include "complex_analysis.hpp"
#include "numerical_integration.hpp"
#include "series_engine.hpp"
#include "symbolic_geometry.hpp"
#include "symbolic_implicit_diff.hpp"
#include "transform_engine.hpp"
#include "vector_calculus.hpp"
LmState global_state;
#if !NDEBUG
namespace {
bool debug_dump_enabled() noexcept {
const char* value = std::getenv("LMX_DEBUG_DUMP");
return value && value[0] != '\0' && value[0] != '0';
}
} // namespace
#endif
extern "C" LM_API int lmx_printf(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
const int result = vprintf(fmt ? fmt : "", args);
va_end(args);
return result;
}
LM_API LmState* lmx_newState() {
auto* node = static_cast<LmLinkedNode *>(malloc(sizeof(LmLinkedNode)));
memset(node, 0, sizeof(LmLinkedNode));
global_state = LmState {.n = node, .vm = nullptr};
return &global_state;
}
LM_API void lmx_deleteState(const LmState* state) {
delete reinterpret_cast<lmx::runtime::LaminaVM*>(state->vm);
const LmLinkedNode* node = state->n;
while (node != nullptr) {
if (node->ptr != nullptr) {
static_cast<lmx::runtime::CodeModuleObj*>(node->ptr)->~CodeModuleObj();
free(node->ptr);
}
const auto last = node->last;
free((void*)node);
node = last;
}
}
static LmLinkedNode* newLickedNode(LmLinkedNode* old) {
auto* node = static_cast<LmLinkedNode *>(malloc(sizeof(LmLinkedNode)));
node->last = old;
return node;
}
static void lmx_state_addNode(LmState* state, void* ptr) {
state->n = newLickedNode(state->n);
state->n->ptr = ptr;
}
static LmModule* lmx_newCodeModule(LmState* state, std::vector<uint8_t>&& binary) {
const auto storage = malloc(sizeof(lmx::runtime::CodeModuleObj));
if (storage == nullptr) return nullptr;
try {
new (storage) lmx::runtime::CodeModuleObj(std::move(binary));
} catch (...) {
free(storage);
throw;
}
lmx_state_addNode(state, storage);
return static_cast<LmModule*>(storage);
}
void lmx_printASTFromString(LmState*, FILE *file, const char *code, const char* name) {
lmx::Compiler compiler;
lmx::CompileResult result;
if (!compiler.compile_source(code, name, result, lmx::CompileStage::Semantic)) return;
const auto ast_str = lmx::AstPrinter::print(*result.module);
if (fwrite(ast_str.c_str(), 1, ast_str.length(), file) != ast_str.length()) {
fprintf(stderr, "Error writing AST to file\n");
}
}
void lmx_printASTFromFile(LmState*, FILE *file, const char *name) {
lmx::Compiler compiler;
lmx::CompileResult result;
if (!compiler.compile_file(name, result, lmx::CompileStage::Semantic)) return;
auto str = lmx::AstPrinter::print(*result.module);
if (fwrite(str.c_str(), 1, str.length(), file) != str.length()) {
fprintf(stderr, "Error writing AST to file\n");
}
}
void lmx_printMIRFromString(LmState*, FILE *file, const char *code, const char *name) {
lmx::Compiler compiler;
lmx::CompileResult result;
if (!compiler.compile_source(code, name, result, lmx::CompileStage::Mir)) return;
const auto mir_str = lmx::mir::MirPrinter::print(*result.mir);
if (fwrite(mir_str.c_str(), 1, mir_str.length(), file) != mir_str.length()) {
fprintf(stderr, "Error writing MIR to file\n");
}
}
void lmx_printMIRFromFile(LmState*, FILE *file, const char *name) {
lmx::Compiler compiler;
lmx::CompileResult result;
if (!compiler.compile_file(name, result, lmx::CompileStage::Mir)) return;
const auto mir_str = lmx::mir::MirPrinter::print(*result.mir);
if (fwrite(mir_str.c_str(), 1, mir_str.length(), file) != mir_str.length()) {
fprintf(stderr, "Error writing MIR to file\n");
}
}
LaminaVM* lmx_newLaminaVM(LmState* state, int argc, char** argv) {
auto* vm = new lmx::runtime::LaminaVM(argc, argv);
if (state->vm) delete reinterpret_cast<lmx::runtime::LaminaVM*>(state->vm);
state->vm = reinterpret_cast<LaminaVM*>(vm);
return state->vm;
}
bool lmx_moduleToFile(LmState*, LmModule *module, const char *name) {
const std::filesystem::path path = name;
std::filesystem::create_directories(path.parent_path());
std::ofstream ofs(path.string() + lmx::file_suffix_binary, std::ios::binary | std::ios::trunc);
const auto* mod = reinterpret_cast<lmx::runtime::CodeModuleObj*>(module);
ofs.write(
reinterpret_cast<const char*>(mod->raw_data.data()),
static_cast<std::streamsize>(mod->raw_data.size())
);
if (!ofs) return false;
ofs.close();
return true;
}
LmModule *lmx_doString(LmState *state, const char *code, const char* name) {
try {
lmx::Compiler compiler;
lmx::CompileResult result;
if (!compiler.compile_source(code, name, result)) return nullptr;
return lmx_newCodeModule(state, std::move(result.binary));
} catch (const lmx::runtime::VmFault& fault) {
std::cerr << fault.what() << std::endl;
return nullptr;
}
}
LmModule *lmx_doFile(LmState *state, const char* name, bool is_main_module) {
try {
lmx::Compiler compiler;
lmx::CompileResult result;
if (!compiler.compile_file(name, result, lmx::CompileStage::Binary,
is_main_module)) return nullptr;
#if !NDEBUG
if (debug_dump_enabled()) {
std::cout << lmx::AstPrinter::print(*result.module) << std::endl;
}
#endif
#if !NDEBUG
if (debug_dump_enabled()) {
std::cout << lmx::mir::MirPrinter::print(*result.mir) << std::endl;
}
#endif
return lmx_newCodeModule(state, std::move(result.binary));
} catch (const lmx::runtime::VmFault& fault) {
std::cerr << fault.what() << std::endl;
return nullptr;
}
}
int lmx_vmRunModule(LmState*, LaminaVM* vm, LmModule* module) {
if (module == nullptr) return 1;
return
reinterpret_cast<lmx::runtime::LaminaVM*>(vm)
->
run(reinterpret_cast<lmx::runtime::CodeModuleObj*>(module));
}
void lmx_vmEval(LmState*, LaminaVM*, LmValue*, const char *code) {
std::string c = code;
auto tks = lmx::Lexer(c).tokenize(c);
auto node = lmx::Parser(tks).parse_stmt();
lmx::hir::TypeCkContext().check_stmt(node);
}