aboutsummaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 3e06bc9e..db62cb57 100644
--- a/builtin.c
+++ b/builtin.c
@@ -3822,6 +3822,44 @@ do_intdiv(int nargs)
return make_number((AWKNUM) 0.0);
}
+/* do_typeof --- return a string with the type of the arg */
+
+NODE *
+do_typeof(int nargs)
+{
+ NODE *arg;
+ char *res = "unknown";
+ int null_str_flags = (STRCUR|STRING|NUMCUR|NUMBER);
+
+ arg = POP();
+ switch (arg->type) {
+ case Node_var_array:
+ res = "array";
+ break;
+ case Node_hardregex:
+ res = "regexp";
+ break;
+ case Node_val:
+ case Node_var:
+ if ((arg->flags & null_str_flags) == null_str_flags)
+ res = "untyped";
+ else if ((arg->flags & STRING) != 0)
+ res = "scalar_s";
+ else if ((arg->flags & NUMBER) != 0)
+ res = "scalar_n";
+ break;
+ case Node_var_new:
+ res = "untyped";
+ break;
+ default:
+ fatal(_("typeof: unknown argument type `%s'"),
+ nodetype2str(arg->type));
+ break;
+ }
+
+ DEREF(arg);
+ return make_string(res, strlen(res));
+}
/* mbc_byte_count --- return number of bytes for corresponding numchars multibyte characters */