ref: 276ae5f65f9eba670b785be72e28b89720aaede9
parent: f59d012d4d7c964fbd2bb1dfcc560fabc1328873
author: zamfofex <zamfofex@twdb.moe>
date: Mon Jan 8 19:58:05 EST 2024
improve minification
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,7 @@
!/manifest.scm
!/moonfish.vcxproj
!/minify.sh
+!/rename.sh
!/moonfish.h
!/chess.c
!/search.c
--- a/minify.sh
+++ b/minify.sh
@@ -12,68 +12,48 @@
sed 's/^#\(include <\)/\1/g' |
# preprocess the file, add '#' back to 'include', remove debug statements
-gcc -E -Dinclude='#include' -D'perror(...)=' -D'fprintf(...)=' -D'free(...)=' -Dmoonfish_mini - |
+# note: this materialises the whole file
+gcc -E -Dinclude='#include' -D'perror(...)=' -D'fprintf(...)=' -Dmoonfish_mini - |
# remove lines starting with '# '
sed '/^# /d' |
+# remove leading white space from '#include' lines
+sed 's/^[\t ]\+#/#/g' |
+
# place all '#include' lines on top
-# note: this materialises the whole file!
-tee xxx.txt |
-( txt="$(tee)" && { grep '^ #' <<< "$txt" ; } && { grep -v '^ #' <<< "$txt" ; } ) |+# note: this materialises the whole file
+( txt="$(tee)" && { grep '^#' <<< "$txt" ; } && { grep -v '^#' <<< "$txt" ; } ) |-# remove all line breaks (replace them with spaces)
-tr '\n' ' ' |
-
# put line breaks around string literals
-sed 's/\("\(\\"\|[^"]\)*"\)/\n\1\n/g' |+sed 's/\("\(\\.\|[^"]\)*"\)/\n\1\n/g' |# put line breaks around character literals
-sed 's/\('"'"'\(\\.\|.\)'"'"'\)/\n\1\n/g' |+sed 's/\('"'"'\(\\.\|.\)*'"'"'\)/\n\1\n/g' |-# in every line that isn't a string literal,
-# join all adjacent white space into a single tab
-sed '/^[^"'"'"']/s/[\t ]\+/\t/g' |
+# in every line that isn't a string literal or '#include' line,
+# put line breaks around identifiers
+sed '/^[^"'"'"'#]/s/[a-z0-9_]\+/\n\0\n/gi' |
-# replace long/common words (ugly but effective)
-sed '/^[^"'"'"']/s/\(\b\|_\)moonfish\(\b\|_\)/\1F\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)white\(\b\|_\)/\1W\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)black\(\b\|_\)/\1Y\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)pawn\(\b\|_\)/\1P\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)knight\(\b\|_\)/\1L\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)bishop\(\b\|_\)/\1B\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)rook\(\b\|_\)/\1R\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)queen\(\b\|_\)/\1Q\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)king\(\b\|_\)/\1K\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)chess\(\b\|_\)/\1X\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)score\(\b\|_\)/\1S\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)board\(\b\|_\)/\1D\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)castle\(\b\|_\)/\1O\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)move\(\b\|_\)/\1M\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)moves\(\b\|_\)/\1N\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)from\(\b\|_\)/\1A\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)to\(\b\|_\)/\1Z\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)piece\(\b\|_\)/\1G\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)promotion\(\b\|_\)/\1H\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)color\(\b\|_\)/\1C\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)captured\(\b\|_\)/\1U\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)empty\(\b\|_\)/\1E\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)type\(\b\|_\)/\1T\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)outside\(\b\|_\)/\1J\2/g' |
+# rename identifiers to be shorter
+./rename.sh |
-# remove inserted line breaks
-tr -d '\n' |
+# replace all white space with tabs (except inside string literals)
+# note: this makes the next 'sed' materialise the whole file
+sed '/^[^"'"'"']/s/[\t ]\+/\t/g' |
+tr '\n' '\t' |
# replace tabs between alphanumeric characters with a single space
# note: there are no tabs inside string literals
-sed 's/\([a-z0-9_]\)\t\([a-z0-9_]\)/\1 \2/gi' |
+sed 's/\([a-z0-9_]\)\t\+\([a-z0-9_]\)/\1 \2/gi' |
# remove all tab characters
tr -d '\t' |
# put line breaks back around '#include' lines again
+# (also remove whitespace betwen '#include' and '<')
# note: there is no white space within include file names
-sed 's/\(#include<[^>]*>\)/\n\1\n/g' |
+sed 's/#include<[^>]*>/\n\0\n/g' |
# remove all empty lines
sed '/^$/d' |
--- /dev/null
+++ b/rename.sh
@@ -1,0 +1,53 @@
+#!/usr/bin/env bash
+
+# moonfish is licensed under the AGPL (v3 or later)
+# copyright 2023, 2024 zamfofex
+
+set -e
+
+alphabet=({a..z} {a..z}{a..z} {a..z}{a..z}{a..z})+alphabet2=("${alphabet[@]}")+declare -A names
+
+functions="main printf fprintf sscanf fgets fflush stdin stdout stderr strcmp strcpy strtok strstr malloc realloc free exit errno clock_gettime timespec tv_sec tv_nsec pthread_create pthread_join pthread_t typedef moonfish_type_t"
+keywords="do while for if else break continue return static struct enum unsigned signed long short int char void sizeof $functions"
+
+while read -r name
+do
+ if ! [[ "$name" =~ ^[a-z] ]]
+ then
+ echo "$name"
+ continue
+ fi
+
+ if [[ " $keywords " =~ " $name " ]]
+ then
+ echo "$name"
+ continue
+ fi
+
+ short="${names["$name"]}"+ if [[ "$short" = "" ]]
+ then
+ if [[ "$name" = moonfish ]]
+ then short=F
+ elif [[ "$name" =~ ^moonfish ]]
+ then
+ short="F${alphabet[0]}"+ alphabet=("${alphabet[@]:1}")+ else
+ while :
+ do
+ short="${alphabet2[0]}"+ alphabet2=("${alphabet2[@]:1}")+ if ! [[ " $keywords " =~ " $short " ]]
+ then break
+ fi
+ done
+ fi
+
+ names["$name"]="$short"
+ fi
+
+ echo "$short"
+done
--
⑨