34std::expected<Program, CompilationError>
Compiler::Compile(std::string_view program_text, std::vector<bf::Token<ParserGrammarType>> *tokens)
37 auto ast = this->parser_.Parse(program_text, tokens);
40 return std::unexpected(ast.error());
43 auto ast_body = std::get<std::unique_ptr<Node>>(std::move(*ast));
46 auto ctx = std::make_unique<llvm::LLVMContext>();
48 auto jit = std::move(*llvm::orc::LLJITBuilder().create());
51 llvm::orc::JITDylib &main = jit->getMainJITDylib();
54 auto module = std::make_unique<llvm::Module>("unlogic", *ctx.get());
60 for (
auto library: this->default_libraries_)
63 llvm::orc::SymbolMap library_symbols;
64 for (
auto symbol: library->symbols)
66 library_symbols.insert({
67 jit->mangleAndIntern(symbol->name),
71 symbol->Define(*ctx.get(), *module);
75 auto std_sym_def = main.define(llvm::orc::absoluteSymbols(library_symbols));
78 return std::unexpected(std::move(std_sym_def));
85 .module = std::move(module),
86 .scope = program_scope,
95 std::visit(generator, *ast_body);
97 catch (std::runtime_error &e)
99 return std::unexpected(
Error{e.what()});
102 llvm::orc::ThreadSafeModule tsm(std::move(ir_ctx.module), std::move(ctx));
103 auto e = jit->addIRModule(std::move(tsm));
105 return Program(std::move(jit));
llvm::LLVMContext & llvm_ctx