// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rsi, %rbx
callq 0x11b89
movq %rax, %r15
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
movl %ebp, %edx
callq 0xfd80
pushq $-0x1
popq %rax
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl %r12d, 0x20(%rsp)
movq %r15, %rdi
callq 0x11d52
testl %eax, %eax
jne 0x4f16
movq %r15, %rdi
callq 0x11d52
testl %eax, %eax
jne 0x4f16
movq %r15, %rdi
callq 0x11d33
movl %eax, %r13d
movsbl %r13b, %r12d
movl %r12d, %edi
callq 0x126b4
testl %eax, %eax
jne 0x4a7b
cmpl $0xd, %r13d
je 0x4ab2
cmpl $0xa, %r13d
jne 0x4ae1
incl %ebp
movq %rbx, %rdi
pushq $0x1
popq %rsi
movl %ebp, %edx
callq 0xfd80
movq %r15, %rdi
callq 0x11d46
cmpl $0xa, %eax
jne 0x4ad6
movq %r15, %rdi
callq 0x11d33
leaq 0x20cd3(%rip), %rax     # 0x257b0
movl %ebp, (%rax)
jmp 0x4a6b
movq %r14, 0x28(%rsp)
movb %r13b, 0x30(%rsp)
pushq $0x1
popq %r14
movq %r15, %rdi
callq 0x11d46
movsbl %al, %edi
movl %r12d, %esi
callq 0x4844
testl %eax, %eax
jne 0x4b2c
cmpl $0x30, %r13d
jne 0x4b4b
cmpq $0x2, %r14
jb 0x4b4b
cmpb $0x78, 0x31(%rsp)
jne 0x4b4b
movq %r15, %rdi
callq 0x11d46
movl %eax, %edi
callq 0x30f0
testl %eax, %eax
je 0x4b4b
movq %r15, %rdi
callq 0x11d33
movb %al, 0x30(%rsp,%r14)
incq %r14
cmpq $0x1f3, %r14            # imm = 0x1F3
jne 0x4aef
movl $0x1f3, %r14d           # imm = 0x1F3
movslq %r14d, %rax
movb $0x0, 0x30(%rsp,%rax)
movb 0x30(%rsp), %al
movb 0x32(%rsp), %cl
cmpb $0x30, %al
movl 0x20(%rsp), %r12d
jne 0x4ba4
cmpb $0x78, 0x31(%rsp)
jne 0x4ba4
movsbl %cl, %edi
callq 0x30f0
testl %eax, %eax
je 0x4bcf
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
pushq $0x10
popq %rdx
callq 0x3140
movq %r14, %rdi
leaq 0x16044(%rip), %rsi     # 0x1abd7
movl %eax, %edx
xorl %eax, %eax
callq 0x30c0
movb 0x30(%rsp), %al
movb 0x32(%rsp), %cl
cmpb $0x27, %al
jne 0x4bc7
cmpb $0x27, %cl
jne 0x4bc7
movsbl 0x31(%rsp), %edx
leaq 0x30(%rsp), %rdi
leaq 0x16019(%rip), %rsi     # 0x1abd7
xorl %eax, %eax
callq 0x30c0
jmp 0x4bcf
cmpb $0x27, %al
je 0x4f6f
movslq %r12d, %rax
leaq 0x20b17(%rip), %rcx     # 0x256f0
movq 0x28(%rcx), %rcx
imulq $0xb0, %rax, %rax
movq %rax, 0x20(%rsp)
cmpw $0x9, 0x20(%rcx,%rax)
jne 0x4c1d
leaq 0x230(%rsp), %r12
movq %r12, %rdi
leaq 0x13d7e(%rip), %rsi     # 0x18981
leaq 0x30(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x30c0
movq %r14, %rdi
movq %r12, %rsi
callq 0x32c0
leaq 0x20acc(%rip), %rdi     # 0x256f0
leaq 0x30(%rsp), %rsi
callq 0x49f0
testl %eax, %eax
js 0x4f66
movl %eax, %r12d
movb 0x30(%rsp), %al
leal -0x30(%rax), %ecx
movl %r12d, %r14d
cmpb $0xa, %cl
jb 0x4c5e
cmpb $0x2d, %al
jne 0x4d1b
movb 0x31(%rsp), %al
addb $-0x30, %al
cmpb $0x9, %al
ja 0x4d1b
leaq 0x30(%rsp), %rdi
pushq $0x2e
popq %rsi
callq 0x3170
imulq $0xb0, %r14, %rdx
leaq 0x20a77(%rip), %rcx     # 0x256f0
movq 0x28(%rcx), %rcx
addq %rcx, %rdx
addq $0x20, %rdx
testq %rax, %rax
je 0x4c90
movw $0x16, (%rdx)
jmp 0x4c95
movw $0x13, (%rdx)
movslq %r12d, %rax
imulq $0xb0, %rax, %rsi
movzwl 0x20(%rcx,%rsi), %edx
movl %edx, %edi
xorl $0x28, %edi
xorl %r8d, %r8d
cmpl $0x6, %edx
sete %r8b
negl %r8d
cmpl $0x5, %edx
pushq $0x1
popq %r9
cmovel %r9d, %r8d
addl %r8d, 0x1c(%rsp)
orl 0x10(%rsp), %edi
movl 0xc(%rsp), %edi
pushq $-0x1
popq %r8
cmovel %r8d, %edi
testl %eax, %eax
jle 0x4e44
movq 0x20(%rsp), %rax
movzwl 0x20(%rcx,%rax), %eax
cmpl $0x1e, %eax
je 0x4d8f
cmpl $0x27, %eax
jne 0x4cfe
testl %edx, %edx
je 0x4e37
cmpl $0x17, %edx
jne 0x4da2
testl %edi, %edi
js 0x4da2
movq 0x10(%rsp), %rax
incl %eax
jmp 0x4dc4
leaq 0x209ce(%rip), %rax     # 0x256f0
movq 0x28(%rax), %rcx
cmpl $0x2a, %r13d
jne 0x4c95
imulq $0xb0, %r14, %rax
cmpw $0x1a, 0x20(%rcx,%rax)
jne 0x4c95
movq 0x20(%rsp), %rax
cmpw $0x2e, 0x20(%rcx,%rax)
jne 0x4c95
leaq 0x20995(%rip), %r14     # 0x256f0
movq %r14, %rdi
leaq 0x1428f(%rip), %rsi     # 0x18ff4
callq 0x1085a
movq %rbx, %rdi
movl %eax, %esi
callq 0xfd1c
movq %r14, %rdi
leaq 0x15ca6(%rip), %rsi     # 0x1aa24
callq 0x1085a
movl %eax, %r12d
movq 0x28(%r14), %rcx
jmp 0x4c95
movq $0x0, 0x10(%rsp)
movl %r12d, 0xc(%rsp)
jmp 0x4e48
cmpl $0x18, %edx
jne 0x4dcb
testl %edi, %edi
js 0x4dcb
movq 0x10(%rsp), %rax
cmpl $0x2, %eax
leal -0x1(%rax), %eax
pushq $-0x1
popq %rcx
movl 0xc(%rsp), %edx
cmovll %ecx, %edx
movl %edx, 0xc(%rsp)
movq %rax, 0x10(%rsp)
jmp 0x4e48
testw $0xfffb, %dx            # imm = 0xFFFB
je 0x4dd7
cmpl $0x1, %edx
jne 0x4e44
testl %edi, %edi
js 0x4e44
cmpl $0x0, 0x1c(%rsp)
setne %cl
cmpl $0x0, 0x10(%rsp)
setle %dl
orb %cl, %dl
jne 0x4e48
cmpl $0x30, %eax
ja 0x4e06
movl %eax, %ecx
movabsq $0x1314100800000, %rdx  # imm = 0x1314100800000
btq %rcx, %rdx
jb 0x4e48
cmpl $0x7, %eax
je 0x4e48
cmpl %edi, %r12d
je 0x4e48
movl %r12d, %esi
callq 0x499c
leaq 0x208d1(%rip), %rdi     # 0x256f0
leaq 0x2059a(%rip), %rsi     # 0x253c0
callq 0x49f0
movl %eax, %r12d
testl %eax, %eax
jns 0x4e48
jmp 0x4f94
leaq (%rcx,%rsi), %rax
addq $0x20, %rax
movw $0xff9d, (%rax)         # imm = 0xFF9D
movl %edi, 0xc(%rsp)
cmpb $0x22, 0x30(%rsp)
jne 0x4f01
leaq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x3190
movb $0x0, -0x1(%rax,%r13)
movslq %r12d, %rax
leaq 0x20880(%rip), %rcx     # 0x256f0
movl %r12d, %r13d
movq %rcx, %r12
movq 0x28(%rcx), %rcx
imulq $0xb0, %rax, %r14
movw $0x12, 0x20(%rcx,%r14)
movq 0x28(%rsp), %rdi
leaq 0x31(%rsp), %rsi
callq 0x12006
movq 0x28(%r12), %rcx
movl %eax, 0x34(%rcx,%r14)
movzwl 0x10(%r12), %eax
movl %r13d, %r12d
movw %ax, 0x30(%rcx,%r14)
leaq 0x30(%rsp), %rdi
leaq 0x13b36(%rip), %rsi     # 0x189f3
pushq $0x12
popq %rdx
callq 0x3310
testl %eax, %eax
jne 0x4f01
xorl %ebp, %ebp
movl $0x100, %esi            # imm = 0x100
leaq 0x206e9(%rip), %r14     # 0x255c0
movq %r14, %rdi
leaq 0x15833(%rip), %rdx     # 0x1a714
leaq 0x42(%rsp), %rcx
xorl %eax, %eax
callq 0x3500
leaq 0x2046c(%rip), %rax     # 0x25360
movq %r14, (%rax)
leaq 0x208b2(%rip), %rax     # 0x257b0
andl $0x0, (%rax)
movq %rbx, %rdi
movl %r12d, %esi
callq 0xfd1c
movq 0x28(%rsp), %r14
jmp 0x4a66
movq %r15, %rdi
callq 0x11be6
pushq $0x2
popq %rsi
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %edx, %edx
callq 0xfd80
leaq 0x207bc(%rip), %rax     # 0x256f0
movq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
subq %rcx, %rax
movl $0xb0, %esi
cqto
idivq %rsi
addq $0x20, %rcx
subq $0x1, %rax
jb 0x4f80
cmpw $-0x63, (%rcx)
jne 0x4f5d
andw $0x0, (%rcx)
addq $0xb0, %rcx
jmp 0x4f4d
leaq 0x13a18(%rip), %rdi     # 0x18985
jmp 0x4f76
leaq 0x139e1(%rip), %rdi     # 0x18957
xorl %eax, %eax
callq 0xf258
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13a1f(%rip), %rdi     # 0x189ba
jmp 0x4f76