Fixed tokenizer code; added command list structure

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2012-03-13 17:24:03 +01:00
parent 74fe14660a
commit c19ba376d3
2 changed files with 52 additions and 30 deletions

View File

@ -2,8 +2,16 @@
#include <gio/gio.h> #include <gio/gio.h>
#include <string.h> #include <string.h>
#include "interpreter.h"
#include "networking.h" #include "networking.h"
#define IS_SPACE(c) (g_ascii_isspace((c)) || (!(c)))
static wmudCommand command_list[] = {
{ "quit", gcmd_quit },
{ NULL, NULL },
};
static void static void
command_part_print(GString *data, gpointer user_data) command_part_print(GString *data, gpointer user_data)
{ {
@ -24,41 +32,47 @@ wmud_interpret_game_command(wmudClient *client)
for (a = client->buffer->str; (a - client->buffer->str) < client->buffer->len + 1; a++) for (a = client->buffer->str; (a - client->buffer->str) < client->buffer->len + 1; a++)
{ {
if ((!g_ascii_isspace(*a) && *a) || in_string) if (IS_SPACE(*a) && prev_space)
{
if (prev_space)
{
last_start = a;
g_print("New token starts here: %s\n", a);
}
prev_space = FALSE;
}
if ((!in_string && ((*a == '\'') || (*a == '"'))) || (in_string && (*a == string_delim)))
{
g_print("String %s\n", (in_string) ? "ended" : "started");
in_string = !in_string;
if (!in_string)
string_delim = 0;
prev_space = FALSE;
continue;
}
if (in_string)
continue; continue;
if (g_ascii_isspace(*a) || !*a) if (IS_SPACE(*a) && !prev_space && !in_string)
{
g_print("Space found.\n");
if (!prev_space)
{ {
GString *token = g_string_new_len(last_start, a - last_start); GString *token = g_string_new_len(last_start, a - last_start);
command_parts = g_slist_prepend(command_parts, token); command_parts = g_slist_prepend(command_parts, token);
g_print("Found new token, %d long\n", a - last_start);
}
prev_space = TRUE; prev_space = TRUE;
continue;
} }
if (!IS_SPACE(*a) && prev_space)
{
last_start = a;
prev_space = FALSE;
}
if (((*a == '\'') || (*a == '"')) && !in_string)
{
in_string = TRUE;
string_delim = *a;
prev_space = TRUE;
continue;
}
if (in_string && (*a == string_delim))
{
in_string = FALSE;
string_delim = 0;
prev_space = TRUE;
continue;
}
}
if (in_string)
{
GString *buf = g_string_new("");
g_string_printf(buf, "You should close quotation characters, like %c...\r\n", string_delim);
/* TODO: error checking */
g_socket_send(client->socket, buf->str, buf->len, NULL, NULL);
g_string_free(buf, TRUE);
} }
command_parts = g_slist_reverse(command_parts); command_parts = g_slist_reverse(command_parts);

View File

@ -3,6 +3,14 @@
#include "networking.h" #include "networking.h"
typedef void (*wmudCommandFunc)(wmudClient *client, gchar *command, GSList *token_list);
#define WMUD_COMMAND(name) void name(wmudClient *client, gchar *command, GSList *token_list)
typedef struct _wmudCommand {
gchar *command;
wmudCommandFunc commandFunc;
} wmudCommand;
gboolean wmud_interpreter_init(void); gboolean wmud_interpreter_init(void);
void wmud_interpret_game_command(wmudClient *client); void wmud_interpret_game_command(wmudClient *client);