Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/graphitemaster/lambdapp
Anonymous functions in C
https://github.com/graphitemaster/lambdapp
anonymous-functions c lambda nested-functions
Last synced: 2 months ago
JSON representation
Anonymous functions in C
- Host: GitHub
- URL: https://github.com/graphitemaster/lambdapp
- Owner: graphitemaster
- License: unlicense
- Created: 2014-06-07T19:51:19.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2020-06-15T23:30:11.000Z (over 4 years ago)
- Last Synced: 2024-08-01T03:29:33.327Z (5 months ago)
- Topics: anonymous-functions, c, lambda, nested-functions
- Language: C
- Size: 46.9 KB
- Stars: 247
- Watchers: 18
- Forks: 17
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Synposis
LambdaPP is a preprocessor for giving you anonymous functions in C.### Examples
```
// for an example the table consists of a string keyed (room) of occupants
// stored in a linked list.
hashtable_t *table;
hashtable_foreach(table,
lambda void(list_t *list) {
list_foreach(list,
lambda void(const char *occupant) {
printf(">> %s\n", occupant);
}
);
}
);
```Closures are not supported by this system. It's important to note these are not
nested functions or blocks, for information on these please see the following
links.[_Nested functions_](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html)
[_Blocks_](http://clang.llvm.org/docs/Block-ABI-Apple.html)
This is a source translation that produces global functions and replaces instances
of the lambda with the literal.### How it works
Given a lambda, a static function is created. The scope which implements the
lambda is replaced with a reference to the static function by taking it's address.#### Example
```
(lambda void(void) { printf("Hello world"); })();
```Would be translated to
```
static void lambda_0(void);
(&lambda_0)();
static void lambda_0(void) { printf("Hello world"); }
```To better see how it works, here's the original example expanded:
```
hashtable_t *table;
static void lambda_0(list_t *list);
hashtable_foreach(table, &lambda_0);
static void lambda_1(const char *occupant);
static void lambda_0(list_t *list) {
list_foreach(list, &lambda_1);
}
static void lambda_1(const char *occupant) {
printf(">> %s\n", occupant);
}
```### Diagnostics
LambdaPP inserts `#file` and `#line` directives into the source code such that
compiler diagnostics will still work.