Created
August 13, 2010 16:30
-
-
Save mattn/523155 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Generated from Zimbu file ./fizzbuzz.zu | |
*/ | |
#include <string.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <dirent.h> | |
#include <fcntl.h> | |
#ifdef __MINGW32__ | |
int _CRT_fmode = _O_BINARY; | |
int _fmode = _O_BINARY; | |
#endif | |
#include <sys/stat.h> | |
#include <stdio.h> | |
#include <signal.h> | |
#include <setjmp.h> | |
#include <strings.h> | |
/* | |
* TYPEDEFS | |
*/ | |
#ifdef __MINGW32__ | |
#define ZINT_FORMAT "%I64d" | |
#define ZINT_XFORMAT "%I64x" | |
#else | |
#define ZINT_FORMAT "%lld" | |
#define ZINT_XFORMAT "%llx" | |
#endif | |
typedef long long Zint; | |
typedef long long Zbbits; | |
typedef int Zbits; | |
typedef int Zbool; | |
typedef int Zstatus; | |
typedef int Zenum; | |
typedef unsigned char char_u; | |
typedef struct Zbytes__S Zbytes; | |
typedef struct Zpieceval__S Zpieceval; | |
typedef struct Zpiece__S Zpiece; | |
typedef struct Zcord__S Zcord; | |
typedef struct Zoref__S Zoref; | |
typedef struct ZforRange__S ZforRange_T; | |
typedef struct ZforList__S ZforList_T; | |
typedef struct CListHead__S CListHead; | |
typedef struct CListItem__S CListItem; | |
#ifndef INC___home__mattn__Develop__zimbu__lib__sysmodule_T | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/sysmodule.t.h" | |
#endif | |
#ifndef INC___home__mattn__Develop__zimbu__lib__iomodule_T | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/iomodule.t.h" | |
#endif | |
/* | |
* STRUCTS | |
*/ | |
#define MIOmodule__Veof EOF | |
typedef struct Zcode__S { | |
char *fileName; | |
char *text; | |
int line; | |
int col; | |
} Zcode; | |
void *Zalloc(size_t size); | |
void ZthrowS(char *format, char *arg); | |
void ZthrowI(char *format, Zint n); | |
void Zthrow(char *text); | |
#define ZbytesT_mask 0x07 | |
#define ZbytesT_tiny 0 | |
#define ZbytesT_bytesval 1 | |
#define ZbytesT_piece 2 | |
#define ZbytesT_pieceval 3 | |
#define ZbytesT_cord 4 | |
#define Zbytes_cow 0x08 | |
#define ZbytesP_mask 0xe0 | |
#define ZbytesP_default 0x00 | |
#define ZbytesP_nocord 0x20 | |
#define ZbytesP_minmem 0x40 | |
#define ZbytesP_minalloc 0x80 | |
struct Zbytes__S { | |
union { | |
void *data; | |
char tiny[sizeof(void *)]; | |
}; | |
Zint byteSize; | |
Zint charSize; | |
short growsize; | |
char flags; | |
}; | |
struct Zpieceval__S { | |
Zint space; | |
Zint offset; | |
void *data; | |
}; | |
struct Zpiece__S { | |
Zint space; | |
Zint offset; | |
}; | |
struct Zcord__S { | |
Zcord *next; | |
Zint byteSize; | |
Zint charSize; | |
char type; | |
Zint space; | |
Zint offset; | |
union { | |
void *data; | |
char tiny[sizeof(void *)]; | |
}; | |
}; | |
struct Zoref__S { | |
char *ptr; | |
char **table; | |
int type; | |
}; | |
struct ZforRange__S { | |
Zint idx; | |
Zint step; | |
Zint last; | |
}; | |
struct ZforList__S { | |
CListItem *listItem; | |
}; | |
struct CListHead__S { | |
CListItem *first; | |
CListItem *last; | |
int itemCount; | |
}; | |
struct CListItem__S { | |
CListItem *next; | |
CListItem *prev; | |
union { | |
int Ival; | |
void *Pval; | |
}; | |
int type; | |
}; | |
#ifndef INC___home__mattn__Develop__zimbu__lib__sysmodule_S | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/sysmodule.s.h" | |
#endif | |
#ifndef INC___home__mattn__Develop__zimbu__lib__iomodule_S | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/iomodule.s.h" | |
#endif | |
/* | |
* DECLARE FUNCTIONS AND GLOBALS | |
*/ | |
#ifndef INC___home__mattn__Develop__zimbu__lib__sysmodule_D | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/sysmodule.d.h" | |
#endif | |
#ifndef INC___home__mattn__Develop__zimbu__lib__iomodule_D | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/iomodule.d.h" | |
#endif | |
int ZglobInit(int round); | |
void *Zalloc(size_t size) { | |
void *p = calloc(1, size); | |
if (p == NULL) Zthrow("Out of memory"); | |
return p; | |
} | |
typedef struct Sstack__S { | |
int *stackEntries; | |
int stackSize; | |
int stackIndex; | |
} Sstack; | |
#ifdef PTHREAD_ONCE_INIT | |
static pthread_key_t ZstackKey; | |
void ZstackFree(void *ptr) { | |
Sstack *p = (Sstack *)ptr; | |
if (p->stackEntries != NULL) free(p->stackEntries); | |
free(ptr); | |
} | |
#else | |
Sstack Zstack = {NULL, 0, -1}; | |
#endif | |
void ZstackInit() { | |
#ifdef PTHREAD_ONCE_INIT | |
(void)pthread_key_create(&ZstackKey, ZstackFree); | |
pthread_setspecific(ZstackKey, NULL); | |
#endif | |
} | |
Sstack *ZstackGet() { | |
#ifdef PTHREAD_ONCE_INIT | |
Sstack *sp = pthread_getspecific(ZstackKey); | |
if (sp == NULL) { | |
sp = Zalloc(sizeof(Sstack)); | |
sp->stackEntries = NULL; | |
sp->stackSize = 0; | |
sp->stackIndex = -1; | |
pthread_setspecific(ZstackKey, sp); | |
} | |
return sp; | |
#else | |
return &Zstack; | |
#endif | |
} | |
Sstack *ZstackDeeper() { | |
#ifdef PTHREAD_ONCE_INIT | |
Sstack *sp = pthread_getspecific(ZstackKey); | |
if (sp == NULL) sp = ZstackGet(); | |
#else | |
Sstack *sp = &Zstack; | |
#endif | |
if (sp->stackEntries == NULL) { | |
sp->stackEntries = Zalloc(sizeof(int) * 50); | |
sp->stackSize = 50; | |
sp->stackIndex = 0; | |
} else if (++sp->stackIndex >= sp->stackSize) { | |
sp->stackEntries = realloc(sp->stackEntries, sizeof(int) * (sp->stackSize + 50)); | |
sp->stackSize += 50; | |
} | |
return sp; | |
} | |
typedef struct Senv__S { | |
jmp_buf *envEntries; | |
int envSize; | |
int envIndex; | |
} Senv; | |
#ifdef PTHREAD_ONCE_INIT | |
static pthread_key_t ZenvKey; | |
void ZenvFree(void *ptr) { | |
Senv *p = (Senv *)ptr; | |
if (p->envEntries != NULL) free(p->envEntries); | |
free(ptr); | |
} | |
#else | |
Senv Zenv = {NULL, 0, -1}; | |
#endif | |
void ZenvInit() { | |
#ifdef PTHREAD_ONCE_INIT | |
(void)pthread_key_create(&ZenvKey, ZenvFree); | |
#endif | |
} | |
Senv *ZenvGet() { | |
#ifdef PTHREAD_ONCE_INIT | |
Senv *sp = pthread_getspecific(ZenvKey); | |
if (sp == NULL) { | |
sp = Zalloc(sizeof(Senv)); | |
sp->envEntries = NULL; | |
sp->envSize = 0; | |
sp->envIndex = -1; | |
pthread_setspecific(ZenvKey, sp); | |
} | |
return sp; | |
#else | |
return &Zenv; | |
#endif | |
} | |
Senv *ZenvDeeper() { | |
#ifdef PTHREAD_ONCE_INIT | |
Senv *sp = pthread_getspecific(ZenvKey); | |
if (sp == NULL) sp = ZenvGet(); | |
#else | |
Senv *sp = &Zenv; | |
#endif | |
if (sp->envEntries == NULL) { | |
sp->envEntries = Zalloc(sizeof(jmp_buf) * 50); | |
sp->envSize = 50; | |
sp->envIndex = 0; | |
} else if (++sp->envIndex >= sp->envSize) { | |
sp->envEntries = realloc(sp->envEntries, sizeof(jmp_buf) * (sp->envSize + 50)); | |
sp->envSize += 50; | |
} | |
return sp; | |
} | |
char Zmethod0[] = "-- started new thread --"; | |
char Zfilename0[] = "/home/mattn/Develop/zimbu/lib/sysmodule.zu"; | |
char Zmethod1[] = "SYS.sleepSec()"; | |
char Zmethod2[] = "SYS.Pos.NEW()"; | |
char Zmethod3[] = "SYS.Pos.copy()"; | |
char Zfilename1[] = "/home/mattn/Develop/zimbu/lib/iomodule.zu"; | |
char Zmethod4[] = "IO"; | |
char Zmethod5[] = "IO.newStdin()"; | |
char Zmethod6[] = "IO.newStdout()"; | |
char Zmethod7[] = "IO.newStderr()"; | |
char Zmethod8[] = "IO.File.writeLine__1()"; | |
char Zmethod9[] = "IO.File.write__1()"; | |
char Zmethod10[] = "IO.File.readAll()"; | |
char Zmethod11[] = "IO.File.readLine()"; | |
char Zmethod12[] = "IO.File.readAllLines()"; | |
char Zmethod13[] = "IO.StringByteReader.NEW()"; | |
char Zmethod14[] = "IO.ByteReaderStack.NEW()"; | |
char Zmethod15[] = "IO.ByteReaderStack.readByte()"; | |
char Zmethod16[] = "IO.ByteReaderStack.push()"; | |
char Zmethod17[] = "IO.fileReader()"; | |
char Zmethod18[] = "IO.fileWriter()"; | |
char Zmethod19[] = "IO.write()"; | |
char Zmethod20[] = "IO.writeLine()"; | |
char Zmethod21[] = "IO.StringWriter.writeLine__1()"; | |
char Zmethod22[] = "IO.StringWriter.write__1()"; | |
char Zmethod23[] = "IO.stat()"; | |
char Zmethod24[] = "IO.dirList()"; | |
char Zmethod25[] = "IO.equalFiles()"; | |
char Zmethod26[] = "IO.findExe()"; | |
char Zmethod27[] = "IO.findExeInDirList()"; | |
char Zmethod28[] = "IO.resolve()"; | |
char Zmethod29[] = "IO.tailSepIndex()"; | |
char Zmethod30[] = "IO.pastHeadIndex()"; | |
char Zmethod31[] = "IO.nextPartIndex()"; | |
char Zfilename2[] = "fizzbuzz.zu"; | |
char Zmethod32[] = "MAIN()"; | |
Zcode ZcodeTable[] = { | |
{NULL, Zmethod0, 0, 0}, | |
{Zfilename0, Zmethod1, 62, 5}, | |
{Zfilename0, Zmethod2, 73, 5}, | |
{Zfilename0, Zmethod2, 80, 5}, | |
{Zfilename0, Zmethod3, 88, 15}, | |
{Zfilename1, Zmethod4, 26, 16}, | |
{Zfilename1, Zmethod4, 29, 17}, | |
{Zfilename1, Zmethod4, 32, 17}, | |
{Zfilename1, Zmethod5, 36, 17}, | |
{Zfilename1, Zmethod6, 49, 17}, | |
{Zfilename1, Zmethod7, 62, 17}, | |
{Zfilename1, Zmethod8, 126, 14}, | |
{Zfilename1, Zmethod8, 121, 14}, | |
{Zfilename1, Zmethod8, 116, 14}, | |
{Zfilename1, Zmethod8, 108, 10}, | |
{Zfilename1, Zmethod8, 108, 32}, | |
{Zfilename1, Zmethod9, 103, 14}, | |
{Zfilename1, Zmethod9, 98, 14}, | |
{Zfilename1, Zmethod9, 93, 14}, | |
{Zfilename1, Zmethod10, 159, 27}, | |
{Zfilename1, Zmethod10, 161, 17}, | |
{Zfilename1, Zmethod10, 165, 11}, | |
{Zfilename1, Zmethod10, 167, 16}, | |
{Zfilename1, Zmethod11, 173, 15}, | |
{Zfilename1, Zmethod11, 177, 24}, | |
{Zfilename1, Zmethod11, 179, 11}, | |
{Zfilename1, Zmethod11, 183, 13}, | |
{Zfilename1, Zmethod11, 185, 16}, | |
{Zfilename1, Zmethod12, 193, 23}, | |
{Zfilename1, Zmethod12, 197, 9}, | |
{Zfilename1, Zmethod13, 301, 5}, | |
{Zfilename1, Zmethod14, 322, 5}, | |
{Zfilename1, Zmethod15, 331, 10}, | |
{Zfilename1, Zmethod15, 336, 23}, | |
{Zfilename1, Zmethod16, 343, 7}, | |
{Zfilename1, Zmethod17, 352, 17}, | |
{Zfilename1, Zmethod18, 370, 17}, | |
{Zfilename1, Zmethod19, 485, 12}, | |
{Zfilename1, Zmethod19, 491, 12}, | |
{Zfilename1, Zmethod19, 497, 12}, | |
{Zfilename1, Zmethod20, 525, 12}, | |
{Zfilename1, Zmethod20, 532, 12}, | |
{Zfilename1, Zmethod20, 539, 12}, | |
{Zfilename1, Zmethod21, 126, 14}, | |
{Zfilename1, Zmethod21, 121, 14}, | |
{Zfilename1, Zmethod21, 116, 14}, | |
{Zfilename1, Zmethod21, 108, 10}, | |
{Zfilename1, Zmethod21, 108, 32}, | |
{Zfilename1, Zmethod22, 103, 14}, | |
{Zfilename1, Zmethod22, 98, 14}, | |
{Zfilename1, Zmethod22, 93, 14}, | |
{Zfilename1, Zmethod23, 733, 15}, | |
{Zfilename1, Zmethod24, 939, 9}, | |
{Zfilename1, Zmethod25, 1011, 15}, | |
{Zfilename1, Zmethod25, 1015, 15}, | |
{Zfilename1, Zmethod25, 1025, 21}, | |
{Zfilename1, Zmethod25, 1029, 21}, | |
{Zfilename1, Zmethod25, 1031, 10}, | |
{Zfilename1, Zmethod25, 1037, 19}, | |
{Zfilename1, Zmethod25, 1038, 19}, | |
{Zfilename1, Zmethod25, 1047, 8}, | |
{Zfilename1, Zmethod25, 1048, 8}, | |
{Zfilename1, Zmethod26, 1065, 15}, | |
{Zfilename1, Zmethod26, 1065, 60}, | |
{Zfilename1, Zmethod26, 1065, 45}, | |
{Zfilename1, Zmethod26, 1070, 12}, | |
{Zfilename1, Zmethod27, 1078, 16}, | |
{Zfilename1, Zmethod27, 1079, 22}, | |
{Zfilename1, Zmethod27, 1080, 10}, | |
{Zfilename1, Zmethod28, 1261, 43}, | |
{Zfilename1, Zmethod28, 1262, 58}, | |
{Zfilename1, Zmethod28, 1267, 13}, | |
{Zfilename1, Zmethod28, 1267, 34}, | |
{Zfilename1, Zmethod28, 1302, 13}, | |
{Zfilename1, Zmethod28, 1302, 33}, | |
{Zfilename1, Zmethod28, 1312, 17}, | |
{Zfilename1, Zmethod28, 1316, 15}, | |
{Zfilename1, Zmethod28, 1318, 21}, | |
{Zfilename1, Zmethod28, 1333, 11}, | |
{Zfilename1, Zmethod28, 1345, 8}, | |
{Zfilename1, Zmethod28, 1350, 26}, | |
{Zfilename1, Zmethod28, 1353, 34}, | |
{Zfilename1, Zmethod28, 1359, 35}, | |
{Zfilename1, Zmethod28, 1371, 15}, | |
{Zfilename1, Zmethod29, 1407, 13}, | |
{Zfilename1, Zmethod29, 1411, 22}, | |
{Zfilename1, Zmethod30, 1426, 11}, | |
{Zfilename1, Zmethod31, 1442, 14}, | |
{Zfilename1, Zmethod31, 1443, 10}, | |
{Zfilename2, Zmethod32, 4, 12}, | |
{Zfilename2, Zmethod32, 6, 10}, | |
{Zfilename2, Zmethod32, 8, 10}, | |
{Zfilename2, Zmethod32, 10, 10}, | |
{Zfilename2, Zmethod32, 12, 10}, | |
{Zfilename2, Zmethod32, 13, 10}, | |
}; | |
void Zbacktrace(FILE *fd) { | |
int i; | |
int last = 0; | |
Sstack *sp = ZstackGet(); | |
fprintf(fd, "Stack backtrace (last called first):\n"); | |
if (sp->stackIndex > 50) last = sp->stackIndex - 50; | |
for (i = sp->stackIndex; i >= last; --i) { | |
Zcode *p = &ZcodeTable[sp->stackEntries[i]]; | |
if (p->fileName == NULL) | |
fprintf(fd, "%s\n", p->text); | |
else | |
fprintf(fd, "%s line %d col %d: %s\n", p->fileName, p->line, p->col, p->text); | |
} | |
} | |
#ifdef SIGSEGV | |
void Zdeadly(int nr) { | |
fprintf(stderr, "Caught deadly signal %d\n", nr); | |
Sstack *sp = ZstackGet(); | |
--sp->stackIndex; /* top entry is invalid */ | |
Zbacktrace(stderr); | |
exit(127); | |
} | |
#endif | |
void ZthrowPos(char *posn, char *text) { | |
if (posn != NULL) { | |
fputs(posn, stderr); | |
fputs(": ", stderr); | |
} | |
fputs("Exception: ", stderr); | |
fputs(text, stderr); | |
fputs("\n", stderr); | |
Zbacktrace(stderr); | |
exit(1); | |
} | |
void ZthrowS(char *format, char *arg) { | |
char buf[200]; | |
snprintf(buf, 200, format, arg); | |
ZthrowPos(NULL, buf); | |
} | |
void ZthrowI(char *format, Zint n) { | |
char buf[200]; | |
snprintf(buf, 200, format, n); | |
ZthrowPos(NULL, buf); | |
} | |
void Zthrow(char *text) { | |
ZthrowPos(NULL, text); | |
} | |
int Zstrcmp(char *s1, char *s2) { | |
if (s1 == NULL || s2 == NULL) { | |
if (s1 == NULL && s2 == NULL) | |
return 0; | |
if (s1 == NULL) | |
return -1; | |
return 1; | |
} | |
return strcmp(s1, s2); | |
} | |
char *Zconcat(char *s1, char *s2) { | |
char *ss1 = s1 == NULL ? "NULL" : s1; | |
char *ss2 = s2 == NULL ? "NULL" : s2; | |
char *p = Zalloc(strlen(ss1) + strlen(ss2) + 1); | |
strcpy(p, ss1); | |
strcat(p, ss2); | |
return p; | |
} | |
char *Zint2string(Zint n) { | |
char *p = Zalloc(30); | |
sprintf(p, ZINT_FORMAT, n); | |
return p; | |
} | |
char *ZStringSlice(char *s, Zint start, Zint end) { | |
char *ss = (s == NULL) ? "NULL" : s; | |
char *r; | |
Zint l = (Zint)strlen(ss); | |
Zint is = start < 0 ? l + start : start; | |
Zint ie = end < 0 ? l + end : end; | |
if (is < 0) is = 0; | |
if (is > l) is = l; | |
if (ie < is - 1) ie = is - 1; | |
if (ie >= l) ie = l - 1; | |
l = ie - is + 1; | |
r = Zalloc(l + 1); | |
strncpy(r, ss + is, l); | |
r[l] = 0; | |
return r; | |
} | |
Zbool ZStringStartsWith(char *big, char *small) { | |
int i; | |
if (big == NULL || small == NULL) | |
return 0; | |
for (i = 0; small[i] != 0; ++i) { | |
if (big[i] != small[i]) | |
return 0; | |
} | |
return 1; | |
} | |
Zbool ZStringEndsWith(char *big, char *small) { | |
int i, j; | |
if (big == NULL || small == NULL) | |
return 0; | |
i = strlen(big) - 1; | |
j = strlen(small) - 1; | |
while (1) { | |
if (j < 0) | |
return 1; | |
if (i < 0) | |
return 0; | |
if (big[i] != small[j]) | |
return 0; | |
--i; | |
--j; | |
} | |
} | |
Zint ZStringIndex2(char *s, Zint c, Zint low) { | |
unsigned char *p; | |
if (s == NULL) return -1; | |
for (p = (unsigned char *)s; *p != 0; ++p) { | |
if ((Zint)((char *)p - s) >= low && *p == c) return (Zint)((char *)p - s); | |
} | |
return -1; | |
} | |
Zint ZStringRindex(char *s, Zint c) { | |
unsigned char *p; | |
unsigned char *found; | |
if (s == NULL) return -1; | |
found = NULL; | |
for (p = (unsigned char *)s; *p != 0; ++p) { | |
if (*p == c) found = p; | |
} | |
if (found == NULL) return -1; | |
return (int)((char *)found - s); | |
} | |
CListHead *ZStringSplit(char *s, char *m) { | |
CListHead *head = Zalloc(sizeof(CListHead)); | |
if (s == NULL) | |
Zthrow("split() string is NIL"); | |
else if (m == NULL) | |
Zthrow("split() argument is NIL"); | |
else if (*s != 0) { | |
int mlen = strlen(m); | |
char *ps; | |
char *pe; | |
CListItem *prev = NULL; | |
for (ps = s; ; ps = pe + mlen) { | |
CListItem *newitem = Zalloc(sizeof(CListItem)); | |
int len; | |
char *ns; | |
pe = strstr(ps, m); | |
if (pe == NULL) { | |
len = strlen(ps); | |
} else { | |
len = pe - ps; | |
} | |
ns = Zalloc(len + 1); | |
strncpy(ns, ps, len); | |
ns[len] = 0; | |
newitem->Pval = ns; | |
newitem->type = 1; | |
if (prev == NULL) | |
head->first = newitem; | |
else | |
prev->next = newitem; | |
newitem->prev = prev; | |
prev = newitem; | |
++head->itemCount; | |
if (pe == NULL) break; | |
} | |
head->last = prev; | |
} | |
return head; | |
} | |
CListItem *ZListFind(CListHead *head, int idx) { | |
if (head == NULL) Zthrow("Accessing NIL list"); | |
CListItem *item; | |
int n; | |
if (idx < 0) { | |
item = head->last; | |
n = -idx; | |
while (--n > 0 && item != NULL) | |
item = item->prev; | |
} else { | |
item = head->first; | |
n = idx; | |
while (--n >= 0 && item != NULL) | |
item = item->next; | |
} | |
return item; | |
} | |
CListHead *ZListAdd(CListHead *head, int after, int nr, void *ptr, int type) { | |
if (head == NULL) Zthrow("Attempt to append to NIL list"); | |
CListItem *item = Zalloc(sizeof(CListItem)); | |
if (type == 0) | |
item->Ival = nr; | |
else | |
item->Pval = ptr; | |
item->type = type; | |
if (head->last == NULL) { | |
head->first = item; | |
head->last = item; | |
} else { | |
CListItem *afterItem = NULL; | |
if (after != -1) { | |
afterItem = ZListFind(head, after); | |
} | |
if (afterItem == NULL || afterItem == head->last) { | |
item->prev = head->last; | |
item->prev->next = item; | |
head->last = item; | |
} else { | |
item->next = afterItem->next; | |
item->next->prev = item; | |
item->prev = afterItem; | |
afterItem->next = item; | |
} | |
} | |
++head->itemCount; | |
return head; | |
} | |
Zint ZListSize(CListHead *head) { | |
if (head == NULL) Zthrow("Attempt to get size of NIL list"); | |
return head->itemCount; | |
} | |
int ZListPopIntItem(CListHead *head, int idx) { | |
if (head == NULL) Zthrow("Invoking pop() on NIL list"); | |
CListItem *item = ZListFind(head, idx); | |
if (item != NULL) { | |
int v = item->Ival; | |
if (item->prev == NULL) | |
head->first = item->next; | |
else | |
item->prev->next = item->next; | |
if (item->next == NULL) | |
head->last = item->prev; | |
else | |
item->next->prev = item->prev; | |
/* TODO: free *item */ | |
head->itemCount--; | |
return v; | |
} | |
return 0; | |
} | |
ZforList_T *ZforListNew(CListHead *p) { | |
if (p == NULL) { | |
Zthrow("FOR on NIL list"); | |
} | |
ZforList_T *s = Zalloc(sizeof(ZforList_T)); | |
s->listItem = p->first; | |
return s; | |
} | |
void ZforListGetPtr(ZforList_T *s, char **p) { | |
if (s->listItem != NULL) | |
*p = s->listItem->Pval; | |
else | |
*p = NULL; | |
} | |
void ZforListGetInt(ZforList_T *s, Zint *p) { | |
if (s->listItem != NULL) | |
*p = s->listItem->Ival; | |
else | |
*p = 0; | |
} | |
int ZforListCont(ZforList_T *s) { | |
return s->listItem != NULL; | |
} | |
void ZforListNextPtr(ZforList_T *s, char **p) { | |
if (s->listItem != NULL) | |
s->listItem = s->listItem->next; | |
ZforListGetPtr(s, p); | |
} | |
void ZforListNextInt(ZforList_T *s, Zint *p) { | |
if (s->listItem != NULL) | |
s->listItem = s->listItem->next; | |
ZforListGetInt(s, p); | |
} | |
ZforRange_T *ZforRangeNew(Zint start, Zint last, Zint step) { | |
ZforRange_T *s = Zalloc(sizeof(ZforRange_T)); | |
s->idx = start; | |
s->last = last; | |
s->step = step; | |
return s; | |
} | |
void ZforRangeGetInt(ZforRange_T *s, Zint *p) { | |
*p = s->idx; | |
} | |
int ZforRangeCont(ZforRange_T *s) { | |
return s->step > 0 ? (s->idx <= s->last) : (s->idx >= s->last); | |
} | |
void ZforRangeNextInt(ZforRange_T *s, Zint *p) { | |
s->idx += s->step; | |
ZforRangeGetInt(s, p); | |
} | |
/* | |
* FUNCTION BODIES | |
*/ | |
#ifndef INC___home__mattn__Develop__zimbu__lib__sysmodule_B | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/sysmodule.b.c" | |
#endif | |
#ifndef INC___home__mattn__Develop__zimbu__lib__iomodule_B | |
#include "/home/mattn/Develop/zimbu/lib/ZUDIR/iomodule.b.c" | |
#endif | |
/* | |
* INIT GLOBALS | |
*/ | |
int ZglobInit(int round) { | |
int done = 1; | |
ZstackInit(); | |
Sstack *stack = ZstackDeeper(); | |
int sidx = stack->stackIndex; | |
if (round == 1) { | |
} | |
done &= Isysmodule(round); | |
done &= Iiomodule(round); | |
if (round == 2001) { | |
} | |
--(stack->stackIndex); | |
return done; | |
} | |
/* | |
* MAIN | |
*/ | |
int main(int argc, char **argv) { | |
int ret; | |
int round = 0; | |
int done; | |
#ifdef SIGSEGV | |
signal(SIGSEGV, Zdeadly); | |
#endif | |
ZglobInit(round++); | |
done = ZglobInit(round++); | |
while (!done) { | |
if (round == 1002) Zthrow("Early initialization not done within 1000 rounds"); | |
done = ZglobInit(round++); | |
} | |
round = 2001; | |
done = ZglobInit(round++); | |
while (!done) { | |
if (round == 3002) Zthrow("Initialization not done within 1000 rounds"); | |
done = ZglobInit(round++); | |
} | |
ret = Fmain(); | |
if (ZstackGet()->stackIndex != -1) fprintf(stderr, "INTERNAL: stack index not -1: %d\n", ZstackGet()->stackIndex); | |
return ret; | |
} | |
int Fmain() { | |
Sstack *stack = ZstackDeeper(); | |
int sidx = stack->stackIndex; | |
Zint ret; | |
ret = 0; | |
{ | |
stack->stackEntries[sidx] = 89; | |
Zint Vi; | |
for (Vi = 1; Vi <= 100; ++Vi) { | |
if (((Vi % 15) == 0)) | |
{ | |
(stack->stackEntries[sidx] = 90, MIOmodule__Fwrite("FizzBuzz\n")); | |
} | |
else if (((Vi % 3) == 0)) | |
{ | |
(stack->stackEntries[sidx] = 91, MIOmodule__Fwrite("Fizz\n")); | |
} | |
else if (((Vi % 5) == 0)) | |
{ | |
(stack->stackEntries[sidx] = 92, MIOmodule__Fwrite("Buzz\n")); | |
} | |
else | |
{ | |
(stack->stackEntries[sidx] = 93, MIOmodule__Fwrite__1(Vi)); | |
(stack->stackEntries[sidx] = 94, MIOmodule__Fwrite("\n")); | |
} | |
} | |
} | |
ret = 0; | |
--(stack->stackIndex); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment