From 22c92bfdd581d4e0d49461b84a6e9348adeed2a6 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 21 Feb 2026 20:47:45 +0300 Subject: [PATCH] plugin: integrate evaluator for runtime comp Signed-off-by: NotAShelf Change-Id: I1e43447957eb0c09b3ee7c2c21abe6496a6a6964 --- src/plugin.cpp | 71 ++++++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 48 deletions(-) diff --git a/src/plugin.cpp b/src/plugin.cpp index e00e035..bad00fd 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -13,6 +13,7 @@ #include "irc/resolver.h" #include "irc/serializer.h" #include "irc/types.h" +#include "irc/evaluator.h" #include #include @@ -36,40 +37,31 @@ static void prim_loadIR(EvalState &state, const PosIdx pos, Value **args, std::string pathStr(path); - // Try to load the IR bundle - std::ifstream file(pathStr, std::ios::binary | std::ios::ate); - if (!file.is_open()) { - state.error("cannot open IR bundle: %s", pathStr) + Deserializer deserializer; + IRModule module; + + try { + module = deserializer.deserialize(pathStr); + } catch (const std::exception &e) { + state.error("failed to deserialize IR bundle: %s", e.what()) .atPos(pos) .debugThrow(); } - // Read magic number to verify it's an IR file - file.seekg(0); - uint32_t magic = 0; - file.read(reinterpret_cast(&magic), sizeof(magic)); - - if (magic != IR_MAGIC) { - state - .error("not a valid IR bundle: %s (bad magic number)", - pathStr) + if (!module.entry) { + state.error("IR bundle has no entry point") .atPos(pos) .debugThrow(); } - // For now, just return a marker that this is an IR file - // FIXME: complete full VM implementation - auto bindings = state.buildBindings(2); - - Value *vType = state.allocValue(); - vType->mkString("ir-bundle"); - bindings.insert(state.symbols.create("type"), vType); - - Value *vPath = state.allocValue(); - vPath->mkString(pathStr); - bindings.insert(state.symbols.create("path"), vPath); - - v.mkAttrs(bindings.finish()); + try { + Evaluator evaluator(state); + evaluator.eval_to_nix(module.entry, v); + } catch (const std::exception &e) { + state.error("failed to evaluate IR: %s", e.what()) + .atPos(pos) + .debugThrow(); + } } /** @@ -85,7 +77,6 @@ static void prim_compileNix(EvalState &state, const PosIdx pos, Value **args, std::string sourceStr(source); try { - // Parse the Nix source Parser parser; auto ast = parser.parse(sourceStr, ""); @@ -95,23 +86,11 @@ static void prim_compileNix(EvalState &state, const PosIdx pos, Value **args, .debugThrow(); } - // Generate IR IRGenerator ir_gen; auto ir = ir_gen.generate(ast); - // For now, return a marker that compilation succeeded - // FIXME: full evaluation - auto bindings = state.buildBindings(2); - - Value *vType = state.allocValue(); - vType->mkString("ir-compiled"); - bindings.insert(state.symbols.create("type"), vType); - - Value *vSource = state.allocValue(); - vSource->mkString(sourceStr.substr(0, 50)); - bindings.insert(state.symbols.create("source"), vSource); - - v.mkAttrs(bindings.finish()); + Evaluator evaluator(state); + evaluator.eval_to_nix(ir, v); } catch (const std::exception &e) { state.error("IR compilation failed: %s", e.what()) @@ -126,22 +105,18 @@ static void prim_compileNix(EvalState &state, const PosIdx pos, Value **args, */ static void prim_info(EvalState &state, const PosIdx pos, Value **args, Value &v) { - auto bindings = state.buildBindings(4); + auto bindings = state.buildBindings(3); Value *vName = state.allocValue(); vName->mkString("nix-ir-plugin"); bindings.insert(state.symbols.create("name"), vName); Value *vVersion = state.allocValue(); - vVersion->mkString("0.1.0-alpha"); + vVersion->mkString("0.1.0"); bindings.insert(state.symbols.create("version"), vVersion); - Value *vPhase = state.allocValue(); - vPhase->mkString("phase-4"); - bindings.insert(state.symbols.create("phase"), vPhase); - Value *vStatus = state.allocValue(); - vStatus->mkString("compiler-complete"); + vStatus->mkString("runtime-active"); bindings.insert(state.symbols.create("status"), vStatus); v.mkAttrs(bindings.finish());