Skip to content

Commit

Permalink
Merge pull request #5 from Vipul-Cariappa/kernel
Browse files Browse the repository at this point in the history
updates to work with a jupyter kernel
  • Loading branch information
Vipul-Cariappa authored Jan 1, 2024
2 parents 252c7ef + ed7bc99 commit 6c64ef6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
46 changes: 36 additions & 10 deletions src/cli_interpreter.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.h"
#include <stdarg.h>

typedef union {
int integer;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
6 changes: 6 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 21 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#include <stdio.h>
#include <string.h>

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);
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand All @@ -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;
// }
}
}

Expand Down

0 comments on commit 6c64ef6

Please sign in to comment.