Skip to content

Instantly share code, notes, and snippets.

@aaronryank aaronryank/c-lambda.bash
Last active Jul 10, 2019

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
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);"
echo -n "$val (*$name)($list) = \""
od -An -t x1 foo | tr -d '\n' | sed 's/ /\\x/g'
echo \"\;
rm f.c f.o foo

This comment has been minimized.

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