Skip to content

Commit

Permalink
libnixt: add AST deserialization (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Apr 8, 2024
2 parents 4981a97 + ccf2d1f commit c34bd4a
Show file tree
Hide file tree
Showing 9 changed files with 901 additions and 31 deletions.
11 changes: 11 additions & 0 deletions libnixt/include/nixt/BinOps.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifdef BinOp

BinOp(OpEq, "==")
BinOp(OpNEq, "!=")
BinOp(OpAnd, "&&")
BinOp(OpOr, "||")
BinOp(OpImpl, "->")
BinOp(OpUpdate, "//")
BinOp(OpConcatLists, "++")

#endif // BinOp
293 changes: 280 additions & 13 deletions libnixt/include/nixt/HookExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,285 @@ namespace nixt {
using ValueMap = std::map<std::uintptr_t, nix::Value>;
using EnvMap = std::map<std::uintptr_t, nix::Env *>;

#define NIX_EXPR(EXPR) \
struct Hook##EXPR : nix::EXPR { \
ValueMap &VMap; \
EnvMap &EMap; \
std::uintptr_t Handle; \
Hook##EXPR(nix::EXPR E, ValueMap &VMap, EnvMap &EMap, \
std::uintptr_t Handle) \
: nix::EXPR(E), VMap(VMap), EMap(EMap), Handle(Handle) {} \
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override; \
std::string getName(); \
};
#include "Nodes.inc"
#undef NIX_EXPR
struct HookExprAssert : nix::ExprAssert {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprAssert(nix::ExprAssert E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprAssert(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprAttrs : nix::ExprAttrs {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprAttrs(nix::ExprAttrs E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprAttrs(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprCall : nix::ExprCall {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprCall(nix::ExprCall E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprCall(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprConcatStrings : nix::ExprConcatStrings {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprConcatStrings(nix::ExprConcatStrings E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprConcatStrings(std::move(E)), VMap(VMap), EMap(EMap),
Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprFloat : nix::ExprFloat {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprFloat(nix::ExprFloat E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprFloat(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprIf : nix::ExprIf {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprIf(nix::ExprIf E, ValueMap &VMap, EnvMap &EMap, std::uintptr_t Handle)
: nix::ExprIf(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprInt : nix::ExprInt {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprInt(nix::ExprInt E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprInt(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprLambda : nix::ExprLambda {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprLambda(nix::ExprLambda E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprLambda(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprLet : nix::ExprLet {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprLet(nix::ExprLet E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprLet(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprList : nix::ExprList {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprList(nix::ExprList E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprList(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpAnd : nix::ExprOpAnd {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpAnd(nix::ExprOpAnd E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpAnd(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpConcatLists : nix::ExprOpConcatLists {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpConcatLists(nix::ExprOpConcatLists E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpConcatLists(std::move(E)), VMap(VMap), EMap(EMap),
Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpEq : nix::ExprOpEq {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpEq(nix::ExprOpEq E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpEq(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpHasAttr : nix::ExprOpHasAttr {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpHasAttr(nix::ExprOpHasAttr E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpHasAttr(std::move(E)), VMap(VMap), EMap(EMap),
Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpImpl : nix::ExprOpImpl {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpImpl(nix::ExprOpImpl E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpImpl(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpNEq : nix::ExprOpNEq {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpNEq(nix::ExprOpNEq E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpNEq(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpNot : nix::ExprOpNot {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpNot(nix::ExprOpNot E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpNot(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpOr : nix::ExprOpOr {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpOr(nix::ExprOpOr E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpOr(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprOpUpdate : nix::ExprOpUpdate {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprOpUpdate(nix::ExprOpUpdate E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprOpUpdate(std::move(E)), VMap(VMap), EMap(EMap),
Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprPath : nix::ExprPath {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprPath(nix::ExprPath E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprPath(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {
v.mkPath(&*accessor, s.c_str());
}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprPos : nix::ExprPos {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprPos(nix::ExprPos E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprPos(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprSelect : nix::ExprSelect {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprSelect(nix::ExprSelect E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprSelect(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprString : nix::ExprString {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprString(nix::ExprString E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprString(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {
v.mkString(s.data());
}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprVar : nix::ExprVar {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprVar(nix::ExprVar E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprVar(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};

struct HookExprWith : nix::ExprWith {
ValueMap &VMap;
EnvMap &EMap;
std::uintptr_t Handle;
HookExprWith(nix::ExprWith E, ValueMap &VMap, EnvMap &EMap,
std::uintptr_t Handle)
: nix::ExprWith(std::move(E)), VMap(VMap), EMap(EMap), Handle(Handle) {}
void eval(nix::EvalState &State, nix::Env &Env, nix::Value &V) override;
std::string getName();
};
} // namespace nixt
Loading

0 comments on commit c34bd4a

Please sign in to comment.