summary refs log tree commit diff
path: root/bin/c.sh
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-09 00:47:50 -0500
committerJune McEnroe <june@causal.agency>2021-01-09 00:47:50 -0500
commit4e86032c2eb6cfc0d4da82ca29e2b27cc221fcda (patch)
tree78e3b89a46eaf21079e9ae12590569c9506e679a /bin/c.sh
parentUpdate tagline (diff)
downloadsrc-4e86032c2eb6cfc0d4da82ca29e2b27cc221fcda.tar.gz
src-4e86032c2eb6cfc0d4da82ca29e2b27cc221fcda.zip
Add c -t flag to print expression type
Also add missing float case.
Diffstat (limited to 'bin/c.sh')
-rw-r--r--bin/c.sh33
1 files changed, 32 insertions, 1 deletions
diff --git a/bin/c.sh b/bin/c.sh
index f1143fd3..4439aaee 100644
--- a/bin/c.sh
+++ b/bin/c.sh
@@ -29,10 +29,12 @@ cat >&3 <<EOF
 EOF
 
 expr=
-while getopts 'e:i:' opt; do
+type=
+while getopts 'e:i:t' opt; do
 	case "${opt}" in
 		(e) expr=$OPTARG;;
 		(i) echo "#include <${OPTARG}>" >&3;;
+		(t) type=1;;
 		(?) exit 1;;
 	esac
 done
@@ -45,6 +47,34 @@ int main(int argc, char *argv[]) {
 	$*;
 EOF
 
+if [ -n "${type}" ]; then
+	cat >&3 <<EOF
+	printf(
+		_Generic(
+			${expr},
+			char: "(char) ",
+			char *: "(char *) ",
+			const char *: "(const char *) ",
+			wchar_t *: "(wchar_t *) ",
+			const wchar_t *: "(const wchar_t *) ",
+			signed char: "(signed char) ",
+			short: "(short) ",
+			int: "(int) ",
+			long: "(long) ",
+			long long: "(long long) ",
+			unsigned char: "(unsigned char) ",
+			unsigned short: "(unsigned short) ",
+			unsigned int: "(unsigned int) ",
+			unsigned long: "(unsigned long) ",
+			unsigned long long: "(unsigned long long) ",
+			float: "(float) ",
+			double: "(double) ",
+			default: "(void *) "
+		)
+	);
+EOF
+fi
+
 if [ -n "${expr}" ]; then
 	cat >&3 <<EOF
 	printf(
@@ -65,6 +95,7 @@ if [ -n "${expr}" ]; then
 			unsigned int: "%u\n",
 			unsigned long: "%lu\n",
 			unsigned long long: "%llu\n",
+			float: "%g\n",
 			double: "%g\n",
 			default: "%p\n"
 		),