Move stuff around
This commit is contained in:
parent
b4243755ef
commit
9764aff184
@ -2,6 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "syntax.h"
|
||||||
#include "printtree.h"
|
#include "printtree.h"
|
||||||
|
|
||||||
|
|
||||||
|
71
src/parser.c
71
src/parser.c
@ -3,78 +3,9 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "parser.h"
|
#include "syntax.h"
|
||||||
#include "extstring.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* parse(FILE* file) {
|
||||||
struct SyntaxElement* syntaxtree = se_create();
|
struct SyntaxElement* syntaxtree = se_create();
|
||||||
struct SyntaxElement* syntaxtreestart = syntaxtree;
|
struct SyntaxElement* syntaxtreestart = syntaxtree;
|
||||||
|
24
src/parser.h
24
src/parser.h
@ -1,27 +1,3 @@
|
|||||||
#include <stdio.h>
|
#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);
|
struct SyntaxElement* parse(FILE* file);
|
||||||
#endif
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "parser.h"
|
#include "syntax.h"
|
||||||
|
|
||||||
void printtree(struct SyntaxElement* syntaxtree, int depth) {
|
void printtree(struct SyntaxElement* syntaxtree, int depth) {
|
||||||
if (syntaxtree == NULL)
|
if (syntaxtree == NULL)
|
||||||
|
73
src/syntax.c
Normal file
73
src/syntax.c
Normal 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
25
src/syntax.h
Normal 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
|
Loading…
Reference in New Issue
Block a user