// File: /workspace/github_c_makefile_star2_O3/goodpaul6[P]generic/script.c
// Function: debug_script
static void debug_script(script_t* script)
{
	printf("\n");

	if (script->pc >= 0)
		print_current_script_line(script);
	else
		printf("Finished execution.\n");

	char cmdbuf[SCRIPT_DEBUG_CMD_BUF_SIZE];

	while (1)
	{
		fgets(cmdbuf, SCRIPT_DEBUG_CMD_BUF_SIZE, stdin);

		// NOTE: empty command
		if (cmdbuf[0] == '\n')
			strcpy(cmdbuf, script->debug_env.cmd);
		else
			strcpy(script->debug_env.cmd, cmdbuf);
			
		// NOTE: Prints code in the vicinty of the error
		if (strcmp(cmdbuf, "list\n") == 0)
		{
			script_module_t* mod = get_executing_module(script);
			if (mod)
			{
				printf("distance: ");
				
				fgets(cmdbuf, SCRIPT_DEBUG_CMD_BUF_SIZE, stdin);
				cmdbuf[strcspn(cmdbuf, "\n")] = '\0';

				char* code = mod->source_code;
				int line = 1;
				int minDist = atoi(cmdbuf);

				while (code && *code)
				{
					char* line_end = strchr(code, '\n');

					int dist = (int)labs(script->cur_line - line);
					if (dist < minDist)
					{
						int n = printf("%d: ", line);
						if (dist == 0)
						{
							// NOTE: prints --> or something at the line (covering the line number)
							putchar('\r');
							n -= 2;
							while(n--) putchar('-');
							putchar('>');
						}

						printf("%.*s\n", (int)(line_end - code), code);
					}

					code = line_end ? line_end + 1 : NULL;
					++line;
				}
			}
			else
				printf("Code being executed does not belong to any modules.\n");
		}
		else if (strcmp(cmdbuf, "local\n") == 0)
		{
			script_call_record_t* record;
			if (script->call_records.length <= 0)
			{
				printf("Not inside function.\n");
				continue;
			}

			record = vec_get(&script->call_records, script->call_records.length - 1);

			printf("local name: ");
			
			fgets(cmdbuf, SCRIPT_DEBUG_CMD_BUF_SIZE, stdin);
			cmdbuf[strcspn(cmdbuf, "\n")] = '\0';

			const char* name = vec_get_value(&script->function_names, record->function.index, char*);
			
			func_decl_t* decl = reference_function(script, name);
			if (!decl)
			{
				printf("Unable to retrieve function data.\n");
				continue;
			}

			var_decl_t* local = NULL;
			for (int i = 0; i < decl->locals.length; ++i)
			{
				var_decl_t* l = vec_get_value(&decl->locals, i, var_decl_t*);
				if (strcmp(l->name, cmdbuf) == 0)
				{
					local = l;
					break;
				}
			}

			for (int i = 0; i < decl->args.length; ++i)
			{
				var_decl_t* l = vec_get_value(&decl->args, i, var_decl_t*);
				if (strcmp(l->name, cmdbuf) == 0)
				{
					local = l;
					break;
				}
			}

			if (!local)
			{
				printf("No local by that name.\n");
				continue;
			}

			printf("local offset = %d\n", local->index);
			printf("local value = ");

			script_value_t* val = vec_get_value(&script->stack, script->fp + local->index, script_value_t*);
			
			write_value(val, 1);
			printf("\n");
		}
		else if (strcmp(cmdbuf, "step\n") == 0)
		{
			script->debug_env.step_file = script->cur_file;
			script->debug_env.step_line = script->cur_line;
			script->debug_env.step = 1;
			break;
		}
		else if (strcmp(cmdbuf, "run_code\n") == 0)
		{
			printf("code: ");
			fgets(cmdbuf, SCRIPT_DEBUG_CMD_BUF_SIZE, stdin);

			// NOTE: It literally creates a module just for this code to run
			// This module automatically has access to the symbols of the other
			// modules since they're already loaded up and running hopefully
			script_parse_code(script, cmdbuf, "stdin", "debug");

			// NOTE: script_parse_code pushes a module onto the end of the array
			script_module_t* module = vec_get(&script->modules, script->modules.length - 1);

			compile_module(script, module);

			int pc = script->pc;
			script->pc = module->start_pc;

			while (script->pc >= 0 && script->pc < module->end_pc)
				script_execute_cycle(script);

			script->pc = pc;

			destroy_module(module);
			vec_pop_back(&script->modules, NULL);
		}
		else if (strcmp(cmdbuf, "stack\n") == 0)
		{
			script_call_record_t* record;
			if (script->call_records.length <= 0)
				record = NULL;
			else
				record = vec_get(&script->call_records, script->call_records.length - 1);

			func_decl_t* decl = NULL;

			if (record)
				decl = reference_function(script, vec_get_value(&script->function_names, record->function.index, char*));

			int min = script->fp;
			if (record)
				min = script->fp - record->nargs;

			for (size_t stackIndex = script->stack.length - 1; stackIndex >= min; --stackIndex)
			{
				if (decl)
				{
					for (int i = 0; i < decl->locals.length; ++i)
					{
						var_decl_t* local = vec_get_value(&decl->locals, i, var_decl_t*);
						if (local->index == stackIndex - script->fp)
						{
							printf("%s = ", local->name);
							break;
						}
					}

					for (int i = 0; i < decl->args.length; ++i)
					{
						var_decl_t* arg = vec_get_value(&decl->args, i, var_decl_t*);
						if (arg->index == stackIndex - script->fp)
						{
							printf("%s = ", arg->name);
							break;
						}
					}
				}

				write_value(vec_get_value(&script->stack, stackIndex, script_value_t*), 1);
				printf("\n");
			}
		}
		else if (strcmp(cmdbuf, "stop\n") == 0)
			break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
movl $0xa, %edi
callq 0x2040
cmpl $0x0, 0x11c(%rbx)
movq %rbx, -0x30(%rbp)
js 0x515a
movq %rbx, %rdi
callq 0xd6ad
movq %rax, -0x38(%rbp)
movq 0x30(%rax), %r15
cmpb $0x0, (%r15)
movl 0x124(%rbx), %r14d
je 0x5168
movl $0x1, %ebx
movq %r15, %r13
movq %r13, %rdi
leaq 0xc822(%rip), %rsi      # 0x11938
callq 0x2130
movq %rax, %r12
cmpl %r14d, %ebx
je 0x5178
movq %r12, %r15
addq %r13, %r15
callq 0x2270
movq (%rax), %rcx
movsbq (%r13,%r12), %rax
testb $0x20, 0x1(%rcx,%rax,2)
je 0x5151
xorl %edx, %edx
cmpb $0xa, %al
sete %dl
addl %edx, %ebx
movsbq 0x1(%r15), %rax
incq %r15
jmp 0x5137
movq %r15, %r13
testb %al, %al
jne 0x510c
jmp 0x5170
leaq 0xc88a(%rip), %rdi      # 0x119eb
callq 0x2060
jmp 0x51a9
xorl %r12d, %r12d
movl $0x1, %ebx
movq %r15, %r13
cmpl %r14d, %ebx
jne 0x5199
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rsi
leaq 0xc7b4(%rip), %rdi      # 0x1193b
movl %r14d, %edx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x20d0
jmp 0x51a5
leaq 0xc912(%rip), %rdi      # 0x11ab2
callq 0x2060
movq -0x30(%rbp), %rbx
leaq 0x8(%rbx), %rax
movq %rax, -0x58(%rbp)
leaq 0x130(%rbx), %rax
movq %rax, -0x48(%rbp)
leaq 0x278(%rbx), %rax
movq %rax, -0x40(%rbp)
leaq 0x198(%rbx), %rax
movq %rax, -0x60(%rbp)
leaq 0x2b8(%rbx), %rax
movq %rax, -0x50(%rbp)
movq 0x10dec(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
leaq -0x160(%rbp), %r13
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
cmpb $0xa, -0x160(%rbp)
jne 0x520d
movq %r13, %rdi
movq -0x58(%rbp), %rsi
jmp 0x5214
movq -0x58(%rbp), %rdi
movq %r13, %rsi
callq 0x2050
movq -0x30(%rbp), %r15
movl -0x160(%rbp), %eax
movl $0x7473696c, %ecx       # imm = 0x7473696C
xorl %ecx, %eax
movzwl -0x15c(%rbp), %ecx
xorl $0xa, %ecx
orl %eax, %ecx
je 0x52eb
movl -0x160(%rbp), %eax
movl $0x61636f6c, %ecx       # imm = 0x61636F6C
xorl %ecx, %eax
movl -0x15d(%rbp), %ecx
movl $0xa6c61, %edx          # imm = 0xA6C61
xorl %edx, %ecx
orl %eax, %ecx
je 0x5403
movl -0x160(%rbp), %eax
movl $0x70657473, %ecx       # imm = 0x70657473
xorl %ecx, %eax
movzwl -0x15c(%rbp), %ecx
xorl $0xa, %ecx
orl %eax, %ecx
je 0x579b
movq -0x160(%rbp), %rax
movabsq $0x65646f635f6e7572, %rcx # imm = 0x65646F635F6E7572
xorq %rcx, %rax
movzwl -0x158(%rbp), %ecx
xorq $0xa, %rcx
orq %rax, %rcx
je 0x5558
movl -0x160(%rbp), %eax
movl $0x63617473, %ecx       # imm = 0x63617473
xorl %ecx, %eax
movl -0x15d(%rbp), %ecx
movl $0xa6b63, %edx          # imm = 0xA6B63
xorl %edx, %ecx
orl %eax, %ecx
je 0x5616
movl -0x160(%rbp), %eax
movl $0x706f7473, %ecx       # imm = 0x706F7473
xorl %ecx, %eax
movzwl -0x15c(%rbp), %ecx
xorl $0xa, %ecx
orl %eax, %ecx
jne 0x51dd
jmp 0x57bf
movq %r15, %rdi
callq 0xd6ad
testq %rax, %rax
je 0x5605
movq %rax, %r14
leaq 0xc5b6(%rip), %rdi      # 0x118bc
xorl %eax, %eax
callq 0x20d0
movq 0x10cbc(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r13, %rdi
leaq 0xba44(%rip), %rsi      # 0x10d72
callq 0x2130
movb $0x0, -0x160(%rbp,%rax)
movq 0x30(%r14), %r15
movq %r13, %rdi
callq 0x2220
testq %r15, %r15
je 0x51dd
movl %eax, %r14d
movl $0x1, %r12d
movl %eax, -0x38(%rbp)
cmpb $0x0, (%r15)
je 0x51dd
movq %r15, %rdi
movl $0xa, %esi
callq 0x20c0
movq %rax, %r13
movq -0x30(%rbp), %rax
movl 0x124(%rax), %ebx
movl %ebx, %eax
subl %r12d, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
cmpl %r14d, %ecx
jge 0x53ee
leaq 0xc52f(%rip), %rdi      # 0x118c7
movl %r12d, %esi
xorl %eax, %eax
callq 0x20d0
cmpl %r12d, %ebx
jne 0x53d3
movl %eax, %r14d
movl $0xd, %edi
callq 0x2040
addl $-0x2, %r14d
je 0x53c9
movl $0x2d, %edi
callq 0x2040
decl %r14d
jne 0x53ba
movl $0x3e, %edi
callq 0x2040
movl %r13d, %esi
subl %r15d, %esi
leaq 0xc565(%rip), %rdi      # 0x11945
movq %r15, %rdx
xorl %eax, %eax
callq 0x20d0
movl -0x38(%rbp), %r14d
leaq 0x1(%r13), %r15
incl %r12d
testq %r13, %r13
jne 0x535c
jmp 0x51dd
movq 0x140(%r15), %rsi
testq %rsi, %rsi
je 0x565d
decq %rsi
movq -0x48(%rbp), %rdi
callq 0xdcf3
movq %rax, %rbx
leaq 0xc4aa(%rip), %rdi      # 0x118d3
xorl %eax, %eax
callq 0x20d0
movq 0x10b99(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r13, %rdi
leaq 0xb921(%rip), %rsi      # 0x10d72
callq 0x2130
movb $0x0, -0x160(%rbp,%rax)
movslq 0x18(%rbx), %rsi
movq -0x40(%rbp), %rdi
callq 0xdcf3
movq (%rax), %rsi
movq %r15, %rdi
callq 0x846c
testq %rax, %rax
je 0x566e
movq %rax, %r14
cmpq $0x0, 0x30(%rax)
je 0x54b7
leaq 0x20(%r14), %r15
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0xdcf3
movq (%rax), %rbx
movq 0x10(%rbx), %rdi
movq %r13, %rsi
callq 0x2160
testl %eax, %eax
je 0x54b9
incq %r12
cmpq %r12, 0x30(%r14)
ja 0x5490
xorl %ebx, %ebx
cmpq $0x0, 0x50(%r14)
je 0x54f5
movq %r14, %r15
addq $0x40, %r15
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0xdcf3
movq (%rax), %r13
movq 0x10(%r13), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2160
testl %eax, %eax
je 0x5501
incq %r12
cmpq %r12, 0x50(%r14)
ja 0x54ca
movq %rbx, %r13
testq %rbx, %rbx
je 0x578a
movl 0x1c(%r13), %esi
leaq 0xc3d4(%rip), %rdi      # 0x118e0
xorl %eax, %eax
callq 0x20d0
leaq 0xc3d9(%rip), %rdi      # 0x118f3
xorl %eax, %eax
callq 0x20d0
movq -0x30(%rbp), %rax
movslq 0x120(%rax), %rax
movslq 0x1c(%r13), %rsi
addq %rax, %rsi
movq -0x60(%rbp), %rdi
callq 0xdcf3
movq (%rax), %rdi
movl $0x1, %esi
callq 0x9be7
movl $0xa, %edi
callq 0x2040
jmp 0x51dd
leaq 0xc3b3(%rip), %rdi      # 0x11912
xorl %eax, %eax
callq 0x20d0
movq 0x10a63(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r15, %rdi
movq %r13, %rsi
leaq 0xc38f(%rip), %rdx      # 0x11919
leaq 0xc38e(%rip), %rcx      # 0x1191f
callq 0x39e1
movq 0x2c8(%r15), %rsi
decq %rsi
movq -0x50(%rbp), %rdi
callq 0xdcf3
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x3d12
movl 0x11c(%r15), %ebx
movl 0x20(%r14), %eax
movl %eax, 0x11c(%r15)
testl %eax, %eax
js 0x55e6
movl %eax, %eax
cmpq %rax, 0x28(%r14)
jbe 0x55e6
movq %r15, %rdi
callq 0x4601
movl 0x11c(%r15), %eax
jmp 0x55c9
movl %ebx, 0x11c(%r15)
movq %r14, %rdi
callq 0x3016
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xddc1
jmp 0x51dd
leaq 0xc441(%rip), %rdi      # 0x11a4d
callq 0x2060
jmp 0x51dd
movq 0x140(%r15), %rsi
testq %rsi, %rsi
je 0x567f
decq %rsi
movq -0x48(%rbp), %rdi
callq 0xdcf3
testq %rax, %rax
je 0x567f
movq %rax, %rbx
movslq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdi
callq 0xdcf3
movq (%rax), %rsi
movq %r15, %rdi
callq 0x846c
movq %rax, %r12
movl 0x120(%r15), %eax
subl 0x10(%rbx), %eax
jmp 0x5689
leaq 0xc3d4(%rip), %rdi      # 0x11a38
callq 0x2060
jmp 0x51dd
leaq 0xc38a(%rip), %rdi      # 0x119ff
callq 0x2060
jmp 0x51dd
movl 0x120(%r15), %eax
xorl %r12d, %r12d
movq 0x1a8(%r15), %r14
cltq
decq %r14
movq %rax, -0x38(%rbp)
cmpq %rax, %r14
jb 0x51dd
leaq 0x20(%r12), %r13
leaq 0x40(%r12), %r15
testq %r12, %r12
je 0x5755
cmpq $0x0, 0x30(%r12)
je 0x5705
xorl %ebx, %ebx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xdcf3
movq (%rax), %rax
movslq 0x1c(%rax), %rcx
movq -0x30(%rbp), %rdx
movslq 0x120(%rdx), %rdx
movq %r14, %rsi
subq %rdx, %rsi
cmpq %rcx, %rsi
je 0x56f3
incq %rbx
cmpq %rbx, 0x30(%r12)
ja 0x56bf
jmp 0x5705
movq 0x10(%rax), %rsi
leaq 0xc22e(%rip), %rdi      # 0x1192c
xorl %eax, %eax
callq 0x20d0
cmpq $0x0, 0x50(%r12)
je 0x5755
xorl %ebx, %ebx
movq %r15, %rdi
movq %rbx, %rsi
callq 0xdcf3
movq (%rax), %rax
movslq 0x1c(%rax), %rcx
movq -0x30(%rbp), %rdx
movslq 0x120(%rdx), %rdx
movq %r14, %rsi
subq %rdx, %rsi
cmpq %rcx, %rsi
je 0x5743
incq %rbx
cmpq %rbx, 0x50(%r12)
ja 0x570f
jmp 0x5755
movq 0x10(%rax), %rsi
leaq 0xc1de(%rip), %rdi      # 0x1192c
xorl %eax, %eax
callq 0x20d0
movq -0x60(%rbp), %rdi
movq %r14, %rsi
callq 0xdcf3
movq (%rax), %rdi
movl $0x1, %esi
callq 0x9be7
movl $0xa, %edi
callq 0x2040
decq %r14
cmpq -0x38(%rbp), %r14
jae 0x56ac
jmp 0x51dd
leaq 0xc290(%rip), %rdi      # 0x11a21
callq 0x2060
jmp 0x51dd
movq 0x128(%r15), %rax
movq %rax, 0x108(%r15)
movl 0x124(%r15), %eax
movl %eax, 0x110(%r15)
movb $0x1, 0x114(%r15)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq