summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2021-03-08 18:31:52 +0200
committerArnold D. Robbins <arnold@skeeve.com>2021-03-08 18:31:52 +0200
commit5fd7d2faf13484c595f7cd74468711282816796d (patch)
tree54a7716c5bb875481044352dfedef90c6bc68199
parentb9931e852ba21180c380639135f96cdcd0a3015f (diff)
downloadgawk-5fd7d2faf13484c595f7cd74468711282816796d.tar.gz
More progress on bool.
-rw-r--r--ChangeLog12
-rw-r--r--awk.h2
-rw-r--r--awkgram.c1
-rw-r--r--awkgram.y1
-rw-r--r--bool.notes9
-rw-r--r--builtin.c37
-rw-r--r--field.c2
-rw-r--r--main.c18
-rw-r--r--test/ChangeLog5
-rw-r--r--test/dumpvars.ok2
-rw-r--r--test/functab5.ok1
-rw-r--r--test/id.ok1
-rw-r--r--test/intest.awk4
-rw-r--r--test/symtab11.ok1
-rw-r--r--test/symtab8.ok2
15 files changed, 74 insertions, 24 deletions
diff --git a/ChangeLog b/ChangeLog
index 52b74a78..e248bcc7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2021-03-08 Arnold D. Robbins <arnold@skeeve.com>
+
+ * awk.h (warn_bool, do_bool): Add function declarations.
+ * awkgram.y (tokentab): Add entry for "bool" builtin.
+ * builtin.c (warn_bool): New function.
+ (do_index, do_substr, do_toupper, do_tolower, do_match,
+ do_length): Call it.
+ (do_sub): If first arg to sub/gsub is bool, fatal error.
+ (do_bool): New function.
+ * field.c (do_split, do_patsplit): Call warn_bool.
+ * main.c (load_procinfo_bools): Removed function and call.
+
2021-03-05 Arnold D. Robbins <arnold@skeeve.com>
Start on a bool type for gawk.
diff --git a/awk.h b/awk.h
index ee055f49..f4fa4d4d 100644
--- a/awk.h
+++ b/awk.h
@@ -1455,6 +1455,7 @@ extern bool is_identchar(int c);
extern NODE *make_regnode(NODETYPE type, NODE *exp);
extern bool validate_qualified_name(char *token);
/* builtin.c */
+extern void warn_bool(const char *func, int argnum, NODE *n);
extern double double_to_int(double d);
extern NODE *do_exp(int nargs);
extern NODE *do_fflush(int nargs);
@@ -1504,6 +1505,7 @@ extern int strncasecmpmbs(const unsigned char *,
const unsigned char *, size_t);
extern int sanitize_exit_status(int status);
extern void check_symtab_functab(NODE *dest, const char *fname, const char *msg);
+extern NODE *do_bool(int nargs);
/* debug.c */
extern void init_debug(void);
extern int debug_prog(INSTRUCTION *pc);
diff --git a/awkgram.c b/awkgram.c
index 9c2700a3..40c3cbd2 100644
--- a/awkgram.c
+++ b/awkgram.c
@@ -4779,6 +4779,7 @@ static const struct token tokentab[] = {
{"asorti", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3), do_asorti, 0},
{"atan2", Op_builtin, LEX_BUILTIN, NOT_OLD|A(2), do_atan2, MPF(atan2)},
{"bindtextdomain", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2), do_bindtextdomain, 0},
+{"bool", Op_builtin, LEX_BUILTIN, GAWKX|A(1), do_bool, 0},
{"break", Op_K_break, LEX_BREAK, 0, 0, 0},
{"case", Op_K_case, LEX_CASE, GAWKX, 0, 0},
{"close", Op_builtin, LEX_BUILTIN, NOT_OLD|A(1)|A(2), do_close, 0},
diff --git a/awkgram.y b/awkgram.y
index 6dfbe7e0..3fe0272b 100644
--- a/awkgram.y
+++ b/awkgram.y
@@ -2277,6 +2277,7 @@ static const struct token tokentab[] = {
{"asorti", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3), do_asorti, 0},
{"atan2", Op_builtin, LEX_BUILTIN, NOT_OLD|A(2), do_atan2, MPF(atan2)},
{"bindtextdomain", Op_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2), do_bindtextdomain, 0},
+{"bool", Op_builtin, LEX_BUILTIN, GAWKX|A(1), do_bool, 0},
{"break", Op_K_break, LEX_BREAK, 0, 0, 0},
{"case", Op_K_case, LEX_CASE, GAWKX, 0, 0},
{"close", Op_builtin, LEX_BUILTIN, NOT_OLD|A(1)|A(2), do_close, 0},
diff --git a/bool.notes b/bool.notes
index 51ea7268..df3ec69d 100644
--- a/bool.notes
+++ b/bool.notes
@@ -33,8 +33,13 @@ numbers, and false before true.
6. These string function generate a runtime fatal error
if given an argument / target of boolean type:
- substr match index gensub gsub
- sub length split patsplit
+ gsub sub
+
+These functions merely treat the value as a string
+but issue a lint warning.
+
+ substr match index gensub
+ length split patsplit
tolower toupper
7. Updates to API needed for an additional type, and the table
diff --git a/builtin.c b/builtin.c
index 67a89bd1..1158fad2 100644
--- a/builtin.c
+++ b/builtin.c
@@ -381,6 +381,8 @@ do_index(int nargs)
s1 = force_string(s1);
s2 = force_string(s2);
+ warn_bool("index", 1, s1);
+ warn_bool("index", 2, s2);
p1 = s1->stptr;
p2 = s2->stptr;
@@ -552,6 +554,7 @@ do_length(int nargs)
if (do_lint && (fixtype(tmp)->flags & STRING) == 0)
lintwarn(_("%s: received non-string argument"), "length");
tmp = force_string(tmp);
+ warn_bool("length", 1, tmp);
if (gawk_mb_cur_max > 1) {
tmp = force_wstring(tmp);
@@ -1779,6 +1782,16 @@ do_sqrt(int nargs)
return make_number((AWKNUM) sqrt(arg));
}
+/* warn_bool --- warn that bool parameter is used as a string */
+
+void
+warn_bool(const char *func, int argnum, NODE *n)
+{
+ if (do_lint && (n->flags & BOOL) != 0)
+ lintwarn(_("%s: argument %d of type bool used as a string"),
+ func, argnum);
+}
+
/* do_substr --- do the substr function */
NODE *
@@ -1802,6 +1815,7 @@ do_substr(int nargs)
DEREF(t1);
t1 = POP_STRING();
+ warn_bool("substr", 1, t1);
if (nargs == 3) {
if (! (d_length >= 1)) {
@@ -2407,6 +2421,7 @@ do_tolower(int nargs)
NODE *t1, *t2;
t1 = POP_SCALAR();
+ warn_bool("tolower", 1, t1);
if (do_lint && (fixtype(t1)->flags & STRING) == 0)
lintwarn(_("%s: received non-string argument"), "tolower");
t1 = force_string(t1);
@@ -2438,6 +2453,7 @@ do_toupper(int nargs)
NODE *t1, *t2;
t1 = POP_SCALAR();
+ warn_bool("toupper", 1, t1);
if (do_lint && (fixtype(t1)->flags & STRING) == 0)
lintwarn(_("%s: received non-string argument"), "toupper");
t1 = force_string(t1);
@@ -2662,6 +2678,7 @@ do_match(int nargs)
tre = POP();
rp = re_update(tre);
t1 = POP_STRING();
+ warn_bool("mastch", 1, t1);
rstart = research(rp, t1->stptr, 0, t1->stlen, RE_NEED_START);
if (rstart >= 0) { /* match succeded */
@@ -2882,6 +2899,7 @@ do_sub(int nargs, unsigned int flags)
rp = re_update(tmp);
target = POP_STRING(); /* original string */
+ warn_bool("gensub", 3, target);
glob_flag = POP_SCALAR(); /* value of global flag */
if ( (glob_flag->flags & STRING) != 0
@@ -2924,6 +2942,10 @@ do_sub(int nargs, unsigned int flags)
}
}
+ if ((target->flags & BOOL) != 0)
+ fatal(_("%s: target cannot be of type bool"),
+ (flags & GSUB) != 0 ? "gsub" : "sub");
+
global = (how_many == -1);
rep_node = POP_STRING(); /* replacement text */
@@ -4324,3 +4346,18 @@ check_symtab_functab(NODE *dest, const char *fname, const char *msg)
else if (dest == func_table)
fatal(msg, fname, "FUNCTAB");
}
+
+/* do_bool --- create boolean values */
+
+NODE *
+do_bool(int nargs)
+{
+ NODE *tmp;
+ bool result;
+
+ tmp = POP_SCALAR();
+ result = boolval(tmp);
+ DEREF(tmp);
+
+ return make_bool_node(result);
+}
diff --git a/field.c b/field.c
index c21046b9..2bd58863 100644
--- a/field.c
+++ b/field.c
@@ -1019,6 +1019,7 @@ do_split(int nargs)
assoc_clear(arr);
src = TOP_STRING();
+ warn_bool("split", 1, src);
if (src->stlen == 0) {
/*
* Skip the work if first arg is the null string.
@@ -1096,6 +1097,7 @@ do_patsplit(int nargs)
_("%s: cannot use %s as second argument"));
src = TOP_STRING();
+ warn_bool("patsplit", 1, src);
if ((sep->flags & REGEX) != 0)
sep = sep->typed_re;
diff --git a/main.c b/main.c
index 9833d848..76f28289 100644
--- a/main.c
+++ b/main.c
@@ -982,23 +982,6 @@ load_procinfo_argv()
assoc_set(PROCINFO_node, sub, argv_array);
}
-/* load_procinfo_bools --- populate PROCINFO["true"] and PROCINFO["false"] */
-
-static void
-load_procinfo_bools()
-{
- NODE *sub;
- NODE *val;
-
- val = make_bool_node(false);
- sub = make_string("false", 5);
- assoc_set(PROCINFO_node, sub, val);
-
- val = make_bool_node(true);
- sub = make_string("true", 4);
- assoc_set(PROCINFO_node, sub, val);
-}
-
/* load_procinfo --- populate the PROCINFO array */
static NODE *
@@ -1085,7 +1068,6 @@ load_procinfo()
}
#endif
load_procinfo_argv();
- load_procinfo_bools();
return PROCINFO_node;
}
diff --git a/test/ChangeLog b/test/ChangeLog
index 53fe5627..2604c270 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,8 @@
+2021-03-08 Arnold D. Robbins <arnold@skeeve.com>
+
+ * dumpvars.ok, functab5.ok, id.ok, intest.awk, symtab11.ok,
+ symtab8.ok: Updated after code changes.
+
2021-02-13 Arnold D. Robbins <arnold@skeeve.com>
* Makefile.am (EXTRA_DIST): argcasfile, new test.
diff --git a/test/dumpvars.ok b/test/dumpvars.ok
index 85d1c859..7caecd35 100644
--- a/test/dumpvars.ok
+++ b/test/dumpvars.ok
@@ -9,7 +9,7 @@ FILENAME: "-"
FNR: 3
FPAT: "[^[:space:]]+"
FS: " "
-FUNCTAB: array, 41 elements
+FUNCTAB: array, 42 elements
IGNORECASE: 0
LINT: 0
NF: 1
diff --git a/test/functab5.ok b/test/functab5.ok
index 9ac4295d..ef110989 100644
--- a/test/functab5.ok
+++ b/test/functab5.ok
@@ -3,6 +3,7 @@ asort'
asorti'
atan2'
bindtextdomain'
+bool'
chdir'
close'
compl'
diff --git a/test/id.ok b/test/id.ok
index ab4df74a..f990546f 100644
--- a/test/id.ok
+++ b/test/id.ok
@@ -34,6 +34,7 @@ asort -> builtin
asorti -> builtin
atan2 -> builtin
bindtextdomain -> builtin
+bool -> builtin
close -> builtin
compl -> builtin
cos -> builtin
diff --git a/test/intest.awk b/test/intest.awk
index f030d07a..18e0cc4d 100644
--- a/test/intest.awk
+++ b/test/intest.awk
@@ -1,4 +1,4 @@
BEGIN {
- bool = ((b = 1) in c);
- print bool, b # gawk-3.0.1 prints "0 "; should print "0 1"
+ bool_result = ((b = 1) in c);
+ print bool_result, b # gawk-3.0.1 prints "0 "; should print "0 1"
}
diff --git a/test/symtab11.ok b/test/symtab11.ok
index 7d4be46c..da2cfcba 100644
--- a/test/symtab11.ok
+++ b/test/symtab11.ok
@@ -37,6 +37,7 @@ BEGIN -- Functab is next
[asorti] = asorti
[atan2] = atan2
[bindtextdomain] = bindtextdomain
+[bool] = bool
[close] = close
[compl] = compl
[cos] = cos
diff --git a/test/symtab8.ok b/test/symtab8.ok
index da29b585..0cf40fe9 100644
--- a/test/symtab8.ok
+++ b/test/symtab8.ok
@@ -9,7 +9,7 @@ FIELDWIDTHS: ""
FNR: 1
FPAT: "[^[:space:]]+"
FS: " "
-FUNCTAB: array, 41 elements
+FUNCTAB: array, 42 elements
IGNORECASE: 0
LINT: 0
NF: 1