From 9bd02dc1cc61e195374d3bf83febb724fb08d739 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 27 Feb 2015 11:32:58 +0200 Subject: First set of changes toward @/.../. --- builtin.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index 38a974fc..ebd7dde5 100644 --- a/builtin.c +++ b/builtin.c @@ -3661,6 +3661,41 @@ do_div(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"; + + 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 & 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 */ -- cgit v1.2.3 From b062ba39d52c7e9dee06ac73030aba248ee81168 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 18 Mar 2015 22:01:19 +0200 Subject: Make @/.../ a terminal. Improve typeof() builtin function. --- builtin.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index f6fed878..4a99ed6f 100644 --- a/builtin.c +++ b/builtin.c @@ -3685,6 +3685,7 @@ do_typeof(int nargs) { NODE *arg; char *res = "unknown"; + int null_str_flags = (STRCUR|STRING|NUMCUR|NUMBER); arg = POP(); switch (arg->type) { @@ -3696,7 +3697,9 @@ do_typeof(int nargs) break; case Node_val: case Node_var: - if ((arg->flags & STRING) != 0) + 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"; -- cgit v1.2.3 From aca30f7d82ec4fa002c6ab5ea4a2d9d77d28c2cd Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 14 Apr 2015 15:06:41 +0300 Subject: Add more tests, make them work. Almost done... --- builtin.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index db62cb57..12a79177 100644 --- a/builtin.c +++ b/builtin.c @@ -3106,7 +3106,8 @@ call_sub(const char *name, int nargs) * push replace * push $0 */ - regex = make_regnode(Node_regex, regex); + if (regex->type != Node_hardregex) + regex = make_regnode(Node_regex, regex); PUSH(regex); PUSH(replace); lhs = r_get_field(zero, (Func_ptr *) 0, true); @@ -3130,7 +3131,8 @@ call_sub(const char *name, int nargs) * nargs++ * } */ - regex = make_regnode(Node_regex, regex); + if (regex->type != Node_hardregex) + regex = make_regnode(Node_regex, regex); PUSH(regex); PUSH(replace); PUSH(glob_flag); @@ -3167,7 +3169,8 @@ call_match(int nargs) /* Don't need to pop the string just to push it back ... */ - regex = make_regnode(Node_regex, regex); + if (regex->type != Node_hardregex) + regex = make_regnode(Node_regex, regex); PUSH(regex); if (array) @@ -3195,7 +3198,8 @@ call_split_func(const char *name, int nargs) if (nargs >= 3) { regex = POP_STRING(); - regex = make_regnode(Node_regex, regex); + if (regex->type != Node_hardregex) + regex = make_regnode(Node_regex, regex); } else { if (name[0] == 's') { regex = make_regnode(Node_regex, FS_node->var_value); -- cgit v1.2.3 From 09c533438cd709076c29bb61585833e2c4d67f97 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Tue, 28 Apr 2015 16:48:19 +0300 Subject: Add lint warning for isarray. --- builtin.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index 0241c297..afa06e43 100644 --- a/builtin.c +++ b/builtin.c @@ -481,6 +481,12 @@ do_isarray(int nargs) { NODE *tmp; int ret = 1; + static bool warned = false; + + if (do_lint && ! warned) { + warned = true; + lintwarn(_("isarray is deprecated. Use typeof() instead")); + } tmp = POP(); if (tmp->type != Node_var_array) { -- cgit v1.2.3 From 3ac8984c62b6a89e86e18376b75e32f0a60bfe9f Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Mon, 11 May 2015 14:53:15 +0300 Subject: Change Node_hardregex to Node_typedregex. --- builtin.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index afa06e43..02b94ed8 100644 --- a/builtin.c +++ b/builtin.c @@ -3145,7 +3145,7 @@ call_sub(const char *name, int nargs) * push replace * push $0 */ - if (regex->type != Node_hardregex) + if (regex->type != Node_typedregex) regex = make_regnode(Node_regex, regex); PUSH(regex); PUSH(replace); @@ -3170,7 +3170,7 @@ call_sub(const char *name, int nargs) * nargs++ * } */ - if (regex->type != Node_hardregex) + if (regex->type != Node_typedregex) regex = make_regnode(Node_regex, regex); PUSH(regex); PUSH(replace); @@ -3208,7 +3208,7 @@ call_match(int nargs) /* Don't need to pop the string just to push it back ... */ - if (regex->type != Node_hardregex) + if (regex->type != Node_typedregex) regex = make_regnode(Node_regex, regex); PUSH(regex); @@ -3237,7 +3237,7 @@ call_split_func(const char *name, int nargs) if (nargs >= 3) { regex = POP_STRING(); - if (regex->type != Node_hardregex) + if (regex->type != Node_typedregex) regex = make_regnode(Node_regex, regex); } else { if (name[0] == 's') { @@ -3879,7 +3879,7 @@ do_typeof(int nargs) case Node_var_array: res = "array"; break; - case Node_hardregex: + case Node_typedregex: res = "regexp"; break; case Node_val: -- cgit v1.2.3 From 20037f36ee350ee64699d311e4296be1f0367dd2 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Fri, 19 Jun 2015 12:26:27 +0300 Subject: Minor edit to warning in do_isarray. --- builtin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index 02b94ed8..61a4398b 100644 --- a/builtin.c +++ b/builtin.c @@ -485,7 +485,7 @@ do_isarray(int nargs) if (do_lint && ! warned) { warned = true; - lintwarn(_("isarray is deprecated. Use typeof() instead")); + lintwarn(_("`isarray' is deprecated. Use `typeof' instead")); } tmp = POP(); -- cgit v1.2.3