Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Interpreter #3

Merged
merged 6 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/DS.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
TYPE name##_table_get(name##_table_t *tb, const char *key); \
TYPE *name##_table_get_ptr(name##_table_t *tb, const char *key); \
size_t name##_table_size(name##_table_t *tb); \
bool name##_table_delete(name##_table_t *tb, const char *key); \
TYPE *name##_table_iter_next(name##_table_t *tb, char **key); \
void name##_table_iter(name##_table_t *tb); \
bool name##_table_clear(name##_table_t *tb);
Expand Down Expand Up @@ -457,6 +458,45 @@
\
size_t name##_table_size(name##_table_t *tb) { return tb->count; } \
\
bool name##_table_clear(name##_table_t *tb) { /* TODO: Implement */ \
bool name##_table_delete(name##_table_t *tb, const char *key) { \
size_t hash = hash_function(key) % tb->array_length; \
\
_##name##_hash_table_list_node *item_list_node = \
tb->table_list + hash; \
\
if (!(item_list_node->item_pair.key)) { \
errno = EINVAL; \
return false; \
} \
\
bool is_first = true; \
\
do { \
if (!strcmp(key, item_list_node->item_pair.key)) { \
delFunc(item_list_node->item_pair.value); \
\
void *to_free = item_list_node->next; \
if (item_list_node->next) { \
*item_list_node = *item_list_node->next; \
if (!is_first) { \
free(to_free); \
} \
} else { \
*item_list_node = (_##name##_hash_table_list_node){0}; \
} \
tb->count--; \
return true; \
} \
\
is_first = false; \
item_list_node = item_list_node->next; \
} while (item_list_node); \
\
errno = EINVAL; \
return false; \
} \
\
bool name##_table_clear(name##_table_t *tb) { \
/* TODO: Implement */ \
return true; \
}
80 changes: 80 additions & 0 deletions src/cli_interpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "common.h"

typedef union {
int integer;
bool boolean;
} ExpressionResult;

