interpret/src/flight.re.c

140 lines
4.1 KiB
C

/** @license 2023 Neil Edelman, distributed under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
@std C11 */
#define BASE
#include "../src/flight.h" /* base */
#include "../src/journal.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
static void flight_to_string(const union line64 line, const struct flight *u,
char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int flight_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME flight
#define TREE_KEY union line64
#define TREE_VALUE struct flight
#define TREE_COMPARE
#define TREE_TO_STRING
#include "../src/tree.h"
#define PROTO
#include "../src/flight.h" /* proto */
/*!conditions:re2c*/
static int scan(struct flights *const f,
union date32 date, const char *const text) {
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *s0, *s1;
enum YYCONDTYPE condition = yycline;
size_t line = 1;
char datestr[12] = {0};
const char *why = "unexpected";
struct flight *flight = 0;
assert(f && text);
YYCURSOR = YYMARKER = yyt1 = text;
/*!re2c /**/
re2c:define:YYCTYPE = char;
re2c:yyfill:enable = 0;
re2c:define:YYGETCONDITION = "condition";
re2c:define:YYSETCONDITION = "condition = @@;";
re2c:define:YYGETCONDITION:naked = 1;
re2c:define:YYSETCONDITION:naked = 1;
//unix_control = [\x01-\x08\x0b-\x1f\x7f];
ws = [ \t];
glyph = [^\x00-\x20\x7f]; // [^\n\t ] + all the other weird
semitext = glyph \ ";";
natural = [1-9][0-9]*;
*/
for( ; ; ) { /*!re2c /**/
/* Default ignore. */
<skip> [^\n\x00] { continue; }
<skip> "\x00" { why = "no newline at end of file"; goto catch; }
<line> "\x00" { return 1; }
<line, skip> "\n" => line { line++; continue; }
<line> * :=> skip
/* Except these two. */
<line> "[glider]" :=> glider_type
<line> "[flight]" :=> flight
/* type; registration; launch -- landing; pic; sic;
single engine day dual; pilot; instrument simulated; actual; remarks */
<flight> * { why = "default unrecognized"; goto catch; }
<glider_type> * { why = "type unrecognized"; goto catch; }
<glider_reg> * { why = "reg unrecognized"; goto catch; }
<glider_launch> * { why = "launch unrecognized"; goto catch; }
<glider_err> * { why = "planned"; goto catch; }
/* type, reg, launch, how, height, landing, pilot, dual, instr, remarks
[glider] 2-33A; C-GCLK; CYQQ; A; 2000'; CYQQ; ;:13;; Peters D1 */
<glider_type> ws* @s0 semitext+ @s1 ws* ";" => glider_reg {
const union line64 key
= {{ (uint32_t)line, {{ date.day, date.month, date.year }} }};
assert(!flight);
if(line > UINT32_MAX) { why = "line overflow"; goto catch; }
switch(flight_tree_try(&f->_, key, &flight)) {
case TREE_PRESENT: why = "duplicate key";
case TREE_ERROR: goto catch;
case TREE_ABSENT: flight->type = GLIDER; break;
}
flight->glider.type.a = s0, flight->glider.type.b = s1;
}
<glider_reg> ws* @s0 semitext+ @s1 ws* ";" => glider_launch {
assert(flight && flight->type == GLIDER);
}
<glider_launch> ws* @s0 semitext+ @s1 *ws* ";" => glider_err {
}
/* "M" - Motor Car Tow
"W" - Winch
"A" - Aero Tow */
*/ }
assert(0); /* Never gets here. */
catch:
if(!errno) errno = EILSEQ;
date32_to_string(date, &datestr);
fprintf(stderr, "%s\n"
"%s line %zu: %s.\n", text, datestr, line, why);
return 0;
}
/** Dynamic memory allocation for `f` will be zero, <fn:flights_is_valid> will
be false. */
void flights_(struct flights *const f) {
if(!f) return;
flight_tree_(&f->_);
}
struct flights flights(/*const*/ struct journal *const j) {
struct flights f;
struct journal_iterator it;
union date32 date;
const char *text;
assert(j);
f._ = flight_tree();
it = journal_iterator(j);
while(journal_next(&it, &date, &text)) if(!scan(&f, date, text)) goto catch;
goto finally;
catch:
flights_(&f);
finally:
return f;
}
struct flight *flights_add(struct flights *const flights,
const union line64 line) {
struct flight *flight;
assert(flights);
/* We assert that the flights are unique. */
switch(flight_tree_try(&flights->_, line, &flight)) {
case TREE_PRESENT: errno = EDOM;
case TREE_ERROR: return 0;
case TREE_ABSENT: return flight;
}
}