icfp11/src/control_parser.c

558 lines
13 KiB
C

#line 1 "control_parser.rl"
#include "control_parser.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
typedef control_parser_ctx context;
static void extract(context *ctx, char *fpc) {
gssize curpos = (fpc - ctx->buffer->str);
gssize len = curpos - ctx->mark;
g_string_truncate(ctx->tmp, 0);
g_string_append_len(ctx->tmp, ctx->buffer->str + ctx->mark, len);
ctx->mark = -1;
/* printf("Read chunk '%s'\n", ctx->tmp->str); */
}
static double extract_double(context *ctx, char *fpc) {
extract(ctx, fpc);
return strtod(ctx->tmp->str, NULL);
}
static timestamp extract_ts(context *ctx, char *fpc) {
extract(ctx, fpc);
return atoi(ctx->tmp->str);
}
#line 123 "control_parser.rl"
#line 35 "control_parser.c"
static const char _control_parser_actions[] = {
0, 1, 0, 1, 1, 1, 2, 1,
3, 1, 4, 1, 5, 1, 6, 1,
7, 1, 8, 1, 9, 1, 10, 1,
11, 1, 12, 1, 13, 1, 14, 1,
15, 1, 16, 1, 17, 1, 18, 1,
19, 1, 20, 1, 21, 1, 22, 1,
23, 1, 24, 1, 25, 1, 26, 1,
27, 1, 28, 1, 29, 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 short _control_parser_key_offsets[] = {
0, 0, 1, 2, 7, 12, 17, 22,
27, 32, 37, 42, 43, 49, 50, 53,
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, 166, 171, 176, 181, 186, 187, 192,
197, 202, 207, 212, 216, 220, 224, 228,
232, 236, 240, 244, 248, 252, 256, 260,
264, 268, 272, 276, 280, 283, 287, 291,
295, 299, 303, 307, 311, 315
};
static const char _control_parser_trans_keys[] = {
73, 32, 32, 45, 46, 48, 57, 32,
45, 46, 48, 57, 32, 45, 46, 48,
57, 32, 45, 46, 48, 57, 32, 45,
46, 48, 57, 32, 45, 46, 48, 57,
32, 45, 46, 48, 57, 32, 45, 46,
48, 57, 59, 66, 67, 69, 75, 83,
84, 32, 32, 48, 57, 32, 48, 57,
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, 98, 99, 104, 109, 66, 67, 69,
75, 83, 84, 32, 32, 45, 46, 48,
57, 32, 45, 46, 48, 57, 32, 45,
46, 48, 57, 59, 98, 99, 104, 109,
32, 32, 45, 46, 48, 57, 32, 45,
46, 48, 57, 32, 45, 46, 48, 57,
59, 98, 99, 104, 109, 32, 32, 45,
46, 48, 57, 32, 45, 46, 48, 57,
32, 45, 46, 48, 57, 59, 98, 99,
104, 109, 32, 32, 45, 46, 48, 57,
32, 45, 46, 48, 57, 32, 45, 46,
48, 57, 32, 45, 46, 48, 57, 59,
98, 99, 104, 109, 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, 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[] = {
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, 5, 6, 1, 1,
1, 1, 5, 1, 1, 1, 1, 5,
1, 1, 1, 1, 5, 1, 1, 1,
1, 1, 5, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 1, 2, 2, 2,
2, 2, 2, 2, 2, 0
};
static const char _control_parser_range_lengths[] = {
0, 0, 0, 2, 2, 2, 2, 2,
2, 2, 2, 0, 0, 0, 1, 1,
0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 0, 1, 0, 0, 0,
2, 2, 2, 2, 0, 0, 0, 2,
2, 2, 0, 0, 2, 2, 2, 0,
0, 2, 2, 2, 0, 0, 2, 2,
2, 2, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0
};
static const short _control_parser_index_offsets[] = {
0, 0, 2, 4, 8, 12, 16, 20,
24, 28, 32, 36, 38, 45, 47, 50,
53, 55, 58, 60, 62, 64, 67, 70,
72, 75, 78, 81, 83, 86, 90, 96,
98, 102, 106, 110, 114, 120, 127, 129,
133, 137, 141, 147, 149, 153, 157, 161,
167, 169, 173, 177, 181, 187, 189, 193,
197, 201, 205, 211, 215, 219, 223, 227,
231, 235, 239, 243, 247, 251, 255, 259,
263, 267, 271, 275, 279, 282, 286, 290,
294, 298, 302, 306, 310, 314
};
static const char _control_parser_trans_targs[] = {
2, 0, 3, 0, 4, 84, 84, 0,
5, 83, 83, 0, 6, 82, 82, 0,
7, 81, 81, 0, 8, 80, 80, 0,
9, 79, 79, 0, 10, 78, 78, 0,
11, 77, 77, 0, 12, 0, 13, 16,
20, 16, 16, 27, 0, 14, 0, 11,
15, 0, 11, 15, 0, 17, 0, 18,
26, 0, 19, 0, 20, 0, 21, 0,
22, 25, 0, 23, 24, 0, 85, 0,
23, 24, 0, 22, 25, 0, 18, 26,
0, 28, 0, 29, 76, 0, 30, 30,
30, 0, 31, 31, 31, 31, 31, 0,
32, 0, 33, 75, 75, 0, 34, 74,
74, 0, 35, 73, 73, 0, 36, 72,
72, 0, 37, 38, 43, 48, 53, 0,
13, 16, 20, 16, 16, 27, 0, 39,
0, 40, 71, 71, 0, 41, 70, 70,
0, 42, 69, 69, 0, 37, 38, 43,
48, 53, 0, 44, 0, 45, 68, 68,
0, 46, 67, 67, 0, 47, 66, 66,
0, 37, 38, 43, 48, 53, 0, 49,
0, 50, 65, 65, 0, 51, 64, 64,
0, 52, 63, 63, 0, 37, 38, 43,
48, 53, 0, 54, 0, 55, 62, 62,
0, 56, 61, 61, 0, 57, 60, 60,
0, 58, 59, 59, 0, 37, 38, 43,
48, 53, 0, 58, 59, 59, 0, 57,
60, 60, 0, 56, 61, 61, 0, 55,
62, 62, 0, 52, 63, 63, 0, 51,
64, 64, 0, 50, 65, 65, 0, 47,
66, 66, 0, 46, 67, 67, 0, 45,
68, 68, 0, 42, 69, 69, 0, 41,
70, 70, 0, 40, 71, 71, 0, 36,
72, 72, 0, 35, 73, 73, 0, 34,
74, 74, 0, 33, 75, 75, 0, 29,
76, 0, 11, 77, 77, 0, 10, 78,
78, 0, 9, 79, 79, 0, 8, 80,
80, 0, 7, 81, 81, 0, 6, 82,
82, 0, 5, 83, 83, 0, 4, 84,
84, 0, 0, 0
};
static const char _control_parser_trans_actions[] = {
0, 0, 0, 0, 61, 3, 3, 0,
64, 3, 3, 0, 0, 0, 0, 0,
67, 3, 3, 0, 70, 3, 3, 0,
73, 3, 3, 0, 76, 3, 3, 0,
79, 3, 3, 0, 0, 0, 0, 0,
0, 0, 0, 5, 0, 0, 0, 97,
3, 0, 51, 0, 0, 0, 0, 97,
3, 0, 0, 0, 0, 0, 0, 0,
97, 3, 0, 100, 3, 0, 1, 0,
53, 0, 0, 51, 0, 0, 51, 0,
0, 0, 0, 97, 3, 0, 37, 35,
39, 0, 45, 41, 49, 43, 47, 0,
0, 0, 82, 3, 3, 0, 85, 3,
3, 0, 91, 3, 3, 0, 94, 3,
3, 0, 9, 9, 9, 9, 9, 0,
7, 7, 7, 7, 7, 103, 0, 0,
0, 82, 3, 3, 0, 85, 3, 3,
0, 88, 3, 3, 0, 55, 55, 55,
55, 55, 0, 0, 0, 82, 3, 3,
0, 85, 3, 3, 0, 88, 3, 3,
0, 57, 57, 57, 57, 57, 0, 0,
0, 82, 3, 3, 0, 85, 3, 3,
0, 88, 3, 3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 82, 3, 3,
0, 85, 3, 3, 0, 91, 3, 3,
0, 94, 3, 3, 0, 59, 59, 59,
59, 59, 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, 29,
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
};
static const int control_parser_start = 1;
static const int control_parser_first_final = 85;
static const int control_parser_error = 0;
static const int control_parser_en_main = 1;
#line 126 "control_parser.rl"
static int control_parser_has_error(context *ctx) {
return ctx->cs == control_parser_error;
}
static int control_parser_is_finished(context *ctx) {
return ctx->cs >= control_parser_first_final;
}
void control_parser_new(trial *t) {
context *ctx = t->parse_ctx = g_slice_new(context);
#line 257 "control_parser.c"
{
( ctx->cs) = control_parser_start;
}
#line 138 "control_parser.rl"
ctx->buffer = g_string_sized_new(0);
ctx->tmp = g_string_sized_new(0);
ctx->mark = -1;
ctx->pos = 0;
}
void control_parser_reset(trial *t) {
context *ctx = t->parse_ctx;
#line 271 "control_parser.c"
{
( ctx->cs) = control_parser_start;
}
#line 147 "control_parser.rl"
g_string_truncate(ctx->tmp, 0);
ctx->mark = -1;
ctx->pos = 0;
}
void control_parser_free(trial *t) {
g_string_free(t->parse_ctx->buffer, TRUE);
g_string_free(t->parse_ctx->tmp, TRUE);
g_slice_free(context, t->parse_ctx);
}
run_t control_parse(trial *t) {
context *ctx = t->parse_ctx;
gsize wehave;
if (0 < (wehave = ctx->buffer->len - ctx->pos)) {
char *p, *pe;
p = ctx->buffer->str + ctx->pos;
pe = p + wehave;
/* fprintf(stderr, "Parsing '%s' (len=%zu)\n", p, wehave); */
#line 299 "control_parser.c"
{
int _klen;
unsigned int _trans;
const char *_acts;
unsigned int _nacts;
const char *_keys;
if ( p == pe )
goto _test_eof;
if ( ( ctx->cs) == 0 )
goto _out;
_resume:
_keys = _control_parser_trans_keys + _control_parser_key_offsets[( ctx->cs)];
_trans = _control_parser_index_offsets[( ctx->cs)];
_klen = _control_parser_single_lengths[( ctx->cs)];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + _klen - 1;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + ((_upper-_lower) >> 1);
if ( (*p) < *_mid )
_upper = _mid - 1;
else if ( (*p) > *_mid )
_lower = _mid + 1;
else {
_trans += (_mid - _keys);
goto _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = _control_parser_range_lengths[( ctx->cs)];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + (_klen<<1) - 2;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( (*p) < _mid[0] )
_upper = _mid - 2;
else if ( (*p) > _mid[1] )
_lower = _mid + 2;
else {
_trans += ((_mid - _keys)>>1);
goto _match;
}
}
_trans += _klen;
}
_match:
( ctx->cs) = _control_parser_trans_targs[_trans];
if ( _control_parser_trans_actions[_trans] == 0 )
goto _again;
_acts = _control_parser_actions + _control_parser_trans_actions[_trans];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
{
switch ( *_acts++ )
{
case 0:
#line 34 "control_parser.rl"
{ {p++; goto _out; } t->alive = 0; }
break;
case 1:
#line 35 "control_parser.rl"
{ ctx->mark = p - ctx->buffer->str; }
break;
case 2:
#line 36 "control_parser.rl"
{ ctx->tm = telemetry_new(); }
break;
case 3:
#line 37 "control_parser.rl"
{
g_queue_push_tail(&t->telemetry, ctx->tm);
if (!t->alive) {
t->vehicle.accel = ctx->tm->vehicle.accel;
t->vehicle.turn = ctx->tm->vehicle.turn;
}
t->last_ts = ctx->tm->ts;
gettimeofday(&t->started, NULL);
ctx->tm = NULL;
t->alive = 1;
}
break;
case 4:
#line 48 "control_parser.rl"
{
ctx->tm->ts = ctx->ts;
ctx->tm->vehicle.x = ctx->x;
ctx->tm->vehicle.y = ctx->y;
ctx->tm->vehicle.dir = ctx->dir;
ctx->tm->vehicle.speed = ctx->speed;
t->vehicle.x = ctx->x;
t->vehicle.y = ctx->y;
t->vehicle.dir = ctx->dir;
t->vehicle.speed = ctx->speed;
/* printf("Vehicle at %f / %f\n", ctx->x, ctx->y); */
}
break;
case 5:
#line 65 "control_parser.rl"
{ t->map.dx = extract_double(ctx, p); }
break;
case 6:
#line 66 "control_parser.rl"
{ t->map.dy = extract_double(ctx, p); }
break;
case 7:
#line 67 "control_parser.rl"
{ t->map.min_sensor = extract_double(ctx, p); }
break;
case 8:
#line 68 "control_parser.rl"
{ t->map.max_sensor = extract_double(ctx, p); }
break;
case 9:
#line 69 "control_parser.rl"
{ t->map.max_speed = extract_double(ctx, p); }
break;
case 10:
#line 70 "control_parser.rl"
{ t->map.max_turn = extract_double(ctx, p); }
break;
case 11:
#line 71 "control_parser.rl"
{ t->map.max_hard_turn = extract_double(ctx, p); }
break;
case 12:
#line 75 "control_parser.rl"
{ ctx->x = extract_double(ctx, p); }
break;
case 13:
#line 76 "control_parser.rl"
{ ctx->y = extract_double(ctx, p); }
break;
case 14:
#line 77 "control_parser.rl"
{ ctx->r = extract_double(ctx, p); }
break;
case 15:
#line 78 "control_parser.rl"
{ ctx->dir = extract_double(ctx, p); }
break;
case 16:
#line 79 "control_parser.rl"
{ ctx->speed = extract_double(ctx, p); }
break;
case 17:
#line 81 "control_parser.rl"
{ ctx->tm->vehicle.accel = ACCEL; }
break;
case 18:
#line 82 "control_parser.rl"
{ ctx->tm->vehicle.accel = ROLL; }
break;
case 19:
#line 83 "control_parser.rl"
{ ctx->tm->vehicle.accel = BREAK; }
break;
case 20:
#line 85 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_LEFT; }
break;
case 21:
#line 86 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_LEFT; }
break;
case 22:
#line 87 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_STRAIGHT; }
break;
case 23:
#line 88 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_RIGHT; }
break;
case 24:
#line 89 "control_parser.rl"
{ ctx->tm->vehicle.turn = TURN_HARD_RIGHT; }
break;
case 25:
#line 91 "control_parser.rl"
{ ctx->ts = extract_ts(ctx, p); }
break;
case 26:
#line 92 "control_parser.rl"
{ printf("Score %u\n", extract_ts(ctx, p)); }
break;
case 27:
#line 94 "control_parser.rl"
{
object o = { BOLDER, ctx->x, ctx->y, ctx->r, 0, 0 };
g_array_append_val(ctx->tm->objects, o);
}
break;
case 28:
#line 98 "control_parser.rl"
{
object o = { CRATER, ctx->x, ctx->y, ctx->r, 0, 0 };
g_array_append_val(ctx->tm->objects, o);
}
break;
case 29:
#line 103 "control_parser.rl"
{
object o = { MARTIAN, ctx->x, ctx->y, ctx->r, ctx->dir, ctx->speed };
g_array_append_val(ctx->tm->objects, o);
}
break;
#line 522 "control_parser.c"
}
}
_again:
if ( ( ctx->cs) == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_test_eof: {}
_out: {}
}
#line 170 "control_parser.rl"
ctx->pos = p - ctx->buffer->str;
if (ctx->mark == -1) {
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)) {
fprintf(stderr, "Parse error\n");
shutdown(t->socket, SHUT_RDWR);
close(t->socket);
exit(126);
return RUN_ERROR;
}
if (control_parser_is_finished(ctx)) return RUN_DONE;
return RUN_GO_ON;
}