More parser fixes

This commit is contained in:
Stefan Bühler 2008-07-12 03:29:24 +02:00
parent 1e4dbb03d6
commit 6387eed01f
6 changed files with 362 additions and 102 deletions

View File

@ -63,7 +63,7 @@ static void telemetry_free(gpointer _te, gpointer x) {
g_slice_free(telemetry, te); g_slice_free(telemetry, te);
} }
static telemetry* telemetry_new() { telemetry* telemetry_new() {
telemetry *te = g_slice_new0(telemetry); telemetry *te = g_slice_new0(telemetry);
te->objects = g_array_new(FALSE, FALSE, sizeof(object)); te->objects = g_array_new(FALSE, FALSE, sizeof(object));
return te; return te;

View File

@ -91,6 +91,9 @@ INLINE void vehicle_straight(trial *t);
INLINE void vehicle_right(trial *t); INLINE void vehicle_right(trial *t);
INLINE void vehicle_hard_right(trial *t); INLINE void vehicle_hard_right(trial *t);
/* needed by parser */
telemetry* telemetry_new();
/* Inline functions */ /* Inline functions */
#define SENDCMD(t, cmd) write(t->socket, cmd, sizeof(cmd)-1) #define SENDCMD(t, cmd) write(t->socket, cmd, sizeof(cmd)-1)

View File

@ -4,14 +4,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h>
typedef control_parser_ctx context; typedef control_parser_ctx context;
static void extract(context *ctx, char *fpc) { static void extract(context *ctx, char *fpc) {
gsize len = (fpc - ctx->buffer->str) - ctx->mark; gssize curpos = (fpc - ctx->buffer->str);
gssize len = curpos - ctx->mark;
g_string_truncate(ctx->tmp, 0); g_string_truncate(ctx->tmp, 0);
g_string_append_len(ctx->tmp, ctx->buffer->str + ctx->mark, len); g_string_append_len(ctx->tmp, ctx->buffer->str + ctx->mark, len);
ctx->read += len; ctx->mark = -1;
printf("Read chunk '%s'\n", ctx->tmp->str);
} }
static double extract_double(context *ctx, char *fpc) { static double extract_double(context *ctx, char *fpc) {
@ -19,86 +22,189 @@ static double extract_double(context *ctx, char *fpc) {
return strtod(ctx->tmp->str, NULL); return strtod(ctx->tmp->str, NULL);
} }
#line 62 "control_parser.rl" static timestamp extract_ts(context *ctx, char *fpc) {
extract(ctx, fpc);
return atoi(ctx->tmp->str);
}
#line 101 "control_parser.rl"
#line 27 "control_parser.c" #line 35 "control_parser.c"
static const char _control_parser_actions[] = { static const char _control_parser_actions[] = {
0, 1, 0, 1, 1, 1, 2, 1, 0, 1, 0, 1, 1, 1, 2, 1,
3, 1, 4, 1, 5, 1, 6, 1, 3, 1, 4, 1, 5, 1, 6, 1,
7, 1, 8, 2, 1, 2, 2, 1, 7, 1, 8, 1, 9, 1, 10, 1,
3, 2, 1, 4, 2, 1, 5, 2, 11, 1, 12, 1, 13, 1, 14, 1,
1, 6, 2, 1, 7, 2, 1, 8 15, 1, 16, 1, 17, 1, 18, 1,
19, 1, 20, 1, 21, 1, 22, 1,
23, 1, 24, 1, 25, 1, 26, 2,
1, 5, 2, 1, 6, 2, 1, 7,
2, 1, 8, 2, 1, 9, 2, 1,
10, 2, 1, 11, 2, 1, 12, 2,
1, 13, 2, 1, 14, 2, 1, 15,
2, 1, 16, 2, 1, 25, 2, 1,
26, 2, 3, 2
}; };
static const char _control_parser_key_offsets[] = { static const unsigned char _control_parser_key_offsets[] = {
0, 0, 1, 2, 6, 10, 14, 18, 0, 0, 1, 2, 7, 12, 17, 22,
22, 26, 30, 34, 35, 39, 43, 47, 27, 32, 37, 42, 43, 49, 50, 53,
51, 55, 59, 63 56, 57, 60, 61, 62, 63, 66, 69,
70, 73, 76, 79, 80, 83, 86, 91,
92, 97, 102, 107, 112, 117, 123, 124,
129, 134, 139, 144, 145, 150, 155, 160,
165, 169, 173, 177, 181, 185, 189, 193,
197, 201, 205, 209, 212, 216, 220, 224,
228, 232, 236, 240, 244
}; };
static const char _control_parser_trans_keys[] = { static const char _control_parser_trans_keys[] = {
73, 32, 32, 46, 48, 57, 32, 46, 73, 32, 32, 45, 46, 48, 57, 32,
48, 57, 32, 46, 48, 57, 32, 46, 45, 46, 48, 57, 32, 45, 46, 48,
48, 57, 32, 46, 48, 57, 32, 46, 57, 32, 45, 46, 48, 57, 32, 45,
48, 57, 32, 46, 48, 57, 32, 46, 46, 48, 57, 32, 45, 46, 48, 57,
48, 57, 59, 32, 46, 48, 57, 32, 32, 45, 46, 48, 57, 32, 45, 46,
46, 48, 57, 32, 46, 48, 57, 32, 48, 57, 59, 66, 67, 69, 75, 83,
46, 48, 57, 32, 46, 48, 57, 32, 84, 32, 32, 48, 57, 32, 48, 57,
46, 48, 57, 32, 46, 48, 57, 0 32, 32, 48, 57, 59, 69, 32, 32,
48, 57, 32, 48, 57, 59, 32, 48,
57, 32, 48, 57, 32, 48, 57, 32,
32, 48, 57, 45, 97, 98, 45, 76,
82, 108, 114, 32, 32, 45, 46, 48,
57, 32, 45, 46, 48, 57, 32, 45,
46, 48, 57, 32, 45, 46, 48, 57,
59, 104, 109, 98, 99, 66, 67, 69,
75, 83, 84, 32, 32, 45, 46, 48,
57, 32, 45, 46, 48, 57, 32, 45,
46, 48, 57, 59, 104, 109, 98, 99,
32, 32, 45, 46, 48, 57, 32, 45,
46, 48, 57, 32, 45, 46, 48, 57,
32, 45, 46, 48, 57, 32, 46, 48,
57, 32, 46, 48, 57, 32, 46, 48,
57, 32, 46, 48, 57, 32, 46, 48,
57, 32, 46, 48, 57, 32, 46, 48,
57, 32, 46, 48, 57, 32, 46, 48,
57, 32, 46, 48, 57, 32, 46, 48,
57, 32, 48, 57, 32, 46, 48, 57,
32, 46, 48, 57, 32, 46, 48, 57,
32, 46, 48, 57, 32, 46, 48, 57,
32, 46, 48, 57, 32, 46, 48, 57,
32, 46, 48, 57, 0
}; };
static const char _control_parser_single_lengths[] = { static const char _control_parser_single_lengths[] = {
0, 1, 1, 2, 2, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 6, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 3, 5, 1,
1, 1, 1, 1, 3, 6, 1, 1,
1, 1, 3, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,
2, 2, 2, 0 2, 2, 2, 2, 0
}; };
static const char _control_parser_range_lengths[] = { static const char _control_parser_range_lengths[] = {
0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 2,
1, 1, 1, 0, 1, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1,
1, 1, 1, 0 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 0, 1, 0, 0, 0,
2, 2, 2, 2, 1, 0, 0, 2,
2, 2, 1, 0, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0
}; };
static const char _control_parser_index_offsets[] = { static const short _control_parser_index_offsets[] = {
0, 0, 2, 4, 8, 12, 16, 20, 0, 0, 2, 4, 8, 12, 16, 20,
24, 28, 32, 36, 38, 42, 46, 50, 24, 28, 32, 36, 38, 45, 47, 50,
54, 58, 62, 66 53, 55, 58, 60, 62, 64, 67, 70,
72, 75, 78, 81, 83, 86, 90, 96,
98, 102, 106, 110, 114, 119, 126, 128,
132, 136, 140, 145, 147, 151, 155, 159,
163, 167, 171, 175, 179, 183, 187, 191,
195, 199, 203, 207, 210, 214, 218, 222,
226, 230, 234, 238, 242
}; };
static const char _control_parser_trans_targs[] = { static const char _control_parser_trans_targs[] = {
2, 0, 3, 0, 4, 18, 18, 0, 2, 0, 3, 0, 4, 67, 67, 0,
5, 17, 17, 0, 6, 5, 5, 0, 5, 66, 66, 0, 6, 65, 65, 0,
7, 16, 16, 0, 8, 15, 15, 0, 7, 64, 64, 0, 8, 63, 63, 0,
9, 14, 14, 0, 10, 13, 13, 0, 9, 62, 62, 0, 10, 61, 61, 0,
11, 12, 12, 0, 19, 0, 11, 12, 11, 60, 60, 0, 12, 0, 13, 16,
12, 0, 10, 13, 13, 0, 9, 14, 20, 16, 16, 27, 0, 14, 0, 11,
14, 0, 8, 15, 15, 0, 7, 16, 15, 0, 11, 15, 0, 17, 0, 18,
16, 0, 5, 17, 17, 0, 4, 18, 26, 0, 19, 0, 20, 0, 21, 0,
18, 0, 0, 0 22, 25, 0, 23, 24, 0, 68, 0,
23, 24, 0, 22, 25, 0, 18, 26,
0, 28, 0, 29, 59, 0, 30, 30,
30, 0, 31, 31, 31, 31, 31, 0,
32, 0, 33, 58, 58, 0, 34, 57,
57, 0, 35, 56, 56, 0, 36, 55,
55, 0, 37, 38, 43, 38, 0, 13,
16, 20, 16, 16, 27, 0, 39, 0,
40, 54, 54, 0, 41, 53, 53, 0,
42, 52, 52, 0, 37, 38, 43, 38,
0, 44, 0, 45, 51, 51, 0, 46,
50, 50, 0, 47, 49, 49, 0, 42,
48, 48, 0, 42, 48, 48, 0, 47,
49, 49, 0, 46, 50, 50, 0, 45,
51, 51, 0, 42, 52, 52, 0, 41,
53, 53, 0, 40, 54, 54, 0, 36,
55, 55, 0, 35, 56, 56, 0, 34,
57, 57, 0, 33, 58, 58, 0, 29,
59, 0, 11, 60, 60, 0, 10, 61,
61, 0, 9, 62, 62, 0, 8, 63,
63, 0, 7, 64, 64, 0, 6, 65,
65, 0, 5, 66, 66, 0, 4, 67,
67, 0, 0, 0
}; };
static const char _control_parser_trans_actions[] = { static const char _control_parser_trans_actions[] = {
0, 0, 0, 0, 19, 3, 3, 0, 0, 0, 0, 0, 55, 3, 3, 0,
22, 3, 3, 0, 0, 0, 0, 0, 58, 3, 3, 0, 0, 0, 0, 0,
25, 3, 3, 0, 28, 3, 3, 0, 61, 3, 3, 0, 64, 3, 3, 0,
31, 3, 3, 0, 34, 3, 3, 0, 67, 3, 3, 0, 70, 3, 3, 0,
37, 3, 3, 0, 1, 0, 17, 0, 73, 3, 3, 0, 0, 0, 0, 0,
0, 0, 15, 0, 0, 0, 13, 0, 0, 0, 0, 5, 0, 0, 0, 91,
0, 0, 11, 0, 0, 0, 9, 0, 3, 0, 51, 0, 0, 0, 0, 91,
0, 0, 7, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 0, 0, 0,
91, 3, 0, 94, 3, 0, 1, 0,
53, 0, 0, 51, 0, 0, 51, 0,
0, 0, 0, 91, 3, 0, 37, 35,
39, 0, 45, 41, 49, 43, 47, 0,
0, 0, 76, 3, 3, 0, 79, 3,
3, 0, 85, 3, 3, 0, 88, 3,
3, 0, 9, 9, 9, 9, 0, 7,
7, 7, 7, 7, 97, 0, 0, 0,
76, 3, 3, 0, 79, 3, 3, 0,
82, 3, 3, 0, 0, 0, 0, 0,
0, 0, 0, 76, 3, 3, 0, 79,
3, 3, 0, 85, 3, 3, 0, 88,
3, 3, 0, 33, 0, 0, 0, 31,
0, 0, 0, 27, 0, 0, 0, 25,
0, 0, 0, 29, 0, 0, 0, 27,
0, 0, 0, 25, 0, 0, 0, 33,
0, 0, 0, 31, 0, 0, 0, 27,
0, 0, 0, 25, 0, 0, 0, 51,
0, 0, 23, 0, 0, 0, 21, 0,
0, 0, 19, 0, 0, 0, 17, 0,
0, 0, 15, 0, 0, 0, 0, 0,
0, 0, 13, 0, 0, 0, 11, 0,
0, 0, 0, 0 0, 0, 0, 0
}; };
static const int control_parser_start = 1; static const int control_parser_start = 1;
static const int control_parser_first_final = 19; static const int control_parser_first_final = 68;
static const int control_parser_error = 0; static const int control_parser_error = 0;
static const int control_parser_en_main = 1; static const int control_parser_en_main = 1;
#line 65 "control_parser.rl" #line 104 "control_parser.rl"
static int control_parser_has_error(context *ctx) { static int control_parser_has_error(context *ctx) {
return ctx->cs == control_parser_error; return ctx->cs == control_parser_error;
@ -111,24 +217,28 @@ static int control_parser_is_finished(context *ctx) {
void control_parser_new(trial *t) { void control_parser_new(trial *t) {
context *ctx = t->parse_ctx = g_slice_new(context); context *ctx = t->parse_ctx = g_slice_new(context);
#line 115 "control_parser.c" #line 221 "control_parser.c"
{ {
( ctx->cs) = control_parser_start; ( ctx->cs) = control_parser_start;
} }
#line 77 "control_parser.rl" #line 116 "control_parser.rl"
ctx->buffer = g_string_sized_new(0); ctx->buffer = g_string_sized_new(0);
ctx->tmp = g_string_sized_new(0); ctx->tmp = g_string_sized_new(0);
ctx->mark = -1;
ctx->pos = 0;
} }
void control_parser_reset(trial *t) { void control_parser_reset(trial *t) {
context *ctx = t->parse_ctx; context *ctx = t->parse_ctx;
#line 127 "control_parser.c" #line 235 "control_parser.c"
{ {
( ctx->cs) = control_parser_start; ( ctx->cs) = control_parser_start;
} }
#line 84 "control_parser.rl" #line 125 "control_parser.rl"
g_string_truncate(ctx->tmp, 0); g_string_truncate(ctx->tmp, 0);
ctx->mark = -1;
ctx->pos = 0;
} }
void control_parser_free(trial *t) { void control_parser_free(trial *t) {
@ -141,14 +251,15 @@ run_t control_parse(trial *t) {
context *ctx = t->parse_ctx; context *ctx = t->parse_ctx;
gsize wehave; gsize wehave;
if (0 < (wehave = ctx->buffer->len - ctx->read)) { if (0 < (wehave = ctx->buffer->len - ctx->pos)) {
char *p, *pe; char *p, *pe;
p = ctx->buffer->str + ctx->read; p = ctx->buffer->str + ctx->pos;
pe = p + wehave; pe = p + wehave;
fprintf(stderr, "Parsing '%s' (len=%zu)\n", p, wehave);
#line 152 "control_parser.c" #line 263 "control_parser.c"
{ {
int _klen; int _klen;
unsigned int _trans; unsigned int _trans;
@ -222,42 +333,122 @@ _match:
switch ( *_acts++ ) switch ( *_acts++ )
{ {
case 0: case 0:
#line 26 "control_parser.rl" #line 34 "control_parser.rl"
{ {p++; goto _out; } } { {p++; goto _out; } t->alive = 0; }
break; break;
case 1: case 1:
#line 27 "control_parser.rl" #line 35 "control_parser.rl"
{ ctx->mark = p - ctx->buffer->str; } { ctx->mark = p - ctx->buffer->str; }
break; break;
case 2: case 2:
#line 35 "control_parser.rl" #line 36 "control_parser.rl"
{ t->map.dx = extract_double(ctx, p); } { ctx->tm = telemetry_new(); }
break; break;
case 3: case 3:
#line 36 "control_parser.rl" #line 37 "control_parser.rl"
{ t->map.dy = extract_double(ctx, p); } {
g_queue_push_tail(&t->telemetry, ctx->tm);
ctx->tm = NULL;
t->alive = 1;
}
break; break;
case 4: case 4:
#line 37 "control_parser.rl" #line 42 "control_parser.rl"
{ t->map.min_sensor = extract_double(ctx, p); } {
t->vehicle.x = ctx->x;
t->vehicle.y = ctx->y;
printf("Vehicle at %f / %f\n", ctx->x, ctx->y);
}
break; break;
case 5: case 5:
#line 38 "control_parser.rl" #line 52 "control_parser.rl"
{ t->map.max_sensor = extract_double(ctx, p); } { t->map.dx = extract_double(ctx, p); }
break; break;
case 6: case 6:
#line 39 "control_parser.rl" #line 53 "control_parser.rl"
{ t->map.max_speed = extract_double(ctx, p); } { t->map.dy = extract_double(ctx, p); }
break; break;
case 7: case 7:
#line 40 "control_parser.rl" #line 54 "control_parser.rl"
{ t->map.max_turn = extract_double(ctx, p); } { t->map.min_sensor = extract_double(ctx, p); }
break; break;
case 8: case 8:
#line 41 "control_parser.rl" #line 55 "control_parser.rl"
{ t->map.max_sensor = extract_double(ctx, p); }
break;
case 9:
#line 56 "control_parser.rl"
{ t->map.max_speed = extract_double(ctx, p); }
break;
case 10:
#line 57 "control_parser.rl"
{ t->map.max_turn = extract_double(ctx, p); }
break;
case 11:
#line 58 "control_parser.rl"
{ t->map.max_hard_turn = extract_double(ctx, p); } { t->map.max_hard_turn = extract_double(ctx, p); }
break; break;
#line 261 "control_parser.c" case 12:
#line 62 "control_parser.rl"
{ ctx->x = extract_double(ctx, p); }
break;
case 13:
#line 63 "control_parser.rl"
{ ctx->y = extract_double(ctx, p); }
break;
case 14:
#line 64 "control_parser.rl"
{ ctx->r = extract_double(ctx, p); }
break;
case 15:
#line 65 "control_parser.rl"
{ ctx->dir = extract_double(ctx, p); }
break;
case 16:
#line 66 "control_parser.rl"
{ ctx->speed = extract_double(ctx, p); }
break;
case 17:
#line 68 "control_parser.rl"
{ ctx->tm->vehicle.accel = ACCEL; }
break;
case 18:
#line 69 "control_parser.rl"
{ ctx->tm->vehicle.accel = ROLL; }
break;
case 19:
#line 70 "control_parser.rl"
{ ctx->tm->vehicle.accel = BREAK; }
break;
case 20:
#line 72 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_LEFT; }
break;
case 21:
#line 73 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_LEFT; }
break;
case 22:
#line 74 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_STRAIGHT; }
break;
case 23:
#line 75 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_RIGHT; }
break;
case 24:
#line 76 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_RIGHT; }
break;
case 25:
#line 78 "control_parser.rl"
{ ctx->ts = extract_ts(ctx, p); }
break;
case 26:
#line 79 "control_parser.rl"
{ printf("Score %u\n", extract_ts(ctx, p)); }
break;
#line 452 "control_parser.c"
} }
} }
@ -269,15 +460,26 @@ _again:
_test_eof: {} _test_eof: {}
_out: {} _out: {}
} }
#line 104 "control_parser.rl" #line 148 "control_parser.rl"
g_string_erase(ctx->buffer, 0, ctx->read); ctx->pos = p - ctx->buffer->str;
ctx->mark -= ctx->read; if (ctx->mark == -1) {
ctx->read = 0; g_string_erase(ctx->buffer, 0, ctx->pos);
ctx->pos = 0;
} else if (ctx->mark) {
g_string_erase(ctx->buffer, 0, ctx->mark);
ctx->pos -= ctx->mark;
ctx->mark = 0;
}
} else {
fprintf(stderr, "No data to parse\n");
} }
if (control_parser_has_error(ctx)) { if (control_parser_has_error(ctx)) {
fprintf(stderr, "Parse error\n"); fprintf(stderr, "Parse error\n");
shutdown(t->socket, SHUT_RDWR);
close(t->socket);
exit(126);
return RUN_ERROR; return RUN_ERROR;
} }
if (control_parser_is_finished(ctx)) return RUN_DONE; if (control_parser_is_finished(ctx)) return RUN_DONE;

View File

@ -11,12 +11,12 @@ typedef enum { RUN_ERROR, RUN_DONE, RUN_GO_ON } run_t;
struct control_parser_ctx { struct control_parser_ctx {
int cs; int cs;
GString *buffer; GString *buffer;
gsize read; gssize mark, pos;
size_t mark;
telemetry *tm;
GString *tmp; GString *tmp;
timestamp ts; timestamp ts;
double x, y, r, dir; double x, y, r, dir, speed;
}; };
void control_parser_new(trial *t); void control_parser_new(trial *t);

View File

@ -3,14 +3,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h>
typedef control_parser_ctx context; typedef control_parser_ctx context;
static void extract(context *ctx, char *fpc) { static void extract(context *ctx, char *fpc) {
gsize len = (fpc - ctx->buffer->str) - ctx->mark; gssize curpos = (fpc - ctx->buffer->str);
gssize len = curpos - ctx->mark;
g_string_truncate(ctx->tmp, 0); g_string_truncate(ctx->tmp, 0);
g_string_append_len(ctx->tmp, ctx->buffer->str + ctx->mark, len); g_string_append_len(ctx->tmp, ctx->buffer->str + ctx->mark, len);
ctx->read += len; ctx->mark = -1;
printf("Read chunk '%s'\n", ctx->tmp->str);
} }
static double extract_double(context *ctx, char *fpc) { static double extract_double(context *ctx, char *fpc) {
@ -18,19 +21,33 @@ static double extract_double(context *ctx, char *fpc) {
return strtod(ctx->tmp->str, NULL); return strtod(ctx->tmp->str, NULL);
} }
static timestamp extract_ts(context *ctx, char *fpc) {
extract(ctx, fpc);
return atoi(ctx->tmp->str);
}
%%{ %%{
machine control_parser; machine control_parser;
variable cs ctx->cs; variable cs ctx->cs;
action done { fbreak; } action done { fbreak; t->alive = 0; }
action mark { ctx->mark = fpc - ctx->buffer->str; } action mark { ctx->mark = fpc - ctx->buffer->str; }
action telemetrystart { } action telemetrystart { ctx->tm = telemetry_new(); }
action vehicle { } action telemetrystop {
g_queue_push_tail(&t->telemetry, ctx->tm);
ctx->tm = NULL;
t->alive = 1;
}
action vehicle {
t->vehicle.x = ctx->x;
t->vehicle.y = ctx->y;
printf("Vehicle at %f / %f\n", ctx->x, ctx->y);
}
SP = " "; SP = " ";
double = (digit | '.')*; double = '-'? (digit | '.')*;
dx = double >mark % { t->map.dx = extract_double(ctx, fpc); }; dx = double >mark % { t->map.dx = extract_double(ctx, fpc); };
dy = double >mark % { t->map.dy = extract_double(ctx, fpc); }; dy = double >mark % { t->map.dy = extract_double(ctx, fpc); };
@ -42,23 +59,45 @@ static double extract_double(context *ctx, char *fpc) {
init = "I" SP dx SP dy SP double SP min_sensor SP max_sensor SP max_speed SP max_turn SP max_hard_turn SP ";"; init = "I" SP dx SP dy SP double SP min_sensor SP max_sensor SP max_speed SP max_turn SP max_hard_turn SP ";";
x = double; x = double >mark % { ctx->x = extract_double(ctx, fpc); };
y = double; y = double >mark % { ctx->y = extract_double(ctx, fpc); };
r = double; r = double >mark % { ctx->r = extract_double(ctx, fpc); };
dir = double; dir = double >mark % { ctx->dir = extract_double(ctx, fpc); };
speed = double; speed = double >mark % { ctx->speed = extract_double(ctx, fpc); };
ctl = [ab\-] [Ll\-rR]; ctl = (
timestamp = double; "a" @ { ctx->tm->vehicle.accel = ACCEL; }
| "-" @ { ctx->tm->vehicle.accel = ROLL; }
| "b" @ { ctx->tm->vehicle.accel = BREAK; }
) (
"L" @ { ctx->tm->vehicle.turn = TURN_HARD_LEFT; }
| "l" @ { ctx->tm->vehicle.turn = TURN_LEFT; }
| "-" @ { ctx->tm->vehicle.turn = TURN_STRAIGHT; }
| "r" @ { ctx->tm->vehicle.turn = TURN_RIGHT; }
| "R" @ { ctx->tm->vehicle.turn = TURN_HARD_RIGHT; }
);
timestamp = (digit*) >mark % { ctx->ts = extract_ts(ctx, fpc); };
score = (digit*) >mark % { printf("Score %u\n", extract_ts(ctx, fpc)); };
boulder = "b" SP x SP y SP r SP; boulder = "b" SP x SP y SP r SP;
crater = "b" SP x SP y SP r SP; crater = "c" SP x SP y SP r SP;
homebase = "b" SP x SP y SP r SP; homebase = "h" SP x SP y SP r SP;
martian = "m" SP x SP y SP dir SP speed SP; martian = "m" SP x SP y SP dir SP speed SP;
object = boulder | crater | homebase | martian; object = boulder | crater | homebase | martian;
objects = object*; objects = object*;
telemetry = ( ("T" SP timestamp SP ctl SP x SP y SP dir SP speed SP) %vehicle objects) >telemetrystart ";"; telemetry = ( ("T" SP timestamp SP ctl SP x SP y SP dir SP speed SP) %vehicle objects) >telemetrystart ";" % telemetrystop;
main := init @ done; # crash
Boulder = "B" SP timestamp SP ";";
# fell
Crater = "C" SP timestamp SP ";";
# killed by martian
Kill = "K" SP timestamp SP ";";
Success = "S" SP timestamp SP ";";
End = "E" SP timestamp SP score SP ";";
main := init (telemetry | Boulder)* (Crater | Kill | Success)? End @ done;
}%% }%%
%% write data; %% write data;
@ -76,12 +115,16 @@ void control_parser_new(trial *t) {
%% write init; %% write init;
ctx->buffer = g_string_sized_new(0); ctx->buffer = g_string_sized_new(0);
ctx->tmp = g_string_sized_new(0); ctx->tmp = g_string_sized_new(0);
ctx->mark = -1;
ctx->pos = 0;
} }
void control_parser_reset(trial *t) { void control_parser_reset(trial *t) {
context *ctx = t->parse_ctx; context *ctx = t->parse_ctx;
%% write init; %% write init;
g_string_truncate(ctx->tmp, 0); g_string_truncate(ctx->tmp, 0);
ctx->mark = -1;
ctx->pos = 0;
} }
void control_parser_free(trial *t) { void control_parser_free(trial *t) {
@ -94,21 +137,33 @@ run_t control_parse(trial *t) {
context *ctx = t->parse_ctx; context *ctx = t->parse_ctx;
gsize wehave; gsize wehave;
if (0 < (wehave = ctx->buffer->len - ctx->read)) { if (0 < (wehave = ctx->buffer->len - ctx->pos)) {
char *p, *pe; char *p, *pe;
p = ctx->buffer->str + ctx->read; p = ctx->buffer->str + ctx->pos;
pe = p + wehave; pe = p + wehave;
fprintf(stderr, "Parsing '%s' (len=%zu)\n", p, wehave);
%% write exec; %% write exec;
g_string_erase(ctx->buffer, 0, ctx->read); ctx->pos = p - ctx->buffer->str;
ctx->mark -= ctx->read; if (ctx->mark == -1) {
ctx->read = 0; g_string_erase(ctx->buffer, 0, ctx->pos);
ctx->pos = 0;
} else if (ctx->mark) {
g_string_erase(ctx->buffer, 0, ctx->mark);
ctx->pos -= ctx->mark;
ctx->mark = 0;
}
} else {
fprintf(stderr, "No data to parse\n");
} }
if (control_parser_has_error(ctx)) { if (control_parser_has_error(ctx)) {
fprintf(stderr, "Parse error\n"); fprintf(stderr, "Parse error\n");
shutdown(t->socket, SHUT_RDWR);
close(t->socket);
exit(126);
return RUN_ERROR; return RUN_ERROR;
} }
if (control_parser_is_finished(ctx)) return RUN_DONE; if (control_parser_is_finished(ctx)) return RUN_DONE;

View File

@ -20,7 +20,7 @@ int main(int argc, char **argv) {
/*create trivial path towards the origin*/ /*create trivial path towards the origin*/
p = path_new(&t->map,&t->vehicle); p = path_new(&t->map,&t->vehicle);
trial_wait_for_input(t); trial_wait_for_start(t);
while(t->alive){ while(t->alive){
trial_wait_for_input(t); trial_wait_for_input(t);
path_execute(t,p); path_execute(t,p);