Last active
May 29, 2024 23:09
-
-
Save aaronryank/c6fd96d543658b19ed9268a3844d0657 to your computer and use it in GitHub Desktop.
Convert a C function to a lambda via executable strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
read -p "Function return type: " val | |
if [ "$1" != "mini" ]; then | |
read -p "Function name: " name | |
fi | |
read -p "Function argument list: " list | |
echo "$val $name($list) {" >f.c | |
echo "Enter code:" | |
cat >>f.c | |
echo "}" >>f.c | |
gcc -Os -c f.c | |
objcopy -O binary -j .text f.o foo | |
if [ "$1" = "mini" ]; then | |
echo -n "(($val(*)($list))\"" | |
od -An -t x1 foo | tr -d '\n' | sed 's/ /\\x/g' | |
echo "\")(INSERT ARGS HERE);" | |
else | |
echo -n "$val (*$name)($list) = \"" | |
od -An -t x1 foo | tr -d '\n' | sed 's/ /\\x/g' | |
echo \"\; | |
fi | |
rm f.c f.o foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggest replacing references to
f.c
with/tmp/f$$.c
andfoo
with/tmp/foo$$
, etc. so you don't accidentally blow away any important files namedf.c
andfoo