// File: /workspace/github_c_makefile_star2_O1/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 0x50e1
movq %rbx, %rdi
callq 0xd851
movq %rax, -0x38(%rbp)
movq 0x30(%rax), %r12
cmpb $0x0, (%r12)
je 0x50ef
movl 0x124(%rbx), %ebx
movl $0x1, %r15d
movq %r12, %r14
movq %r14, %rdi
leaq 0xc8a0(%rip), %rsi      # 0x11938
callq 0x2130
movq %rax, %r13
cmpl %ebx, %r15d
je 0x50fa
movq %r13, %r12
addq %r14, %r12
callq 0x2260
movq (%rax), %rax
movsbq (%r14,%r13), %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x50d5
xorl %edx, %edx
cmpb $0xa, %cl
sete %dl
addl %edx, %r15d
movsbq 0x1(%r12), %rcx
incq %r12
jmp 0x50b8
cmpb $0x0, (%r12)
movq %r12, %r14
jne 0x508e
jmp 0x50fd
leaq 0xc903(%rip), %rdi      # 0x119eb
callq 0x2060
jmp 0x513b
movl $0x1, %r15d
xorl %r13d, %r13d
jmp 0x50fd
movq %r14, %r12
movq -0x30(%rbp), %rax
cmpl 0x124(%rax), %r15d
jne 0x512b
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rsi
leaq 0xc822(%rip), %rdi      # 0x1193b
movl %r15d, %edx
movq %r13, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x20d0
jmp 0x5137
leaq 0xc980(%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 0x10e5a(%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 0x519f
movq %r13, %rdi
movq -0x58(%rbp), %rsi
jmp 0x51a6
movq -0x58(%rbp), %rdi
movq %r13, %rsi
callq 0x2050
movq -0x30(%rbp), %r12
movl -0x160(%rbp), %eax
movl $0x7473696c, %ecx       # imm = 0x7473696C
xorl %ecx, %eax
movzwl -0x15c(%rbp), %ecx
xorl $0xa, %ecx
orl %eax, %ecx
je 0x527d
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 0x5399
movl -0x160(%rbp), %eax
movl $0x70657473, %ecx       # imm = 0x70657473
xorl %ecx, %eax
movzwl -0x15c(%rbp), %ecx
xorl $0xa, %ecx
orl %eax, %ecx
je 0x5752
movq -0x160(%rbp), %rax
movabsq $0x65646f635f6e7572, %rcx # imm = 0x65646F635F6E7572
xorq %rcx, %rax
movzwl -0x158(%rbp), %ecx
xorq $0xa, %rcx
orq %rax, %rcx
je 0x5465
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 0x5528
movl -0x160(%rbp), %eax
movl $0x706f7473, %ecx       # imm = 0x706F7473
xorl %ecx, %eax
movzwl -0x15c(%rbp), %ecx
xorl $0xa, %ecx
orl %eax, %ecx
jne 0x516f
jmp 0x577b
movq %r12, %rdi
callq 0xd851
testq %rax, %rax
je 0x5517
movq %rax, %r14
leaq 0xc624(%rip), %rdi      # 0x118bc
xorl %eax, %eax
callq 0x20d0
movq 0x10d2a(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r13, %rdi
leaq 0xbab2(%rip), %rsi      # 0x10d72
callq 0x2130
movb $0x0, -0x160(%rbp,%rax)
movq 0x30(%r14), %r15
movq %r13, %rdi
callq 0x2210
testq %r15, %r15
je 0x516f
movl %eax, %r14d
movl $0x1, %r12d
movl %eax, -0x38(%rbp)
cmpb $0x0, (%r15)
je 0x516f
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 0x5380
leaq 0xc59d(%rip), %rdi      # 0x118c7
movl %r12d, %esi
xorl %eax, %eax
callq 0x20d0
cmpl %r12d, %ebx
jne 0x5365
movl %eax, %r14d
movl $0xd, %edi
callq 0x2040
addl $-0x2, %r14d
je 0x535b
movl $0x2d, %edi
callq 0x2040
decl %r14d
jne 0x534c
movl $0x3e, %edi
callq 0x2040
movl %r13d, %esi
subl %r15d, %esi
leaq 0xc5d3(%rip), %rdi      # 0x11945
movq %r15, %rdx
xorl %eax, %eax
callq 0x20d0
movl -0x38(%rbp), %r14d
leaq 0x1(%r13), %r15
incl %r12d
testq %r13, %r13
cmoveq %r13, %r15
jne 0x52ee
jmp 0x516f
movq 0x140(%r12), %rsi
testq %rsi, %rsi
je 0x554d
decq %rsi
movq -0x48(%rbp), %rdi
callq 0xdf24
movq %rax, %rbx
leaq 0xc513(%rip), %rdi      # 0x118d3
xorl %eax, %eax
callq 0x20d0
movq 0x10c02(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r13, %rdi
leaq 0xb98a(%rip), %rsi      # 0x10d72
callq 0x2130
movb $0x0, -0x160(%rbp,%rax)
movslq 0x18(%rbx), %rsi
movq -0x40(%rbp), %rdi
callq 0xdf24
movq (%rax), %rsi
movq %r12, %rdi
callq 0x84c0
testq %rax, %rax
je 0x555e
movq %rax, %r14
cmpq $0x0, 0x30(%rax)
je 0x556f
leaq 0x20(%r14), %r15
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %r15, %rdi
movq %r12, %rsi
callq 0xdf24
movq (%rax), %r13
movq 0x10(%r13), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2150
testl %eax, %eax
cmoveq %r13, %rbx
je 0x5571
incq %r12
cmpq %r12, 0x30(%r14)
ja 0x542d
jmp 0x5571
leaq 0xc4a6(%rip), %rdi      # 0x11912
xorl %eax, %eax
callq 0x20d0
movq 0x10b56(%rip), %rax     # 0x15fd0
movq (%rax), %rdx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x2140
movq %r12, %rdi
movq %r13, %rsi
leaq 0xc482(%rip), %rdx      # 0x11919
leaq 0xc481(%rip), %rcx      # 0x1191f
callq 0x39a3
movq 0x2c8(%r12), %rsi
decq %rsi
movq -0x50(%rbp), %rdi
callq 0xdf24
movq %rax, %r14
movq %r12, %rdi
movq %rax, %rsi
callq 0x3d15
movl 0x11c(%r12), %ebx
movl 0x20(%r14), %eax
movl %eax, 0x11c(%r12)
testl %eax, %eax
js 0x54f7
movl %eax, %eax
cmpq %rax, 0x28(%r14)
jbe 0x54f7
movq %r12, %rdi
callq 0x45f3
movl 0x11c(%r12), %eax
jmp 0x54d9
movl %ebx, 0x11c(%r12)
movq %r14, %rdi
callq 0x300c
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0xdfda
jmp 0x516f
leaq 0xc52f(%rip), %rdi      # 0x11a4d
callq 0x2060
jmp 0x516f
movq 0x140(%r12), %rsi
testq %rsi, %rsi
je 0x561d
decq %rsi
movq -0x48(%rbp), %rdi
callq 0xdf24
movq %rax, %r14
jmp 0x5620
leaq 0xc4e4(%rip), %rdi      # 0x11a38
callq 0x2060
jmp 0x516f
leaq 0xc49a(%rip), %rdi      # 0x119ff
callq 0x2060
jmp 0x516f
xorl %ebx, %ebx
cmpq $0x0, 0x50(%r14)
je 0x55b1
movq %r14, %r15
addq $0x40, %r15
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0xdf24
movq (%rax), %r13
movq 0x10(%r13), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2150
testl %eax, %eax
cmoveq %r13, %rbx
je 0x55b1
incq %r12
cmpq %r12, 0x50(%r14)
ja 0x5582
testq %rbx, %rbx
je 0x560c
movl 0x1c(%rbx), %esi
leaq 0xc320(%rip), %rdi      # 0x118e0
xorl %eax, %eax
callq 0x20d0
leaq 0xc325(%rip), %rdi      # 0x118f3
xorl %eax, %eax
callq 0x20d0
movq -0x30(%rbp), %rax
movslq 0x120(%rax), %rax
movslq 0x1c(%rbx), %rsi
addq %rax, %rsi
movq -0x60(%rbp), %rdi
callq 0xdf24
movq (%rax), %rdi
movl $0x1, %esi
callq 0x9dc1
movl $0xa, %edi
callq 0x2040
jmp 0x516f
leaq 0xc40e(%rip), %rdi      # 0x11a21
callq 0x2060
jmp 0x516f
xorl %r14d, %r14d
testq %r14, %r14
je 0x5642
movslq 0x18(%r14), %rsi
movq -0x40(%rbp), %rdi
callq 0xdf24
movq (%rax), %rsi
movq %r12, %rdi
callq 0x84c0
movq %rax, %r15
jmp 0x5645
xorl %r15d, %r15d
movl 0x120(%r12), %eax
testq %r14, %r14
je 0x5656
subl 0x10(%r14), %eax
movq 0x1a8(%r12), %r14
cltq
decq %r14
movq %rax, -0x38(%rbp)
cmpq %rax, %r14
jb 0x516f
leaq 0x20(%r15), %r13
leaq 0x40(%r15), %r12
testq %r15, %r15
je 0x571d
cmpq $0x0, 0x30(%r15)
je 0x56cf
xorl %ebx, %ebx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xdf24
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 0x56bd
incq %rbx
cmpq %rbx, 0x30(%r15)
ja 0x568a
jmp 0x56cf
movq 0x10(%rax), %rsi
leaq 0xc264(%rip), %rdi      # 0x1192c
xorl %eax, %eax
callq 0x20d0
cmpq $0x0, 0x50(%r15)
je 0x571d
xorl %ebx, %ebx
movq %r12, %rdi
movq %rbx, %rsi
callq 0xdf24
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 0x570b
incq %rbx
cmpq %rbx, 0x50(%r15)
ja 0x56d8
jmp 0x571d
movq 0x10(%rax), %rsi
leaq 0xc216(%rip), %rdi      # 0x1192c
xorl %eax, %eax
callq 0x20d0
movq -0x60(%rbp), %rdi
movq %r14, %rsi
callq 0xdf24
movq (%rax), %rdi
movl $0x1, %esi
callq 0x9dc1
movl $0xa, %edi
callq 0x2040
decq %r14
cmpq -0x38(%rbp), %r14
jae 0x5678
jmp 0x516f
movq 0x128(%r12), %rax
movq %rax, 0x108(%r12)
movl 0x124(%r12), %eax
movl %eax, 0x110(%r12)
movb $0x1, 0x114(%r12)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq