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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include "kpl.h"
typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]);
typedef struct {
int32_t flags;
char match, *help_option_argument, *help_message;
kpl_config_option_parse_fn *parse_fn;
} kpl_config_option;
#define KPL_CONFIG_MATCH(CHAR) (CHAR - 'a')
static kpl_exit_code kpl_config_option_parse_eval(kpl_env *env, int32_t arg_index, char *argv[]) {
env->kpl_entry = argv[arg_index];
env->flags |= KPL_ENV_FLAG_RUN_EVAL;
return KPL_EXIT_CODE_SUCCESS;
}
static const kpl_config_option config_options[];
static kpl_exit_code kpl_config_option_parse_help(kpl_env *env, int32_t arg_index, char *argv[]) {
(void) env;
(void) arg_index;
printf(KPL_COLOR(LIGHT_GREEN) "Kevin's Programming Language" KPL_COLOR(RESET) " (" KPL_COLOR(LIGHT_CYAN)
"KPL" KPL_COLOR(RESET) ") Version: " KPL_COLOR(LIGHT_MAGENTA) KPL_VERSION KPL_COLOR(RESET)
" Website: " KPL_COLOR(LIGHT_BLUE) KPL_WEBSITE KPL_COLOR(RESET) "\n");
printf("Usage:" KPL_COLOR(BOLD) " %s " KPL_COLOR(RESET) KPL_COLOR(LIGHT_YELLOW) "OPTIONS" KPL_COLOR(RESET)
KPL_COLOR(BOLD) " [-r|-e STRING|FILE" KPL_FILE_EXTENSION KPL_COLOR(RESET) "]\n", argv[0]);
for (int32_t option_index = KPL_CONFIG_MATCH('a'); option_index < KPL_CONFIG_MATCH('z'); option_index++) {
const kpl_config_option *option = &config_options[option_index];
if (!option->match)
continue;
printf("\t" KPL_COLOR(LIGHT_YELLOW) "-%c" KPL_COLOR(RESET), option->match);
if (option->help_option_argument)
printf(KPL_COLOR(GREEN) " [%s]" KPL_COLOR(RESET), option->help_option_argument);
printf(", %s\n", option->help_message);
}
return KPL_EXIT_CODE_HELP;
}
static kpl_exit_code kpl_config_option_parse_io_queue(kpl_env *env, int32_t arg_index, char *argv[]) {
int32_t io_uring_queue_size_pow2 = atoi(argv[arg_index]);
if (io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN ||
io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX) {
printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid power of two for io_uring, must be between %d and %d\n"
KPL_COLOR(RESET), KPL_IO_URING_QUEUE_SIZE_POW2_MIN, KPL_IO_URING_QUEUE_SIZE_POW2_MAX);
return KPL_EXIT_CODE_ERROR;
}
env->io_uring_queue_size_pow2 = io_uring_queue_size_pow2;
return KPL_EXIT_CODE_SUCCESS;
}
static kpl_exit_code kpl_config_option_parse_repl(kpl_env *env, int32_t arg_index, char *argv[]) {
(void) arg_index;
(void) argv;
env->flags |= KPL_ENV_FLAG_RUN_REPL;
return KPL_EXIT_CODE_SUCCESS;
}
static kpl_exit_code kpl_config_option_parse_thread_count(kpl_env *env, int32_t arg_index, char *argv[]) {
int32_t thread_count = atoi(argv[arg_index]);
if (thread_count < KPL_THEAD_THREAD_COUNT_MIN || thread_count > KPL_THEAD_THREAD_COUNT_MAX) {
printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid thread count, must be between %d and %d\n" KPL_COLOR(RESET),
KPL_THEAD_THREAD_COUNT_MIN, KPL_THEAD_THREAD_COUNT_MAX);
return KPL_EXIT_CODE_ERROR;
}
env->thread_count = thread_count;
return KPL_EXIT_CODE_SUCCESS;
}
static kpl_exit_code kpl_config_option_parse_version(kpl_env *env, int32_t arg_index, char *argv[]) {
(void) env;
(void) arg_index;
(void) argv;
printf("kpl version " KPL_VERSION "\n");
return KPL_EXIT_CODE_HELP;
}
static const kpl_config_option config_options[] = {
[KPL_CONFIG_MATCH('a')] = {},
[KPL_CONFIG_MATCH('e')] = {
.match = 'e',
.help_option_argument = "STRING",
.help_message = "Evaluate KPL String",
.parse_fn = kpl_config_option_parse_eval
},
[KPL_CONFIG_MATCH('h')] = {
.match = 'h',
.help_message = "Show Help",
.parse_fn = kpl_config_option_parse_help
},
[KPL_CONFIG_MATCH('i')] = {
.match = 'i',
.help_option_argument = "NUMBER",
.help_message = "Power Of Two For io_uring Queue Size",
.parse_fn = kpl_config_option_parse_io_queue
},
[KPL_CONFIG_MATCH('r')] = {
.match = 'r',
.help_message = "Start KPL REPL",
.parse_fn = kpl_config_option_parse_repl
},
[KPL_CONFIG_MATCH('t')] = {
.match = 't',
.help_option_argument = "NUMBER",
.help_message = "Number Of Threads To Use",
.parse_fn = kpl_config_option_parse_thread_count
},
[KPL_CONFIG_MATCH('v')] = {
.match = 'v',
.help_message = "Show Version",
.parse_fn = kpl_config_option_parse_version
},
[KPL_CONFIG_MATCH('z')] = {}
};
static void kpl_env_set_defaults(kpl_env *env) {
env->io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT;
env->thread_count = KPL_THREAD_DETERIMINE_COUNT;
}
kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]) {
kpl_env_set_defaults(env);
if (argc == KPL_ENV_CONFIG_NO_ARGS)
return KPL_EXIT_CODE_SUCCESS;
int32_t arg_index = 1, exit_code = KPL_EXIT_CODE_SUCCESS;
while (arg_index < argc) {
if (argv[arg_index][0] != '-') {
env->kpl_entry = argv[arg_index];
return KPL_EXIT_CODE_SUCCESS;
}
if (argv[arg_index][1] && argv[arg_index][2]) {
printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid option %s\n" KPL_COLOR(RESET), argv[arg_index]);
return KPL_EXIT_CODE_ERROR;
}
if (argv[arg_index][1] == '-') {
arg_index++;
if (arg_index >= argc) {
printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, no arguments after --\n" KPL_COLOR(RESET));
return KPL_EXIT_CODE_ERROR;
}
env->kpl_entry = argv[arg_index];
return KPL_EXIT_CODE_SUCCESS;
}
const kpl_config_option *option = &config_options[KPL_CONFIG_MATCH(argv[arg_index][1])];
if (option->help_option_argument) {
arg_index++;
if (arg_index >= argc) {
printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, option -%c requires argument\n" KPL_COLOR(RESET),
option->match);
return KPL_EXIT_CODE_ERROR;
}
}
if ((exit_code = option->parse_fn(env, arg_index, argv)) != KPL_EXIT_CODE_SUCCESS)
return exit_code;
if (env->flags & (KPL_ENV_FLAG_RUN_EVAL | KPL_ENV_FLAG_RUN_REPL))
return KPL_EXIT_CODE_SUCCESS;
arg_index++;
}
return kpl_config_option_parse_help(env, 0, argv);
}
|