Changes to main file
This commit is contained in:
46
src/main.c
46
src/main.c
@@ -1,10 +1,52 @@
|
|||||||
#include "lexer/lexer.h"
|
#include "lexer/lexer.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main() {
|
char* getFileContents(const char* filename) {
|
||||||
ResultType(SolsLexer, charptr) lexer = createLexer("fds \"");
|
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
|
||||||
|
FILE* fp;
|
||||||
|
long lSize;
|
||||||
|
char* file;
|
||||||
|
|
||||||
|
fp = fopen(filename, "rb");
|
||||||
|
if (!fp) {
|
||||||
|
perror(filename);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(fp, 0L, SEEK_END);
|
||||||
|
lSize = ftell(fp);
|
||||||
|
rewind(fp);
|
||||||
|
|
||||||
|
file = calloc(1, lSize + 1);
|
||||||
|
if (!file) {
|
||||||
|
fclose(fp);
|
||||||
|
fputs("memory allocation fail when reading file", stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1!=fread(file, lSize, 1, fp)) {
|
||||||
|
fclose(fp);
|
||||||
|
free(file);
|
||||||
|
fputs("couldn't read entire file", stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// we done
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Usage: %s [file]\n", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
char* fileContents = getFileContents(argv[1]);
|
||||||
|
ResultType(SolsLexer, charptr) lexer = createLexer(fileContents);
|
||||||
if (lexer.error) {
|
if (lexer.error) {
|
||||||
printf("Error while creating lexer: %s", lexer.as.error);
|
printf("Error while creating lexer: %s", lexer.as.error);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
ResultType(voidptr, charptr) lexed = lex(&lexer.as.success);
|
ResultType(voidptr, charptr) lexed = lex(&lexer.as.success);
|
||||||
if (lexed.error) {
|
if (lexed.error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user