2023-05-25 23:28:10 -07:00

#### 293 lines 8.1 KiB C Raw Permalink Blame History

 ```/** Tyler Durden: calculates a day number according to the Gregorian calendar. */ ``` ```function g(y,m,d) ``` ```m = (m + 9) % 12 ``` ```y = y - m/10 ``` ```return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) ``` ``` ``` ```Difference between two dates = g(y2,m2,d2) - g(y1,m1,d1) ``` ``` ``` ```/** Tomohiko Sakamoto comp.lang.c 1993-04-10. */ ``` ```static unsigned weekday(union date32 d) { ``` ``` d.year -= d.month < 3; ``` ``` return (d.year + d.year / 4 - d.year / 100 + d.year / 400 ``` ``` + "-bed=pen+mad."[d.month] + d.day) % 7; ``` ```} ``` ```/* ``` ```### plot with steps ``` ```reset session ``` ``` ``` ```\$Data <cursor; ``` ``` re2c:define:YYMARKER = s->marker; ``` ``` re2c:define:YYCONDTYPE = 'condition'; ``` ``` re2c:define:YYGETCONDITION = 's->condition'; ``` ``` re2c:define:YYGETCONDITION:naked = 1; ``` ``` re2c:define:YYSETCONDITION = 's->condition = @@;'; ``` ``` re2c:define:YYSETCONDITION:naked = 1; ``` ``` sentinel = "\x00"; ``` ``` newline = "\n"; ``` ``` unix_control = [\x01-\x08\x0a-\x1f\x7f]; ``` ``` ws = [ \t]; ``` ``` glyph = [^] \ (sentinel | unix_control | newline | ws); ``` ``` keyword = ([a-zA-Z] | [0-9][0-9_\-]*[a-zA-Z]) [a-zA-Z0-9_\-]*; ``` ``` decimal = "-"? ([1-9][0-9]* | [0])? "." [0-9]+ | [1-9][0-9]* | [0]; ``` ``` natural = [1-9][0-9]*; ``` ``` date = "-"? natural "-" [0-1][0-9] "-" [0-1][0-9]; ``` ``` */ ``` ``` const char *s0, *s1; ``` ``` /*!stags:re2c format = 'const char *@@;\n'; */ ``` ``` assert(s && x); ``` ``` if(!s->buffer) return 0; ``` ``` x->line = s->line; ``` ``` x->s0 = x->s1 = 0; ``` ```scan: ``` ``` /*!re2c /**/ ``` ``` <*> unix_control { return x->symbol = ILLEGAL, 0; } ``` ``` <*> * { return x->symbol = SYNTAX, 0; } ``` ``` <*> sentinel /* New line always delimits. */ ``` ``` { return x->symbol = s->condition == yycline ? END : ILLEGAL, 0; } ``` ``` newline => line { x->line = ++s->line; goto scan; } ``` ``` /* Symbols that go at the beginning of a line. */ ``` ``` newline { x->line = ++s->line; goto scan; } ``` ``` "[" :=> edict ``` ``` "--" :=> source ``` ``` "->" :=> location ``` ``` "!" => text { return x->symbol = COMPLETE, 1; } ``` ``` "^" => text { return x->symbol = CANCELLED, 1; } ``` ``` "#" => text { return x->symbol = HEADING, 1; } ``` ``` * :=> text ``` ``` ``` ``` newline => line { x->line = ++s->line; goto scan; } ``` ``` ws+ { goto scan; } ``` ``` @s0 glyph+ @s1 ``` ``` { x->s0 = s0, x->s1 = s1; return x->symbol = TEXT, 1; } ``` ``` ``` ``` ``` ``` @s0 keyword @s1 => expect_line ``` ``` { x->s0 = s0, x->s1 = s1; return x->symbol = SOURCE_RECALL, 1; } ``` ``` ``` ``` "" / "(" :=> map ``` ``` "[" ws* @s0 keyword @s1 ws* "]" ``` ``` { x->s0 = s0, x->s1 = s1; return x->symbol = LOCATION_SAVE, 1; } ``` ``` @s0 keyword @s1 => expect_line ``` ``` { x->s0 = s0, x->s1 = s1; return x->symbol = LOCATION_RECALL, 1; } ``` ``` ``` ``` "(" @s0 decimal "," @s1 decimal ")" => expect_caption ``` ``` { x->s0 = s0, x->s1 = s1; return x->symbol = LOCATION, 1; } ``` ``` ``` ``` "source" :=> source ``` ``` "ed" :=> ed ``` ``` "contact" :=> contact ``` ``` "glider" :=> glider ``` ``` "flight" :=> flight ``` ``` "bible" :=> bible ``` ``` "book" :=> book ``` ``` "movie" :=> movie ``` ``` "tv" :=> tv ``` ``` "medication" :=> medication ``` ``` "idea" :=> idea ``` ``` "vaccine" :=> vaccine ``` ``` "in" :=> in ``` ``` "" / natural :=> significant ``` ``` [0-1][0-9] "-" [0-3][0-9] ``` ``` ", " [0-2][0-9] ":" [0-5][0-9] "] " ``` ``` :=> text /* This is likely WhatsApp conversations. Ignore. */ ``` ``` /* missed, show, 'First, Second', 'Sounds', 'CSS', ..., 'Swanky', 'Shields' */ ``` ``` ``` ``` /* How did it get into my journal? */ ``` ``` "source" ``` ``` { if(s->is_ws_expected || s->edict.size) ``` ``` return x->symbol = SYNTAX, 0; ``` ``` s->is_ws_expected = 1, s->is_source = 1; ``` ``` s->edict.size = 2; ``` ``` s->edict.expect[1] = EXPECT_KEYWORD; ``` ``` s->edict.expect[0] = EXPECT_END_TEXT; ``` ``` return x->symbol = SOURCE, 1; } ``` ``` "default" ``` ``` { if(s->is_ws_expected || !s->is_source) ``` ``` return x->symbol = SYNTAX, 0; ``` ``` s->is_ws_expected = 1, s->is_source = 0; ``` ``` return x->symbol = DEFAULT, 1; } ``` ``` ``` ``` /* Editorializing; looking back. */ ``` ``` "ed" ``` ``` { if(s->is_ws_expected || s->edict.size) ``` ``` return x->symbol = SYNTAX, 0; ``` ``` s->is_ws_expected = 1; /* no idea, just copy; probably should do sth */ ``` ``` s->edict.size = 1; ``` ``` s->edict.expect[0] = EXPECT_END_TEXT; /* Pithy comment. */ ``` ``` return x->symbol = EDITORIALIZING, 1; } ``` ``` ``` ``` /* Score. */ ``` ``` "significant" ``` ``` { if(s->is_ws_expected || s->edict.size) ``` ``` return x->symbol = SYNTAX, 0; ``` ``` s->is_ws_expected = 1; ``` ``` s->edict.size = 3; ``` ``` s->edict.expect[2] = EXPECT_NATURAL; /* Ordinal. */ ``` ``` s->edict.expect[1] = EXPECT_RESTRICT_TEXT; /* Name. */ ``` ``` s->edict.expect[0] = EXPECT_DATE; /* Birthday. */ ``` ``` return x->symbol = SIGNIFICANT, 1; } ``` ``` @s0 natural @s1 ``` ``` { if(s->is_ws_expected || s->edict.size) ``` ``` return x->symbol = SYNTAX, 0; ``` ``` s->is_ws_expected = 1; ``` ``` x->s0 = s0, x->s1 = s1; ``` ``` return x->symbol = SIGNIFICANT_RECALL, 1; } ``` ``` ``` ``` /* General [edict: whatever]. */ ``` ``` ws+ { s->is_ws_expected = 0; goto scan; } ``` ``` ":" ``` ``` { if(!s->edict.size) return x->symbol = SYNTAX, 0; ``` ``` s->is_ws_expected = 0, s->is_source = 0; ``` ``` expect_pop(); goto scan; } ``` ``` ws* @s0 keyword @s1 ws* ";"? ``` ``` { x->s0 = s0, x->s1 = s1; expect_pop(); ``` ``` return x->symbol = ARG_KEYWORD, 1; } ``` ``` ws* @s0 date @s1 ws* ";"? ``` ``` { x->s0 = s0, x->s1 = s1; expect_pop(); ``` ``` return x->symbol = ARG_DATE, 1; } ``` ``` ws* @s0 natural @s1 ws* ";"? ``` ``` { x->s0 = s0, x->s1 = s1; expect_pop(); ``` ``` return x->symbol = ARG_NATURAL, 1; } ``` ``` ``` ``` ws* @s0 (glyph \ [;[\]]) ((glyph \ [;[\]]) | ws)* @s1 ws* ";"? ``` ``` { x->s0 = s0, x->s1 = s1; expect_pop(); ``` ``` return x->symbol = ARG_RESTRICT_TEXT, 1; } ``` ``` ``` ``` ws* @s0 (glyph \ [[\]]) ((glyph \ [[\]]) | ws)* @s1 ws* ``` ``` { x->s0 = s0, x->s1 = s1; expect_pop(); ``` ``` return x->symbol = ARG_END_TEXT, 1; } ``` ``` "]" => expect_line ``` ``` { if(s->edict.size) return 0; goto scan; } ``` ``` */ ``` ```} ``` ``` ``` ```#define FOR_SYMBOL(X) \ ``` ``` \ ``` ``` /* Results. */ \ ``` ``` X(END, 0), \ ``` ``` X(SYNTAX, 0), \ ``` ``` X(ILLEGAL, 0), \ ``` ``` X(NOT_FOUND, 0), \ ``` ``` \ ``` ``` X(ORDERED_LIST_ITEM, &no_vt), \ ``` ``` X(LIST_ITEM, 0), \ ``` ``` X(COMPLETE, 0), \ ``` ``` X(CANCELLED, 0), \ ``` ``` X(HEADING, 0), \ ``` ``` /* Text. */ \ ``` ``` X(PARAGRAPH, 0), \ ``` ``` X(TEXT, &word_vt), \ ``` ``` /*This is lazy.*/X(CAPTION, &word_vt), \ ``` ``` \ ``` ``` /* Edicts. */ \ ``` ``` X(SOURCE, &word_vt), \ ``` ``` X(DEFAULT, 0), \ ``` ``` X(SOURCE_RECALL, &word_vt), \ ``` ``` X(LOCATION, 0), \ ``` ``` X(LOCATION_SAVE, 0), \ ``` ``` X(LOCATION_RECALL, 0), \ ``` ``` X(SIGNIFICANT, 0), \ ``` ``` X(SIGNIFICANT_RECALL, 0), \ ``` ``` X(EDITORIALIZING, 0), \ ``` ``` \ ``` ``` /* Arguments. */ \ ``` ``` X(ARG_KEYWORD, 0), \ ``` ``` X(ARG_DATE, &date_vt), \ ``` ``` X(ARG_NATURAL, &no_vt), \ ``` ``` X(ARG_RESTRICT_TEXT, &word_vt), \ ``` ``` X(ARG_END_TEXT, &word_vt), \ ``` ``` \ ``` ``` /* Bible */ \ ``` ``` X(KJV_BOOK, &kjv_book_vt), \ ``` ``` X(KJV_CHAPTER_VERSE, &word_vt), \ ``` ``` X(KJV_TEXT, &word_vt), \ ``` ``` X(KJV_NEXT, 0) ``` ``` ``` ```int scan(union date32 date, const char *const buffer); ``` ``` ``` ```struct scan { ``` ``` const char *marker, *from, *cursor, *limit, *label, *buffer; ``` ``` int condition; ``` ``` size_t line; ``` ``` int is_ws_expected; ``` ```}; ``` ``` ``` ```struct lex { ``` ``` size_t line; ``` ```#define ARG1(n, m) n ``` ``` enum lex_symbol { FOR_SYMBOL(ARG1) } symbol; ``` ```#undef ARG1 ``` ``` const char *s0, *s1; ``` ```}; ``` ```#define STR1(n, m) #n ``` ```static const char *const lex_symbols[] = { FOR_SYMBOL(STR1) }; ``` ```#undef X ``` ``` ``` ```struct scan scan(const char *); ``` ```int scan_next(struct scan *const s, struct lex *const x); ```