added mote syntax elements

This commit is contained in:
Sven Vogel 2024-05-07 13:04:22 +02:00
parent 0a54dd5783
commit 86c74f66c2
3 changed files with 79 additions and 46 deletions

View File

@ -23,27 +23,64 @@ struct AST_Node_t *AST_new_node(enum AST_SyntaxElement_t kind, const char* value
return node; return node;
} }
static const char* lookup_table[AST_ELEMENT_COUNT] = { "__UNINIT__" };
void AST_init() {
DEBUG("initializing global syntax tree...");
INFO("filling lookup table...");
lookup_table[AST_Expr] = "expr";
lookup_table[AST_Add] = "+";
lookup_table[AST_Sub] = "-";
lookup_table[AST_Mul] = "*";
lookup_table[AST_Div] = "/";
lookup_table[AST_BitAnd] = "&";
lookup_table[AST_BitOr] = "|";
lookup_table[AST_BitXor] = "^";
lookup_table[AST_BitNot] = "!";
lookup_table[AST_Eq] = "==";
lookup_table[AST_Less] = "<";
lookup_table[AST_Greater] = ">";
lookup_table[AST_BoolAnd] = "&&";
lookup_table[AST_BoolOr] = "||";
lookup_table[AST_BoolXor] = "^^";
lookup_table[AST_BoolNot] = "!!";
lookup_table[AST_While] = "while";
lookup_table[AST_If] = "if";
lookup_table[AST_IfElse] = "else if";
lookup_table[AST_Else] = "else";
lookup_table[AST_Decl] = "decl";
lookup_table[AST_Assign] = "assign";
lookup_table[AST_Def] = "def";
lookup_table[AST_Typedef] = "typedef";
lookup_table[AST_Box] = "box";
lookup_table[AST_Fun] = "fun";
}
const char* AST_node_to_string(struct AST_Node_t* node) { const char* AST_node_to_string(struct AST_Node_t* node) {
DEBUG("converting AST node to string: %p", node); DEBUG("converting AST node to string: %p", node);
const char* string = "unknown"; const char* string = "unknown";
switch (node->kind) { switch(node->kind) {
case AST_Expression: case AST_Int:
string = "expression"; case AST_Float:
break; case AST_String:
case AST_Statement: case AST_Ident:
string = "statement"; case AST_Macro:
break; case AST_Import:
case AST_Branch:
string = "if";
break;
case AST_IntegerLiteral:
string = node->value; string = node->value;
break; break;
case AST_OperatorAdd: default:
string = "+"; string = lookup_table[node->kind];
break;
} }
return string; return string;

View File

@ -6,7 +6,7 @@
// Syntax elements which are stored in a syntax tree // Syntax elements which are stored in a syntax tree
enum AST_SyntaxElement_t { enum AST_SyntaxElement_t {
AST_Stmt, AST_Stmt = 0,
AST_Expr, AST_Expr,
// Literals // Literals
AST_Int, AST_Int,
@ -50,7 +50,10 @@ enum AST_SyntaxElement_t {
AST_Typedef, AST_Typedef,
AST_Box, AST_Box,
AST_Fun, AST_Fun,
AST_Import AST_Import,
// amount of variants
// in this enum
AST_ELEMENT_COUNT
}; };
struct AST_Node_t { struct AST_Node_t {
@ -69,6 +72,11 @@ struct AST_Node_t {
struct AST_Node_t **children; struct AST_Node_t **children;
}; };
typedef struct AST_Node_t* AST_NODE_PTR;
// initialize the global AST state
void AST_init(void);
// return a string representation of the nodes type and its value // return a string representation of the nodes type and its value
// does not take into account its children or parent // does not take into account its children or parent
const char* AST_node_to_string(struct AST_Node_t* node); const char* AST_node_to_string(struct AST_Node_t* node);

View File

@ -1,7 +1,7 @@
#include <ast/ast.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/log.h> #include <sys/log.h>
#include <yacc/parser.tab.h>
#include <ast/ast.h>
#define LOG_LEVEL LOG_LEVEL_DEBUG #define LOG_LEVEL LOG_LEVEL_DEBUG
@ -9,7 +9,7 @@ extern FILE* yyin;
/** /**
* @brief Log a debug message to inform about beginning exit procedures * @brief Log a debug message to inform about beginning exit procedures
* *
*/ */
void notify_exit(void) void notify_exit(void)
{ {
@ -18,7 +18,7 @@ void notify_exit(void)
/** /**
* @brief Closes File after compiling. * @brief Closes File after compiling.
* *
*/ */
void close_file(void) void close_file(void)
@ -31,7 +31,7 @@ void close_file(void)
/** /**
* @brief Run compiler setup here * @brief Run compiler setup here
* *
*/ */
void setup(void) void setup(void)
{ {
@ -40,12 +40,13 @@ void setup(void)
log_init(); log_init();
DEBUG("starting gemstone..."); DEBUG("starting gemstone...");
#if LOG_LEVEL <= LOG_LEVEL_DEBUG #if LOG_LEVEL <= LOG_LEVEL_DEBUG
atexit(&notify_exit); atexit(&notify_exit);
#endif #endif
// actual setup // actual setup
AST_init();
DEBUG("finished starting up gemstone..."); DEBUG("finished starting up gemstone...");
} }
@ -53,39 +54,26 @@ int main(int argc, char *argv[]) {
setup(); setup();
atexit(close_file); atexit(close_file);
// Check for file input as argument // Check for file input as argument
if (2 != argc) if (2 != argc)
{ {
INFO("Usage: %s <filename>\n", argv[0]); INFO("Usage: %s <filename>\n", argv[0]);
PANIC("No File could be found"); PANIC("No File could be found");
} }
// filename as first argument // filename as first argument
char *filename = argv[1]; char *filename = argv[1];
FILE *file = fopen(filename, "r"); FILE *file = fopen(filename, "r");
struct AST_Node_t* node = AST_new_node(AST_Branch, NULL); if (NULL == file)
{
PANIC("File couldn't be opened!");
}
yyin = file;
struct AST_Node_t* child = AST_new_node(AST_OperatorAdd, NULL); yyparse();
AST_push_node(child, AST_new_node(AST_IntegerLiteral, "43"));
AST_push_node(child, AST_new_node(AST_IntegerLiteral, "9"));
AST_push_node(node, child);
AST_push_node(node, AST_new_node(AST_Expression, NULL));
AST_push_node(node, AST_new_node(AST_Expression, NULL));
FILE* out = fopen("ast.gv", "w+");
// convert this file ^^^^^^
// to an svg with: `dot -Tsvg ast.gv > graph.svg`
AST_fprint_graphviz(out, node);
AST_delete_node(node);
fflush(out);
fclose(out);
return 0; return 0;
} }