Last active
October 10, 2022 02:19
-
-
Save llimllib/92f71817255f86a0c7a5a98279130338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdarg.h> | |
// https://github.com/nothings/stb | |
#define STB_DS_IMPLEMENTATION | |
#include "stb_ds.h" | |
// compile with: gcc -Wall -I . dabeaz_stb.c -o a && ./a | |
typedef struct datum datum; | |
typedef enum valtype { | |
INT, | |
STR, | |
LIST | |
} valtype; | |
typedef union val { | |
int integer; | |
char* string; | |
datum** child; | |
} val; | |
struct datum { | |
valtype type; | |
val value; | |
}; | |
void print_list(datum** data) { | |
printf("["); | |
for (int i=0; i < arrlen(data); i++) { | |
switch(data[i]->type) { | |
case INT: | |
printf(" %d ", data[i]->value.integer); | |
break; | |
case STR: | |
printf(" %s ", data[i]->value.string); | |
break; | |
case LIST: | |
print_list(data[i]->value.child); | |
break; | |
} | |
} | |
printf("]"); | |
} | |
datum* int_(int i) { | |
datum* d = malloc(sizeof(datum*)); | |
d->type = INT; | |
d->value = (val) { .integer = i }; | |
return d; | |
} | |
datum* str(char* str) { | |
datum* d = malloc(sizeof(datum*)); | |
d->type = STR; | |
d->value = (val) { .string = str }; | |
return d; | |
} | |
datum* list(int n, ...) { | |
datum** lst = NULL; | |
va_list values; | |
va_start(values, n); | |
for (int i=0; i < n; i++) { | |
arrput(lst, va_arg(values, datum*)); | |
} | |
datum* d = malloc(sizeof(datum*)); | |
d->type = LIST; | |
d->value = (val) { .child = lst }; | |
return d; | |
} | |
int main() { | |
datum* data = | |
list(3, | |
str("+"), | |
list(3, str("*"), int_(1), int_(2)), | |
list(4, str("*"), int_(3), | |
list(3, str("+"), int_(4), int_(5)), | |
str("x") | |
)); | |
print_list(data->value.child); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment