Skip to content

Commit

Permalink
Merge pull request #3 from Dayof/dev
Browse files Browse the repository at this point in the history
Third parser version before deadline
  • Loading branch information
Dayof authored Oct 30, 2020
2 parents e170382 + 069bdc7 commit ca0fad0
Show file tree
Hide file tree
Showing 15 changed files with 818 additions and 367 deletions.
89 changes: 71 additions & 18 deletions src/core/ast.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "ast.h"

ast_node* create_bin_expr(char *operator, ast_node* left, ast_node* right) {
ast_node* create_bin_expr(char* operator, ast_node* left, ast_node* right) {
if (PARSER_VERBOSE) printf("\nCreating binary expression node: .%d. .%s. .%d.\n",
left->op.integer_expr, operator, right->op.integer_expr);
ast_node* expr = (ast_node*) malloc(sizeof(ast_node));
Expand All @@ -21,53 +22,105 @@ ast_node* create_int_expr(int value) {
return expr;
}

void create_ast(ast_node* expression) {
ast_node* create_float_expr(float value) {
if (PARSER_VERBOSE) printf("\n\nCreating float expression node: %f\n", value);
ast_node* expr = (ast_node*) malloc(sizeof(ast_node));
expr->tag = FLOAT_TYPE;
expr->op.float_expr = value;
return expr;
}

ast_node* create_bool_expr(int value) {
if (PARSER_VERBOSE) printf("\n\nCreating boolean expression node: %d\n", value);
ast_node* expr = (ast_node*) malloc(sizeof(ast_node));
expr->tag = BOOL_TYPE;
expr->op.integer_expr = value;
return expr;
}

ast_node* create_var_expr(char* value) {
if (PARSER_VERBOSE) printf("\n\nCreating variable expression node: %s\n", value);
ast_node* expr = (ast_node*) malloc(sizeof(ast_node));
expr->tag = VAR_TYPE;
strcpy(expr->op.variable_expr, value);
return expr;
}

void add_ast(ast_node* expression) {
if (PARSER_VERBOSE) printf("\nCreating AST.\n");
root = expression;
if (ast_root == NULL) {
if (PARSER_VERBOSE) printf("First AST added.\n");
ast_root = (ast_list*) malloc(sizeof(ast_list));
ast_root->elem = expression;
ast_root->next = NULL;
} else {
ast_list* cur_ast = ast_root;
while (cur_ast->next != NULL) cur_ast = cur_ast->next;
ast_list* new_ast = (ast_list*) malloc(sizeof(ast_list));
new_ast->elem = expression;
new_ast->next = NULL;
cur_ast->next = new_ast;
}
return;
}

void create_empy_ast() {
if (PARSER_VERBOSE) printf("\nCreating empty AST.\n");
root = NULL;
ast_root = NULL;
return;
}

ast_node* print_exp(ast_node* node) {
if (PARSER_VERBOSE) {
if (node == NULL) {
printf("Empty expression.\n");
} else {
printf("TAG: %d\n", node->tag);
}
if (node == NULL) printf("Empty expression.\n");
else printf("TAG: %d\n", node->tag);
}
return node;
}

void print_ast(ast_node* node) {
if (node == NULL) {
printf("Empty AST.\n");
void print_asts(ast_list* root) {
if (root == NULL) {
printf("\nEmpty AST.\n");
return;
}


ast_list* cur_ast = root;
int ast_idx = 0;

while (cur_ast != NULL) {
printf("\n\n---- AST: %d\n\n", ast_idx + 1);
print_ast(cur_ast->elem, 0);
cur_ast = cur_ast->next;
ast_idx += 1;
}
}

void print_ast(ast_node* node, int lvl) {
if (PARSER_VERBOSE) printf("TAG: %d\n", node->tag);

for (int i=0; i < AST_LVL; ++i) printf(" ");
for (int i=0; i < lvl; ++i) printf(" ");

// terminal leaf
if (node->tag == INTEGER_TYPE) {
printf("INT: %d\n", node->op.integer_expr);
return;
// terminal leaf
} else if (node->tag == BOOL_TYPE) {
printf("BOOL: %d\n", node->op.integer_expr);
return;
// terminal leaf
} else if (node->tag == FLOAT_TYPE) {
printf("FLOAT: %.2f\n", node->op.float_expr);
return;
// terminal leaf
} else if (node->tag == VAR_TYPE) {
printf("VAR: %s\n", node->op.variable_expr);
return;
// non terminal node
} else if (node->tag == BINARY_TYPE) {
printf("OP: %s\n", node->op.binary_expr.operator);
AST_LVL += 1;
print_ast(node->op.binary_expr.right);
print_ast(node->op.binary_expr.left);
print_ast(node->op.binary_expr.right, lvl+1);
print_ast(node->op.binary_expr.left, lvl+1);
} else {
printf("Print AST unknown error.\n");
return;
Expand Down
31 changes: 22 additions & 9 deletions src/core/ast.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
#ifndef __AST_H__
#define __AST_H__

int LEX_VERBOSE, PARSER_VERBOSE, MAIN_VERBOSE, AST_LVL;
int line, column;
int LEX_VERBOSE, PARSER_VERBOSE, MAIN_VERBOSE;
int lex_line, lex_column, parser_line, parser_column;

enum TAG {
INTEGER_TYPE=0,
VAR_TYPE,
FLOAT_TYPE,
BOOL_TYPE,
VAR_TYPE,
BINARY_TYPE
};

typedef struct exp {
int tag;
union {
int integer_expr;
char *variable_expr;
float float_expr;
char variable_expr[79];
struct {
char *operator;
char* operator;
struct exp* left;
struct exp* right;
} binary_expr;
} op;
} ast_node;

ast_node* root;
typedef struct exps {
ast_node* elem;
struct exps* next;
} ast_list;

void print_ast(ast_node* node);
ast_list* ast_root;

int len_st();
void print_asts(ast_list* root);
void print_ast(ast_node* node, int lvl);
ast_node* print_exp(ast_node* node);

void create_empy_ast();
void create_ast(ast_node* expression);
void add_ast(ast_node* expression);
ast_node* create_int_expr(int value);
ast_node* create_bin_expr(char *operator, ast_node* left, ast_node* right);
ast_node* create_float_expr(float value);
ast_node* create_var_expr(char* value);
ast_node* create_bool_expr(int value);
ast_node* create_bin_expr(char* operator, ast_node* left, ast_node* right);

void handle_token(int token);

Expand Down
18 changes: 11 additions & 7 deletions src/core/main.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#include <stdio.h>
#include "sym_tab.h"
#include "ast.h"
#include "lexer.h"
#include "parser.h"

int main (int argc, char *argv[]) {
int main (int argc, char* argv[]) {
printf("Welcome to CPPython interpreter:\n");
MAIN_VERBOSE = LEX_VERBOSE = PARSER_VERBOSE = AST_LVL = 0;
root = NULL;
MAIN_VERBOSE = LEX_VERBOSE = PARSER_VERBOSE = 0;
ast_root = NULL;

// init lexer and parser
printf("Lexer/parser:\n");
line = column = 1;
lex_line = lex_column = parser_line = parser_column = 1;
yyin = fopen(argv[1], "r");
if (MAIN_VERBOSE) printf("\nline %d. ", line);
if (MAIN_VERBOSE) printf("\nline %d. ", lex_line);
do {
yyparse();
} while (!feof(yyin));
fclose(yyin);
printf("\nLexer and parser finished.\n\n");

printf("Abstract Syntax Tree:\n");
print_ast(root);
printf("## Abstract Syntax Trees ##");
print_asts(ast_root);

printf("\n## Symbol Table ##\n");
print_st();

return 0;
}
37 changes: 29 additions & 8 deletions src/core/sym_tab.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
#include <stdio.h>
#include "sym_tab.h"


void add_word(int key, char *name) {
struct word *s;
void add_word(int key, char* name) {
word* s;
HASH_FIND_INT(symbol_table, &key, s);
if (s == NULL) {
s = (struct word *) malloc(sizeof *s);
s = (word*) malloc(sizeof(word));
s->key = key;
HASH_ADD_INT(symbol_table, key, s);
}
strcpy(s->name, name);
}

struct word *find_word(int word_key) {
struct word *s;

word* find_word(int word_key) {
word* s;
HASH_FIND_INT(symbol_table, &word_key, s);
return s;
}

void delete_word(struct word *s) {
void delete_word(word* s) {
HASH_DEL(symbol_table, s);
free(s);
}

void delete_all() {
struct word *cur_word, *tmp;
word* cur_word, *tmp;
HASH_ITER(hh, symbol_table, cur_word, tmp) {
HASH_DEL(symbol_table, cur_word);
free(cur_word);
}
}

int len_st() {
return HASH_COUNT(symbol_table);
}

void print_st() {
int num_symbols = HASH_COUNT(symbol_table);

if (num_symbols == 0) {
printf("Empty symbol table.\n");
return;
}

printf("Size: %u\n\n", num_symbols);

word* cur_word, *tmp;
HASH_ITER(hh, symbol_table, cur_word, tmp) {
printf("KEY: %d, NAME: %s\n", cur_word->key, cur_word->name);
HASH_DEL(symbol_table, cur_word);
free(cur_word);
}
Expand Down
12 changes: 7 additions & 5 deletions src/core/sym_tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

#include "uthash.h"

struct word {
typedef struct word_pos {
int key;
char name[50];
UT_hash_handle hh; /* makes this structure hashable */
};
} word;

struct word *symbol_table;
word* symbol_table;

void print_st();

void add_word(int key, char *name);
struct word *find_word(int word_key);
void delete_word(struct word *s);
word* find_word(int word_key);
void delete_word(word* s);
void delete_all();

#endif // __SYM_TAB_H__
Loading

0 comments on commit ca0fad0

Please sign in to comment.