Skip to content

Instantly share code, notes, and snippets.

@aaronryank
Last active December 8, 2021 09:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronryank/c6fd96d543658b19ed9268a3844d0657 to your computer and use it in GitHub Desktop.
Save aaronryank/c6fd96d543658b19ed9268a3844d0657 to your computer and use it in GitHub Desktop.
Convert a C function to a lambda via executable strings.
#!/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
@MostParsingVex
Copy link

MostParsingVex commented Feb 16, 2018

Suggest replacing references to f.c with /tmp/f$$.c and foo with /tmp/foo$$, etc. so you don't accidentally blow away any important files named f.c and foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment