26 lines
645 B
Markdown
26 lines
645 B
Markdown
# Metalang99-based library, adding Python-like "with" blocks
|
|
|
|
```c
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include <with99.h>
|
|
|
|
#define WITH99_resource_type_intptr_IMPL(...) v(int*)
|
|
#define WITH99_resource_init_intptr_IMPL(var, init, ...) v((printf("Allocated to %p\n", var), var = malloc(sizeof(int)), *var = init, 0))
|
|
#define WITH99_resource_free_intptr_IMPL(var, ...) v((printf("Freed %p at %p\n", var, var), free(var)))
|
|
#define WITH99_resource_throws_intptr_IMPL(...) ML99_false()
|
|
|
|
#define With WITH99_With
|
|
|
|
int main(void) {
|
|
With(
|
|
(intptr, a, 1),
|
|
(intptr, b, 3)
|
|
) {
|
|
*a += *b;
|
|
printf("%d\n", *a);
|
|
}
|
|
}
|
|
```
|