aboutsummaryrefslogtreecommitdiff
path: root/src/fix-ng
diff options
context:
space:
mode:
Diffstat (limited to 'src/fix-ng')
-rw-r--r--src/fix-ng/eval.cc108
-rw-r--r--src/fix-ng/fix-expr.cc22
-rw-r--r--src/fix-ng/fix.cc36
-rw-r--r--src/fix-ng/parser.cc36
-rw-r--r--src/fix-ng/primops.cc43
5 files changed, 115 insertions, 130 deletions
diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc
index 2b45b4798..55cb5fbcf 100644
--- a/src/fix-ng/eval.cc
+++ b/src/fix-ng/eval.cc
@@ -16,6 +16,7 @@ EvalState::EvalState()
/* Substitute an argument set into the body of a function. */
static Expr substArgs(Expr body, ATermList formals, Expr arg)
{
+ ATMatcher m;
ATermMap subs;
Expr undefined = ATmake("Undefined");
@@ -23,9 +24,9 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
while (!ATisEmpty(formals)) {
ATerm t = ATgetFirst(formals);
Expr name, def;
- if (ATmatch(t, "NoDefFormal(<term>)", &name))
+ if (atMatch(m, t) >> "NoDefFormal" >> name)
subs.set(name, undefined);
- else if (ATmatch(t, "DefFormal(<term>, <term>)", &name, &def))
+ else if (atMatch(m, t) >> "DefFormal" >> name >> def)
subs.set(name, def);
else abort(); /* can't happen */
formals = ATgetNext(formals);
@@ -67,15 +68,17 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg)
(e.x) (e.y), y = e.x}'. */
ATerm expandRec(ATerm e, ATermList bnds)
{
+ ATMatcher m;
+
/* Create the substitution list. */
ATermMap subs;
ATermList bs = bnds;
while (!ATisEmpty(bs)) {
- char * s;
+ string s;
Expr e2;
- if (!ATmatch(ATgetFirst(bs), "Bind(<str>, <term>)", &s, &e2))
+ if (!(atMatch(m, ATgetFirst(bs)) >> "Bind" >> s >> e2))
abort(); /* can't happen */
- subs.set(s, ATmake("Select(<term>, <str>)", e, s));
+ subs.set(s, ATmake("Select(<term>, <str>)", e, s.c_str()));
bs = ATgetNext(bs);
}
@@ -83,9 +86,9 @@ ATerm expandRec(ATerm e, ATermList bnds)
ATermMap as;
bs = bnds;
while (!ATisEmpty(bs)) {
- char * s;
+ string s;
Expr e2;
- if (!ATmatch(ATgetFirst(bs), "Bind(<str>, <term>)", &s, &e2))
+ if (!(atMatch(m, ATgetFirst(bs)) >> "Bind" >> s >> e2))
abort(); /* can't happen */
as.set(s, substitute(subs, e2));
bs = ATgetNext(bs);
@@ -98,8 +101,9 @@ ATerm expandRec(ATerm e, ATermList bnds)
string evalString(EvalState & state, Expr e)
{
e = evalExpr(state, e);
- char * s;
- if (!ATmatch(e, "Str(<str>)", &s))
+ ATMatcher m;
+ string s;
+ if (!(atMatch(m, e) >> "Str" >> s))
throw badTerm("string expected", e);
return s;
}
@@ -108,8 +112,9 @@ string evalString(EvalState & state, Expr e)
Path evalPath(EvalState & state, Expr e)
{
e = evalExpr(state, e);
- char * s;
- if (!ATmatch(e, "Path(<str>)", &s))
+ ATMatcher m;
+ string s;
+ if (!(atMatch(m, e) >> "Path" >> s))
throw badTerm("path expected", e);
return s;
}
@@ -118,78 +123,79 @@ Path evalPath(EvalState & state, Expr e)
bool evalBool(EvalState & state, Expr e)
{
e = evalExpr(state, e);
- if (ATmatch(e, "Bool(True)")) return true;
- else if (ATmatch(e, "Bool(False)")) return false;
+ ATMatcher m;
+ if (atMatch(m, e) >> "Bool" >> "True") return true;
+ else if (atMatch(m, e) >> "Bool" >> "False") return false;
else throw badTerm("expecting a boolean", e);
}
Expr evalExpr2(EvalState & state, Expr e)
{
+ ATMatcher m;
Expr e1, e2, e3, e4;
- char * s1;
+ string s1;
/* Normal forms. */
- if (ATmatch(e, "Str(<str>)", &s1) ||
- ATmatch(e, "Path(<str>)", &s1) ||
- ATmatch(e, "Uri(<str>)", &s1) ||
- ATmatch(e, "Bool(<term>)", &e1) ||
- ATmatch(e, "Function([<list>], <term>)", &e1, &e2) ||
- ATmatch(e, "Attrs([<list>])", &e1) ||
- ATmatch(e, "List([<list>])", &e1))
+ if (atMatch(m, e) >> "Str" ||
+ atMatch(m, e) >> "Path" ||
+ atMatch(m, e) >> "Uri" ||
+ atMatch(m, e) >> "Bool" ||
+ atMatch(m, e) >> "Function" ||
+ atMatch(m, e) >> "Attrs" ||
+ atMatch(m, e) >> "List")
return e;
/* Any encountered variables must be undeclared or primops. */
- if (ATmatch(e, "Var(<str>)", &s1)) {
- if ((string) s1 == "null") return primNull(state);
+ if (atMatch(m, e) >> "Var" >> s1) {
+ if (s1 == "null") return primNull(state);
return e;
}
/* Function application. */
- if (ATmatch(e, "Call(<term>, <term>)", &e1, &e2)) {
+ if (atMatch(m, e) >> "Call" >> e1 >> e2) {
+
+ ATermList formals;
/* Evaluate the left-hand side. */
e1 = evalExpr(state, e1);
/* Is it a primop or a function? */
- if (ATmatch(e1, "Var(<str>)", &s1)) {
- string primop(s1);
- if (primop == "import") return primImport(state, e2);
- if (primop == "derivation") return primDerivation(state, e2);
- if (primop == "toString") return primToString(state, e2);
- if (primop == "baseNameOf") return primBaseNameOf(state, e2);
- if (primop == "isNull") return primIsNull(state, e2);
+ if (atMatch(m, e1) >> "Var" >> s1) {
+ if (s1 == "import") return primImport(state, e2);
+ if (s1 == "derivation") return primDerivation(state, e2);
+ if (s1 == "toString") return primToString(state, e2);
+ if (s1 == "baseNameOf") return primBaseNameOf(state, e2);
+ if (s1 == "isNull") return primIsNull(state, e2);
else throw badTerm("undefined variable/primop", e1);
}
- else if (ATmatch(e1, "Function([<list>], <term>)", &e3, &e4)) {
+ else if (atMatch(m, e1) >> "Function" >> formals >> e4)
return evalExpr(state,
- substArgs(e4, (ATermList) e3, evalExpr(state, e2)));
- }
+ substArgs(e4, formals, evalExpr(state, e2)));
else throw badTerm("expecting a function or primop", e1);
}
/* Attribute selection. */
- if (ATmatch(e, "Select(<term>, <str>)", &e1, &s1)) {
- string name(s1);
- Expr a = queryAttr(evalExpr(state, e1), name);
- if (!a) throw badTerm(format("missing attribute `%1%'") % name, e);
+ if (atMatch(m, e) >> "Select" >> e1 >> s1) {
+ Expr a = queryAttr(evalExpr(state, e1), s1);
+ if (!a) throw badTerm(format("missing attribute `%1%'") % s1, e);
return evalExpr(state, a);
}
/* Mutually recursive sets. */
ATermList bnds;
- if (ATmatch(e, "Rec([<list>])", &bnds))
- return expandRec(e, (ATermList) bnds);
+ if (atMatch(m, e) >> "Rec" >> bnds)
+ return expandRec(e, bnds);
/* Let expressions `let {..., body = ...}' are just desugared
into `(rec {..., body = ...}).body'. */
- if (ATmatch(e, "LetRec(<term>)", &e1))
- return evalExpr(state, ATmake("Select(Rec(<term>), \"body\")", e1));
+ if (atMatch(m, e) >> "LetRec" >> bnds)
+ return evalExpr(state, ATmake("Select(Rec(<term>), \"body\")", bnds));
/* Conditionals. */
- if (ATmatch(e, "If(<term>, <term>, <term>)", &e1, &e2, &e3)) {
+ if (atMatch(m, e) >> "If" >> e1 >> e2 >> e3) {
if (evalBool(state, e1))
return evalExpr(state, e2);
else
@@ -197,33 +203,33 @@ Expr evalExpr2(EvalState & state, Expr e)
}
/* Assertions. */
- if (ATmatch(e, "Assert(<term>, <term>)", &e1, &e2)) {
+ if (atMatch(m, e) >> "Assert" >> e1 >> e2) {
if (!evalBool(state, e1)) throw badTerm("guard failed", e);
return evalExpr(state, e2);
}
/* Generic equality. */
- if (ATmatch(e, "OpEq(<term>, <term>)", &e1, &e2))
+ if (atMatch(m, e) >> "OpEq" >> e1 >> e2)
return makeBool(evalExpr(state, e1) == evalExpr(state, e2));
/* Generic inequality. */
- if (ATmatch(e, "OpNEq(<term>, <term>)", &e1, &e2))
+ if (atMatch(m, e) >> "OpNEq" >> e1 >> e2)
return makeBool(evalExpr(state, e1) != evalExpr(state, e2));
/* Negation. */
- if (ATmatch(e, "OpNot(<term>)", &e1))
+ if (atMatch(m, e) >> "OpNot" >> e1)
return makeBool(!evalBool(state, e1));
/* Implication. */
- if (ATmatch(e, "OpImpl(<term>, <term>)", &e1, &e2))
+ if (atMatch(m, e) >> "OpImpl" >> e1 >> e2)
return makeBool(!evalBool(state, e1) || evalBool(state, e2));
/* Conjunction (logical AND). */
- if (ATmatch(e, "OpAnd(<term>, <term>)", &e1, &e2))
+ if (atMatch(m, e) >> "OpAnd" >> e1 >> e2)
return makeBool(evalBool(state, e1) && evalBool(state, e2));
/* Disjunction (logical OR). */
- if (ATmatch(e, "OpOr(<term>, <term>)", &e1, &e2))
+ if (atMatch(m, e) >> "OpOr" >> e1 >> e2)
return makeBool(evalBool(state, e1) || evalBool(state, e2));
/* Barf. */
@@ -234,7 +240,7 @@ Expr evalExpr2(EvalState & state, Expr e)
Expr evalExpr(EvalState & state, Expr e)
{
startNest(nest, lvlVomit,
- format("evaluating expression: %1%") % printTerm(e));
+ format("evaluating expression: %1%") % e);
state.nrEvaluated++;
diff --git a/src/fix-ng/fix-expr.cc b/src/fix-ng/fix-expr.cc
index 1ce4a55e4..a3d24ba0e 100644
--- a/src/fix-ng/fix-expr.cc
+++ b/src/fix-ng/fix-expr.cc
@@ -118,14 +118,15 @@ ATerm bottomupRewrite(TermFun & f, ATerm e)
void queryAllAttrs(Expr e, ATermMap & attrs)
{
+ ATMatcher m;
ATermList bnds;
- if (!ATmatch(e, "Attrs([<list>])", &bnds))
+ if (!(atMatch(m, e) >> "Attrs" >> bnds))
throw badTerm("expected attribute set", e);
while (!ATisEmpty(bnds)) {
- char * s;
+ string s;
Expr e;
- if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
+ if (!(atMatch(m, ATgetFirst(bnds)) >> "Bind" >> s >> e))
abort(); /* can't happen */
attrs.set(s, e);
bnds = ATgetNext(bnds);
@@ -156,9 +157,10 @@ Expr makeAttrs(const ATermMap & attrs)
Expr substitute(const ATermMap & subs, Expr e)
{
- char * s;
+ ATMatcher m;
+ string s;
- if (ATmatch(e, "Var(<str>)", &s)) {
+ if (atMatch(m, e) >> "Var" >> s) {
Expr sub = subs.get(s);
return sub ? sub : e;
}
@@ -167,13 +169,13 @@ Expr substitute(const ATermMap & subs, Expr e)
function. */
ATermList formals;
ATerm body;
- if (ATmatch(e, "Function([<list>], <term>)", &formals, &body)) {
+ if (atMatch(m, e) >> "Function" >> formals >> body) {
ATermMap subs2(subs);
ATermList fs = formals;
while (!ATisEmpty(fs)) {
Expr def;
- if (!ATmatch(ATgetFirst(fs), "NoDefFormal(<str>)", &s) &&
- !ATmatch(ATgetFirst(fs), "DefFormal(<str>, <term>)", &s))
+ if (!(atMatch(m, ATgetFirst(fs)) >> "NoDefFormal" >> s) &&
+ !(atMatch(m, ATgetFirst(fs)) >> "DefFormal" >> s >> def))
abort();
subs2.remove(s);
fs = ATgetNext(fs);
@@ -184,12 +186,12 @@ Expr substitute(const ATermMap & subs, Expr e)
/* Idem for a mutually recursive attribute set. */
ATermList bindings;
- if (ATmatch(e, "Rec([<list>])", &bindings)) {
+ if (atMatch(m, e) >> "Rec" >> bindings) {
ATermMap subs2(subs);
ATermList bnds = bindings;
while (!ATisEmpty(bnds)) {
Expr e;
- if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
+ if (!(atMatch(m, ATgetFirst(bnds)) >> "Bind" >> s >> e))
abort(); /* can't happen */
subs2.remove(s);
bnds = ATgetNext(bnds);
diff --git a/src/fix-ng/fix.cc b/src/fix-ng/fix.cc
index dc2790a60..49f19669a 100644
--- a/src/fix-ng/fix.cc
+++ b/src/fix-ng/fix.cc
@@ -27,37 +27,6 @@ static Path searchPath(const Paths & searchDirs, const Path & relPath)
#endif
-#if 0
-static Expr evalExpr2(EvalState & state, Expr e)
-{
- /* Ad-hoc function for string matching. */
- if (ATmatch(e, "HasSubstr(<term>, <term>)", &e1, &e2)) {
- e1 = evalExpr(state, e1);
- e2 = evalExpr(state, e2);
-
- char * s1, * s2;
- if (!ATmatch(e1, "<str>", &s1))
- throw badTerm("expecting a string", e1);
- if (!ATmatch(e2, "<str>", &s2))
- throw badTerm("expecting a string", e2);
-
- return
- string(s1).find(string(s2)) != string::npos ?
- ATmake("True") : ATmake("False");
- }
-
- /* BaseName primitive function. */
- if (ATmatch(e, "BaseName(<term>)", &e1)) {
- e1 = evalExpr(state, e1);
- if (!ATmatch(e1, "<str>", &s1))
- throw badTerm("string expected", e1);
- return ATmake("<str>", baseNameOf(s1).c_str());
- }
-
-}
-#endif
-
-
static Expr evalStdin(EvalState & state)
{
startNest(nest, lvlTalkative, format("evaluating standard input"));
@@ -70,9 +39,10 @@ static Expr evalStdin(EvalState & state)
static void printNixExpr(EvalState & state, Expr e)
{
+ ATMatcher m;
ATermList es;
- if (ATmatch(e, "Attrs([<list>])", &es)) {
+ if (atMatch(m, e) >> "Attrs" >> es) {
Expr a = queryAttr(e, "type");
if (a && evalString(state, a) == "derivation") {
a = queryAttr(e, "drvPath");
@@ -83,7 +53,7 @@ static void printNixExpr(EvalState & state, Expr e)
}
}
- if (ATmatch(e, "[<list>]", &es)) {
+ if (ATgetType(e) == AT_LIST) {
while (!ATisEmpty(es)) {
printNixExpr(state, evalExpr(state, ATgetFirst(es)));
es = ATgetNext(es);
diff --git a/src/fix-ng/parser.cc b/src/fix-ng/parser.cc
index 93afe0627..710ea6a86 100644
--- a/src/fix-ng/parser.cc
+++ b/src/fix-ng/parser.cc
@@ -28,40 +28,40 @@ struct Cleanup : TermFun
virtual ATerm operator () (ATerm e)
{
- char * s;
+ ATMatcher m;
+ string s;
- if (ATmatch(e, "Str(<str>)", &s)) {
- string s2(s);
+ if (atMatch(m, e) >> "Str" >> s) {
return ATmake("Str(<str>)",
- string(s2, 1, s2.size() - 2).c_str());
+ string(s, 1, s.size() - 2).c_str());
}
- if (ATmatch(e, "Path(<str>)", &s)) {
- string path(s);
- if (path[0] != '/')
- path = basePath + "/" + path;
- return ATmake("Path(<str>)", canonPath(path).c_str());
+ if (atMatch(m, e) >> "Path" >> s) {
+ if (s[0] != '/')
+ s = basePath + "/" + s;
+ return ATmake("Path(<str>)", canonPath(s).c_str());
}
- if (ATmatch(e, "Int(<str>)", &s)) {
+ if (atMatch(m, e) >> "Int" >> s) {
istringstream s2(s);
int n;
s2 >> n;
return ATmake("Int(<int>)", n);
}
- if (ATmatch(e, "Bool(\"true\")", &s))
+ if (atMatch(m, e) >> "Bool" >> "true")
return ATmake("Bool(True)");
- if (ATmatch(e, "Bool(\"false\")", &s))
+ if (atMatch(m, e) >> "Bool" >> "false")
return ATmake("Bool(False)");
- if (ATmatch(e, "ExprNil"))
+ if (atMatch(m, e) >> "ExprNil")
return (ATerm) ATempty;
- ATerm e1, e2;
- if (ATmatch(e, "ExprCons(<term>, [<list>])", &e1, &e2))
- return (ATerm) ATinsert((ATermList) e2, e1);
+ ATerm e1;
+ ATermList e2;
+ if (atMatch(m, e) >> "ExprCons" >> e1 >> e2)
+ return (ATerm) ATinsert(e2, e1);
return e;
}
@@ -133,7 +133,7 @@ Expr parseExprFromFile(Path path)
throw SysError(format("parse failed in `%1%'") % path);
if (SGisParseError(result))
throw Error(format("parse error in `%1%': %2%")
- % path % printTerm(result));
+ % path % result);
/* Implode it. */
PT_ParseTree tree = PT_makeParseTreeFromTerm(result);
@@ -156,7 +156,7 @@ Expr parseExprFromFile(Path path)
throw Error(format("cannot implode parse tree"));
debug(format("imploded parse tree of `%1%': %2%")
- % path % printTerm(imploded));
+ % path % imploded);
/* Finally, clean it up. */
Cleanup cleanup;
diff --git a/src/fix-ng/primops.cc b/src/fix-ng/primops.cc
index 07281e89b..a68352579 100644
--- a/src/fix-ng/primops.cc
+++ b/src/fix-ng/primops.cc
@@ -5,8 +5,9 @@
Expr primImport(EvalState & state, Expr arg)
{
- char * path;
- if (!ATmatch(arg, "Path(<str>)", &path))
+ ATMatcher m;
+ string path;
+ if (!(atMatch(m, arg) >> "Path" >> path))
throw badTerm("path expected", arg);
return evalFile(state, path);
}
@@ -79,15 +80,16 @@ static string processBinding(EvalState & state, Expr e, NixExpr & ne)
{
e = evalExpr(state, e);
- char * s;
+ ATMatcher m;
+ string s;
ATermList es;
- if (ATmatch(e, "Str(<str>)", &s)) return s;
- if (ATmatch(e, "Uri(<str>)", &s)) return s;
- if (ATmatch(e, "Bool(True)")) return "1";
- if (ATmatch(e, "Bool(False)")) return "";
+ if (atMatch(m, e) >> "Str" >> s) return s;
+ if (atMatch(m, e) >> "Uri" >> s) return s;
+ if (atMatch(m, e) >> "Bool" >> "True") return "1";
+ if (atMatch(m, e) >> "Bool" >> "False") return "";
- if (ATmatch(e, "Attrs([<list>])", &es)) {
+ if (atMatch(m, e) >> "Attrs" >> es) {
Expr a = queryAttr(e, "type");
if (a && evalString(state, a) == "derivation") {
a = queryAttr(e, "drvPath");
@@ -98,12 +100,12 @@ static string processBinding(EvalState & state, Expr e, NixExpr & ne)
}
}
- if (ATmatch(e, "Path(<str>)", &s)) {
+ if (atMatch(m, e) >> "Path" >> s) {
Path drvPath = copyAtom(state, s);
return addInput(state, drvPath, ne);
}
- if (ATmatch(e, "List([<list>])", &es)) {
+ if (atMatch(m, e) >> "List" >> es) {
string s;
bool first = true;
while (!ATisEmpty(es)) {
@@ -115,7 +117,7 @@ static string processBinding(EvalState & state, Expr e, NixExpr & ne)
return s;
}
- if (ATmatch(e, "Null")) return "";
+ if (atMatch(m, e) >> "Null") return "";
throw badTerm("invalid derivation binding", e);
}
@@ -148,14 +150,17 @@ Expr primDerivation(EvalState & state, Expr args)
/* The `args' attribute is special: it supplies the
command-line arguments to the builder. */
if (key == "args") {
+ throw Error("args not implemented");
+#if 0
ATermList args;
- if (!ATmatch(value, "[<list>]", &args))
+ if (!(ATmatch(value, "[<list>]", &args))
throw badTerm("list expected", value);
while (!ATisEmpty(args)) {
Expr arg = evalExpr(state, ATgetFirst(args));
ne.derivation.args.push_back(processBinding(state, arg, ne));
args = ATgetNext(args);
}
+#endif
}
/* All other attributes are passed to the builder through the
@@ -220,11 +225,12 @@ Expr primBaseNameOf(EvalState & state, Expr arg)
Expr primToString(EvalState & state, Expr arg)
{
arg = evalExpr(state, arg);
- char * s;
- if (ATmatch(arg, "Str(<str>)", &s) ||
- ATmatch(arg, "Path(<str>)", &s) ||
- ATmatch(arg, "Uri(<str>)", &s))
- return ATmake("Str(<str>)", s);
+ ATMatcher m;
+ string s;
+ if (atMatch(m, arg) >> "Str" >> s ||
+ atMatch(m, arg) >> "Path" >> s ||
+ atMatch(m, arg) >> "Uri" >> s)
+ return ATmake("Str(<str>)", s.c_str());
else throw badTerm("cannot coerce to string", arg);
}
@@ -238,5 +244,6 @@ Expr primNull(EvalState & state)
Expr primIsNull(EvalState & state, Expr arg)
{
arg = evalExpr(state, arg);
- return makeBool(ATmatch(arg, "Null"));
+ ATMatcher m;
+ return makeBool(atMatch(m, arg) >> "Null");
}