// File: /workspace/github_cpp_makefile_star2_O1/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 $0x428, %rsp            # imm = 0x428
movq %rdx, 0x28(%rsp)
movq %rsi, %r13
callq 0x1333f
movq %rax, %r15
movq %r13, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x110a0
movq %r15, %rdi
callq 0x1350f
testl %eax, %eax
je 0x4c8d
movq %r15, %rdi
callq 0x133a1
xorl %ebp, %ebp
movq %r13, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x110a0
leaq 0x20aa8(%rip), %rcx     # 0x256e0
movq 0x28(%rcx), %rax
movq 0x30(%rcx), %rdx
subq %rax, %rdx
je 0x4c79
sarq $0x4, %rdx
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rdx, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x20, %rax
cmpw $-0x63, (%rax)
jne 0x4c6e
movw $0x0, (%rax)
addq $0xb0, %rax
decq %rcx
jne 0x4c63
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %r12
movl $0x1, %ebp
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0xc(%rsp)
movq %r13, 0x20(%rsp)
movq %r15, %rdi
callq 0x1350f
testl %eax, %eax
sete %bl
jne 0x4ce4
movq %r15, %rdi
callq 0x134f0
movl %eax, %r14d
movsbl %r14b, %edi
callq 0x13f40
testl %eax, %eax
jne 0x4cbd
movl $0x3, %eax
testb %bl, %bl
je 0x51af
cmpl $0xd, %r14d
je 0x4cfd
cmpl $0xa, %r14d
jne 0x4d36
incl %ebp
movq %r13, %rdi
movl $0x1, %esi
movl %ebp, %edx
callq 0x110a0
movq %r15, %rdi
callq 0x13503
cmpl $0xa, %eax
jne 0x4d23
movq %r15, %rdi
callq 0x134f0
leaq 0x20a76(%rip), %rax     # 0x257a0
movl %ebp, (%rax)
movl $0x2, %eax
jmp 0x51af
movb %r14b, 0x30(%rsp)
movl $0x1, %ebx
movq %r15, %rdi
callq 0x13503
movsbl %r14b, %esi
movsbl %al, %edi
callq 0x4a03
testl %eax, %eax
jne 0x4d7e
cmpl $0x30, %r14d
jne 0x4d9b
cmpq $0x2, %rbx
jb 0x4d9b
cmpb $0x78, 0x31(%rsp)
jne 0x4d9b
movq %r15, %rdi
callq 0x13503
movl %eax, %edi
callq 0x3100
testl %eax, %eax
je 0x4d9b
movq %r15, %rdi
callq 0x134f0
movb %al, 0x30(%rsp,%rbx)
incq %rbx
cmpq $0x1f3, %rbx            # imm = 0x1F3
jne 0x4d40
movl $0x1f3, %ebx            # imm = 0x1F3
movslq %ebx, %rax
movb $0x0, 0x30(%rsp,%rax)
cmpb $0x30, 0x30(%rsp)
jne 0x4de1
cmpb $0x78, 0x31(%rsp)
jne 0x4de1
movsbl 0x32(%rsp), %edi
callq 0x3100
testl %eax, %eax
je 0x4de1
movq %r12, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3150
movq %r12, %rdi
leaq 0x17dee(%rip), %rsi     # 0x1cbc6
movl %eax, %edx
xorl %eax, %eax
callq 0x30d0
movb 0x30(%rsp), %al
cmpb $0x27, %al
jne 0x4e08
cmpb $0x27, 0x32(%rsp)
jne 0x4e08
movsbl 0x31(%rsp), %edx
movq %r12, %rdi
leaq 0x17dc7(%rip), %rsi     # 0x1cbc6
xorl %eax, %eax
callq 0x30d0
jmp 0x4e2e
cmpb $0x27, %al
jne 0x4e2e
leaq 0x15b64(%rip), %rdi     # 0x1a977
xorl %eax, %eax
callq 0x10418
movl $0x1, %eax
movl 0x14(%rsp), %r12d
movq 0x20(%rsp), %r13
jmp 0x51a5
movl %ebp, 0x10(%rsp)
movslq 0xc(%rsp), %rax
leaq 0x208a2(%rip), %rcx     # 0x256e0
movq 0x28(%rcx), %rcx
imulq $0xb0, %rax, %r13
cmpw $0x9, 0x20(%rcx,%r13)
jne 0x4e79
leaq 0x230(%rsp), %rbx
movq %rbx, %rdi
leaq 0x15b3d(%rip), %rsi     # 0x1a9a1
movq %r12, %rdx
xorl %eax, %eax
callq 0x30d0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32d0
leaq 0x20860(%rip), %rdi     # 0x256e0
movq %r12, %rsi
callq 0x4bac
testl %eax, %eax
js 0x4fce
movl %eax, %ebp
movb 0x30(%rsp), %al
leal -0x30(%rax), %ecx
movl %ebp, %ebx
cmpb $0xa, %cl
jb 0x4eae
cmpb $0x2d, %al
jne 0x4ee0
movb 0x31(%rsp), %al
addb $-0x30, %al
cmpb $0x9, %al
ja 0x4ee0
movq %r12, %rdi
movl $0x2e, %esi
callq 0x3180
imulq $0xb0, %rbx, %rcx
xorl %edx, %edx
testq %rax, %rax
leaq 0x20812(%rip), %rax     # 0x256e0
movq 0x28(%rax), %rax
setne %dl
leal (%rdx,%rdx,2), %edx
addl $0x13, %edx
movw %dx, 0x20(%rax,%rcx)
leaq 0x207f9(%rip), %rax     # 0x256e0
movq 0x28(%rax), %rax
imulq $0xb0, %rbx, %rcx
cmpw $0x1a, 0x20(%rax,%rcx)
sete %cl
cmpl $0x2a, %r14d
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x4f45
cmpw $0x2e, 0x20(%rax,%r13)
jne 0x4f45
leaq 0x207c7(%rip), %rbx     # 0x256e0
movq %rbx, %rdi
leaq 0x18375(%rip), %rsi     # 0x1d298
callq 0x11f96
movq 0x20(%rsp), %rdi
movl %eax, %esi
callq 0x11046
movq %rbx, %rdi
leaq 0x17b06(%rip), %rsi     # 0x1ca44
callq 0x11f96
movl %eax, %ebp
movslq %ebp, %rax
leaq 0x20791(%rip), %rcx     # 0x256e0
movq 0x28(%rcx), %rdx
imulq $0xb0, %rax, %rsi
movzwl 0x20(%rdx,%rsi), %ecx
movl %ecx, %edi
xorl $0x28, %edi
xorl %r12d, %r12d
cmpl $0x6, %ecx
sete %r12b
negl %r12d
cmpl $0x5, %ecx
movl $0x1, %r8d
cmovel %r8d, %r12d
addl 0x14(%rsp), %r12d
orl 0x18(%rsp), %edi
movl 0x8(%rsp), %ebx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmovel %edi, %ebx
testl %eax, %eax
jle 0x4ff4
movzwl 0x20(%rdx,%r13), %eax
cmpl $0x1e, %eax
je 0x4ffa
cmpl $0x27, %eax
movq 0x20(%rsp), %r13
jne 0x4fb4
testl %ecx, %ecx
je 0x50b4
cmpl $0x17, %ecx
jne 0x5015
testl %ebx, %ebx
js 0x5015
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
jmp 0x50c1
leaq 0x159d0(%rip), %rdi     # 0x1a9a5
xorl %eax, %eax
callq 0x10418
movl $0x1, %eax
movl 0x14(%rsp), %r12d
movq 0x20(%rsp), %r13
movl 0x10(%rsp), %ebp
jmp 0x51a5
movl %ebx, 0x8(%rsp)
jmp 0x5007
movq $0x0, 0x18(%rsp)
movl %ebp, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x20(%rsp), %r13
jmp 0x50c9
cmpl $0x18, %ecx
jne 0x5043
testl %ebx, %ebx
js 0x5043
movq 0x18(%rsp), %rax
cmpl $0x2, %eax
leal -0x1(%rax), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl 0x8(%rsp), %edx
cmovll %ecx, %edx
movl %edx, 0x8(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x50c5
testl $0xfffb, %ecx           # imm = 0xFFFB
je 0x5050
cmpl $0x1, %ecx
jne 0x50c1
testl %ebx, %ebx
js 0x50c1
testl %r12d, %r12d
setne %cl
cmpl $0x0, 0x18(%rsp)
setle %dl
orb %cl, %dl
jne 0x50c1
cmpl $0x30, %eax
ja 0x507d
movl %eax, %ecx
movabsq $0x1314100800000, %rdx  # imm = 0x1314100800000
btq %rcx, %rdx
jb 0x50c1
cmpl $0x7, %eax
je 0x50c1
cmpl %ebx, %ebp
je 0x50c1
movl %ebx, %edi
movl %ebp, %esi
callq 0x4b58
leaq 0x2064a(%rip), %rdi     # 0x256e0
leaq 0x20313(%rip), %rsi     # 0x253b0
callq 0x4bac
testl %eax, %eax
js 0x51cd
movl %ebx, 0x8(%rsp)
movl %eax, 0xc(%rsp)
jmp 0x50c9
leaq (%rdx,%rsi), %rax
addq $0x20, %rax
movw $0xff9d, (%rax)         # imm = 0xFF9D
movl %ebx, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
cmpb $0x22, 0x30(%rsp)
jne 0x5193
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x31a0
movb $0x0, -0x1(%rax,%rbx)
movslq 0xc(%rsp), %rax
leaq 0x205ee(%rip), %rbp     # 0x256e0
movq 0x28(%rbp), %rcx
imulq $0xb0, %rax, %rbx
movw $0x12, 0x20(%rcx,%rbx)
movq 0x28(%rsp), %rdi
leaq 0x31(%rsp), %rsi
callq 0x13876
movq 0x28(%rbp), %rcx
movl %eax, 0x34(%rcx,%rbx)
movzwl 0x10(%rbp), %eax
movw %ax, 0x30(%rcx,%rbx)
movzwl 0x40(%rsp), %eax
movd %eax, %xmm0
pcmpeqb 0x156fb(%rip), %xmm0    # 0x1a830
movdqa 0x30(%rsp), %xmm1
pcmpeqb 0x156dd(%rip), %xmm1    # 0x1a820
pand %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
movl 0x10(%rsp), %ebp
jne 0x5197
xorl %ebp, %ebp
movl $0x100, %esi            # imm = 0x100
leaq 0x2044c(%rip), %rbx     # 0x255b0
movq %rbx, %rdi
leaq 0x175c6(%rip), %rdx     # 0x1c734
leaq 0x42(%rsp), %rcx
xorl %eax, %eax
callq 0x34f0
leaq 0x201df(%rip), %rax     # 0x25360
movq %rbx, (%rax)
leaq 0x20615(%rip), %rax     # 0x257a0
movl $0x0, (%rax)
jmp 0x5197
movl 0x10(%rsp), %ebp
movq %r13, %rdi
movl 0xc(%rsp), %esi
callq 0x11046
xorl %eax, %eax
movl %r12d, 0x14(%rsp)
leaq 0x30(%rsp), %r12
testl %eax, %eax
je 0x51b8
cmpl $0x2, %eax
jne 0x51ec
movq %r15, %rdi
callq 0x1350f
testl %eax, %eax
je 0x4cbd
jmp 0x4c18
xorl %r12d, %r12d
leaq 0x15803(%rip), %rdi     # 0x1a9da
xorl %eax, %eax
callq 0x10418
movl $0x1, %eax
movl %ebx, 0x8(%rsp)
jmp 0x4feb
cmpl $0x3, %eax
je 0x4c18
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x4c79