Skip to content

Instantly share code, notes, and snippets.

@aaronryank aaronryank/c-lambda.bash
Last active Feb 16, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.