Changed to read from a file
This commit is contained in:
parent
f6049fc644
commit
145ba2c181
26
src/main.c
26
src/main.c
|
@ -1,4 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "sync/types.h"
|
#include "sync/types.h"
|
||||||
#include "sync/lexer.h"
|
#include "sync/lexer.h"
|
||||||
|
|
||||||
|
@ -6,17 +7,35 @@ static void print_token(Token token) {
|
||||||
printf("Token: %-15s | Text: '%.*s'\n",
|
printf("Token: %-15s | Text: '%.*s'\n",
|
||||||
(const char *[]){
|
(const char *[]){
|
||||||
"EOF", "IDENTIFIER", "NUMBER", "OPERATOR",
|
"EOF", "IDENTIFIER", "NUMBER", "OPERATOR",
|
||||||
"LPAREN", "RPAREN", "SEMICOLON", "UNKNOWN"
|
"LPAREN", "RPAREN", "SEMICOLON", "LBRACE",
|
||||||
|
"RBRACE", "LBRACKET", "RBRACKET"
|
||||||
}[token.type],
|
}[token.type],
|
||||||
(int)token.length, token.start
|
(int)token.length, token.start
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const char *source = "sum = a + b123;\nprint(sum);";
|
const char *filename = "test/example1.zn";
|
||||||
|
FILE *file = fopen(filename, "rb");
|
||||||
|
if (!file) {
|
||||||
|
fprintf(stderr, "Failed to open file: %s\n", filename);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long filesize = ftell(file);
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
char *source = malloc(filesize + 1);
|
||||||
|
if (!source) {
|
||||||
|
fprintf(stderr, "Failed to allocate memory.\n");
|
||||||
|
fclose(file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fread(source, 1, filesize, file);
|
||||||
|
source[filesize] = '\0';
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
Lexer lexer;
|
Lexer lexer;
|
||||||
lexer_init(&lexer, "<stdin>", source);
|
lexer_init(&lexer, filename, source);
|
||||||
|
|
||||||
TokenResult result;
|
TokenResult result;
|
||||||
do {
|
do {
|
||||||
|
@ -28,5 +47,6 @@ int main(void) {
|
||||||
}
|
}
|
||||||
} while (result.type != SYNC_ERROR && result.result.type != TOKEN_EOF);
|
} while (result.type != SYNC_ERROR && result.result.type != TOKEN_EOF);
|
||||||
|
|
||||||
|
free(source);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
static void print_token(Token token) {
|
||||||
|
//printf("Token: %-15s | Text: '%.*s'\n",
|
||||||
|
(const char *[]){
|
||||||
|
//"EOF", "IDENTIFIER", "NUMBER", "OPERATOR",
|
||||||
|
//"LPAREN", "RPAREN", "SEMICOLON", "UNKNOWN"
|
||||||
|
}[token.type],
|
||||||
|
(int)token.length, token.start
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
//const char *filename = "test/example1.zn";
|
||||||
|
FILE *file = fopen(filename, //"rb");
|
||||||
|
if (!file) {
|
||||||
|
//fprintf(stderr, "Failed to open file: %s\n", filename);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long filesize = ftell(file);
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
char *source = malloc(filesize + 1);
|
||||||
|
if (!source) {
|
||||||
|
//fprintf(stderr, "Failed to allocate memory.\n");
|
||||||
|
fclose(file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fread(source, 1, filesize, file);
|
||||||
|
source[filesize] = //'\0';
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
Lexer lexer;
|
||||||
|
lexer_init(&lexer, filename, source);
|
||||||
|
|
||||||
|
TokenResult result;
|
||||||
|
do {
|
||||||
|
result = lexer_next(&lexer);
|
||||||
|
if (result.type == SYNC_RESULT) {
|
||||||
|
print_token(result.result);
|
||||||
|
} else {
|
||||||
|
//fprintf(stderr, "Error: %s\n", result.error.message);
|
||||||
|
}
|
||||||
|
} while (result.type != SYNC_ERROR && result.result.type != TOKEN_EOF);
|
||||||
|
|
||||||
|
free(source);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue