From cb5fb46803a2e988f731b0183a4d4c7547bc4041 Mon Sep 17 00:00:00 2001 From: Ray Toal Date: Sat, 6 Apr 2024 19:49:24 -0700 Subject: [PATCH] New C function --- c/inspect.c | 30 ++++++++++++++++++++++++++++++ projects/bella/src/generator.js | 5 ++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 c/inspect.c diff --git a/c/inspect.c b/c/inspect.c new file mode 100644 index 00000000..7d3377a9 --- /dev/null +++ b/c/inspect.c @@ -0,0 +1,30 @@ +#include +#include + +// A few global variables +int g0 = 1; // initialized +int g1[1000]; // uninitialized +int g2 = 3; // initialized +int g3; // uninitialized + +// A little function with its own local variables +void f() { + double x; printf("x: %p\n", &x); + if (g2-- > 0) f(); +} + +// The main function has two local variables, p and q, and +// allocates two blocks of memory at run time. +int main() { + int* p = malloc(sizeof(int) * 20); // dynamically allocated + int* q = malloc(sizeof(int) * 50); // dynamically allocated + printf("g0: %p, g1: %p, g2: %p, g3: %p\n", &g0, g1, &g2, &g3); + f(); + printf("main: %p, f: %p\n", &main, &f); + printf("&p: %p, &q: %p, p: %p, q: %p\n", &p, &q, p, q); + + // C requires manual cleanup of dynamically allocated memory + free(p); + free(q); + return EXIT_SUCCESS; +} diff --git a/projects/bella/src/generator.js b/projects/bella/src/generator.js index 39dc1961..aef3d484 100644 --- a/projects/bella/src/generator.js +++ b/projects/bella/src/generator.js @@ -17,7 +17,10 @@ export default function generate(program) { } })(new Map()) - const gen = node => generators?.[node?.kind]?.(node) ?? node + function gen(node) { + // Node types without explicit handlers just "pass through" + return generators?.[node?.kind]?.(node) ?? node + } const generators = { // Key idea: when generating an expression, return the translated