Skip to content

Commit 6cc620d

Browse files
committed
Merge branch 'command-dispatch'
2 parents f223c37 + e8bec25 commit 6cc620d

File tree

5 files changed

+64
-52
lines changed

5 files changed

+64
-52
lines changed

src/commands.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,48 @@
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 },
33+
34+
/* Debugger commands */
35+
{ debug_info_matches, handle_debug_info },
36+
37+
{ NULL, NULL }
38+
};
3039

31-
static inline bool is_debug_info(const char* command) {
32-
return laco_is_match(debug_info_matches, command);
40+
/* External API */
41+
42+
void laco_dispatch(const struct LacoCommand* commands,
43+
struct LacoState* laco, const char* command_keyword,
44+
const char** arguments) {
45+
int i;
46+
const char** matches;
47+
48+
for(i = 0; (matches = commands[i].matches); i++) {
49+
if(laco_is_match(matches, command_keyword)) {
50+
commands[i].handler(laco, arguments);
51+
break;
52+
}
53+
}
3354
}
3455

3556
void laco_handle_command(struct LacoState* laco, char* line) {
@@ -41,13 +62,7 @@ void laco_handle_command(struct LacoState* laco, char* line) {
4162
const char* command = command_words[0];
4263
const char** arguments = (const char**) command_words + 1;
4364

44-
if(is_quit(command)) {
45-
laco_kill(laco, 0, "Exiting laco...");
46-
} else if(is_help(command)) {
47-
print_commands_help();
48-
} else if(is_debug_info(command)) {
49-
laco_print_debug_info(laco, arguments[0]);
50-
}
65+
laco_dispatch(line_commands, laco, command, arguments);
5166

5267
free(command_line);
5368
free(command_words);

src/commands.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33

44
struct LacoState;
55

6+
typedef void (*LacoHandler)(struct LacoState* laco, const char** arguments);
7+
8+
struct LacoCommand {
9+
const char** matches;
10+
LacoHandler handler;
11+
};
12+
13+
/**
14+
* Goes through each instance from the list of commands and see if there is
15+
* a match with for command_keyword. When there is a match, the defined
16+
* handler inside the LacoCommand gets called -- passing in LacoState and
17+
* the arguments. The list of commands expects the last entry of the array
18+
* to be `{ NULL, NULL }` for ease of iteration.
19+
*/
20+
void laco_dispatch(const struct LacoCommand* commands,
21+
struct LacoState* laco, const char* command_keyword,
22+
const char** arguments);
23+
624
/**
725
* Gets passed ever line to see if it matches one of the REPL command. If it
826
* does, that command will be executed.

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
}

src/util/line.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ static bool pushline(LacoState* laco, bool isFirstLine) {
112112
return result;
113113
}
114114

115+
/* External API */
116+
115117
bool laco_load_line(LacoState* laco) {
116118
int status = laco_get_laco_status(laco);
117119
lua_State* L = laco_get_laco_lua_state(laco);

src/util/print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void print_table(lua_State* L) {
7777
lua_pop(L, 1);
7878
}
7979

80-
/* External API*/
80+
/* External API */
8181

8282
int laco_print_type(LacoState* laco) {
8383
lua_State* L = laco_get_laco_lua_state(laco);

0 commit comments

Comments
 (0)