// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x9790
cmpl $0x0, -0x50(%rbp)
je 0xa369
movl $0x0, -0x54(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x9790
movl -0x90(%rbp), %eax
movq %rax, -0xb0(%rbp)
subq $0x1d, %rax
ja 0xa24f
movq -0xb0(%rbp), %rax
leaq 0x8687(%rip), %rcx      # 0x12884
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, -0x54(%rbp)
jmp 0xa24f
movl $0x0, -0x58(%rbp)
jmp 0xa24f
movl $0x0, -0x58(%rbp)
jmp 0xa24f
movl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0xa24f
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x28, %rdi
leaq 0x896d(%rip), %rsi      # 0x12ba8
movb $0x0, %al
callq 0x3880
movq $0x0, -0x8(%rbp)
jmp 0xa371
movups -0x90(%rbp), %xmm0
movups -0x80(%rbp), %xmm1
movups -0x70(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x9aa0
movq %rax, -0x98(%rbp)
movq -0x60(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x98(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0xa29a
jmp 0xa2a8
movq -0x10(%rbp), %rdi
callq 0x97e0
jmp 0xa1cc
jmp 0xa2aa
cmpq $0x0, -0x60(%rbp)
je 0xa2da
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x99d0
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0xa2aa
movl -0x58(%rbp), %eax
movl %eax, -0xb4(%rbp)
testl %eax, %eax
je 0xa2f6
jmp 0xa2e9
movl -0xb4(%rbp), %eax
subl $0x1, %eax
je 0xa308
jmp 0xa318
movq -0x10(%rbp), %rdi
callq 0xa380
movq %rax, -0xa8(%rbp)
jmp 0xa318
movq -0x10(%rbp), %rdi
callq 0xa540
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0xa327
jmp 0xa19f
movq -0xa8(%rbp), %rax
movq $0x0, 0x60(%rax)
cmpq $0x0, -0x18(%rbp)
jne 0xa34a
movq -0xa8(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0xa359
movq -0xa8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0xa8(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0xa19f
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nop