// File: menhits[P]phplus/src/Parser.cpp
// Function: parseType(std::vector<std::shared_ptr<TokenData>, std::allocator<std::shared_ptr<TokenData>>> const&, int, int)
unique_ptr<AstType> parseType(const vector<Token>& tokens, int left, int right)
{
	vector<AstTupleType::NamedType> types;
	
	while (left<=right)
	{
		//	skip commas
		while (left<=right && tokens[left]->getOp()==ops->comma)
		{
			left++;
		}
		
		//	if this is a named subtype
		if (left+2<=right && tokens[left+1]->getOp()==ops->colon)
		{
			if (tokens[left]->getType()!=TokenData::IDENTIFIER)
			{
				throw PhplusError("identifier must be to the left of ':' in type", SOURCE_ERROR, tokens[left]);
			}
			
			Token name=tokens[left];
			unique_ptr<AstType> type;
			
			if (tokens[left+2]->getType()==TokenData::IDENTIFIER)
			{
				type=AstTokenType::make(tokens[left+2]);
				left+=3;
			}
			else if (tokens[left+2]->getOp()==ops->openCrBrac)
			{
				int j=skipBrace(tokens, left+2);
				
				if (j>right)
				{
					throw PhplusError(FUNC+" skipping brance went outside of range", INTERNAL_ERROR, tokens[left+1]);
				}
				
				type=parseType(tokens, left+2+1, j-1);
				left=j+1;
			}
			else
			{
				throw PhplusError("invalid thingy '"+tokens[left+2]->getText()+"' in type", SOURCE_ERROR, tokens[left+2]);
			}
			
			types.push_back(AstTupleType::NamedType{name, move(type)});
		}
		else //	this is an unnamed subtype
		{
			types.push_back(AstTupleType::NamedType{nullptr, AstTokenType::make(tokens[left])});
			left+=1;
		}
	}
	
	if (types.size()==0)
		return AstVoidType::make();
	else if (types.size()==1 && !types[0].name)
		return move(types[0].type);
	else
		return AstTupleType::make(types);
}

