From ed7bc994144117f6f3d0d522a8c60c4ac572fc7e Mon Sep 17 00:00:00 2001 From: Vipul-Cariappa Date: Mon, 1 Jan 2024 08:55:26 +0530 Subject: [PATCH] updates to work with a jupyter kernel --- src/cli_interpreter.h | 46 +++++++++++++++++++++++++++++++++---------- src/common.h | 6 ++++++ src/main.c | 24 +++++++++++++++++++--- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/cli_interpreter.h b/src/cli_interpreter.h index 9e27648..b17cb7e 100644 --- a/src/cli_interpreter.h +++ b/src/cli_interpreter.h @@ -1,4 +1,5 @@ #include "common.h" +#include typedef union { int integer; @@ -13,6 +14,7 @@ typedef struct { ExpressionResult evaluate_expression(Expression *exp, Context *cxt); bool verify_expression_type(Expression *exp, Type type, Context *cxt); bool verify_ast_semantics(AST *tree); +static inline int my_print(FILE *file, const char *msg, ...); static inline bool cli_interpret(AST tree) { if (tree.type == AST_VARIABLE) { @@ -23,7 +25,7 @@ static inline bool cli_interpret(AST tree) { errno = 0; } if (!ast_table_insert(ast, tree.value.var->name, tree)) { - fprintf(stderr, "AST insertion Error\n"); + my_print(stderr, "AST insertion Error\n"); errno = 0; return false; } @@ -35,31 +37,32 @@ static inline bool cli_interpret(AST tree) { errno = 0; } if (!ast_table_insert(ast, tree.value.func->funcname, tree)) { - fprintf(stderr, "AST insertion Error\n"); + my_print(stderr, "AST insertion Error\n"); errno = 0; return false; } } else { if (verify_expression_type(tree.value.exp, BOOL, NULL)) { - printf(evaluate_expression(tree.value.exp, NULL).boolean - ? "true\n" - : "false\n"); + my_print(stdout, evaluate_expression(tree.value.exp, NULL).boolean + ? "true\n" + : "false\n"); return true; } if (verify_expression_type(tree.value.exp, INT, NULL)) { - printf("%d\n", evaluate_expression(tree.value.exp, NULL).integer); + my_print(stdout, "%d\n", + evaluate_expression(tree.value.exp, NULL).integer); return true; } - fprintf(stderr, - "Error While Evaluation Expression\nSemantic Error: %s\n", - semantic_error_msg); + my_print(stderr, + "Error While Evaluation Expression\nSemantic Error: %s\n", + semantic_error_msg); semantic_error_msg[0] = 0; return false; } if (!verify_ast_semantics(&tree)) { - fprintf(stderr, "Semantic Error: %s\n", semantic_error_msg); + my_print(stderr, "Semantic Error: %s\n", semantic_error_msg); semantic_error_msg[0] = 0; return false; } @@ -78,3 +81,26 @@ static inline bool cli_interpret(AST tree) { return true; } + +static inline int my_print(FILE *file, const char *msg, ...) { + va_list args; + va_start(args, msg); + int len = 0; + + if ((file == stdout) && (STDOUT_REDIRECT_STRING)) { + if (STDOUT_REDIRECT_STRING) + len = vsnprintf(STDOUT_REDIRECT_STRING, STDOUT_STRING_LENGTH, msg, + args); + else + len = vprintf(msg, args); + } else { + if (STDERR_REDIRECT_STRING) + len = vsnprintf(STDERR_REDIRECT_STRING, STDERR_STRING_LENGTH, msg, + args); + else + len = vfprintf(stderr, msg, args); + } + + va_end(args); + return len; +} diff --git a/src/common.h b/src/common.h index 2ff142e..e02aaaa 100644 --- a/src/common.h +++ b/src/common.h @@ -131,6 +131,12 @@ bool verify_semantics(); extern char runtime_error_msg[]; bool interpret(int input, int *output); +#define STDOUT_STRING_LENGTH 500 +#define STDERR_STRING_LENGTH 500 + +extern char *STDOUT_REDIRECT_STRING; +extern char *STDERR_REDIRECT_STRING; + DS_TABLE_DEC(integer, int); DS_TABLE_DEC(boolean, bool); diff --git a/src/main.c b/src/main.c index 03c62e3..65f2e56 100644 --- a/src/main.c +++ b/src/main.c @@ -2,7 +2,10 @@ #include #include -void* yy_scan_string(const char *); +void *yy_scan_string(const char *); + +char *STDOUT_REDIRECT_STRING; +char *STDERR_REDIRECT_STRING; IMPLEMENT_HASH_FUNCTION; DS_TABLE_DEF(ast, AST, clear_ast); @@ -15,6 +18,9 @@ int interactive_interpretation(); int file_interpretation(const char *file_name, int input); int main(int argc, char *argv[]) { + STDOUT_REDIRECT_STRING = NULL; + STDERR_REDIRECT_STRING = NULL; + if (argc == 1) { return interactive_interpretation(); } @@ -33,10 +39,13 @@ int interactive_interpretation() { globalBooleans = boolean_table_new(100); globalIntegers = integer_table_new(100); - char string[100]; + // STDOUT_REDIRECT_STRING = calloc(STDOUT_STRING_LENGTH, 1); + // STDERR_REDIRECT_STRING = calloc(STDERR_STRING_LENGTH, 1); + + static char string[500]; while (true) { printf(">>> "); - if (!fgets(string, 100, stdin)) { + if (!fgets(string, 500, stdin)) { fprintf(stderr, "Error while getting input\n"); return 1; } @@ -46,6 +55,15 @@ int interactive_interpretation() { yy_scan_string(string); yyparse(); + + // if (STDOUT_REDIRECT_STRING[0]) { + // fprintf(stdout, ":: %s", STDOUT_REDIRECT_STRING); + // STDOUT_REDIRECT_STRING[0] = 0; + // } + // if (STDERR_REDIRECT_STRING[0]) { + // fprintf(stderr, ":: %s", STDERR_REDIRECT_STRING); + // STDERR_REDIRECT_STRING[0] = 0; + // } } }