plugin: integrate evaluator for runtime comp

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I1e43447957eb0c09b3ee7c2c21abe6496a6a6964
This commit is contained in:
raf 2026-02-21 20:47:45 +03:00
commit 22c92bfdd5
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -13,6 +13,7 @@
#include "irc/resolver.h"
#include "irc/serializer.h"
#include "irc/types.h"
#include "irc/evaluator.h"
#include <fstream>
#include <iostream>
@ -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<EvalError>("cannot open IR bundle: %s", pathStr)
Deserializer deserializer;
IRModule module;
try {
module = deserializer.deserialize(pathStr);
} catch (const std::exception &e) {
state.error<EvalError>("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<char *>(&magic), sizeof(magic));
if (magic != IR_MAGIC) {
state
.error<EvalError>("not a valid IR bundle: %s (bad magic number)",
pathStr)
if (!module.entry) {
state.error<EvalError>("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<EvalError>("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, "<inline>");
@ -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<EvalError>("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());