Print parsed tree on run

This commit is contained in:
bʰedoh₂ swé 2024-06-03 22:27:50 +05:00
parent 4c1f95370c
commit 5392258a0d
4 changed files with 44 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include "parser.h"
#include "printtree.h"
int main(int argc, char *argv[]) {
@ -18,5 +19,6 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Error during parsing.\n");
return 1;
}
printtree(code,0);
return 0;
}

View File

@ -1,5 +1,7 @@
#include <stdio.h>
#ifndef PARSER_H
#define PARSER_H
enum SyntaxElementType {
TOPTREE,
TREE,
@ -17,3 +19,4 @@ struct SyntaxElement {
};
struct SyntaxElement* parse(FILE* file);
#endif

37
src/printtree.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
void printtree(struct SyntaxElement* syntaxtree, int depth) {
if (syntaxtree == NULL)
return;
for (int i = 1; i <= depth; i++)
putc(' ',stdout);
switch (syntaxtree->type) {
case TOPTREE:
printf("TOPTREE\n");
printtree((struct SyntaxElement*)syntaxtree->content, depth + 1);
break;
case TREE:
printf("TREE\n");
printtree((struct SyntaxElement*)syntaxtree->content, depth + 1);
break;
case TOKEN:
printf("TOKEN: %s\n", (char*)syntaxtree->content);
break;
case STRING:
printf("STRING: %s\n", (char*)syntaxtree->content);
break;
case NUMBER:
// TODO: printtree NUMBER
fprintf(stderr,"TODO: printtree NUMBER\n");
exit(1);
case NONE:
printf("NONE\n");
break;
}
if (syntaxtree->next != NULL)
printtree(syntaxtree->next, depth);
return;
}

2
src/printtree.h Normal file
View File

@ -0,0 +1,2 @@
#include "parser.h"
void printtree(struct SyntaxElement* syntaxtree, int depth);