// File: /workspace/github_cpp_makefile_star2_O3/rofl0r[P]ascc/cs_parser.cpp
// Function: cc_tokenize(char const*, ccInternalList*, ccCompiledScript*)
int cc_tokenize(const char*inpl, ccInternalList*targ, ccCompiledScript*scrip) {
    // *** create the symbol table and parse the text code into symbol code
    int linenum=1,in_struct_declr=-1,bracedepth = 0, last_time=0;
    int parenthesisdepth = 0;
    FMEM*iii=fmem_open(inpl);
    targ->write_meta(SMETA_LINENUM,1);
    while (!fmem_eof(iii)) {
        int thischar, waseof = 0;
        do {  // skip the whitespace
            if (fmem_eof(iii)) {
                waseof = 1;
                break;
            }
            thischar = fmem_getc(iii);
        } while (is_whitespace(thischar));
        // if it was the end of file, abort
        if (waseof)
            break;
        if ((thischar == '\r') || (thischar == '\n')) {
            // write the line number (for debugging)
            linenum++;
            targ->write_meta(SMETA_LINENUM,linenum);
            if (fmem_peekc(iii) =='\n') fmem_getc(iii);
            currentline=linenum;
            // go back and get the whitespace after the CRLF
            continue;
        }
        // it's some sort of symbol, so read it in
        char thissymbol[MAX_SYM_LEN];
        int symlen=1;
        thissymbol[0]=thischar;
        int thisIsEscaped = 0;
        while (is_part_of_symbol(fmem_peekc(iii),thischar) ||
               (thischar == '0' && symlen >= 2 && thissymbol[1] == 'x' && isxdigit(fmem_peekc(iii)))
        ) {

            thissymbol[symlen] = fmem_getc(iii);
            symlen++;
            if (symlen >= MAX_SYM_LEN - 1) break;
        }
        thissymbol[symlen]=0;
        if (thissymbol[0] == '0' && thissymbol[1] == 'x' && isxdigit(thissymbol[2])) {
            long tmp = strtoul(thissymbol, (char**) 0, 16);
            sprintf(thissymbol, "%d", (int)tmp);
        }
        if ((thissymbol[0] == '\'') && (thissymbol[2] == '\'')) {
            // convert the character to its ASCII equivalent
            sprintf(thissymbol,"%d",thissymbol[1]);
        }
        else if (thissymbol[0] == '\'') {
            cc_error("incorrectly terminated character constant");
            return -1;
        }

        if (sym.entries[last_time].stype == SYM_DOT) {
            // mangle member variable accesses so that you can have a
            // struct called Room but also a member property called Room
            char thissymbol_mangled[MAX_SYM_LEN + 1];
            sprintf(thissymbol_mangled, ".%s", thissymbol);
            strcpy(thissymbol, thissymbol_mangled);
        }

        int towrite = sym_find_or_add(sym, thissymbol);
        if (towrite < 0) {
            cc_error("symbol table overflow - could not ensure new symbol.");
            return -1;
        }
        if (
            (thissymbol[0] >= '0') && (thissymbol[0] <= '9') ||
            (thissymbol[0] == '-' && (thissymbol[1] >= '0') && (thissymbol[1] <= '9'))
        ) {
            if (strchr(thissymbol, '.') != NULL)
                sym.entries[towrite].stype = SYM_LITERALFLOAT;
            else
                sym.entries[towrite].stype = SYM_LITERALVALUE;
        }
        if (sym.entries[towrite].stype == SYM_MASSIGN && thischar == '*' && sym.entries[last_time].stype == SYM_VARTYPE) {
            // Break up *= when a parameter is declared with only the type and an initial value
            // e.g. import void someMethod(InventoryItem *= 0);
            targ->write(sym.find("*"));
            towrite = sym.find("=");
        }

        if (sym.entries[towrite].stype == SYM_OPENPARENTHESIS)
            parenthesisdepth++;
        else if (sym.entries[towrite].stype == SYM_CLOSEPARENTHESIS)
            parenthesisdepth--;

        // deal with forward-declas
        if ((sym.entries[towrite].stype == SYM_SEMICOLON) && (bracedepth == 0))
            in_struct_declr = -1;

        // this bit sorts out renaming struct members to allow different
        // structs to have same member names at different offsets
        if (towrite < 1) ;
        else if (sym.entries[last_time].stype == SYM_STRUCT) {
            in_struct_declr = towrite;
            bracedepth = 0;
        }
        else if ((sym.entries[last_time].stype == SYM_ENUM) && (sym.entries[towrite].stype == 0)) {
            // make sure it doens't get jibbled when used within
            // structs
            sym.entries[towrite].stype = SYM_TEMPORARYTYPE;
        }
        else if ((sym.entries[towrite].stype == SYM_OPENBRACE) && (in_struct_declr >= 0))
            bracedepth++;
        else if ((sym.entries[towrite].stype == SYM_CLOSEBRACE) && (in_struct_declr >= 0)) {
            bracedepth--;
            if (bracedepth <= 0)
                in_struct_declr = -1;
        }
        else if ((sym.entries[towrite].stype == 0 || sym.entries[towrite].stype == SYM_FUNCTION || sym.entries[towrite].stype == SYM_GLOBALVAR) &&
            (in_struct_declr >= 0) && (parenthesisdepth == 0) && (bracedepth > 0)) {
                // change the name of structure members so that the same member name
                // can be used in multiple structs
                // (but only if not currently in a function params list
                // and not an imported func/property type)
                // (and if not the struct type (this allows member functions
                // which return the struct)
                if ((sym.entries[last_time].stype != SYM_PROPERTY) &&
                    (sym.entries[last_time].stype != SYM_IMPORT) &&
                    (sym.entries[last_time].stype != SYM_STATIC) &&
                    (sym.entries[last_time].stype != SYM_PROTECTED) &&
                    (sym.entries[last_time].stype != SYM_WRITEPROTECTED) &&
                    (sym.entries[last_time].stype != SYM_SEMICOLON) &&
                    (sym.entries[last_time].stype != SYM_OPENBRACE) &&
                    (sym.entries[last_time].stype != SYM_OPENBRACKET) &&
                    (towrite != in_struct_declr)) {
                        const char *new_name = get_member_full_name(in_struct_declr, towrite);
                        //      printf("changed '%s' to '%s'\n",sym.get_friendly_name(towrite).c_str(),new_name);
                        towrite = sym_find_or_add(sym, new_name);
                        if (towrite < 0) {
                            cc_error("symbol table error - could not ensure new struct symbol.");
                            return -1;
                        }
                }
        }

        if (thissymbol[0]=='\"') {
            // strip closing speech mark
            thissymbol[strlen(thissymbol)-1] = 0;
            // save the string into the string table area
            sym.entries[towrite].stype = SYM_STRING;
            sym.entries[towrite].soffs = scrip->add_string(&thissymbol[1]);
            // set it to be recognised as a string
            sym.entries[towrite].vartype = sym.normalStringSym;

            if (strncmp(thissymbol, NEW_SCRIPT_TOKEN_PREFIX, 18) == 0)
            {
                snprintf(scriptNameBuffer, sizeof(scriptNameBuffer), "%s", &thissymbol[18]);
                ccCurScriptName = scriptNameBuffer;

                linenum = 0;
                currentline = 0;
            }
        }
        targ->write(towrite);
        last_time = towrite;
    }
    fmem_close(iii);
    targ->write_meta(SMETA_END,0);
    // clear any temporary tpyes set
    for (int ii = 0; (size_t)ii < sym.entries.size(); ii++) {
        if (sym.entries[ii].stype == SYM_TEMPORARYTYPE)
            sym.entries[ii].stype = 0;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdx, 0x18(%rsp)
movq %rsi, %rbx
callq 0x131a7
movq %rax, %r15
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x10f68
movq %r15, %rdi
callq 0x13377
testl %eax, %eax
je 0x4c70
movq %r15, %rdi
callq 0x13209
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x10f68
leaq 0x20ac5(%rip), %rcx     # 0x256e0
movq 0x28(%rcx), %rax
movq 0x30(%rcx), %rdx
subq %rax, %rdx
je 0x4c5c
sarq $0x4, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rdx, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x20, %rax
cmpw $-0x63, (%rax)
jne 0x4c51
movw $0x0, (%rax)
addq $0xb0, %rax
decq %rcx
jne 0x4c46
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r13d
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, 0x8(%rsp)
movl $0x0, 0x14(%rsp)
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x13377
testl %eax, %eax
jne 0x4bfb
movq %r15, %rdi
callq 0x13358
movl %eax, %ebp
movsbl %bpl, %r14d
movl %r14d, %edi
callq 0x13dc8
testl %eax, %eax
jne 0x4c92
cmpl $0xd, %ebp
je 0x4cc6
cmpl $0xa, %ebp
jne 0x4cfd
incl %r13d
movq %rbx, %rdi
movl $0x1, %esi
movl %r13d, %edx
callq 0x10f68
movq %r15, %rdi
callq 0x1336b
cmpl $0xa, %eax
jne 0x4cee
movq %r15, %rdi
callq 0x13358
leaq 0x20aab(%rip), %rax     # 0x257a0
movl %r13d, (%rax)
jmp 0x5149
movl %r13d, 0x10(%rsp)
movb %bpl, 0x20(%rsp)
movl $0x1, %r13d
movq %r15, %rdi
callq 0x1336b
movsbl %al, %edi
movl %r14d, %esi
callq 0x49e3
testl %eax, %eax
jne 0x4d49
cmpl $0x30, %ebp
jne 0x4d68
cmpq $0x2, %r13
jb 0x4d68
cmpb $0x78, 0x21(%rsp)
jne 0x4d68
movq %r15, %rdi
callq 0x1336b
movl %eax, %edi
callq 0x3100
testl %eax, %eax
je 0x4d68
movq %r15, %rdi
callq 0x13358
movb %al, 0x20(%rsp,%r13)
incq %r13
cmpq $0x1f3, %r13            # imm = 0x1F3
jne 0x4d0d
movl $0x1f3, %r13d           # imm = 0x1F3
movslq %r13d, %rax
movb $0x0, 0x20(%rsp,%rax)
movb 0x20(%rsp), %al
movb 0x22(%rsp), %cl
cmpb $0x30, %al
jne 0x4dc7
cmpb $0x78, 0x21(%rsp)
leaq 0x15c19(%rip), %r13     # 0x1a9a1
jne 0x4dce
movsbl %cl, %edi
callq 0x3100
testl %eax, %eax
je 0x4df9
leaq 0x20(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3150
movq %r14, %rdi
leaq 0x17e12(%rip), %rsi     # 0x1cbc6
movl %eax, %edx
xorl %eax, %eax
callq 0x30d0
movb 0x20(%rsp), %al
movb 0x22(%rsp), %cl
jmp 0x4dce
leaq 0x15bd3(%rip), %r13     # 0x1a9a1
cmpb $0x27, %al
jne 0x4df1
cmpb $0x27, %cl
jne 0x4df1
movsbl 0x21(%rsp), %edx
leaq 0x20(%rsp), %rdi
leaq 0x17dde(%rip), %rsi     # 0x1cbc6
xorl %eax, %eax
callq 0x30d0
jmp 0x4df9
cmpb $0x27, %al
je 0x5167
movslq %r12d, %rax
leaq 0x208dd(%rip), %rcx     # 0x256e0
movq 0x28(%rcx), %rcx
imulq $0xb0, %rax, %r14
cmpw $0x9, 0x20(%rcx,%r14)
jne 0x4e3f
movq %r13, %rsi
leaq 0x220(%rsp), %r13
movq %r13, %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rdx
xorl %eax, %eax
callq 0x30d0
movq %r12, %rdi
movq %r13, %rsi
callq 0x32d0
leaq 0x2089a(%rip), %rdi     # 0x256e0
leaq 0x20(%rsp), %rsi
callq 0x4b8f
testl %eax, %eax
js 0x515e
movl %eax, %r12d
movb 0x20(%rsp), %al
leal -0x30(%rax), %ecx
cmpb $0xa, %cl
jb 0x4e7d
cmpb $0x2d, %al
jne 0x4f3c
movb 0x21(%rsp), %al
addb $-0x30, %al
cmpb $0x9, %al
ja 0x4f3c
leaq 0x20(%rsp), %rdi
movl $0x2e, %esi
callq 0x3180
movl %r12d, %ecx
imulq $0xb0, %rcx, %rdx
leaq 0x20843(%rip), %rcx     # 0x256e0
movq 0x28(%rcx), %rcx
addq %rcx, %rdx
addq $0x20, %rdx
testq %rax, %rax
je 0x4eb4
movw $0x16, (%rdx)
jmp 0x4eb9
movw $0x13, (%rdx)
movslq %r12d, %rax
imulq $0xb0, %rax, %rsi
movzwl 0x20(%rcx,%rsi), %edx
movl %edx, %edi
xorl $0x28, %edi
xorl %ebp, %ebp
cmpl $0x6, %edx
sete %bpl
negl %ebp
cmpl $0x5, %edx
movl $0x1, %r8d
cmovel %r8d, %ebp
addl 0x14(%rsp), %ebp
orl 0x8(%rsp), %edi
movl 0x4(%rsp), %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmovel %r8d, %edi
testl %eax, %eax
jle 0x5063
movzwl 0x20(%rcx,%r14), %eax
cmpl $0x1e, %eax
je 0x4fae
cmpl $0x27, %eax
jne 0x4f1f
testl %edx, %edx
je 0x5056
cmpl $0x17, %edx
jne 0x4fc1
testl %edi, %edi
js 0x4fc1
movq 0x8(%rsp), %rax
incl %eax
jmp 0x4fe5
leaq 0x2079d(%rip), %rax     # 0x256e0
movq 0x28(%rax), %rcx
cmpl $0x2a, %ebp
jne 0x4eb9
movl %r12d, %eax
imulq $0xb0, %rax, %rax
cmpw $0x1a, 0x20(%rcx,%rax)
jne 0x4eb9
cmpw $0x2e, 0x20(%rcx,%r14)
jne 0x4eb9
leaq 0x20766(%rip), %rbp     # 0x256e0
movq %rbp, %rdi
leaq 0x16090(%rip), %rsi     # 0x1b014
callq 0x11e22
movq %rbx, %rdi
movl %eax, %esi
callq 0x10f04
movq %rbp, %rdi
leaq 0x17aa7(%rip), %rsi     # 0x1ca44
callq 0x11e22
movl %eax, %r12d
movq 0x28(%rbp), %rcx
jmp 0x4eb9
movq $0x0, 0x8(%rsp)
movl %r12d, 0x4(%rsp)
jmp 0x5067
cmpl $0x18, %edx
jne 0x4fec
testl %edi, %edi
js 0x4fec
movq 0x8(%rsp), %rax
cmpl $0x2, %eax
leal -0x1(%rax), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl 0x4(%rsp), %edx
cmovll %ecx, %edx
movl %edx, 0x4(%rsp)
movq %rax, 0x8(%rsp)
jmp 0x5067
testl $0xfffb, %edx           # imm = 0xFFFB
je 0x4ff9
cmpl $0x1, %edx
jne 0x5063
testl %edi, %edi
js 0x5063
testl %ebp, %ebp
setne %cl
cmpl $0x0, 0x8(%rsp)
setle %dl
orb %cl, %dl
jne 0x5067
cmpl $0x30, %eax
ja 0x5025
movl %eax, %ecx
movabsq $0x1314100800000, %rdx  # imm = 0x1314100800000
btq %rcx, %rdx
jb 0x5067
cmpl $0x7, %eax
je 0x5067
cmpl %edi, %r12d
je 0x5067
movl %r12d, %esi
callq 0x4b3b
leaq 0x206a2(%rip), %rdi     # 0x256e0
leaq 0x2036b(%rip), %rsi     # 0x253b0
callq 0x4b8f
movl %eax, %r12d
testl %eax, %eax
jns 0x5067
jmp 0x517f
leaq (%rcx,%rsi), %rax
addq $0x20, %rax
movw $0xff9d, (%rax)         # imm = 0xFF9D
movl %edi, 0x4(%rsp)
cmpb $0x22, 0x20(%rsp)
jne 0x5135
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x31a0
movb $0x0, -0x1(%rax,%r14)
movslq %r12d, %rax
leaq 0x20651(%rip), %r13     # 0x256e0
movq 0x28(%r13), %rcx
imulq $0xb0, %rax, %r14
movw $0x12, 0x20(%rcx,%r14)
movq 0x18(%rsp), %rdi
leaq 0x21(%rsp), %rsi
callq 0x136ea
movq 0x28(%r13), %rcx
movl %eax, 0x34(%rcx,%r14)
movzwl 0x10(%r13), %eax
movw %ax, 0x30(%rcx,%r14)
movzwl 0x30(%rsp), %eax
movd %eax, %xmm0
pcmpeqb 0x1575a(%rip), %xmm0    # 0x1a830
movdqa 0x20(%rsp), %xmm1
pcmpeqb 0x1573c(%rip), %xmm1    # 0x1a820
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
leaq 0x204b8(%rip), %r14     # 0x255b0
leaq 0x17635(%rip), %rdx     # 0x1c734
leaq 0x2025a(%rip), %r13     # 0x25360
jne 0x5135
movl $0x0, 0x10(%rsp)
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
leaq 0x32(%rsp), %rcx
xorl %eax, %eax
callq 0x34f0
movq %r14, (%r13)
leaq 0x20671(%rip), %rax     # 0x257a0
movl $0x0, (%rax)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x10f04
movl %ebp, 0x14(%rsp)
movl 0x10(%rsp), %r13d
movq %r15, %rdi
callq 0x13377
testl %eax, %eax
je 0x4c92
jmp 0x4bfb
leaq 0x15840(%rip), %rdi     # 0x1a9a5
jmp 0x516e
leaq 0x15809(%rip), %rdi     # 0x1a977
xorl %eax, %eax
callq 0x102f4
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x4c5c
leaq 0x15854(%rip), %rdi     # 0x1a9da
jmp 0x516e