// File: /workspace/github_c_makefile_star2_O2/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
pushq $0xa
popq %rdi
callq 0x2040
cmpl $0x0, 0x11c(%rbx)
movq %rbx, -0x30(%rbp)
js 0x4b73
movq %rbx, %rdi
callq 0xbb6c
movq %rax, -0x38(%rbp)
movq 0x30(%rax), %r14
movb (%r14), %al
movl 0x124(%rbx), %r15d
pushq $0x1
popq %rbx
xorl %r13d, %r13d
leaq 0xae08(%rip), %r12      # 0xf940
testb %al, %al
je 0x4b81
movq %r14, %rdi
movq %r12, %rsi
callq 0x2130
movq %rax, %r13
cmpl %r15d, %ebx
je 0x4b86
addq %r13, %r14
callq 0x2270
movq (%rax), %rcx
movsbq (%r14), %rax
testb $0x20, 0x1(%rcx,%rax,2)
je 0x4b38
xorl %edx, %edx
cmpb $0xa, %al
sete %dl
addl %edx, %ebx
incq %r14
jmp 0x4b5a
leaq 0xae79(%rip), %rdi      # 0xf9f3
callq 0x2060
jmp 0x4bb7
cmpl %r15d, %ebx
jne 0x4ba7
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rsi
leaq 0xadae(%rip), %rdi      # 0xf943
movl %r15d, %edx
movq %r13, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x20d0
jmp 0x4bb3
leaq 0xaf0c(%rip), %rdi      # 0xfaba
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 0xf3de(%rip), %rax      # 0x13fd0
movq (%rax), %rdx
leaq -0x160(%rbp), %r12
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
cmpb $0xa, -0x160(%rbp)
jne 0x4c1b
movq %r12, %rdi
movq -0x58(%rbp), %rsi
jmp 0x4c22
movq -0x58(%rbp), %rdi
movq %r12, %rsi
callq 0x2050
movq -0x30(%rbp), %r15
movq %r12, %rdi
leaq 0xa584(%rip), %rsi      # 0xf1b9
pushq $0x6
popq %rdx
callq 0x21c0
testl %eax, %eax
je 0x4ccc
movq %r12, %rdi
leaq 0xac85(%rip), %rsi      # 0xf8d4
pushq $0x7
popq %rdx
callq 0x21c0
testl %eax, %eax
je 0x4ddb
movq %r12, %rdi
leaq 0xaca1(%rip), %rsi      # 0xf90a
pushq $0x6
popq %rdx
callq 0x21c0
testl %eax, %eax
je 0x5148
movq %r12, %rdi
leaq 0xac8d(%rip), %rsi      # 0xf910
pushq $0xa
popq %rdx
callq 0x21c0
testl %eax, %eax
je 0x4e98
movq %r12, %rdi
leaq 0xac90(%rip), %rsi      # 0xf92d
pushq $0x7
popq %rdx
callq 0x21c0
testl %eax, %eax
je 0x4f56
movq %r12, %rdi
leaq 0xac83(%rip), %rsi      # 0xf93a
pushq $0x6
popq %rdx
callq 0x21c0
testl %eax, %eax
jne 0x4beb
jmp 0x516c
movq %r15, %rdi
callq 0xbb6c
testq %rax, %rax
je 0x4f45
movq %rax, %r14
leaq 0xabdd(%rip), %rdi      # 0xf8c4
xorl %eax, %eax
callq 0x20d0
movq 0xf2db(%rip), %rax      # 0x13fd0
movq (%rax), %rdx
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r12, %rdi
leaq 0xa06b(%rip), %rsi      # 0xed7a
callq 0x2130
movb $0x0, -0x160(%rbp,%rax)
movq 0x30(%r14), %r15
movq %r12, %rdi
callq 0x2220
movl %eax, %r14d
pushq $0x1
popq %r12
movl %eax, -0x38(%rbp)
testq %r15, %r15
je 0x4beb
cmpb $0x0, (%r15)
je 0x4beb
movq %r15, %rdi
pushq $0xa
popq %rsi
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 0x4dc8
leaq 0xab5a(%rip), %rdi      # 0xf8cf
movl %r12d, %esi
xorl %eax, %eax
callq 0x20d0
cmpl %r12d, %ebx
jne 0x4dad
movl %eax, %r14d
pushq $0xd
popq %rdi
callq 0x2040
addl $-0x2, %r14d
pushq $0x2d
popq %rbx
subl $0x1, %r14d
jb 0x4da5
movl %ebx, %edi
callq 0x2040
jmp 0x4d96
pushq $0x3e
popq %rdi
callq 0x2040
movl %r13d, %esi
subl %r15d, %esi
leaq 0xab93(%rip), %rdi      # 0xf94d
movq %r15, %rdx
xorl %eax, %eax
callq 0x20d0
movl -0x38(%rbp), %r14d
leaq 0x1(%r13), %r15
incl %r12d
testq %r13, %r13
cmoveq %r13, %r15
jmp 0x4d32
movq 0x140(%r15), %rsi
testq %rsi, %rsi
je 0x4f9d
decq %rsi
movq -0x48(%rbp), %rdi
callq 0xc15c
movq %rax, %rbx
leaq 0xaada(%rip), %rdi      # 0xf8db
xorl %eax, %eax
callq 0x20d0
movq 0xf1c1(%rip), %rax      # 0x13fd0
movq (%rax), %rdx
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r12, %rdi
leaq 0x9f51(%rip), %rsi      # 0xed7a
callq 0x2130
movb $0x0, -0x160(%rbp,%rax)
movslq 0x18(%rbx), %rsi
movq -0x40(%rbp), %rdi
callq 0xc15c
movq (%rax), %rsi
movq %r15, %rdi
callq 0x760e
testq %rax, %rax
je 0x4fae
movq %rax, %r14
movq %rax, %rbx
leaq 0x20(%rax), %r15
xorl %r12d, %r12d
cmpq %r12, 0x30(%rbx)
jbe 0x50a2
movq %r15, %rdi
movq %r12, %rsi
callq 0xc15c
movq (%rax), %r13
movq 0x10(%r13), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2150
incq %r12
testl %eax, %eax
jne 0x4e64
jmp 0x50a5
leaq 0xaa7b(%rip), %rdi      # 0xf91a
xorl %eax, %eax
callq 0x20d0
movq 0xf123(%rip), %rax      # 0x13fd0
movq (%rax), %rdx
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r15, %rdi
movq %r12, %rsi
leaq 0xaa57(%rip), %rdx      # 0xf921
leaq 0xaa56(%rip), %rcx      # 0xf927
callq 0x3620
movq 0x2c8(%r15), %rsi
decq %rsi
movq -0x50(%rbp), %rdi
callq 0xc15c
movq %rax, %r14
movq %r15, %rdi
movq %rax, %rsi
callq 0x3934
movl 0x11c(%r15), %ebx
movl 0x20(%r14), %eax
movl %eax, 0x11c(%r15)
testl %eax, %eax
js 0x4f26
movl %eax, %eax
cmpq %rax, 0x28(%r14)
jbe 0x4f26
movq %r15, %rdi
callq 0x41b4
movl 0x11c(%r15), %eax
jmp 0x4f09
movl %ebx, 0x11c(%r15)
movq %r14, %rdi
callq 0x2c87
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xc219
jmp 0x4beb
leaq 0xab09(%rip), %rdi      # 0xfa55
callq 0x2060
jmp 0x4beb
movq 0x140(%r15), %rsi
testq %rsi, %rsi
je 0x4fbf
decq %rsi
movq -0x48(%rbp), %rdi
callq 0xc15c
testq %rax, %rax
je 0x4fbf
movq %rax, %rbx
movslq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdi
callq 0xc15c
movq (%rax), %rsi
movq %r15, %rdi
callq 0x760e
movq %rax, %r13
movl 0x120(%r15), %eax
subl 0x10(%rbx), %eax
jmp 0x4fc9
leaq 0xaa9c(%rip), %rdi      # 0xfa40
callq 0x2060
jmp 0x4beb
leaq 0xaa52(%rip), %rdi      # 0xfa07
callq 0x2060
jmp 0x4beb
movl 0x120(%r15), %eax
xorl %r13d, %r13d
movq 0x1a8(%r15), %r15
cltq
movq %rax, -0x38(%rbp)
leaq 0x20(%r13), %r12
leaq 0x40(%r13), %r14
decq %r15
cmpq -0x38(%rbp), %r15
jb 0x4beb
testq %r13, %r13
je 0x507e
xorl %ebx, %ebx
cmpq %rbx, 0x30(%r13)
jbe 0x5039
movq %r12, %rdi
movq %rbx, %rsi
callq 0xc15c
movq (%rax), %rax
movslq 0x1c(%rax), %rcx
movq -0x30(%rbp), %rdx
movslq 0x120(%rdx), %rdx
movq %r15, %rsi
subq %rdx, %rsi
incq %rbx
cmpq %rcx, %rsi
jne 0x4ff6
movq 0x10(%rax), %rsi
leaq 0xa902(%rip), %rdi      # 0xf934
xorl %eax, %eax
callq 0x20d0
xorl %ebx, %ebx
cmpq %rbx, 0x50(%r13)
jbe 0x507e
movq %r14, %rdi
movq %rbx, %rsi
callq 0xc15c
movq (%rax), %rax
movslq 0x1c(%rax), %rcx
movq -0x30(%rbp), %rdx
movslq 0x120(%rdx), %rdx
movq %r15, %rsi
subq %rdx, %rsi
incq %rbx
cmpq %rcx, %rsi
jne 0x503b
movq 0x10(%rax), %rsi
leaq 0xa8bd(%rip), %rdi      # 0xf934
xorl %eax, %eax
callq 0x20d0
movq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0xc15c
movq (%rax), %rdi
pushq $0x1
popq %rsi
callq 0x8dd9
pushq $0xa
popq %rdi
callq 0x2040
jmp 0x4fde
xorl %r13d, %r13d
addq $0x40, %r14
xorl %r15d, %r15d
cmpq %r15, 0x50(%rbx)
jbe 0x50da
movq %r14, %rdi
movq %r15, %rsi
callq 0xc15c
movq (%rax), %r12
movq 0x10(%r12), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2150
incq %r15
testl %eax, %eax
jne 0x50ac
jmp 0x50e2
movq %r13, %r12
testq %r13, %r13
je 0x5137
movl 0x1c(%r12), %esi
leaq 0xa7fa(%rip), %rdi      # 0xf8e8
xorl %eax, %eax
callq 0x20d0
leaq 0xa7ff(%rip), %rdi      # 0xf8fb
xorl %eax, %eax
callq 0x20d0
movq -0x30(%rbp), %rax
movslq 0x120(%rax), %rax
movslq 0x1c(%r12), %rsi
addq %rax, %rsi
movq -0x60(%rbp), %rdi
callq 0xc15c
movq (%rax), %rdi
pushq $0x1
popq %rsi
callq 0x8dd9
pushq $0xa
popq %rdi
callq 0x2040
jmp 0x4beb
leaq 0xa8eb(%rip), %rdi      # 0xfa29
callq 0x2060
jmp 0x4beb
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