added mote syntax elements
This commit is contained in:
parent
0a54dd5783
commit
86c74f66c2
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
|
|
30
src/main.c
30
src/main.c
|
@ -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
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ void setup(void)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// actual setup
|
// actual setup
|
||||||
|
AST_init();
|
||||||
|
|
||||||
DEBUG("finished starting up gemstone...");
|
DEBUG("finished starting up gemstone...");
|
||||||
}
|
}
|
||||||
|
@ -66,26 +67,13 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
Loading…
Reference in New Issue