// File: /workspace/github_c_makefile_star2_O3/Yamsman[P]scc/src/parse.c
// Function: parse
ast_n *parse(lexer *lx) {
	ast_n *list = NULL; // = parse_stmt();
	ast_n *cur = NULL;
	while (lex_peek(lx).type != TOK_END) {
		//Look ahead to see if next is decl or function def
		int par = 0;
		int fdef = -1;
		token_n *toklist = NULL;
		for (;;) {
			token t = lex_peek(lx);
			switch (t.type) {
				case TOK_RPR: par = 1;    break;
				case TOK_EQ:  fdef = 0;   break;
				case TOK_SEM: fdef = 0;   break; 
				case TOK_LBR: fdef = par; break;
				case TOK_END:
					c_error(&lx->tgt->loc, "Premature end of input\n");
					return NULL;
			}
			token_n *tn = make_tok_node(t);
			tn->next = toklist;
			toklist = tn;

			if (fdef >= 0) break;
			lex_adv(lx);
		}

		//Unget tokens
		while (toklist != NULL) {
			token_n *next = toklist->next;
			lex_unget(lx, toklist);
			toklist = next;
		}

		//Parse accordingly
		ast_n *node;
		switch (fdef) {
			case 0: node = parse_decl(lx); break;
			case 1: node = parse_fdef(lx); break;
		}
		if (node == NULL) continue;
		node->next = NULL;

		//Add declaration/function definition to list
		if (list == NULL) {
			list = node;
		} else {
			cur->next = node;
		}
		cur = node;
	}

	return list;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x79dc
cmpl $0x0, (%r14)
je 0x82f5
xorl %eax, %eax
leaq 0x48(%rsp), %r14
leaq 0x63cc(%rip), %rbp      # 0xe540
xorl %r15d, %r15d
movq %r15, 0x30(%rsp)
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x79dc
movl 0x48(%rsp), %eax
cmpq $0x8, %rax
ja 0x81de
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movups 0x48(%rsp), %xmm0
movups 0x58(%rsp), %xmm1
movups 0x68(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x7c14
movq %rax, %r13
movq %r15, 0x30(%rax)
testl %r12d, %r12d
js 0x820c
jmp 0x8248
movl $0x1, %r12d
jmp 0x81e3
cmpl $0x1d, %eax
je 0x821c
movups 0x48(%rsp), %xmm0
movups 0x58(%rsp), %xmm1
movups 0x68(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x7c14
movq %rax, %r13
movq %r15, 0x30(%rax)
movq %rbx, %rdi
callq 0x7a19
movq %r13, %r15
jmp 0x8187
movups 0x48(%rsp), %xmm0
movups 0x58(%rsp), %xmm1
movups 0x68(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x7c14
movq %rax, %r13
movq %r15, 0x30(%rax)
xorl %r12d, %r12d
movq 0x30(%r13), %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7ba8
movq %r15, %r13
testq %r15, %r15
jne 0x8248
cmpl $0x1, %r12d
je 0x8279
testl %r12d, %r12d
movq 0x40(%rsp), %rax
jne 0x8281
movq %rbx, %rdi
callq 0x830d
jmp 0x8281
movq %rbx, %rdi
callq 0x8459
testq %rax, %rax
je 0x82af
movq $0x0, 0x60(%rax)
movq %rax, %r15
movq %rax, %r13
movq 0x38(%rsp), %rcx
testq %rcx, %rcx
je 0x82b9
movq 0x30(%rsp), %rdx
movq %rax, 0x60(%rdx)
movq %rax, %r15
movq %rcx, %r13
jmp 0x82b9
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r13
movq %rax, 0x40(%rsp)
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0x79dc
cmpl $0x0, 0x78(%rsp)
movq %r13, %rax
jne 0x8177
jmp 0x82f8
movq (%rbx), %rdi
addq $0x28, %rdi
leaq 0x6417(%rip), %rsi      # 0xe700
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x3250
jmp 0x82f8
xorl %r13d, %r13d
movq %r13, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq