// File: blakewford[P]kab/parser.cpp
// Function: parse(char const*, target*)
void parse(const char* json, target* t)
{
    int i = 0;
    object* temp = nullptr;
    char current = json[i];
    parse_state state = UNKNOWN;
    char attribute[MAX_BUFFER_SIZE];
    std::stack<container_object*> object_stack;
    while(current != '\0')
    {
        while(current == ' ')
        {
            i++;
            current = json[i];
        }
        if(current == '{')
        {
            switch(state)
            {
                case UNKNOWN:
                case VALUE:
                    object_stack.push(new container_object(attribute));
                    memset(attribute, '\0', MAX_BUFFER_SIZE);
                    state = ATTRIBUTE_NAME;
                    break;
                case ATTRIBUTE_NAME:
                    assert(0);
                    break;
            }
        }
        else if(current == '}')
        {
            if(temp != nullptr)
            {
                object_stack.top()->add(temp);
            }
            memset(attribute, '\0', MAX_BUFFER_SIZE);
            temp = nullptr;
            container_object* obj = object_stack.top();
            object_stack.pop();
            if(!object_stack.empty())
            {
                object_stack.top()->add(obj);
            }
            else
            {
                //Top level Weave object
                t->populate(obj);
            }
        }
        else if(current == ':')
        {
            switch(state)
            {
                case ATTRIBUTE_NAME:
                    state = VALUE;
                    break;
                case UNKNOWN:
                case VALUE:
                    assert(0);
            }
        }
        else if(current == ',')
        {
            switch(state)
            {
                case VALUE:
                    object_stack.top()->add(temp);
                    memset(attribute, '\0', MAX_BUFFER_SIZE);
                    temp = nullptr;
                    state = ATTRIBUTE_NAME;
                    break;
                case UNKNOWN:
                case ATTRIBUTE_NAME:
                    assert(0);
            }
        }
        else if(current == '"')
        {
            switch(state)
            {
                case ATTRIBUTE_NAME:
                    break;
                case VALUE:
                    if(temp == nullptr)
                    {
                        temp = new string_object(attribute);
                    }
                    break;
                case UNKNOWN:
                    assert(0);
            }
        }
        else
        {
            switch(state)
            {
                case ATTRIBUTE_NAME:
                    attribute[strlen(attribute)] = current;
                    break;
                case VALUE:
                    if(temp == nullptr)
                    {
                        long_object* obj = new long_object(attribute);
                        obj->set_value(atoi(json+i)); //HACK ALERT
                        temp = obj;
                    }
                    switch(temp->get_type())
                    {
                        case STRING:
                            ((string_object*)temp)->add(current);
                            break;
                        case LONG:
                        case CONTAINER:
                            break;
                    }
                    break;
                case UNKNOWN:
                    assert(0);
            }
        }
        current = json[++i];
    }

    if(temp != nullptr) delete temp;
    assert(object_stack.size() == 0);

}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movl $0x0, 0x154(%rsp)
movq $0x0, 0x148(%rsp)
movq 0x160(%rsp), %rax
movslq 0x154(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0x147(%rsp)
movl $0x0, 0x140(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5070
movsbl 0x147(%rsp), %eax
cmpl $0x0, %eax
je 0x4f62
jmp 0x4983
movsbl 0x147(%rsp), %eax
cmpl $0x20, %eax
jne 0x49bd
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
movq 0x160(%rsp), %rax
movslq 0x154(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0x147(%rsp)
jmp 0x4983
movsbl 0x147(%rsp), %eax
cmpl $0x7b, %eax
jne 0x4ad0
movl 0x140(%rsp), %eax
movl %eax, 0x84(%rsp)
testl %eax, %eax
je 0x4a06
jmp 0x49e2
movl 0x84(%rsp), %eax
subl $0x1, %eax
je 0x4aac
jmp 0x49f4
movl 0x84(%rsp), %eax
subl $0x2, %eax
jne 0x4acb
jmp 0x4a06
movl $0x68, %edi
callq 0x2240
movq %rax, 0x78(%rsp)
jmp 0x4a17
movq 0x78(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x50d0
jmp 0x4a2b
movq 0x78(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x50a0
jmp 0x4a4f
leaq 0x100(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2130
movl $0x1, 0x140(%rsp)
jmp 0x4acb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x4fe1
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
callq 0x2210
jmp 0x4fe1
leaq 0x3a74(%rip), %rdi      # 0x8527
leaq 0x3982(%rip), %rsi      # 0x843c
movl $0x9c, %edx
leaq 0x3a27(%rip), %rcx      # 0x84ed
callq 0x2160
jmp 0x4f37
movsbl 0x147(%rsp), %eax
cmpl $0x7d, %eax
jne 0x4be3
cmpq $0x0, 0x148(%rsp)
je 0x4b19
leaq 0xb0(%rsp), %rdi
callq 0x5120
movq %rax, 0x70(%rsp)
jmp 0x4b00
movq 0x70(%rsp), %rax
movq (%rax), %rdi
movq 0x148(%rsp), %rsi
callq 0x5130
jmp 0x4b17
jmp 0x4b19
xorps %xmm0, %xmm0
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movq $0x0, 0x148(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5120
movq %rax, 0x68(%rsp)
jmp 0x4b5c
movq 0x68(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x90(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5160
jmp 0x4b7b
leaq 0xb0(%rsp), %rdi
callq 0x5170
movb %al, 0x67(%rsp)
jmp 0x4b8e
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0x4bc5
jmp 0x4b98
leaq 0xb0(%rsp), %rdi
callq 0x5120
movq %rax, 0x58(%rsp)
jmp 0x4bac
movq 0x58(%rsp), %rax
movq (%rax), %rdi
movq 0x90(%rsp), %rsi
callq 0x5130
jmp 0x4bc3
jmp 0x4bde
movq 0x158(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x45f0
jmp 0x4bdc
jmp 0x4bde
jmp 0x4f35
movsbl 0x147(%rsp), %eax
cmpl $0x3a, %eax
jne 0x4c48
movl 0x140(%rsp), %eax
movl %eax, 0x54(%rsp)
testl %eax, %eax
je 0x4c24
jmp 0x4c01
movl 0x54(%rsp), %eax
subl $0x1, %eax
je 0x4c17
jmp 0x4c0c
movl 0x54(%rsp), %eax
subl $0x2, %eax
je 0x4c24
jmp 0x4c43
movl $0x2, 0x140(%rsp)
jmp 0x4c43
leaq 0x38fc(%rip), %rdi      # 0x8527
leaq 0x380a(%rip), %rsi      # 0x843c
movl $0xbd, %edx
leaq 0x38af(%rip), %rcx      # 0x84ed
callq 0x2160
jmp 0x4f33
movsbl 0x147(%rsp), %eax
cmpl $0x2c, %eax
jne 0x4cf2
movl 0x140(%rsp), %eax
movl %eax, 0x50(%rsp)
subl $0x2, %eax
jb 0x4cce
jmp 0x4c6b
movl 0x50(%rsp), %eax
subl $0x2, %eax
jne 0x4ced
jmp 0x4c76
leaq 0xb0(%rsp), %rdi
callq 0x5120
movq %rax, 0x48(%rsp)
jmp 0x4c8a
movq 0x48(%rsp), %rax
movq (%rax), %rdi
movq 0x148(%rsp), %rsi
callq 0x5130
jmp 0x4ca1
leaq 0x100(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2130
movq $0x0, 0x148(%rsp)
movl $0x1, 0x140(%rsp)
jmp 0x4ced
leaq 0x3852(%rip), %rdi      # 0x8527
leaq 0x3760(%rip), %rsi      # 0x843c
movl $0xcc, %edx
leaq 0x3805(%rip), %rcx      # 0x84ed
callq 0x2160
jmp 0x4f31
movsbl 0x147(%rsp), %eax
cmpl $0x22, %eax
jne 0x4dbe
movl 0x140(%rsp), %eax
movl %eax, 0x44(%rsp)
testl %eax, %eax
je 0x4d9a
jmp 0x4d18
movl 0x44(%rsp), %eax
subl $0x1, %eax
je 0x4d31
jmp 0x4d23
movl 0x44(%rsp), %eax
subl $0x2, %eax
je 0x4d36
jmp 0x4db9
jmp 0x4db9
cmpq $0x0, 0x148(%rsp)
jne 0x4d98
movl $0x90, %edi
callq 0x2240
movq %rax, 0x38(%rsp)
jmp 0x4d52
movq 0x38(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x5190
jmp 0x4d66
movq 0x38(%rsp), %rax
movq %rax, 0x148(%rsp)
jmp 0x4d98
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
callq 0x2210
jmp 0x4fe1
jmp 0x4db9
leaq 0x3786(%rip), %rdi      # 0x8527
leaq 0x3694(%rip), %rsi      # 0x843c
movl $0xdc, %edx
leaq 0x3739(%rip), %rcx      # 0x84ed
callq 0x2160
jmp 0x4f2f
movl 0x140(%rsp), %eax
movl %eax, 0x34(%rsp)
testl %eax, %eax
je 0x4f0e
jmp 0x4dd3
movl 0x34(%rsp), %eax
subl $0x1, %eax
je 0x4dec
jmp 0x4dde
movl 0x34(%rsp), %eax
subl $0x2, %eax
je 0x4e14
jmp 0x4f2d
movb 0x147(%rsp), %al
movb %al, 0x33(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x2100
movb 0x33(%rsp), %cl
movb %cl, 0x100(%rsp,%rax)
jmp 0x4f2d
cmpq $0x0, 0x148(%rsp)
jne 0x4ebe
movl $0x58, %edi
callq 0x2240
movq %rax, 0x28(%rsp)
jmp 0x4e34
movq 0x28(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x51e0
jmp 0x4e48
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x160(%rsp), %rdi
movslq 0x154(%rsp), %rax
addq %rax, %rdi
callq 0x2350
movq 0x20(%rsp), %rdi
movslq %eax, %rsi
callq 0x5220
jmp 0x4e89
movq 0x88(%rsp), %rax
movq %rax, 0x148(%rsp)
jmp 0x4ebe
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
callq 0x2210
jmp 0x4fe1
movq 0x148(%rsp), %rdi
callq 0x5050
movl %eax, 0x1c(%rsp)
jmp 0x4ed1
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x4f0a
jmp 0x4edb
movl 0x1c(%rsp), %eax
subl $0x1, %eax
je 0x4ef1
jmp 0x4ee6
movl 0x1c(%rsp), %eax
subl $0x2, %eax
je 0x4f0a
jmp 0x4f0c
movq 0x148(%rsp), %rdi
movsbl 0x147(%rsp), %esi
callq 0x5240
jmp 0x4f08
jmp 0x4f0c
jmp 0x4f0c
jmp 0x4f2d
leaq 0x3612(%rip), %rdi      # 0x8527
leaq 0x3520(%rip), %rsi      # 0x843c
movl $0xf8, %edx
leaq 0x35c5(%rip), %rcx      # 0x84ed
callq 0x2160
jmp 0x4f2f
jmp 0x4f31
jmp 0x4f33
jmp 0x4f35
jmp 0x4f37
movq 0x160(%rsp), %rax
movl 0x154(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0x154(%rsp)
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x147(%rsp)
jmp 0x4970
cmpq $0x0, 0x148(%rsp)
je 0x4f8c
movq 0x148(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x4f8a
movq 0x10(%rsp), %rdi
callq 0x2210
jmp 0x4f8c
leaq 0xb0(%rsp), %rdi
callq 0x5280
movq %rax, 0x8(%rsp)
jmp 0x4fa0
movq 0x8(%rsp), %rax
cmpq $0x0, %rax
jne 0x4fad
jmp 0x4fcc
leaq 0x355c(%rip), %rdi      # 0x8510
leaq 0x3481(%rip), %rsi      # 0x843c
movl $0xff, %edx
leaq 0x3526(%rip), %rcx      # 0x84ed
callq 0x2160
leaq 0xb0(%rsp), %rdi
callq 0x5290
addq $0x168, %rsp            # imm = 0x168
retq
leaq 0xb0(%rsp), %rdi
callq 0x5290
movq 0xa0(%rsp), %rdi
callq 0x23a0
nopl (%rax,%rax)