typedef struct {
size_t arglen;
Argument *args;
} Context;

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 bool cli_interpret(AST tree) {
if (tree.type == AST_VARIABLE) {
if (ast_table_get_ptr(ast, tree.value.var->name)) {
ast_table_delete(ast, tree.value.var->name);
integer_table_delete(globalIntegers, tree.value.var->name);
boolean_table_delete(globalBooleans, tree.value.var->name);
errno = 0;
}
if (!ast_table_insert(ast, tree.value.var->name, tree)) {
fprintf(stderr, "AST insertion Error\n");
errno = 0;
return false;
}
} else if (tree.type == AST_FUNCTION) {
if (ast_table_get_ptr(ast, tree.value.func->funcname)) {
ast_table_delete(ast, tree.value.func->funcname);
integer_table_delete(globalIntegers, tree.value.func->funcname);
boolean_table_delete(globalBooleans, tree.value.func->funcname);
errno = 0;
}
if (!ast_table_insert(ast, tree.value.func->funcname, tree)) {
fprintf(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");
return true;
}
if (verify_expression_type(tree.value.exp, INT, NULL)) {
printf("%d\n", evaluate_expression(tree.value.exp, NULL).integer);
return true;
}

fprintf(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);
semantic_error_msg[0] = 0;
return false;
}

if (tree.type == AST_VARIABLE) {
if (tree.value.var->type == INT) {
assert(integer_table_insert(
globalIntegers, tree.value.var->name,
evaluate_expression(tree.value.var->expression, NULL).integer));
} else {
assert(boolean_table_insert(
globalBooleans, tree.value.var->name,
evaluate_expression(tree.value.var->expression, NULL).boolean));
}
}

return true;
}
28 changes: 26 additions & 2 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ extern void yyerror(char const *s);
extern int yylineno;
extern int column;
extern char *yytext;
extern char *filename;
extern const char *filename;

extern bool cli_interpretation_mode;

extern char syntax_error_msg[];

Expand Down Expand Up @@ -104,6 +106,7 @@ struct _Expression {
typedef enum {
AST_VARIABLE,
AST_FUNCTION,
AST_EXPRESSION,
} AST_TYPE;

struct _AST {
Expand All @@ -112,6 +115,7 @@ struct _AST {
union {
Function *func;
Variable *var;
Expression *exp;
} value;
};

Expand All @@ -127,6 +131,12 @@ bool verify_semantics();
extern char runtime_error_msg[];
bool interpret(int input, int *output);

DS_TABLE_DEC(integer, int);
DS_TABLE_DEC(boolean, bool);

extern integer_table_t *globalIntegers;
extern boolean_table_t *globalBooleans;

// FIXME: Check if memory allocations fail

static inline const char *const Type_to_string(Type type) {
Expand Down Expand Up @@ -422,6 +432,9 @@ static inline void print_ast_table(ast_table_t *ast) {
case AST_VARIABLE:
print_variable(tree->value.var);
break;
case AST_EXPRESSION:
// TODO: print expression
break;
}
printf("\n");
}
Expand All @@ -431,6 +444,17 @@ static inline void clear_expression(Expression *exp) {
// TODO: implement
}

static inline void clear_variable(Expression *exp) {
static inline void clear_variable(Variable *var) {
// TODO: implement
}

static inline void clear_function(Function *func) {
// TODO: implement
}

static inline void clear_ast(AST tree) {
// TODO: implement
}

static inline void clean_integer(int x) {}
static inline void clean_boolean(bool x) {}
12 changes: 7 additions & 5 deletions src/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ typedef struct {

size_t hash_function(const char *str);

DS_TABLE_DEC(integer, int);
DS_TABLE_DEC(boolean, bool);

DS_TABLE_DEF(integer, int, NULL);
DS_TABLE_DEF(boolean, bool, NULL);
DS_TABLE_DEF(integer, int, clean_integer);
DS_TABLE_DEF(boolean, bool, clean_boolean);

ExpressionResult evaluate_expression(Expression *exp, Context *cxt);
ExpressionResult execute_function_call(Function *func, Expression **args,
Expand Down Expand Up @@ -90,6 +87,11 @@ bool interpret(int input, int *output) {
return false;
}
}
break;
case AST_EXPRESSION:
snprintf(syntax_error_msg, ERROR_MSG_LEN, "Internal Error");
// TODO: clean memory
return false;
}
}

Expand Down
63 changes: 46 additions & 17 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,57 +1,86 @@
#include "common.h"
#include <stdio.h>
#include <string.h>

void* yy_scan_string(const char *);

IMPLEMENT_HASH_FUNCTION;
DS_TABLE_DEF(ast, AST, NULL);
DS_TABLE_DEF(ast, AST, clear_ast);

ast_table_t *ast;
char *filename;
const char *filename;

bool cli_interpretation_mode = false;
int interactive_interpretation();
int file_interpretation(const char *file_name, int input);

int main(int argc, char *argv[]) {
if (argc == 1) {
return interactive_interpretation();
}

if (argc != 3) {
fprintf(stderr, "File and input required to execute the program\n");
return 1;
}

filename = argv[1];
return file_interpretation(argv[1], atoi(argv[2]));
}

int interactive_interpretation() {
cli_interpretation_mode = true;
ast = ast_table_new(100);
globalBooleans = boolean_table_new(100);
globalIntegers = integer_table_new(100);

char string[100];
while (true) {
printf(">>> ");
if (!fgets(string, 100, stdin)) {
fprintf(stderr, "Error while getting input\n");
return 1;
}
if ((!strcmp("exit\n", string)) || (!strcmp("exit;\n", string))) {
return 0;
}

yy_scan_string(string);
yyparse();
}
}

int file_interpretation(const char *file_name, int input) {
filename = file_name;

FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\"\n", filename);
return 0;
}

yyin = file;

/* BEGIN */
// int name_token, value_token = 0;
// while (0 != (name_token = yylex())) {
// printf("NameToken: %d, ValueToken: %d, String: %s\n", name_token,
// value_token, yytext);
// }

/* Initialization of Variables and Functions Table */
ast = ast_table_new(100);

/* Parsing */
yyin = file;
if (yyparse()) {
fclose(file);
fprintf(stderr, "%s", syntax_error_msg);
fprintf(stderr, "%s\n", syntax_error_msg);
return 1;
}
/* END */

fclose(file);

/* Sematic Analysis */
if (!verify_semantics()) {
fprintf(stderr, "\n\nSemantic Error:\n%s\n", semantic_error_msg);
fprintf(stderr, "Semantic Error: %s\n", semantic_error_msg);
return 1;
}

/* Interpreting */
int output;
int input = atoi(argv[2]);
if (!interpret(input, &output)) {
fprintf(stderr, "\n\nRuntime Error:\n%s\n", runtime_error_msg);
fprintf(stderr, "Runtime Error: %s\n", runtime_error_msg);
return 1;
}

Expand Down
30 changes: 27 additions & 3 deletions src/parser.y
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
#include "common.h"
#include "cli_interpreter.h"

#define ERROR_MSG_LEN 500
char syntax_error_msg[ERROR_MSG_LEN];
Expand Down Expand Up @@ -63,8 +64,31 @@

%%
input: %empty
| input value_definition { (ast_table_insert(ast, ($2)->name, (AST){.type = AST_VARIABLE, .value.var = $2})); }
| input function_definition { (ast_table_insert(ast, ($2)->funcname, (AST){.type = AST_FUNCTION, .value.func = $2})); };
| input expression STATEMENT_END {
if (cli_interpretation_mode) {
cli_interpret((AST){.type = AST_EXPRESSION, .value.exp = $2});
}
else {
yyerror("Standalone expression are not allowed\n");
// ???: Don't exit here
return 1;
}
}
| input value_definition {
if (cli_interpretation_mode) {
cli_interpret((AST){.type = AST_VARIABLE, .value.var = $2});
}
else {
assert(ast_table_insert(ast, ($2)->name, (AST){.type = AST_VARIABLE, .value.var = $2}));
}
}
| input function_definition {
if (cli_interpretation_mode) {
cli_interpret((AST){.type = AST_FUNCTION, .value.func = $2});
} else {
assert(ast_table_insert(ast, ($2)->funcname, (AST){.type = AST_FUNCTION, .value.func = $2}));
}
};

function_definition: KW_FUNCDEF IDENTIFIER OPEN_BRACKETS function_definition_arguments CLOSE_BRACKETS RETURN KW_BOOL ASSIGN expression STATEMENT_END { $$ = set_function_return_value(set_function_name($4, $2), BOOL, $9); }
| KW_FUNCDEF IDENTIFIER OPEN_BRACKETS function_definition_arguments CLOSE_BRACKETS RETURN KW_INT ASSIGN expression STATEMENT_END { $$ = set_function_return_value(set_function_name($4, $2), INT, $9);};
Expand Down Expand Up @@ -105,5 +129,5 @@ function_call_arguments: expression { $$ = add_function_call_argument_expression

void yyerror(char const *str) {
snprintf(syntax_error_msg, ERROR_MSG_LEN,
"ERROR: %s in %s:%d:%d\n", str, filename, yylineno, column);
"ERROR: %s in %s:%d:%d", str, filename, yylineno, column);
}
Loading