From 8d9709720b8b7007d7178723c93feb6c881e546a Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 11 Jan 2024 12:10:45 -0800 Subject: add more documentation --- src/main.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 884a198..cbf7f34 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -13,17 +14,38 @@ extern array_t *EVAL_STACK; extern ht_t *OBJ_TABLE; extern ht_t *FLIT; +/*! prints usage then exits */ void usage() { printf("Usage: stem [-hv] [file]\n"); exit(1); } +/*! prints version and exits */ void version() { printf("Author: Preston Pan, MIT License 2023\n"); printf("stem, version 1.2 alpha\n"); exit(0); } +/*! frees all global variables */ +void global_free() { + free(PARSER->source); + ht_free(WORD_TABLE, value_free); + ht_free(FLIT, func_free); + ht_free(OBJ_TABLE, custom_free); + array_free(STACK); + free(PARSER); + array_free(EVAL_STACK); +} + +/*! handles SIGINT signal; frees memory before exit */ +void sigint_handler(int signum) { + signal(SIGINT, sigint_handler); + global_free(); + fflush(stdout); + exit(1); +} + int main(int argc, char **argv) { value_t *v; size_t len; @@ -59,6 +81,7 @@ int main(int argc, char **argv) { OBJ_TABLE = init_ht(500); add_funcs(); + signal(SIGINT, sigint_handler); /* parse and eval loop */ while (1) { @@ -69,12 +92,6 @@ int main(int argc, char **argv) { } /* Free all global variables */ - free(PARSER->source); - ht_free(WORD_TABLE, value_free); - ht_free(FLIT, func_free); - ht_free(OBJ_TABLE, custom_free); - array_free(STACK); - free(PARSER); - array_free(EVAL_STACK); + global_free(); return 0; } -- cgit