Skip to content

Commit 4bbda9e

Browse files
committed
Reimplement previous functionality with dispatch
The way command matches are defined outside the LacoCommand isn't ideal, but this is the only what to keep the compiler to complaining. I do wish to change this unfortunately I'm not sure how at the moment.
1 parent a6fa324 commit 4bbda9e

File tree

2 files changed

+28
-52
lines changed

2 files changed

+28
-52
lines changed

src/commands.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,33 @@
99

1010
static const char* quit_matches[] = {"quit", "q", NULL};
1111
static const char* help_matches[] = {"help", "?", NULL};
12-
13-
/* Debugger commands */
1412
static const char* debug_info_matches[] = {"info", NULL};
1513

16-
static void print_commands_help() {
14+
static void handle_quit(struct LacoState* laco, const char** arguments) {
15+
laco_kill(laco, 0, "Exiting laco...");
16+
}
17+
18+
static void handle_help(struct LacoState* laco, const char** arguments) {
1719
puts(" Commands available:\n");
1820
puts(" :quit, :q \tExit laco");
1921
puts(" :help, :? \tDisplay this list of commands");
2022
puts(" :info <name> \tShow information on given function");
2123
}
2224

23-
static inline bool is_quit(const char* command) {
24-
return laco_is_match(quit_matches, command);
25+
static void handle_debug_info(struct LacoState* laco,
26+
const char** arguments) {
27+
laco_print_debug_info(laco, arguments[0]);
2528
}
2629

27-
static inline bool is_help(const char* command) {
28-
return laco_is_match(help_matches, command);
29-
}
30+
static const struct LacoCommand line_commands[] = {
31+
{ quit_matches, handle_quit },
32+
{ help_matches, handle_help },
3033

31-
static inline bool is_debug_info(const char* command) {
32-
return laco_is_match(debug_info_matches, command);
33-
}
34+
/* Debugger commands */
35+
{ debug_info_matches, handle_debug_info },
36+
37+
{ NULL, NULL }
38+
};
3439

3540
void laco_dispatch(const struct LacoCommand* commands,
3641
struct LacoState* laco, const char* command_keyword,
@@ -55,13 +60,7 @@ void laco_handle_command(struct LacoState* laco, char* line) {
5560
const char* command = command_words[0];
5661
const char** arguments = (const char**) command_words + 1;
5762

58-
if(is_quit(command)) {
59-
laco_kill(laco, 0, "Exiting laco...");
60-
} else if(is_help(command)) {
61-
print_commands_help();
62-
} else if(is_debug_info(command)) {
63-
laco_print_debug_info(laco, arguments[0]);
64-
}
63+
laco_dispatch(line_commands, laco, command, arguments);
6564

6665
free(command_line);
6766
free(command_words);

src/flags.c

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,23 @@
22

33
#include <stdio.h>
44

5+
#include "commands.h"
56
#include "laco.h"
67
#include "util.h"
78

8-
enum LacoFlags {
9-
LACO_HELP,
10-
LACO_VERSION
11-
};
12-
139
static const char* version_matches[] = {"-v", "--version", NULL};
1410
static const char* help_matches[] = {"-h", "-?", "--help", NULL};
1511

16-
/* Check if the command flag is for version */
17-
static inline bool is_version(const char* arg) {
18-
return laco_is_match(version_matches, arg);
19-
}
20-
21-
/* Check if the command flag is for help */
22-
static inline bool is_help(const char* arg) {
23-
return laco_is_match(help_matches, arg);
24-
}
25-
2612
/* Print off the current version of laco */
27-
static void print_version(LacoState* laco) {
13+
static void handle_version(LacoState* laco, const char** arguments) {
2814
const char* version = laco_get_laco_version(laco);
2915

3016
printf("laco version %s\n", version);
3117
laco_kill(laco, 0, NULL);
3218
}
3319

3420
/* Print off the help screen */
35-
static void print_help(LacoState* laco) {
21+
static void handle_help(LacoState* laco, const char** arguments) {
3622
puts("A better REPL for Lua.\n");
3723
puts("Usage: laco [options]\n");
3824
puts("-h | -? | --help \tPrint this help screen");
@@ -41,25 +27,16 @@ static void print_help(LacoState* laco) {
4127
laco_kill(laco, 0, NULL);
4228
}
4329

30+
static const struct LacoCommand flag_commands[] = {
31+
{ version_matches, handle_version },
32+
{ help_matches, handle_help },
33+
{ NULL, NULL}
34+
};
35+
4436
/* External API */
4537

4638
void laco_handle_flag(LacoState* laco) {
47-
int arg_type;
48-
const char* arg = laco_get_laco_args(laco)[1];
49-
if(arg == NULL) return;
50-
51-
if(is_version(arg)) {
52-
arg_type = LACO_VERSION;
53-
} else if(is_help(arg)) {
54-
arg_type = LACO_HELP;
55-
}
39+
const char* command = laco_get_laco_args(laco)[1];
5640

57-
switch(arg_type) {
58-
case LACO_VERSION:
59-
print_version(laco);
60-
case LACO_HELP:
61-
print_help(laco);
62-
default:
63-
break;
64-
}
41+
laco_dispatch(flag_commands, laco, command, NULL);
6542
}

0 commit comments

Comments
 (0)