Compare commits
2 Commits
7ed5367f20
...
e473615eca
Author | SHA1 | Date | |
---|---|---|---|
e473615eca | |||
6fae121463 |
@ -1,34 +1,34 @@
|
||||
#include <stdlib.h>
|
||||
#include "extstring.h"
|
||||
|
||||
void es_addsymbol(struct Extstring* string, char symbol) {
|
||||
void es_addsymbol(tExtstring* string, char symbol) {
|
||||
if (string == NULL)
|
||||
return;
|
||||
struct Extstring* ptr = string;
|
||||
tExtstring* ptr = string;
|
||||
while (ptr->next != NULL) {
|
||||
ptr = ptr->next;
|
||||
};
|
||||
ptr->next = malloc(sizeof(struct Extstring));
|
||||
ptr->next = malloc(sizeof(tExtstring));
|
||||
ptr->next->symbol = symbol;
|
||||
return;
|
||||
}
|
||||
|
||||
int es_size(struct Extstring* string) {
|
||||
int es_size(tExtstring* string) {
|
||||
int size = 0;
|
||||
struct Extstring* ptr = string;
|
||||
tExtstring* ptr = string;
|
||||
for (; ptr != NULL; size++) {
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void es_free(struct Extstring* string) {
|
||||
void es_free(tExtstring* string) {
|
||||
if (string->next == NULL) {
|
||||
free(string);
|
||||
return;
|
||||
}
|
||||
struct Extstring* ptr = string;
|
||||
struct Extstring* nextptr = ptr->next;
|
||||
tExtstring* ptr = string;
|
||||
tExtstring* nextptr = ptr->next;
|
||||
while (ptr->next != NULL) {
|
||||
nextptr = ptr->next;
|
||||
free(ptr);
|
||||
@ -37,8 +37,8 @@ void es_free(struct Extstring* string) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
char* es_tostring(struct Extstring* string) {
|
||||
struct Extstring* ptr = string;
|
||||
char* es_tostring(tExtstring* string) {
|
||||
tExtstring* ptr = string;
|
||||
int size = es_size(string) + 1;
|
||||
char* cstring = malloc(size);
|
||||
for (int i = 0; i != size; i++) {
|
||||
|
@ -1,9 +1,9 @@
|
||||
struct Extstring {
|
||||
typedef struct Extstring {
|
||||
char symbol;
|
||||
struct Extstring* next;
|
||||
};
|
||||
} tExtstring;
|
||||
|
||||
void es_addsymbol(struct Extstring* string, char symbol);
|
||||
int es_size(struct Extstring* string);
|
||||
void es_free(struct Extstring* string);
|
||||
char* es_tostring(struct Extstring* string);
|
||||
void es_addsymbol(tExtstring* string, char symbol);
|
||||
int es_size(tExtstring* string);
|
||||
void es_free(tExtstring* string);
|
||||
char* es_tostring(tExtstring* string);
|
||||
|
@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr,"Unable to open file.\n");
|
||||
return 1;
|
||||
}
|
||||
struct SyntaxElement* code = parse(file);
|
||||
tSyntaxElement* code = parse(file);
|
||||
fclose(file);
|
||||
if (code == NULL) {
|
||||
fprintf(stderr, "Error during parsing.\n");
|
||||
|
18
src/parser.c
18
src/parser.c
@ -3,20 +3,20 @@
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "syntax.h"
|
||||
#include "parser.h"
|
||||
#include "extstring.h"
|
||||
|
||||
struct SyntaxElement* parse(FILE* file) {
|
||||
struct SyntaxElement* syntaxtree = se_create();
|
||||
struct SyntaxElement* syntaxtreestart = syntaxtree;
|
||||
tSyntaxElement* parse(FILE* file) {
|
||||
tSyntaxElement* syntaxtree = se_create();
|
||||
tSyntaxElement* syntaxtreestart = syntaxtree;
|
||||
syntaxtree->type = TREE;
|
||||
char symbol;
|
||||
bool intree = false;
|
||||
bool incomment = false;
|
||||
bool instring = false;
|
||||
bool intoken = false;
|
||||
struct Extstring* exttoken = NULL;
|
||||
struct Extstring* extstring = NULL;
|
||||
tExtstring* exttoken = NULL;
|
||||
tExtstring* extstring = NULL;
|
||||
while (1) {
|
||||
symbol = fgetc(file);
|
||||
if (feof(file))
|
||||
@ -29,7 +29,7 @@ struct SyntaxElement* parse(FILE* file) {
|
||||
if (instring) {
|
||||
if (symbol == '"') {
|
||||
if (extstring == NULL) {
|
||||
extstring = malloc(sizeof(struct Extstring));
|
||||
extstring = malloc(sizeof(tExtstring));
|
||||
extstring->next = NULL;
|
||||
}
|
||||
instring = false;
|
||||
@ -41,7 +41,7 @@ struct SyntaxElement* parse(FILE* file) {
|
||||
syntaxtree = se_next(syntaxtree);
|
||||
} else {
|
||||
if (extstring == NULL) {
|
||||
extstring = malloc(sizeof(struct Extstring));
|
||||
extstring = malloc(sizeof(tExtstring));
|
||||
extstring->symbol = symbol;
|
||||
extstring->next = NULL;
|
||||
continue;
|
||||
@ -90,7 +90,7 @@ struct SyntaxElement* parse(FILE* file) {
|
||||
instring = true;
|
||||
continue;
|
||||
default:
|
||||
exttoken = malloc(sizeof(struct Extstring));
|
||||
exttoken = malloc(sizeof(tExtstring));
|
||||
exttoken->symbol = symbol;
|
||||
exttoken->next = NULL;
|
||||
intoken = true;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
struct SyntaxElement* parse(FILE* file);
|
||||
#include "syntax.h"
|
||||
|
||||
tSyntaxElement* parse(FILE* file);
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include "syntax.h"
|
||||
|
||||
void printtree(struct SyntaxElement* syntaxtree, int depth) {
|
||||
void printtree(tSyntaxElement* syntaxtree, int depth) {
|
||||
if (syntaxtree == NULL)
|
||||
return;
|
||||
for (int i = 1; i <= depth; i++)
|
||||
@ -11,7 +11,7 @@ void printtree(struct SyntaxElement* syntaxtree, int depth) {
|
||||
switch (syntaxtree->type) {
|
||||
case TREE:
|
||||
printf("TREE\n");
|
||||
printtree((struct SyntaxElement*)syntaxtree->content, depth + 1);
|
||||
printtree((tSyntaxElement*)syntaxtree->content, depth + 1);
|
||||
break;
|
||||
case TOKEN:
|
||||
printf("TOKEN: %s\n", (char*)syntaxtree->content);
|
||||
|
@ -1,2 +1,2 @@
|
||||
#include "parser.h"
|
||||
void printtree(struct SyntaxElement* syntaxtree, int depth);
|
||||
void printtree(tSyntaxElement* syntaxtree, int depth);
|
||||
|
26
src/syntax.c
26
src/syntax.c
@ -2,8 +2,12 @@
|
||||
|
||||
#include "syntax.h"
|
||||
|
||||
struct SyntaxElement* se_create(void) {
|
||||
struct SyntaxElement* syntaxelement = malloc(sizeof(struct SyntaxElement));
|
||||
bool se_istraversable(tSyntaxElement* syntaxtree) {
|
||||
return syntaxtree->type == TREE && syntaxtree->content != NULL;
|
||||
}
|
||||
|
||||
tSyntaxElement* se_create(void) {
|
||||
tSyntaxElement* syntaxelement = malloc(sizeof(tSyntaxElement));
|
||||
syntaxelement->type = NONE;
|
||||
syntaxelement->content = NULL;
|
||||
syntaxelement->next = NULL;
|
||||
@ -12,26 +16,26 @@ struct SyntaxElement* se_create(void) {
|
||||
|
||||
}
|
||||
|
||||
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement) {
|
||||
tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement) {
|
||||
syntaxelement->content = se_create();
|
||||
((struct SyntaxElement*)syntaxelement->content)->top = syntaxelement;
|
||||
return ((struct SyntaxElement*)syntaxelement->content);
|
||||
((tSyntaxElement*)syntaxelement->content)->top = syntaxelement;
|
||||
return ((tSyntaxElement*)syntaxelement->content);
|
||||
}
|
||||
|
||||
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
|
||||
tSyntaxElement* se_next(tSyntaxElement* syntaxelement) {
|
||||
syntaxelement->next = se_create();
|
||||
syntaxelement->next->top = syntaxelement->top;
|
||||
return syntaxelement->next;
|
||||
}
|
||||
|
||||
void se_free(struct SyntaxElement* syntaxtree) {
|
||||
void se_free(tSyntaxElement* syntaxtree) {
|
||||
switch (syntaxtree->type) {
|
||||
case NONE:
|
||||
free(syntaxtree);
|
||||
return;
|
||||
case TREE:
|
||||
if (syntaxtree->content != NULL) {
|
||||
se_free((struct SyntaxElement*)syntaxtree->content);
|
||||
se_free((tSyntaxElement*)syntaxtree->content);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -45,7 +49,7 @@ void se_free(struct SyntaxElement* syntaxtree) {
|
||||
}
|
||||
|
||||
// Remove NONEs from the syntax tree.
|
||||
void se_clean(struct SyntaxElement* syntaxtree) {
|
||||
void se_clean(tSyntaxElement* syntaxtree) {
|
||||
if (syntaxtree->next != NULL) {
|
||||
switch (syntaxtree->next->type) {
|
||||
case NONE:
|
||||
@ -58,13 +62,13 @@ void se_clean(struct SyntaxElement* syntaxtree) {
|
||||
}
|
||||
}
|
||||
if (syntaxtree->content != NULL && (syntaxtree->type == TREE)) {
|
||||
switch (((struct SyntaxElement*)syntaxtree->content)->type) {
|
||||
switch (((tSyntaxElement*)syntaxtree->content)->type) {
|
||||
case NONE:
|
||||
free(syntaxtree->content);
|
||||
syntaxtree->content = NULL;
|
||||
break;
|
||||
default:
|
||||
se_clean((struct SyntaxElement*)syntaxtree->content);
|
||||
se_clean((tSyntaxElement*)syntaxtree->content);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
19
src/syntax.h
19
src/syntax.h
@ -1,5 +1,7 @@
|
||||
#ifndef SYNTAX_H
|
||||
#define SYNTAX_H
|
||||
#include <stdbool.h>
|
||||
|
||||
enum SyntaxElementType {
|
||||
TREE,
|
||||
TOKEN,
|
||||
@ -8,17 +10,18 @@ enum SyntaxElementType {
|
||||
NONE
|
||||
};
|
||||
|
||||
struct SyntaxElement {
|
||||
typedef struct SyntaxElement {
|
||||
enum SyntaxElementType type;
|
||||
void* content;
|
||||
struct SyntaxElement* next;
|
||||
struct SyntaxElement* top;
|
||||
};
|
||||
} tSyntaxElement;
|
||||
|
||||
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);
|
||||
void se_clean(tSyntaxElement* syntaxtree);
|
||||
bool se_istraversable(tSyntaxElement* syntaxtree);
|
||||
tSyntaxElement* se_create(void);
|
||||
tSyntaxElement* se_init(void);
|
||||
tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement);
|
||||
tSyntaxElement* se_next(tSyntaxElement* syntaxelement);
|
||||
void se_free(tSyntaxElement* syntaxtree);
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user