vm-translator/parser.c

145 lines
2.6 KiB
C
Raw Normal View History

2020-10-29 20:22:03 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
2020-11-19 06:10:51 -05:00
#include <stdbool.h>
2020-10-29 20:22:03 -04:00
#include "parser.h"
2020-11-19 06:10:51 -05:00
void freelns(LINEARRAY* lns) {
2020-11-01 08:53:20 -05:00
for(int i = 0; i < lns->count; i++) {
int tkcount = lns->lns[i]->tokenscount;
2020-10-29 21:06:33 -04:00
for(int j = 0; j < tkcount; j++) {
2020-11-01 08:53:20 -05:00
free(lns->lns[i]->tokens[j]);
2020-10-29 21:06:33 -04:00
}
2020-11-01 08:53:20 -05:00
free(lns->lns[i]->tokens);
free(lns->lns[i]);
2020-10-29 21:06:33 -04:00
}
2020-11-01 08:53:20 -05:00
free(lns->lns);
2020-10-29 21:06:33 -04:00
free(lns);
}
2020-11-19 06:10:51 -05:00
void freeparser(PARSER* p) {
2020-11-01 08:53:20 -05:00
freelns(p->lns);
free(p);
}
2020-10-29 20:22:03 -04:00
void gountilbrk (FILE* input) {
char c;
while(c = fgetc(input), c != -1) {
if(c == '\n') {
ungetc(c, input);
return;
}
}
}
2020-11-19 06:10:51 -05:00
void getinfo(PARSER* p) {
2020-11-01 08:53:20 -05:00
p->lns->count = 0;
p->maxtokens = 0;
p->widestln = 0;
2020-10-29 20:22:03 -04:00
char c, nc;
int widest = 0;
int currsz = 0;
int tokens = 0;
short readsmt = 0;
2020-11-01 08:53:20 -05:00
while(c = fgetc(p->input), c != -1) {
2020-10-29 21:06:33 -04:00
currsz++;
if(isspace(c)) {
if(readsmt) {
tokens++;
readsmt = 0;
}
if(c == '\n' && tokens > 0) {
2020-11-01 08:53:20 -05:00
p->lns->count++;
if(currsz > p->widestln)
p->widestln = currsz;
if(tokens > p->maxtokens)
p->maxtokens = tokens;
2020-10-29 20:22:03 -04:00
currsz = 0;
tokens = 0;
}
continue;
}
if(c == '/') {
2020-11-01 08:53:20 -05:00
nc = fgetc(p->input);
2020-10-29 20:22:03 -04:00
if(nc == '/') {
2020-11-01 08:53:20 -05:00
gountilbrk(p->input);
2020-10-29 20:22:03 -04:00
continue;
}
2020-11-01 08:53:20 -05:00
ungetc(nc, p->input);
2020-10-29 20:22:03 -04:00
}
readsmt = 1;
}
2020-11-01 08:53:20 -05:00
rewind(p->input);
}
2020-11-19 06:10:51 -05:00
PARSER* mkparser(FILE* input) {
PARSER* p = (PARSER*)malloc(sizeof(PARSER));
LINEARRAY* lns = (LINEARRAY*)malloc(sizeof(LINEARRAY));
2020-11-01 08:53:20 -05:00
p->input = input;
p->lns = lns;
getinfo(p);
return p;
2020-10-29 20:22:03 -04:00
}
2020-11-19 06:10:51 -05:00
void parse(PARSER* p) {
LINE** lns = (LINE**)malloc(sizeof(LINE*)*p->lns->count);
2020-11-01 08:53:20 -05:00
p->lns->lns = lns;
p->lns->count = 0;
char tmp[p->widestln];
char* tokens[p->maxtokens];
2020-10-29 20:22:03 -04:00
char c, nc;
short readsmt = 0;
int tmpind = 0;
int tokensind = 0;
int truelncount = 0;
2020-11-01 08:53:20 -05:00
while(c = fgetc(p->input), c != -1) {
2020-10-29 20:22:03 -04:00
if(isspace(c)) {
if(readsmt) {
tmp[tmpind] = '\0';
char* newtoken = (char*)malloc(sizeof(char)*tmpind+1);
strcpy(newtoken, tmp);
tokens[tokensind] = newtoken;
tmpind = 0;
readsmt = 0;
tokensind++;
}
if(c == '\n') {
truelncount++;
if(tokensind > 0) {
2020-11-19 06:10:51 -05:00
LINE* newln = (LINE*)malloc(sizeof(LINE));
2020-10-29 20:22:03 -04:00
newln->tokens = (char**)malloc(sizeof(char*)*tokensind);
for(int i = 0; i < tokensind; i++) {
newln->tokens[i] = tokens[i];
}
newln->tokenscount = tokensind;
newln->truen = truelncount;
2020-11-01 08:53:20 -05:00
lns[p->lns->count] = newln;
p->lns->count++;
2020-10-29 20:22:03 -04:00
tokensind = 0;
}
}
continue;
}
if(c == '/') {
2020-11-01 08:53:20 -05:00
nc = fgetc(p->input);
2020-10-29 20:22:03 -04:00
if(nc == '/') {
2020-11-01 08:53:20 -05:00
gountilbrk(p->input);
2020-10-29 20:22:03 -04:00
continue;
}
2020-11-01 08:53:20 -05:00
ungetc(nc, p->input);
2020-10-29 20:22:03 -04:00
}
tmp[tmpind] = c;
tmpind++;
readsmt = 1;
}
2020-11-01 08:53:20 -05:00
fclose(p->input);
2020-10-29 20:22:03 -04:00
}