diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2013-10-24T00·49+0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2013-10-24T00·49+0200 |
commit | 05d02f798f65bf18e8ca71f3d23bfdf9df63fb7c (patch) | |
tree | 8b95f70bd39c526cb03a8c5bd43c900378f203e9 | |
parent | 6da92d96aec29ab09cf909ac6a270bc2753cd34a (diff) |
Add a typeOf primop
We already have some primops for determining the type of a value, such as isString, but they're incomplete: for instance, there is no isPath. Rather than adding more isBla functions, the generic typeOf function returns a string representing the type of the argument (e.g. "int").
-rw-r--r-- | src/libexpr/primops.cc | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 30dd15ac0daf..badff1ca3f2d 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -92,6 +92,30 @@ static void prim_import(EvalState & state, Value * * args, Value & v) } +/* Return a string representing the type of the expression. */ +static void prim_typeOf(EvalState & state, Value * * args, Value & v) +{ + state.forceValue(*args[0]); + string t; + switch (args[0]->type) { + case tInt: t = "int"; break; + case tBool: t = "bool"; break; + case tString: t = "string"; break; + case tPath: t = "path"; break; + case tNull: t = "null"; break; + case tAttrs: t = "attrs"; break; + case tList: t = "list"; break; + case tLambda: + case tPrimOp: + case tPrimOpApp: + t = "lambda"; + break; + default: abort(); + } + mkString(v, state.symbols.create(t)); +} + + /* Determine whether the argument is the null value. */ static void prim_isNull(EvalState & state, Value * * args, Value & v) { @@ -108,7 +132,7 @@ static void prim_isFunction(EvalState & state, Value * * args, Value & v) } -/* Determine whether the argument is an Int. */ +/* Determine whether the argument is an integer. */ static void prim_isInt(EvalState & state, Value * * args, Value & v) { state.forceValue(*args[0]); @@ -116,7 +140,7 @@ static void prim_isInt(EvalState & state, Value * * args, Value & v) } -/* Determine whether the argument is an String. */ +/* Determine whether the argument is a string. */ static void prim_isString(EvalState & state, Value * * args, Value & v) { state.forceValue(*args[0]); @@ -124,7 +148,7 @@ static void prim_isString(EvalState & state, Value * * args, Value & v) } -/* Determine whether the argument is an Bool. */ +/* Determine whether the argument is a Boolean. */ static void prim_isBool(EvalState & state, Value * * args, Value & v) { state.forceValue(*args[0]); @@ -1189,6 +1213,7 @@ void EvalState::createBaseEnv() // Miscellaneous addPrimOp("import", 1, prim_import); + addPrimOp("__typeOf", 1, prim_typeOf); addPrimOp("isNull", 1, prim_isNull); addPrimOp("__isFunction", 1, prim_isFunction); addPrimOp("__isString", 1, prim_isString); |