// Assembly:
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, 0x80(%rsp)
movq %rdi, %rax
movq %rax, 0x88(%rsp)
movq %rdi, 0x2c0(%rsp)
movq %rsi, 0x2b8(%rsp)
movl %edx, 0x2b4(%rsp)
movl %ecx, 0x2b0(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x1fc20
movl 0x2b4(%rsp), %eax
cmpl 0x2b0(%rsp), %eax
jg 0x4792d
jmp 0x46f2b
movl 0x2b4(%rsp), %ecx
movl 0x2b0(%rsp), %edx
movb $0x0, 0x27b(%rsp)
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x7f(%rsp)
jg 0x46fa1
movq 0x2b8(%rsp), %rdi
movslq 0x2b4(%rsp), %rsi
callq 0x48650
movq %rax, %rdi
callq 0x1bb70
movq %rax, %rsi
leaq 0x288(%rsp), %rdi
callq 0x1bc10
jmp 0x46f7a
movb $0x1, 0x27b(%rsp)
leaq 0xd142f(%rip), %rax     # 0x1183b8
movq (%rax), %rsi
addq $0x50, %rsi
leaq 0x288(%rsp), %rdi
callq 0xfd20
movb %al, 0x7f(%rsp)
movb 0x7f(%rsp), %al
movb %al, 0x7e(%rsp)
testb $0x1, 0x27b(%rsp)
jne 0x46fb5
jmp 0x46fc2
leaq 0x288(%rsp), %rdi
callq 0xf9a0
movb 0x7e(%rsp), %al
testb $0x1, %al
jne 0x46fcc
jmp 0x46ffb
movl 0x2b4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2b4(%rsp)
jmp 0x46f2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x47a44
movl 0x2b4(%rsp), %ecx
addl $0x2, %ecx
movl 0x2b0(%rsp), %edx
movb $0x0, 0x267(%rsp)
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x7d(%rsp)
jg 0x47078
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
incl %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rdi
callq 0x1bb70
movq %rax, %rsi
leaq 0x268(%rsp), %rdi
callq 0x1bc10
jmp 0x47051
movb $0x1, 0x267(%rsp)
leaq 0xd1358(%rip), %rax     # 0x1183b8
movq (%rax), %rsi
addq $0x30, %rsi
leaq 0x268(%rsp), %rdi
callq 0xfd20
movb %al, 0x7d(%rsp)
movb 0x7d(%rsp), %al
movb %al, 0x7c(%rsp)
testb $0x1, 0x267(%rsp)
jne 0x4708c
jmp 0x47099
leaq 0x268(%rsp), %rdi
callq 0xf9a0
movb 0x7c(%rsp), %al
testb $0x1, %al
jne 0x470a6
jmp 0x477ea
movq 0x2b8(%rsp), %rdi
movslq 0x2b4(%rsp), %rsi
callq 0x48650
movq %rax, %rdi
callq 0x1bb70
movq %rax, %rdi
callq 0x1bee0
movl %eax, 0x78(%rsp)
jmp 0x470d1
movl 0x78(%rsp), %eax
cmpl $0x2, %eax
je 0x471f8
movb $0x1, 0x227(%rsp)
movl $0x38, %edi
callq 0xb1f0
movq %rax, 0x68(%rsp)
leaq 0x23f(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0xb690
movq 0x70(%rsp), %rdx
leaq 0x8b532(%rip), %rsi     # 0xd2645
leaq 0x240(%rsp), %rdi
callq 0xb4c0
jmp 0x47122
movq 0x2b8(%rsp), %rdi
movslq 0x2b4(%rsp), %rsi
callq 0x48650
movq %rax, %rsi
leaq 0x228(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x1bc90
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rcx
leaq 0x240(%rsp), %rsi
xorl %edx, %edx
callq 0x35600
jmp 0x47167
movq 0x68(%rsp), %rdi
movb $0x0, 0x227(%rsp)
leaq 0xce44d(%rip), %rsi     # 0x1155c8
leaq -0x2cb12(%rip), %rdx    # 0x1a670
callq 0xb640
jmp 0x47a5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x471d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
leaq 0x228(%rsp), %rdi
callq 0xfc00
leaq 0x240(%rsp), %rdi
callq 0xb6e8
leaq 0x23f(%rsp), %rdi
callq 0xb3f0
testb $0x1, 0x227(%rsp)
jne 0x471e9
jmp 0x471f3
movq 0x68(%rsp), %rdi
callq 0xb2d0
jmp 0x47a44
movq 0x2b8(%rsp), %rdi
movslq 0x2b4(%rsp), %rsi
callq 0x48650
movq %rax, %rsi
leaq 0x210(%rsp), %rdi
callq 0x1bc90
leaq 0x208(%rsp), %rdi
callq 0x491c0
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rdi
callq 0x1bb70
movq %rax, %rdi
callq 0x1bee0
movl %eax, 0x5c(%rsp)
jmp 0x4725a
movl 0x5c(%rsp), %eax
cmpl $0x2, %eax
jne 0x4732e
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rsi
leaq 0x1f0(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x1bc90
movq 0x50(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0x491f0
jmp 0x472aa
leaq 0x208(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x492b0
leaq 0x200(%rsp), %rdi
callq 0x49310
leaq 0x1f0(%rsp), %rdi
callq 0xfc00
movl 0x2b4(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x2b4(%rsp)
jmp 0x4773b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x477cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0xfc00
jmp 0x477cb
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rdi
callq 0x1bb70
movq %rax, %rsi
leaq 0x1e0(%rsp), %rdi
callq 0x1bc10
jmp 0x47362
leaq 0xd104f(%rip), %rax     # 0x1183b8
movq (%rax), %rsi
addq $0x1d0, %rsi            # imm = 0x1D0
leaq 0x1e0(%rsp), %rdi
callq 0xfd20
movb %al, 0x4f(%rsp)
leaq 0x1e0(%rsp), %rdi
callq 0xf9a0
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x4739e
jmp 0x4759e
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %esi
addl $0x2, %esi
callq 0x43610
movl %eax, 0x48(%rsp)
jmp 0x473bb
movl 0x48(%rsp), %eax
movl %eax, 0x1dc(%rsp)
movl 0x1dc(%rsp), %eax
cmpl 0x2b0(%rsp), %eax
jle 0x4753c
movb $0x1, 0x17f(%rsp)
movl $0x38, %edi
callq 0xb1f0
movq %rax, 0x38(%rsp)
leaq 0x197(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xb690
movq 0x40(%rsp), %rdx
leaq 0x8b264(%rip), %rsi     # 0xd2673
leaq 0x198(%rsp), %rdi
callq 0xb4c0
jmp 0x4741e
leaq 0x8b258(%rip), %rdx     # 0xd267d
leaq 0x1b8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0xfa60
jmp 0x4743c
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
incl %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rsi
leaq 0x180(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x1bc90
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rcx
leaq 0x1b8(%rsp), %rsi
movl $0x3, %edx
callq 0x35600
jmp 0x47488
movq 0x38(%rsp), %rdi
movb $0x0, 0x17f(%rsp)
leaq 0xce12c(%rip), %rsi     # 0x1155c8
leaq -0x2ce33(%rip), %rdx    # 0x1a670
callq 0xb640
jmp 0x47a5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x47514
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x47507
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xfc00
leaq 0x1b8(%rsp), %rdi
callq 0xb6e8
leaq 0x198(%rsp), %rdi
callq 0xb6e8
leaq 0x197(%rsp), %rdi
callq 0xb3f0
testb $0x1, 0x17f(%rsp)
jne 0x4752d
jmp 0x47537
movq 0x38(%rsp), %rdi
callq 0xb2d0
jmp 0x477cb
movq 0x2b8(%rsp), %rsi
movl 0x2b4(%rsp), %edx
addl $0x3, %edx
movl 0x1dc(%rsp), %ecx
decl %ecx
leaq 0x170(%rsp), %rdi
callq 0x46ed0
jmp 0x47566
leaq 0x208(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x49370
leaq 0x170(%rsp), %rdi
callq 0x20890
movl 0x1dc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2b4(%rsp)
jmp 0x47739
movb $0x1, 0xff(%rsp)
movl $0x38, %edi
callq 0xb1f0
movq %rax, 0x28(%rsp)
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rdi
callq 0x1bb70
movq %rax, %rsi
leaq 0x110(%rsp), %rdi
callq 0x1bb80
jmp 0x475e9
leaq 0x8b0b4(%rip), %rsi     # 0xd26a4
leaq 0x130(%rsp), %rdi
leaq 0x110(%rsp), %rdx
callq 0x1bcf0
jmp 0x47607
leaq 0x8b05b(%rip), %rdx     # 0xd2669
leaq 0x150(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xfa60
jmp 0x47625
movq 0x2b8(%rsp), %rdi
movl 0x2b4(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x48650
movq %rax, %rsi
leaq 0x100(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x1bc90
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rcx
leaq 0x150(%rsp), %rsi
xorl %edx, %edx
callq 0x35600
jmp 0x4766f
movq 0x28(%rsp), %rdi
movb $0x0, 0xff(%rsp)
leaq 0xcdf45(%rip), %rsi     # 0x1155c8
leaq -0x2d01a(%rip), %rdx    # 0x1a670
callq 0xb640
jmp 0x47a5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x4771e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x47711
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x47704
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xfc00
leaq 0x150(%rsp), %rdi
callq 0xb6e8
leaq 0x130(%rsp), %rdi
callq 0xb6e8
leaq 0x110(%rsp), %rdi
callq 0xb6e8
testb $0x1, 0xff(%rsp)
jne 0x4772a
jmp 0x47734
movq 0x28(%rsp), %rdi
callq 0xb2d0
jmp 0x477cb
jmp 0x4773b
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
leaq 0x210(%rsp), %rsi
callq 0x1bc90
leaq 0xf0(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x1fff0
movq 0x18(%rsp), %rsi
leaq 0x298(%rsp), %rdi
callq 0x1fb90
jmp 0x4777e
leaq 0xe0(%rsp), %rdi
callq 0x1fbf0
leaq 0x208(%rsp), %rdi
callq 0x20890
leaq 0x210(%rsp), %rdi
callq 0xfc00
jmp 0x47928
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1fbf0
leaq 0x208(%rsp), %rdi
callq 0x20890
leaq 0x210(%rsp), %rdi
callq 0xfc00
jmp 0x47a44
leaq 0xc8(%rsp), %rdi
movq %rdi, (%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0xfbe0
movb $0x1, 0xaf(%rsp)
leaq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x2b8(%rsp), %rdi
movslq 0x2b4(%rsp), %rsi
callq 0x48650
movq %rax, %rsi
leaq 0xb0(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x1bc90
movq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x491f0
jmp 0x47852
movq 0x8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x493a0
movb $0x0, 0xaf(%rsp)
leaq 0x298(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x1fb90
jmp 0x47883
leaq 0xc8(%rsp), %rdi
callq 0x1fbf0
leaq 0xc0(%rsp), %rdi
callq 0x49310
leaq 0xb0(%rsp), %rdi
callq 0xfc00
movl 0x2b4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2b4(%rsp)
jmp 0x47928
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
jmp 0x47901
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x280(%rsp)
movl %eax, 0x27c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x1fbf0
leaq 0xc0(%rsp), %rdi
callq 0x49310
leaq 0xb0(%rsp), %rdi
callq 0xfc00
testb $0x1, 0xaf(%rsp)
jne 0x4791a
jmp 0x47923
movq (%rsp), %rdi
callq 0xfc00
jmp 0x47a44
jmp 0x46f15
leaq 0x298(%rsp), %rdi
callq 0x1c010
cmpq $0x0, %rax
jne 0x47981
leaq 0xa0(%rsp), %rdi
callq 0x49050
jmp 0x4794f
movq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x49400
leaq 0xa0(%rsp), %rdi
callq 0x49140
movl $0x1, 0x9c(%rsp)
jmp 0x47a27
leaq 0x298(%rsp), %rdi
callq 0x1c010
cmpq $0x1, %rax
jne 0x479e3
leaq 0x298(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1c030
movq %rax, %rdi
callq 0x1c050
testb $0x1, %al
jne 0x479e3
leaq 0x298(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1c030
movq 0x80(%rsp), %rdi
movq %rax, %rsi
addq $0x10, %rsi
callq 0x1fff0
movl $0x1, 0x9c(%rsp)
jmp 0x47a27
leaq 0x90(%rsp), %rdi
leaq 0x298(%rsp), %rsi
callq 0x49460
jmp 0x479fa
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x49520
leaq 0x90(%rsp), %rdi
callq 0x49580
movl $0x1, 0x9c(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x495e0
movq 0x88(%rsp), %rax
addq $0x2c8, %rsp            # imm = 0x2C8
retq
leaq 0x298(%rsp), %rdi
callq 0x495e0
movq 0x280(%rsp), %rdi
callq 0xb680
nop