/* Generated by Nimrod Compiler v0.9.5 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, i386, gcc */
/* Command for C compiler:
   gcc -c  -w -O3 -fno-strict-aliasing  -I/build/nimrod/src/nimrod/lib -o compiler/nimcache/nimlexbase.o compiler/nimcache/nimlexbase.c */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <string.h>

#include <stdio.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tbaselexer178018 tbaselexer178018;
typedef struct TNimObject TNimObject;
typedef struct tllstream176204 tllstream176204;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef N_NIMCALL_PTR(void, TY2289) (void* p, NI op);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY2289 marker;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  tbaselexer178018  {
  TNimObject Sup;
NI Bufpos;
NCSTRING Buf;
NI Buflen;
tllstream176204* Stream;
NI Linenumber;
NI Sentinel;
NI Linestart;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tllstream176204  {
  TNimObject Sup;
NU8 Kind;
FILE* F;
NimStringDesc* S;
NI Rd;
NI Wr;
NI Lineoffset;
};
N_NOCONV(void*, alloc_4601)(NI size);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(void, fillbuffer_178072)(tbaselexer178018* l);
N_NIMCALL(NI, llstreamread_176242)(tllstream176204* s, void* buf, NI buflen);
N_NOCONV(void*, realloc_4631)(void* p, NI newsize);
N_NIMCALL(void, skiputf8bom_179419)(tbaselexer178018* l);
N_NIMCALL(NI, fillbaselexer_178812)(tbaselexer178018* l, NI pos);
N_NOCONV(void, dealloc_4645)(void* p);
N_NIMCALL(void, llstreamclose_176238)(tllstream176204* s);
extern TNimType NTI2409; /* TObject */
TNimType NTI178018; /* TBaseLexer */
extern TNimType NTI106; /* int */
extern TNimType NTI145; /* cstring */
extern TNimType NTI176206; /* PLLStream */

N_NIMCALL(void, fillbuffer_178072)(tbaselexer178018* l) {
	NI charsread;
	NI tocopy;
	NI s;
	NI oldbuflen;
	NI LOC5;
	charsread = 0;
	tocopy = 0;
	s = 0;
	oldbuflen = 0;
	tocopy = (NI32)((NI32)((*l).Buflen - (*l).Sentinel) - 1);
	{
		if (!(0 < tocopy)) goto LA3;
		memmove(((void*) ((*l).Buf)), ((void*) (&(*l).Buf[(NI32)((*l).Sentinel + 1)])), (NI32)(tocopy * 1));
	}
	LA3: ;
	LOC5 = 0;
	LOC5 = llstreamread_176242((*l).Stream, ((void*) (&(*l).Buf[tocopy])), (NI32)((NI32)((*l).Sentinel + 1) * 1));
	charsread = (NI32)(LOC5 / 1);
	s = (NI32)(tocopy + charsread);
	{
		if (!(charsread < (NI32)((*l).Sentinel + 1))) goto LA8;
		(*l).Buf[s] = 0;
		(*l).Sentinel = s;
	}
	goto LA6;
	LA8: ;
	{
		s -= 1;
		while (1) {
			while (1) {
				NIM_BOOL LOC13;
				LOC13 = 0;
				LOC13 = (0 <= s);
				if (!(LOC13)) goto LA14;
				LOC13 = !((((NU8)((*l).Buf[s])) == ((NU8)(13)) || ((NU8)((*l).Buf[s])) == ((NU8)(10))));
				LA14: ;
				if (!LOC13) goto LA12;
				s -= 1;
			} LA12: ;
			{
				if (!(0 <= s)) goto LA17;
				(*l).Sentinel = s;
				goto LA11;
			}
			goto LA15;
			LA17: ;
			{
				void* LOC20;
				NI LOC21;
				oldbuflen = (*l).Buflen;
				(*l).Buflen = (NI32)((*l).Buflen * 2);
				LOC20 = 0;
				LOC20 = realloc_4631(((void*) ((*l).Buf)), (NI32)((*l).Buflen * 1));
				(*l).Buf = ((NCSTRING) (LOC20));
				LOC21 = 0;
				LOC21 = llstreamread_176242((*l).Stream, ((void*) (&(*l).Buf[oldbuflen])), (NI32)(oldbuflen * 1));
				charsread = (NI32)(LOC21 / 1);
				{
					if (!(charsread < oldbuflen)) goto LA24;
					(*l).Buf[(NI32)(oldbuflen + charsread)] = 0;
					(*l).Sentinel = (NI32)(oldbuflen + charsread);
					goto LA11;
				}
				LA24: ;
				s = (NI32)((*l).Buflen - 1);
			}
			LA15: ;
		} LA11: ;
	}
	LA6: ;
}

N_NIMCALL(void, skiputf8bom_179419)(tbaselexer178018* l) {
	{
		NIM_BOOL LOC3;
		NIM_BOOL LOC4;
		LOC3 = 0;
		LOC4 = 0;
		LOC4 = ((NU8)((*l).Buf[0]) == (NU8)(239));
		if (!(LOC4)) goto LA5;
		LOC4 = ((NU8)((*l).Buf[1]) == (NU8)(187));
		LA5: ;
		LOC3 = LOC4;
		if (!(LOC3)) goto LA6;
		LOC3 = ((NU8)((*l).Buf[2]) == (NU8)(191));
		LA6: ;
		if (!LOC3) goto LA7;
		(*l).Bufpos += 3;
		(*l).Linestart += 3;
	}
	LA7: ;
}

N_NIMCALL(void, openbaselexer_178027)(tbaselexer178018* l, tllstream176204* inputstream, NI buflen) {
	void* LOC1;
	(*l).Bufpos = 0;
	(*l).Buflen = buflen;
	LOC1 = 0;
	LOC1 = alloc_4601((NI32)(buflen * 1));
	(*l).Buf = ((NCSTRING) (LOC1));
	(*l).Sentinel = (NI32)(buflen - 1);
	(*l).Linestart = 0;
	(*l).Linenumber = 1;
	unsureAsgnRef((void**) &(*l).Stream, inputstream);
	fillbuffer_178072(l);
	skiputf8bom_179419(l);
}

N_NIMCALL(NI, getcolnumber_178045)(tbaselexer178018* l, NI pos) {
	NI result;
	result = 0;
	result = (NI32)abs((NI32)(pos - (*l).Linestart));
	return result;
}

N_NIMCALL(NI, fillbaselexer_178812)(tbaselexer178018* l, NI pos) {
	NI result;
	result = 0;
	{
		if (!(pos < (*l).Sentinel)) goto LA3;
		result = (NI32)(pos + 1);
	}
	goto LA1;
	LA3: ;
	{
		fillbuffer_178072(l);
		(*l).Bufpos = 0;
		result = 0;
	}
	LA1: ;
	(*l).Linestart = result;
	return result;
}

N_NIMCALL(NI, handlecr_178050)(tbaselexer178018* l, NI pos) {
	NI result;
	result = 0;
	(*l).Linenumber += 1;
	result = fillbaselexer_178812(l, pos);
	{
		if (!((NU8)((*l).Buf[result]) == (NU8)(10))) goto LA3;
		result = fillbaselexer_178812(l, result);
	}
	LA3: ;
	return result;
}

N_NIMCALL(NI, handlelf_178056)(tbaselexer178018* l, NI pos) {
	NI result;
	result = 0;
	(*l).Linenumber += 1;
	result = fillbaselexer_178812(l, pos);
	return result;
}

N_NIMCALL(void, closebaselexer_178035)(tbaselexer178018* l) {
	dealloc_4645(((void*) ((*l).Buf)));
	llstreamclose_176238((*l).Stream);
}
N_NOINLINE(void, nimlexbaseInit)(void) {
}

N_NOINLINE(void, nimlexbaseDatInit)(void) {
static TNimNode* TMP1382[7];
static TNimNode TMP703[8];
NTI178018.size = sizeof(tbaselexer178018);
NTI178018.kind = 17;
NTI178018.base = (&NTI2409);
TMP1382[0] = &TMP703[1];
TMP703[1].kind = 1;
TMP703[1].offset = offsetof(tbaselexer178018, Bufpos);
TMP703[1].typ = (&NTI106);
TMP703[1].name = "bufpos";
TMP1382[1] = &TMP703[2];
TMP703[2].kind = 1;
TMP703[2].offset = offsetof(tbaselexer178018, Buf);
TMP703[2].typ = (&NTI145);
TMP703[2].name = "buf";
TMP1382[2] = &TMP703[3];
TMP703[3].kind = 1;
TMP703[3].offset = offsetof(tbaselexer178018, Buflen);
TMP703[3].typ = (&NTI106);
TMP703[3].name = "bufLen";
TMP1382[3] = &TMP703[4];
TMP703[4].kind = 1;
TMP703[4].offset = offsetof(tbaselexer178018, Stream);
TMP703[4].typ = (&NTI176206);
TMP703[4].name = "stream";
TMP1382[4] = &TMP703[5];
TMP703[5].kind = 1;
TMP703[5].offset = offsetof(tbaselexer178018, Linenumber);
TMP703[5].typ = (&NTI106);
TMP703[5].name = "lineNumber";
TMP1382[5] = &TMP703[6];
TMP703[6].kind = 1;
TMP703[6].offset = offsetof(tbaselexer178018, Sentinel);
TMP703[6].typ = (&NTI106);
TMP703[6].name = "sentinel";
TMP1382[6] = &TMP703[7];
TMP703[7].kind = 1;
TMP703[7].offset = offsetof(tbaselexer178018, Linestart);
TMP703[7].typ = (&NTI106);
TMP703[7].name = "lineStart";
TMP703[0].len = 7; TMP703[0].kind = 2; TMP703[0].sons = &TMP1382[0];
NTI178018.node = &TMP703[0];
}

