Parser sort of works

This commit is contained in:
bʰedoh₂ swé 2024-06-03 01:48:09 +05:00
parent 4d6eff1042
commit 7906e6c4a8
5 changed files with 235 additions and 0 deletions

55
src/extstring.c Normal file
View File

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

14
src/extstring.h Normal file
View File

@ -0,0 +1,14 @@
struct Extstring {
char symbol;
struct Extstring* next;
};
struct SString {
int size;
char* string;
};
void es_addsymbol(struct Extstring* string, char symbol);
int es_size(struct Extstring* string);
void es_free(struct Extstring* string);
struct SString* es_tostring(struct Extstring* string);

22
src/main.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
int main(int argc, char *argv[]) {
FILE *file;
if (argc != 2) {
fprintf(stderr,
"Invalid usage!\n"
"%s FILENAME\n", argv[0]);
return 1;
}
file = fopen(argv[1],"r");
struct SyntaxElement* code = parse(file);
if (code == NULL) {
fprintf(stderr, "Error during parsing.\n");
return 1;
}
return 0;
}

125
src/parser.c Normal file
View File

@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include "parser.h"
#include "extstring.h"
struct SyntaxElement* parse(FILE* file) {
struct SyntaxElement* syntaxtree = malloc(sizeof(struct SyntaxElement));
struct SyntaxElement* syntaxtreestart = syntaxtree;
syntaxtree->type = TOPTREE;
syntaxtree->next = NULL;
syntaxtree->content = NULL;
syntaxtree->top = NULL;
char symbol;
bool intree = false;
bool incomment = false;
bool instring = false;
bool intoken = false;
struct Extstring* token;
while (1) {
symbol = fgetc(file);
if (feof(file))
break;
if (incomment) {
if (symbol == '\n')
incomment = false;
continue;
}
if (instring) {
// TODO: String parsing
fprintf(stderr, "TODO: String parsing\n");
return NULL;
}
if (intoken) {
if (isspace(symbol) || symbol == '(' || symbol == ')') {
intoken = false;
syntaxtree->type = TOKEN;
syntaxtree->content = es_tostring(token)->string;
es_free(token);
token = NULL;
continue;
} else {
es_addsymbol(token, symbol);
continue;
}
}
if (isspace(symbol))
continue;
if (intree) {
switch (symbol) {
case '(':
syntaxtree->content = malloc(sizeof(struct SyntaxElement));
((struct SyntaxElement*)syntaxtree->content)->top = syntaxtree;
syntaxtree = syntaxtree->content;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
syntaxtree->type = TREE;
break;
case ')':
if (syntaxtree->top->type == TOPTREE) {
intree = false;
syntaxtree = syntaxtree->top;
syntaxtree->next = malloc(sizeof(struct SyntaxElement));
syntaxtree = syntaxtree->next;
syntaxtree->type = TOPTREE;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
syntaxtree->top = NULL;
} else {
syntaxtree = syntaxtree->top;
syntaxtree->next = malloc(sizeof(struct SyntaxElement));
syntaxtree->next->top = syntaxtree->top;
syntaxtree = syntaxtree->next;
syntaxtree->type = TREE;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
}
break;
default:
token = malloc(sizeof(struct Extstring));
token->symbol = symbol;
intoken = true;
break;
}
continue;
} else {
switch (symbol) {
case ')':
fprintf(stderr, "Unbalanced brackets.\n");
return NULL;
case ';':
incomment = true;
continue;
case '(':
intree = true;
syntaxtree->content = malloc(sizeof(struct SyntaxElement));
((struct SyntaxElement*)syntaxtree->content)->top = syntaxtree;
syntaxtree = syntaxtree->content;
syntaxtree->content = NULL;
syntaxtree->next = NULL;
syntaxtree->type = TREE;
continue;
default:
fprintf(stderr, "Expression outside of brackets.\n");
return NULL;
}
}
}
if (intree) {
fprintf(stderr, "Unexpected EOF.\n");
return NULL;
}
syntaxtree = syntaxtreestart;
while (1) {
if (syntaxtree->next->next == NULL) {
free(syntaxtree->next);
syntaxtree->next = NULL;
break;
}
syntaxtree = syntaxtree->next;
}
return syntaxtreestart;
}

19
src/parser.h Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
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);