From adf9a45469f55258446d383333aa2ca79cfb0536 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sun, 2 Nov 2003 16:31:35 +0000 Subject: [PATCH] * Primops: baseNameOf, toString. --- src/fix-ng/eval.cc | 2 ++ src/fix-ng/fix.cc | 4 ---- src/fix-ng/primops.cc | 19 +++++++++++++++++++ src/fix-ng/primops.hh | 9 +++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/fix-ng/eval.cc b/src/fix-ng/eval.cc index c58a06dff8..38a1d81fcf 100644 --- a/src/fix-ng/eval.cc +++ b/src/fix-ng/eval.cc @@ -145,6 +145,8 @@ Expr evalExpr2(EvalState & state, Expr e) 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); else throw badTerm("undefined variable/primop", e1); } diff --git a/src/fix-ng/fix.cc b/src/fix-ng/fix.cc index 1c37a0b7ba..c24ca4d9c4 100644 --- a/src/fix-ng/fix.cc +++ b/src/fix-ng/fix.cc @@ -30,10 +30,6 @@ static Path searchPath(const Paths & searchDirs, const Path & relPath) #if 0 static Expr evalExpr2(EvalState & state, Expr e) { - /* Conditional. */ - if (ATmatch(e, "If(, , )", &e1, &e2, &e3)) { - } - /* Ad-hoc function for string matching. */ if (ATmatch(e, "HasSubstr(, )", &e1, &e2)) { e1 = evalExpr(state, e1); diff --git a/src/fix-ng/primops.cc b/src/fix-ng/primops.cc index f86f9eb38d..7d060124b7 100644 --- a/src/fix-ng/primops.cc +++ b/src/fix-ng/primops.cc @@ -204,3 +204,22 @@ Expr primDerivation(EvalState & state, Expr args) return makeAttrs(attrs); } + + +Expr primBaseNameOf(EvalState & state, Expr arg) +{ + string s = evalString(state, arg); + return ATmake("Str()", baseNameOf(s).c_str()); +} + + +Expr primToString(EvalState & state, Expr arg) +{ + arg = evalExpr(state, arg); + char * s; + if (ATmatch(arg, "Str()", &s) || + ATmatch(arg, "Path()", &s) || + ATmatch(arg, "Uri()", &s)) + return ATmake("Str()", s); + else throw badTerm("cannot coerce to string", arg); +} diff --git a/src/fix-ng/primops.hh b/src/fix-ng/primops.hh index 41b572c688..e48883b0bb 100644 --- a/src/fix-ng/primops.hh +++ b/src/fix-ng/primops.hh @@ -19,4 +19,13 @@ Expr primImport(EvalState & state, Expr arg); Expr primDerivation(EvalState & state, Expr args); +/* Return the base name of the given string, i.e., everything + following the last slash. */ +Expr primBaseNameOf(EvalState & state, Expr arg); + + +/* Convert the argument (which can be a path or a uri) to a string. */ +Expr primToString(EvalState & state, Expr arg); + + #endif /* !__PRIMOPS_H */