-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
146 lines (125 loc) · 5.33 KB
/
main.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "common.h"
#include "path_variable_scanner.h"
#include "file_reader.h"
#include "tokenizer.h"
#include "my_parser.h"
#include "yoinkle_runtime.h"
int main(int argc, char *argv[]) {
// Getting the execution arguments and checking for flags and a file path
flags *my_flags = path_variable_scanner(argc, argv);
// Printing the help message and exiting
if (my_flags->help) {
printf(
"Usage: %s [flags] [file_path]\n"
"Arguments and flags:\n"
" -h, --help Display this help message\n"
" -v, --version Display the version\n"
" -d, --debug Add behind your script to run in debug\n"
" -t Add behind your script to view the tokens\n"
" -a Add behind your script to view abstract syntax tree\n"
" -f Add behind your script to view the file path\n"
" -c Add behind your script to view the code\n"
" file The path to the file you want to run\n"
"Flags can be combined and the order does not matter\n"
"It is also valid to set the file first and then the flags\n"
"But for simplicity and clarity, it is recommended to set the flags first\n"
"For more information, visit the GitHub repository: https://github.com/JordyDevrix/YoinkleScript\n"
"Happy Yoinkling!\n",
argv[0]
);
free(my_flags);
exit(0);
}
// Printing the version number and exiting
if (my_flags->version) {
printf("Version: 04/11/2024 v0.3.0-alpha\n");
free(my_flags);
exit(0);
}
// Reading the file_path argument and getting the file contents
if (my_flags->file_path) {
// Printing the file path when the -f flag is set
if (my_flags->file) {
printf("v v v FILE v v v\n");
printf("%s\n", my_flags->file_path);
}
// Reading the file contents and checking if the file exists
char *p_code = file_reader(my_flags->file_path);
if (p_code == NULL) {
free(my_flags);
printf("\nExited with code 1\n");
exit(1);
}
// Printing the code when the -c flag is set
if (my_flags->code) {
printf("v v v CODE v v v\n");
printf("%s\n", p_code);
}
// Tokenizing the code and optionally printing the tokens when the -t flag is set
Token *p_tokens = tokenize_code(p_code);
if (p_tokens == NULL) {
free(my_flags);
free(p_code);
printf("\nExited with code 1\n");
exit(1);
}
// Checking if the tokens flag has been set to 1 and printing the tokens
int print_tokens = my_flags->tokens;
if (print_tokens) {
printf("v v v TOKENS v v v\n");
}
int token_list_length = 0;
for (int i = 0; p_tokens[i].type != TOKEN_NULL; i++) {
token_list_length++;
if (print_tokens) {
switch (p_tokens[i].type)
{
case TOKEN_STRING:
printf("(%d)\tType %d\t(STRING)\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
case TOKEN_INTEGER:
printf("(%d)\tType %d\t(INTEGER)\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
case TOKEN_FLOAT:
printf("(%d)\tType %d\t(FLOAT)\t\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
case TOKEN_BOOLEAN:
printf("(%d)\tType %d\t(BOOLEAN)\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
case TOKEN_IDENTIFIER:
printf("(%d)\tType %d\t(IDENTIFIER)\t%s\tpos:%d\n", i, p_tokens[i].type, p_tokens[i].value, p_tokens[i].start);
break;
case TOKEN_KEYWORD:
printf("(%d)\tType %d\t(KEYWORD)\t%s\tpos:%d\n", i, p_tokens[i].type, p_tokens[i].value, p_tokens[i].start);
break;
case TOKEN_OPERATOR:
printf("(%d)\tType %d\t(OPERATOR)\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
case TOKEN_SYMBOL:
printf("(%d)\tType %d\t(SYMBOL)\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
case TOKEN_COMPARATOR:
printf("(%d)\tType %d\t(COMPARATOR)\t%s\n", i, p_tokens[i].type, p_tokens[i].value);
break;
default:
break;
}
}
}
// Parsing the tokens and optionally printing the abstract syntax tree when the -a flag is set
Node *AST = parse_tokens(p_tokens, my_flags->ast);
// Running the runtime
run_runtime(AST, p_tokens);
free(p_tokens);
p_tokens = NULL;
}
// Freeing the flags
free(my_flags);
// Freeing the file path
if (my_flags->file_path) {
free(my_flags->file_path);
}
// Freeing the code
printf("\nExited with code 0\n");
return 0;
}