Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Functional Programming in C/C++

Name: Anonymous 2012-10-01 20:15

I present you with a simple BASH script which adds lambdas to C/C++.

Now you can finally run your:

qsort(array, nelts, sizeof(int), lambda (int) (int *x, int *y) () return *x<*y; endLambda)


It expands code like

#include <stdio.h>

int main(int argc, char **argv) {
  int a = 1;
  int b = 2;
  printf("%d\n",
   (lambda (int) (int x) (int a, int b) return x+a+b; endLambda)(4));
  return 0;
}


into:

static int *_la_1337_a;
static int *_la_1337_b;
static int _lf_1337(int x);
#include <stdio.h>

int main(int argc, char **argv) {
  int a = 1;
  int b = 2;
  printf("%d\n",
   ((_la_1337_a=&a,_la_1337_b=&b,_lf_1337))(4));
  return 0;
}


int _lf_1337(int x) {
#define a (*_la_1337_a)
#define b (*_la_1337_b)
 return x+a+b;
#undef a
#undef b
}


So you worst!



#!/bin/bash

File=$(cat "$1" | tr '\n' '$')
Out="$2"
CODE=1337
Lambs=$(echo "$File" | grep -o "lambda.*endLambda")
trim() { sed 's/^ *\(.*\) *$/\1/'; }
part() { sed "s/lambda.*(\(.*\)).*(\(.*\)).*(\(.*\))\(.*\)endLambda/\\$1/"; }
RetType="$(echo "$Lambs" | part 1)"
Args="$(echo "$Lambs" | part 2)"
Vars="$(echo "$Lambs" | part 3)"
Body="$(echo "$Lambs" | part 4)"


Result="$(echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/_la_${CODE}_\2=\\\\\&\2/" | tr '\n' ',')"
Result="(${Result}_lf_${CODE})"

echo "$Result"
echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/static \1 *_la_${
CODE}_\2;/" > "$Out"
echo "static ${RetType} _lf_${CODE}($Args);" >> "$Out"

echo "$File" | sed "s/lambda.*endLambda/$Result/" | tr '$' '\n' >> "$Out"

echo "${RetType} _lf_${CODE}($Args) {" >> "$Out"
echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/#define \2 (*_la_${CODE}_\2)/" >> "$Out"
echo "$Body" >> "$Out"
echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/#undef \2/" >> "$Out"
echo "}" >> "$Out"

Name: Anonymous 2012-10-01 22:59

Scheme supports this out of box:

(lambda (w)
  (lambda (f)
    (f ((w w) f))))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List