Instead of using the same memory, now it gets new memory.
This commit is contained in:
parent
73d09965f2
commit
6524d06c2b
120
src/interpret.c
120
src/interpret.c
@ -56,12 +56,60 @@ finally:
|
||||
return success;
|
||||
}
|
||||
|
||||
/** Is `y` a leap-year? */
|
||||
static int leap(int y) {
|
||||
assert(y >= 1582);
|
||||
if(!(y % 400)) return 1;
|
||||
if(!(y % 100)) return 0;
|
||||
if(!(y % 4)) return 1;
|
||||
return 0;
|
||||
}
|
||||
/** Not defined for some implementations. */
|
||||
struct date32 { unsigned year : 23, month : 4, day : 5; };
|
||||
/** Convert or narrower type or return zero. */
|
||||
static struct date32 date_to_32(const int y, const int m, const int d) {
|
||||
struct date32 d32 = { 0, 0, 0 };
|
||||
/* Leap year calculations only work at y>=1 and Gregorian Calendar and max
|
||||
23 bits. */
|
||||
if(y < 1582 || y > 8388607 || m < 1 || m > 12 || d < 1 || d > 31)
|
||||
return d32;
|
||||
switch(m) {
|
||||
case 1: case 3: case 5: case 7: case 8: case 10: case 12: break;
|
||||
case 4: case 6: case 9: case 11: if(d > 30) return d32; break;
|
||||
case 2: if(d > 28 + leap(y)) return d32;
|
||||
default: assert(0); break;
|
||||
}
|
||||
d32.year = (unsigned)y, d32.month = (unsigned)m, d32.day = (unsigned)d;
|
||||
return d32;
|
||||
}
|
||||
|
||||
/** Tomohiko Sakamoto comp.lang.c 1993-04-10. */
|
||||
static unsigned weekday(struct 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;
|
||||
}
|
||||
|
||||
#define ARRAY_NAME lex
|
||||
#define ARRAY_TYPE struct lex
|
||||
#include "array.h"
|
||||
struct page {
|
||||
struct date32 date;
|
||||
struct char_array entry;
|
||||
struct lex_array lexx;
|
||||
};
|
||||
#define ARRAY_NAME page
|
||||
#define ARRAY_TYPE struct page
|
||||
#include "array.h"
|
||||
|
||||
struct source { char *key, *desc; };
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int success = EXIT_FAILURE;
|
||||
DIR *dir = 0;
|
||||
struct dirent *de;
|
||||
struct int_array years = ARRAY_IDLE, months = ARRAY_IDLE, days = ARRAY_IDLE;
|
||||
struct char_array entry = ARRAY_IDLE;
|
||||
struct page_array pages = ARRAY_IDLE;
|
||||
int *y, *y_end;
|
||||
|
||||
/* Get the years list as directories matching a year in order. */
|
||||
@ -80,16 +128,16 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
closedir(dir), dir = 0;
|
||||
qsort(years.data, years.size, sizeof *years.data, &void_int_cmp);
|
||||
fprintf(stderr, "%s: %s.\n", argv[1], int_array_to_string(&years));
|
||||
fprintf(stderr, "Files in %s: %s.\n", argv[1], int_array_to_string(&years));
|
||||
|
||||
/* Go though each year. */
|
||||
for(y = years.data, y_end = y + years.size; y < y_end; y++) {
|
||||
char temp[64];
|
||||
char fn[64];
|
||||
int *m, *m_end;
|
||||
sprintf(temp, "%d", *y);
|
||||
sprintf(fn, "%d", *y);
|
||||
|
||||
/* Get the months as directories. */
|
||||
if(chdir(temp) == -1 || !(dir = opendir("."))) goto catch;
|
||||
if(chdir(fn) == -1 || !(dir = opendir("."))) goto catch;
|
||||
while((de = readdir(dir))) {
|
||||
struct stat st;
|
||||
int month, *p;
|
||||
@ -101,15 +149,16 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
closedir(dir), dir = 0;
|
||||
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
|
||||
fprintf(stderr, "%s: %s.\n", temp, int_array_to_string(&months));
|
||||
fprintf(stderr, "Files in %s: %s.\n",
|
||||
fn, int_array_to_string(&months));
|
||||
|
||||
/* Go though each month. */
|
||||
for(m = months.data, m_end = m + months.size; m < m_end; m++) {
|
||||
int *d, *d_end;
|
||||
sprintf(temp, "%.2d", *m);
|
||||
sprintf(fn, "%.2d", *m);
|
||||
|
||||
/* Get the days as files. */
|
||||
if(chdir(temp) == -1 || !(dir = opendir("."))) goto catch;
|
||||
if(chdir(fn) == -1 || !(dir = opendir("."))) goto catch;
|
||||
while((de = readdir(dir))) {
|
||||
struct stat st;
|
||||
int day, *p;
|
||||
@ -122,33 +171,42 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
closedir(dir), dir = 0;
|
||||
qsort(days.data, days.size, sizeof *days.data, &void_int_cmp);
|
||||
fprintf(stderr, "%s: %s.\n", temp, int_array_to_string(&days));
|
||||
fprintf(stderr, "Files in %s: %s.\n",
|
||||
fn, int_array_to_string(&days));
|
||||
|
||||
for(d = days.data, d_end = d + days.size; d < d_end; d++) {
|
||||
struct lex lex;
|
||||
printf("%d-%.2d-%.2d\n", *y, *m, *d);
|
||||
sprintf(temp, "%.2d.txt", *d);
|
||||
if(!append_file(&entry, temp)) goto catch;
|
||||
printf("%s", entry.data);
|
||||
struct page *page;
|
||||
if(!(page = page_array_new(&pages))) goto catch;
|
||||
char_array(&page->entry);
|
||||
lex_array(&page->lexx);
|
||||
printf("Date: %d-%.2d-%.2d\n", *y, *m, *d);
|
||||
if(!((page->date = date_to_32(*y, *m, *d))).year)
|
||||
{ errno = EILSEQ; goto catch; }
|
||||
sprintf(fn, "%.2d.txt", *d);
|
||||
if(!append_file(&page->entry, fn)) goto catch;
|
||||
printf("%s", page->entry.data);
|
||||
printf("Lexing:\n");
|
||||
lex_reset(entry.data);
|
||||
while(lex_next(&lex)) {
|
||||
for(lex_reset(page->entry.data); ; ) {
|
||||
struct lex *lex;
|
||||
if(!(lex = lex_array_new(&page->lexx))) goto catch;
|
||||
if(!lex_next(lex)) {
|
||||
if(lex->symbol != END) { errno = EILSEQ; goto catch; }
|
||||
printf("Lexing finished: %s on %lu.\n",
|
||||
lex_symbols[lex->symbol], lex->line);
|
||||
break;
|
||||
}
|
||||
printf("%lu: %s",
|
||||
(unsigned long)lex.line, lex_symbols[lex.symbol]);
|
||||
if(lex.symbol == TEXT || lex.symbol == ARG_KEYWORD
|
||||
|| lex.symbol == ARG_DATE
|
||||
|| lex.symbol == ARG_FREEFORM
|
||||
|| lex.symbol == CAPTION) {
|
||||
if(lex.s0 + INT_MAX < lex.s1)
|
||||
(unsigned long)lex->line, lex_symbols[lex->symbol]);
|
||||
if(lex->symbol == TEXT || lex->symbol == ARG_KEYWORD
|
||||
|| lex->symbol == ARG_DATE
|
||||
|| lex->symbol == ARG_FREEFORM
|
||||
|| lex->symbol == CAPTION) {
|
||||
if(lex->s0 + INT_MAX < lex->s1)
|
||||
{ errno = EILSEQ; goto catch; }
|
||||
printf(" <<%.*s>>", (int)(lex.s1 - lex.s0), lex.s0);
|
||||
printf(" <<%.*s>>", (int)(lex->s1 - lex->s0), lex->s0);
|
||||
}
|
||||
printf(".\n");
|
||||
}
|
||||
printf("Lexing finished: %s on %lu.\n",
|
||||
lex_symbols[lex.symbol], lex.line);
|
||||
if(lex.symbol != END) { errno = EILSEQ; goto catch; }
|
||||
char_array_clear(&entry);
|
||||
}
|
||||
|
||||
int_array_clear(&days);
|
||||
@ -166,5 +224,11 @@ finally:
|
||||
if(dir && closedir(dir)) success = EXIT_FAILURE, perror("dir");
|
||||
int_array_(&years);
|
||||
int_array_(&months);
|
||||
return EXIT_FAILURE;
|
||||
int_array_(&days);
|
||||
{
|
||||
struct page *page;
|
||||
while(page = page_array_pop(&pages))
|
||||
lex_array_(&page->lexx), char_array_(&page->entry);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
@ -143,6 +143,7 @@ int lex_next(struct lex *const x) {
|
||||
/*!stags:re2c format = 'const char *@@;\n'; */
|
||||
assert(x);
|
||||
if(!scan.buffer) return 0;
|
||||
x->line = scan.line;
|
||||
x->s0 = x->s1 = 0;
|
||||
scan:
|
||||
/*!re2c
|
||||
|
Loading…
Reference in New Issue
Block a user