-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathmain.c
More file actions
78 lines (62 loc) · 1.67 KB
/
main.c
File metadata and controls
78 lines (62 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
The One Programming Language
File: parser/lexer/main.c
_ _
/ \ |\ | |_ Max Base
\_/ | \| |_ Copyright 2021; One Language Contributors
**/
#include <stdio.h>
#include <stdlib.h>
#include "../../builtins/file.h"
#include "lexer.h"
extern Token* current;
int main(int argc, char** argv)
{
char* input_file;
char* output_file;
FILE* file_out = stdout;
if (argc == 1)
{
printf("Error: arguments are not correct!\n");
printf("./lexer input.one output.tokens\n");
printf("./lexer \"your input here as string\"\n");
return 1;
}
else if (argc == 2)
{
input_file = argv[1];
}
else if (argc == 3)
{
input_file = argv[1];
output_file = argv[2];
file_out = fopen(output_file, "wa+");
if (!file_out)
{
fprintf(stderr, "Error: it's unable to write output to %s file!\n", output_file);
exit(1);
}
}
char* data = file_reads(input_file);
lexer_init(data);
// Lexer Tracer for debugging
while (!peekFor(TOKEN_EOF))
{
char* t_name = token_name(current->type);
bool has1 = file_convert_index_to_rc(data, current->pos.index, ¤t->pos.line, ¤t->pos.column);
bool has2 = file_convert_index_to_rc(data, current->pos_end.index, ¤t->pos_end.line, ¤t->pos_end.column);
fprintf(file_out, "[%zu:%zu] [%zu:%zu - %zu:%zu] %s", current->pos.tokens, current->length, current->pos.line, current->pos.column, current->pos_end.line, current->pos_end.column, t_name);
if (current->value != NULL)
{
fprintf(file_out, ": \"%s\"", current->value);
}
fprintf(file_out, "\n");
advance();
}
assert(current->type == TOKEN_EOF);
free(current);
lexer_free();
free(data);
if (file_out != stdout) fclose(file_out);
return 0;
}