Compare commits

...

2 Commits

Author SHA1 Message Date
e473615eca Remove "struct" keyword whenever possible 2024-06-04 00:12:16 +05:00
6fae121463 Add se_istravesable 2024-06-04 00:06:51 +05:00
9 changed files with 58 additions and 49 deletions

View File

@ -1,34 +1,34 @@
#include <stdlib.h> #include <stdlib.h>
#include "extstring.h" #include "extstring.h"
void es_addsymbol(struct Extstring* string, char symbol) { void es_addsymbol(tExtstring* string, char symbol) {
if (string == NULL) if (string == NULL)
return; return;
struct Extstring* ptr = string; tExtstring* ptr = string;
while (ptr->next != NULL) { while (ptr->next != NULL) {
ptr = ptr->next; ptr = ptr->next;
}; };
ptr->next = malloc(sizeof(struct Extstring)); ptr->next = malloc(sizeof(tExtstring));
ptr->next->symbol = symbol; ptr->next->symbol = symbol;
return; return;
} }
int es_size(struct Extstring* string) { int es_size(tExtstring* string) {
int size = 0; int size = 0;
struct Extstring* ptr = string; tExtstring* ptr = string;
for (; ptr != NULL; size++) { for (; ptr != NULL; size++) {
ptr = ptr->next; ptr = ptr->next;
} }
return size; return size;
} }
void es_free(struct Extstring* string) { void es_free(tExtstring* string) {
if (string->next == NULL) { if (string->next == NULL) {
free(string); free(string);
return; return;
} }
struct Extstring* ptr = string; tExtstring* ptr = string;
struct Extstring* nextptr = ptr->next; tExtstring* nextptr = ptr->next;
while (ptr->next != NULL) { while (ptr->next != NULL) {
nextptr = ptr->next; nextptr = ptr->next;
free(ptr); free(ptr);
@ -37,8 +37,8 @@ void es_free(struct Extstring* string) {
free(ptr); free(ptr);
} }
char* es_tostring(struct Extstring* string) { char* es_tostring(tExtstring* string) {
struct Extstring* ptr = string; tExtstring* ptr = string;
int size = es_size(string) + 1; int size = es_size(string) + 1;
char* cstring = malloc(size); char* cstring = malloc(size);
for (int i = 0; i != size; i++) { for (int i = 0; i != size; i++) {

View File

@ -1,9 +1,9 @@
struct Extstring { typedef struct Extstring {
char symbol; char symbol;
struct Extstring* next; struct Extstring* next;
}; } tExtstring;
void es_addsymbol(struct Extstring* string, char symbol); void es_addsymbol(tExtstring* string, char symbol);
int es_size(struct Extstring* string); int es_size(tExtstring* string);
void es_free(struct Extstring* string); void es_free(tExtstring* string);
char* es_tostring(struct Extstring* string); char* es_tostring(tExtstring* string);

View File

@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr,"Unable to open file.\n"); fprintf(stderr,"Unable to open file.\n");
return 1; return 1;
} }
struct SyntaxElement* code = parse(file); tSyntaxElement* code = parse(file);
fclose(file); fclose(file);
if (code == NULL) { if (code == NULL) {
fprintf(stderr, "Error during parsing.\n"); fprintf(stderr, "Error during parsing.\n");

View File

@ -3,20 +3,20 @@
#include <stdbool.h> #include <stdbool.h>
#include <ctype.h> #include <ctype.h>
#include "syntax.h" #include "parser.h"
#include "extstring.h" #include "extstring.h"
struct SyntaxElement* parse(FILE* file) { tSyntaxElement* parse(FILE* file) {
struct SyntaxElement* syntaxtree = se_create(); tSyntaxElement* syntaxtree = se_create();
struct SyntaxElement* syntaxtreestart = syntaxtree; tSyntaxElement* syntaxtreestart = syntaxtree;
syntaxtree->type = TREE; syntaxtree->type = TREE;
char symbol; char symbol;
bool intree = false; bool intree = false;
bool incomment = false; bool incomment = false;
bool instring = false; bool instring = false;
bool intoken = false; bool intoken = false;
struct Extstring* exttoken = NULL; tExtstring* exttoken = NULL;
struct Extstring* extstring = NULL; tExtstring* extstring = NULL;
while (1) { while (1) {
symbol = fgetc(file); symbol = fgetc(file);
if (feof(file)) if (feof(file))
@ -29,7 +29,7 @@ struct SyntaxElement* parse(FILE* file) {
if (instring) { if (instring) {
if (symbol == '"') { if (symbol == '"') {
if (extstring == NULL) { if (extstring == NULL) {
extstring = malloc(sizeof(struct Extstring)); extstring = malloc(sizeof(tExtstring));
extstring->next = NULL; extstring->next = NULL;
} }
instring = false; instring = false;
@ -41,7 +41,7 @@ struct SyntaxElement* parse(FILE* file) {
syntaxtree = se_next(syntaxtree); syntaxtree = se_next(syntaxtree);
} else { } else {
if (extstring == NULL) { if (extstring == NULL) {
extstring = malloc(sizeof(struct Extstring)); extstring = malloc(sizeof(tExtstring));
extstring->symbol = symbol; extstring->symbol = symbol;
extstring->next = NULL; extstring->next = NULL;
continue; continue;
@ -90,7 +90,7 @@ struct SyntaxElement* parse(FILE* file) {
instring = true; instring = true;
continue; continue;
default: default:
exttoken = malloc(sizeof(struct Extstring)); exttoken = malloc(sizeof(tExtstring));
exttoken->symbol = symbol; exttoken->symbol = symbol;
exttoken->next = NULL; exttoken->next = NULL;
intoken = true; intoken = true;

View File

@ -1,3 +1,5 @@
#include <stdio.h> #include <stdio.h>
struct SyntaxElement* parse(FILE* file); #include "syntax.h"
tSyntaxElement* parse(FILE* file);

View File

@ -3,7 +3,7 @@
#include "syntax.h" #include "syntax.h"
void printtree(struct SyntaxElement* syntaxtree, int depth) { void printtree(tSyntaxElement* syntaxtree, int depth) {
if (syntaxtree == NULL) if (syntaxtree == NULL)
return; return;
for (int i = 1; i <= depth; i++) for (int i = 1; i <= depth; i++)
@ -11,7 +11,7 @@ void printtree(struct SyntaxElement* syntaxtree, int depth) {
switch (syntaxtree->type) { switch (syntaxtree->type) {
case TREE: case TREE:
printf("TREE\n"); printf("TREE\n");
printtree((struct SyntaxElement*)syntaxtree->content, depth + 1); printtree((tSyntaxElement*)syntaxtree->content, depth + 1);
break; break;
case TOKEN: case TOKEN:
printf("TOKEN: %s\n", (char*)syntaxtree->content); printf("TOKEN: %s\n", (char*)syntaxtree->content);

View File

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

View File

@ -2,8 +2,12 @@
#include "syntax.h" #include "syntax.h"
struct SyntaxElement* se_create(void) { bool se_istraversable(tSyntaxElement* syntaxtree) {
struct SyntaxElement* syntaxelement = malloc(sizeof(struct SyntaxElement)); return syntaxtree->type == TREE && syntaxtree->content != NULL;
}
tSyntaxElement* se_create(void) {
tSyntaxElement* syntaxelement = malloc(sizeof(tSyntaxElement));
syntaxelement->type = NONE; syntaxelement->type = NONE;
syntaxelement->content = NULL; syntaxelement->content = NULL;
syntaxelement->next = 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(); syntaxelement->content = se_create();
((struct SyntaxElement*)syntaxelement->content)->top = syntaxelement; ((tSyntaxElement*)syntaxelement->content)->top = syntaxelement;
return ((struct SyntaxElement*)syntaxelement->content); return ((tSyntaxElement*)syntaxelement->content);
} }
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) { tSyntaxElement* se_next(tSyntaxElement* syntaxelement) {
syntaxelement->next = se_create(); syntaxelement->next = se_create();
syntaxelement->next->top = syntaxelement->top; syntaxelement->next->top = syntaxelement->top;
return syntaxelement->next; return syntaxelement->next;
} }
void se_free(struct SyntaxElement* syntaxtree) { void se_free(tSyntaxElement* syntaxtree) {
switch (syntaxtree->type) { switch (syntaxtree->type) {
case NONE: case NONE:
free(syntaxtree); free(syntaxtree);
return; return;
case TREE: case TREE:
if (syntaxtree->content != NULL) { if (syntaxtree->content != NULL) {
se_free((struct SyntaxElement*)syntaxtree->content); se_free((tSyntaxElement*)syntaxtree->content);
} }
break; break;
default: default:
@ -45,7 +49,7 @@ void se_free(struct SyntaxElement* syntaxtree) {
} }
// Remove NONEs from the syntax tree. // Remove NONEs from the syntax tree.
void se_clean(struct SyntaxElement* syntaxtree) { void se_clean(tSyntaxElement* syntaxtree) {
if (syntaxtree->next != NULL) { if (syntaxtree->next != NULL) {
switch (syntaxtree->next->type) { switch (syntaxtree->next->type) {
case NONE: case NONE:
@ -58,13 +62,13 @@ void se_clean(struct SyntaxElement* syntaxtree) {
} }
} }
if (syntaxtree->content != NULL && (syntaxtree->type == TREE)) { if (syntaxtree->content != NULL && (syntaxtree->type == TREE)) {
switch (((struct SyntaxElement*)syntaxtree->content)->type) { switch (((tSyntaxElement*)syntaxtree->content)->type) {
case NONE: case NONE:
free(syntaxtree->content); free(syntaxtree->content);
syntaxtree->content = NULL; syntaxtree->content = NULL;
break; break;
default: default:
se_clean((struct SyntaxElement*)syntaxtree->content); se_clean((tSyntaxElement*)syntaxtree->content);
break; break;
} }
} }

View File

@ -1,5 +1,7 @@
#ifndef SYNTAX_H #ifndef SYNTAX_H
#define SYNTAX_H #define SYNTAX_H
#include <stdbool.h>
enum SyntaxElementType { enum SyntaxElementType {
TREE, TREE,
TOKEN, TOKEN,
@ -8,17 +10,18 @@ enum SyntaxElementType {
NONE NONE
}; };
struct SyntaxElement { typedef struct SyntaxElement {
enum SyntaxElementType type; enum SyntaxElementType type;
void* content; void* content;
struct SyntaxElement* next; struct SyntaxElement* next;
struct SyntaxElement* top; struct SyntaxElement* top;
}; } tSyntaxElement;
void se_clean(struct SyntaxElement* syntaxtree); void se_clean(tSyntaxElement* syntaxtree);
struct SyntaxElement* se_create(void); bool se_istraversable(tSyntaxElement* syntaxtree);
struct SyntaxElement* se_init(void); tSyntaxElement* se_create(void);
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement); tSyntaxElement* se_init(void);
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement); tSyntaxElement* se_bottom(tSyntaxElement* syntaxelement);
void se_free(struct SyntaxElement* syntaxtree); tSyntaxElement* se_next(tSyntaxElement* syntaxelement);
void se_free(tSyntaxElement* syntaxtree);
#endif #endif