* A `map' primop.

This commit is contained in:
Eelco Dolstra 2004-07-06 17:20:34 +00:00
parent af54a60204
commit 5d48dd6912
3 changed files with 36 additions and 0 deletions

View File

@ -35,6 +35,7 @@ EvalState::EvalState()
addPrimOp1("baseNameOf", primBaseNameOf);
addPrimOp1("toString", primToString);
addPrimOp1("isNull", primIsNull);
addPrimOp1("map", primMap);
primOpsAll.add(primOps0);
primOpsAll.add(primOps1);

View File

@ -329,3 +329,35 @@ Expr primCurTime(EvalState & state)
{
return ATmake("Int(<int>)", time(0));
}
Expr primMap(EvalState & state, Expr arg)
{
arg = evalExpr(state, arg);
ATMatcher m;
ATermList es;
if (!(atMatch(m, arg) >> "Attrs" >> es))
throw Error("function `map' expects an attribute set");
Expr function = queryAttr(arg, "function");
if (!function)
throw Error("function `map' expects an attribute `function'");
Expr list = queryAttr(arg, "list");
if (!list)
throw Error("function `map' expects an attribute `list'");
list = evalExpr(state, list);
ATermList es2;
if (!(atMatch(m, list) >> "List" >> es2))
throw Error("attribute `list' in call to `map' must be a list");
ATermList res = ATempty;
for (ATermIterator i(es2); i; ++i)
res = ATinsert(res,
ATmake("Call(<term>, <term>)", function, *i));
return ATmake("List(<term>)", ATreverse(res));
}

View File

@ -39,5 +39,8 @@ Expr primIsNull(EvalState & state, Expr arg);
value for a particular run of the program. */
Expr primCurTime(EvalState & state);
/* Apply a function to each element of a list. */
Expr primMap(EvalState & state, Expr arg);
#endif /* !__PRIMOPS_H */