Move stuff around

This commit is contained in:
bʰedoh₂ swé 2024-06-03 23:49:56 +05:00
parent b4243755ef
commit 9764aff184
6 changed files with 101 additions and 95 deletions

View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include "parser.h"
#include "syntax.h"
#include "printtree.h"

View File

@ -3,78 +3,9 @@
#include <stdbool.h>
#include <ctype.h>
#include "parser.h"
#include "syntax.h"
#include "extstring.h"
struct SyntaxElement* se_create(void) {
struct SyntaxElement* syntaxelement = malloc(sizeof(struct SyntaxElement));
syntaxelement->type = NONE;
syntaxelement->content = NULL;
syntaxelement->next = NULL;
syntaxelement->top = NULL;
return syntaxelement;
}
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement) {
syntaxelement->content = se_create();
((struct SyntaxElement*)syntaxelement->content)->top = syntaxelement;
return ((struct SyntaxElement*)syntaxelement->content);
}
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
syntaxelement->next = se_create();
syntaxelement->next->top = syntaxelement->top;
return syntaxelement->next;
}
void se_free(struct SyntaxElement* syntaxtree) {
switch (syntaxtree->type) {
case NONE:
free(syntaxtree);
return;
case TREE:
case TOPTREE:
if (syntaxtree->content != NULL) {
se_free((struct SyntaxElement*)syntaxtree->content);
}
break;
default:
free(syntaxtree->content);
break;
}
if (syntaxtree->next != NULL) {
se_free(syntaxtree->next);
}
free(syntaxtree);
}
// Remove NONEs from the syntax tree.
void se_clean(struct SyntaxElement* syntaxtree) {
if (syntaxtree->next != NULL) {
switch (syntaxtree->next->type) {
case NONE:
free(syntaxtree->next);
syntaxtree->next = NULL;
break;
default:
se_clean(syntaxtree->next);
break;
}
}
if (syntaxtree->content != NULL && (syntaxtree->type == TOPTREE || syntaxtree->type == TREE)) {
switch (((struct SyntaxElement*)syntaxtree->content)->type) {
case NONE:
free(syntaxtree->content);
syntaxtree->content = NULL;
break;
default:
se_clean((struct SyntaxElement*)syntaxtree->content);
break;
}
}
}
struct SyntaxElement* parse(FILE* file) {
struct SyntaxElement* syntaxtree = se_create();
struct SyntaxElement* syntaxtreestart = syntaxtree;

View File

@ -1,27 +1,3 @@
#include <stdio.h>
#ifndef PARSER_H
#define PARSER_H
struct SyntaxElement* se_init(void);
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement);
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement);
void se_free(struct SyntaxElement* syntaxtree);
enum SyntaxElementType {
TOPTREE,
TREE,
TOKEN,
STRING,
NUMBER,
NONE
};
struct SyntaxElement {
enum SyntaxElementType type;
void* content;
struct SyntaxElement* next;
struct SyntaxElement* top;
};
struct SyntaxElement* parse(FILE* file);
#endif

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#include "syntax.h"
void printtree(struct SyntaxElement* syntaxtree, int depth) {
if (syntaxtree == NULL)

73
src/syntax.c Normal file
View File

@ -0,0 +1,73 @@
#include <stdlib.h>
#include "syntax.h"
struct SyntaxElement* se_create(void) {
struct SyntaxElement* syntaxelement = malloc(sizeof(struct SyntaxElement));
syntaxelement->type = NONE;
syntaxelement->content = NULL;
syntaxelement->next = NULL;
syntaxelement->top = NULL;
return syntaxelement;
}
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement) {
syntaxelement->content = se_create();
((struct SyntaxElement*)syntaxelement->content)->top = syntaxelement;
return ((struct SyntaxElement*)syntaxelement->content);
}
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
syntaxelement->next = se_create();
syntaxelement->next->top = syntaxelement->top;
return syntaxelement->next;
}
void se_free(struct SyntaxElement* syntaxtree) {
switch (syntaxtree->type) {
case NONE:
free(syntaxtree);
return;
case TREE:
case TOPTREE:
if (syntaxtree->content != NULL) {
se_free((struct SyntaxElement*)syntaxtree->content);
}
break;
default:
free(syntaxtree->content);
break;
}
if (syntaxtree->next != NULL) {
se_free(syntaxtree->next);
}
free(syntaxtree);
}
// Remove NONEs from the syntax tree.
void se_clean(struct SyntaxElement* syntaxtree) {
if (syntaxtree->next != NULL) {
switch (syntaxtree->next->type) {
case NONE:
free(syntaxtree->next);
syntaxtree->next = NULL;
break;
default:
se_clean(syntaxtree->next);
break;
}
}
if (syntaxtree->content != NULL && (syntaxtree->type == TOPTREE || syntaxtree->type == TREE)) {
switch (((struct SyntaxElement*)syntaxtree->content)->type) {
case NONE:
free(syntaxtree->content);
syntaxtree->content = NULL;
break;
default:
se_clean((struct SyntaxElement*)syntaxtree->content);
break;
}
}
}

25
src/syntax.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef SYNTAX_H
#define SYNTAX_H
enum SyntaxElementType {
TOPTREE,
TREE,
TOKEN,
STRING,
NUMBER,
NONE
};
struct SyntaxElement {
enum SyntaxElementType type;
void* content;
struct SyntaxElement* next;
struct SyntaxElement* top;
};
void se_clean(struct SyntaxElement* syntaxtree);
struct SyntaxElement* se_create(void);
struct SyntaxElement* se_init(void);
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement);
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement);
void se_free(struct SyntaxElement* syntaxtree);
#endif