// File: /workspace/github_cpp_makefile_star2_O3/TsukiGva2[P]Blade-lang/blade.cpp
// Function: lc_eval(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::string lc_eval(std::string expr){
/* 
1. detect the innermost function [x]
2. call that function            [x]
3. return to 1                   [x]
*/

	std::cout << "got: " << expr << endl;
	FnPtr tempfun = err;
	std::string temp;
	std::string ufun;
	bool userfun = false;
    
	//       f a a
	// [ + [ - 9 9] 9 ]
	//   f   arg1  arg2	

	// so, it works like this
	//-         1 1 1     1 1 1
	//  [ [ [ [ ] ] ] [ [ ] ] ]
	//+ 1 1 1 1       1 1   
	//  you do + 1 for the opening parens
	//  and - 1 for the closing ones

	int acc = 0;
	std::map<int,int> depth_index;
	std::vector<int> depths;
	std::vector<std::string> args;

	bool instr = false;
	
	for (int i = 0; i < expr.size(); i++){
		if (expr[i] == '"') instr = !instr;

		if (expr[i] == '[' && !instr) {
			acc += 1;
			depths.push_back(acc);
		}

		else if (expr[i] == ']' && !instr) {
			acc -= 1;
			depths.push_back(acc);
		}

		if (depth_index.find(acc) == depth_index.end())
			depth_index[acc] = i;
	}

	if (instr) return "SYNTAX-ERROR: no matching double quote. err-unfinished-str";

	if (acc != 0){
		return "SYNTAX-ERROR: wrong number of brackets. err-unbalanced-brackets";
	}

	int maxd_index = depth_index[max_element(depths)];
	int replace_end = 0;

	for (int i = maxd_index+1; i < expr.size(); i++){
		if (expr[i] == ']' && !instr) {
			replace_end = i+1;
		}

		if (expr[i] == '"' && !instr){
			instr = true;
			continue;
		}

		if (!((expr[i] == ' ' && !instr && expr[i-1] != '"') || (expr[i] == '"' && instr) || (expr[i] == ']' && !instr))){ // XXX
			temp += expr[i];
		} else {
			if (!instr){
				std::remove(temp.begin(), temp.end(), ' ');
			}

			if (funcs_map.find(temp) != funcs_map.end() && !instr) tempfun = funcs_map[temp];
			else if (usfuns_map.find(temp) != usfuns_map.end() && !instr){
				ufun = temp;
				userfun = true;
			} else {
				if (vars.find(temp) != vars.end() && !instr) {
					args.push_back(vars[temp]);
				} else if (!temp.empty()){
					args.push_back(temp);
				}
			}

			instr = false;
			if (expr[i] == ']' && !instr) break;
			temp = "";
		}
	}
	std::string result;
	if (!userfun) result = tempfun(args);
	else {
		globl_args = args;
		result = lc_eval(usfuns_map[ufun]);
	}

	if (max_element(depths) < 2) return result;
	
	// else
	
	return lc_eval(replace_str(expr,maxd_index,replace_end,result));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
leaq 0x108215(%rip), %r15    # 0x11f980
leaq 0xc19bf(%rip), %rsi     # 0xd9131
movl $0x5, %edx
movq %r15, %rdi
callq 0x96cb0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x96cb0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x77e00
movsbl %al, %esi
movq %r15, %rdi
callq 0x967b0
movq %rax, %rdi
callq 0x95d80
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
xorl %ebx, %ebx
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq -0xf8(%rbp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
movl %ebx, -0x2c(%rbp)
leaq -0x200(%rbp), %r13
movl %ebx, (%r13)
movq %rbx, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rbx, 0x20(%r13)
movq %rbx, -0x90(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq %rbx, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
cmpq %rbx, 0x8(%r14)
je 0x17918
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%r14), %rax
movzbl (%rax,%r15), %eax
cmpl $0x22, %eax
sete %r12b
cmpl $0x5b, %eax
je 0x1784a
cmpl $0x5d, %eax
jne 0x17886
testb $0x1, %bl
jne 0x17886
movl -0x2c(%rbp), %eax
decl %eax
jmp 0x17854
testb $0x1, %bl
jne 0x17886
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
movq -0x98(%rbp), %rsi
cmpq -0x90(%rbp), %rsi
je 0x17876
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x98(%rbp)
jmp 0x17886
leaq -0xa0(%rbp), %rdi
leaq -0x2c(%rbp), %rdx
callq 0x261b4
movq -0x1f8(%rbp), %rdi
testq %rdi, %rdi
je 0x178ca
movl -0x2c(%rbp), %eax
movq %r13, %r8
movq %r8, %rcx
movq %rdi, %rdx
movl 0x20(%rdi), %esi
xorl %edi, %edi
cmpl %eax, %esi
setl %dil
movq %rdx, %r8
cmovlq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x17898
cmpq %r13, %r8
je 0x178ca
cmpl %eax, %esi
cmovlq %rcx, %rdx
cmpl 0x20(%rdx), %eax
jge 0x178dd
leaq -0x208(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x1a2f2
movl %r15d, (%rax)
xorb %r12b, %bl
incq %r15
cmpq %r15, 0x8(%r14)
ja 0x17825
testb $0x1, %bl
je 0x17918
movq -0x38(%rbp), %r15
leaq 0x10(%r15), %rax
movq %rax, (%r15)
leaq 0xc1833(%rip), %rsi     # 0xd9137
leaq 0xc1866(%rip), %rdx     # 0xd9171
movq %r15, %rdi
callq 0x1ac90
jmp 0x17e2a
cmpl $0x0, -0x2c(%rbp)
je 0x17944
movq -0x38(%rbp), %r15
leaq 0x10(%r15), %rax
movq %rax, (%r15)
leaq 0xc1842(%rip), %rsi     # 0xd9172
leaq 0xc187a(%rip), %rdx     # 0xd91b1
movq %r15, %rdi
callq 0x1ac90
jmp 0x17e2a
leaq -0xe8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1a3c2
movq -0xe8(%rbp), %rcx
movq -0xe0(%rbp), %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
je 0x17979
movl (%rcx), %esi
cmpl %eax, %esi
cmovgl %esi, %eax
addq $0x4, %rcx
jmp 0x17967
leaq -0x80(%rbp), %rsi
movl %eax, (%rsi)
leaq -0x208(%rbp), %rdi
callq 0x1a35a
movl (%rax), %r12d
movslq %r12d, %r15
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x179ac
movq -0xd8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2a9a0
incq %r15
cmpq %r15, 0x8(%r14)
movl %r12d, -0xa4(%rbp)
jbe 0x17c13
movl $0x0, -0x3c(%rbp)
leaq -0x384(%rip), %rax      # 0x1764a
movq %rax, -0xb0(%rbp)
movl %r12d, %r13d
xorl %r12d, %r12d
movl %r12d, %esi
movq (%r14), %rcx
movzbl (%rcx,%r15), %eax
cmpl $0x5d, %eax
setne %dl
leal 0x2(%r13), %r12d
orb %bl, %dl
testb $0x1, %dl
cmovnel %esi, %r12d
cmpl $0x20, %eax
je 0x17a0f
cmpl $0x22, %eax
jne 0x17a75
testb $0x1, %bl
jne 0x17a35
movb $0x1, %bl
jmp 0x17b35
testb $0x1, %bl
jne 0x17a7a
movslq %r13d, %rdx
cmpb $0x22, (%rcx,%rdx)
je 0x17a7a
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
addq %rdi, %rsi
movb $0x20, -0x80(%rbp)
leaq -0x80(%rbp), %rdx
callq 0x265a3
leaq 0x106ed4(%rip), %rdi    # 0x11e910
leaq -0x60(%rbp), %rsi
callq 0x26684
leaq 0x106ecc(%rip), %rcx    # 0x11e918
cmpq %rcx, %rax
je 0x17a8b
testb $0x1, %bl
jne 0x17a8b
leaq 0x106eb3(%rip), %rdi    # 0x11e910
leaq -0x60(%rbp), %rsi
callq 0x1a442
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
jmp 0x17b10
testb $0x1, %dl
je 0x17a1d
movsbl %al, %esi
leaq -0x60(%rbp), %rdi
callq 0xa7bd0
jmp 0x17b35
leaq 0x106eae(%rip), %rdi    # 0x11e940
leaq -0x60(%rbp), %rsi
callq 0x26a42
leaq 0x106ea6(%rip), %rcx    # 0x11e948
cmpq %rcx, %rax
je 0x17ac3
testb $0x1, %bl
jne 0x17ac3
movb $0x1, %al
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xa7170
jmp 0x17b10
leaq 0x106ebe(%rip), %rdi    # 0x11e988
leaq -0x60(%rbp), %rsi
callq 0x26a42
leaq 0x106eb6(%rip), %rcx    # 0x11e990
cmpq %rcx, %rax
je 0x17af6
testb $0x1, %bl
jne 0x17af6
leaq 0x106e9d(%rip), %rdi    # 0x11e988
leaq -0x60(%rbp), %rsi
callq 0x1a50e
jmp 0x17b01
cmpq $0x0, -0x58(%rbp)
leaq -0x60(%rbp), %rax
je 0x17b10
leaq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0x1a4c2
movq (%r14), %rax
cmpb $0x5d, (%rax,%r15)
je 0x17b45
xorl %ebx, %ebx
movq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
leaq 0xc183f(%rip), %rcx     # 0xd936c
xorl %r8d, %r8d
callq 0xa8190
incq %r15
incl %r13d
cmpq %r15, 0x8(%r14)
ja 0x179db
leaq -0x70(%rbp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
testb $0x1, -0x3c(%rbp)
je 0x17c34
leaq 0x106e05(%rip), %rdi    # 0x11e970
leaq -0xd0(%rbp), %rsi
callq 0x1a622
leaq 0x106dc2(%rip), %rdi    # 0x11e940
leaq -0x108(%rbp), %rsi
callq 0x1a50e
leaq -0x178(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x188(%rbp), %rdi
callq 0x1abf2
leaq -0x128(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x17749
leaq -0x80(%rbp), %rdi
leaq -0x128(%rbp), %r15
movq %r15, %rsi
callq 0xa7590
movq (%r15), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
je 0x17bef
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq -0x188(%rbp), %rdi
cmpq %rbx, %rdi
je 0x17c9b
movq -0x178(%rbp), %rsi
incq %rsi
callq 0x2a9a0
jmp 0x17c9b
leaq -0x5d0(%rip), %rbx      # 0x1764a
leaq -0x70(%rbp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
xorl %r12d, %r12d
jmp 0x17c3b
movq -0xb0(%rbp), %rbx
leaq -0x1d8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x1a58e
leaq -0x128(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq *%rbx
leaq -0x80(%rbp), %rdi
leaq -0x128(%rbp), %r15
movq %r15, %rsi
callq 0xa7590
movq (%r15), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
je 0x17c8f
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x2a9a0
leaq -0x1d8(%rbp), %rdi
callq 0x1a1ee
leaq -0x1c0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1a3c2
movq -0x1c0(%rbp), %rdi
movq -0x1b8(%rbp), %rax
cmpq %rax, %rdi
je 0x17ce2
xorl %ecx, %ecx
movq %rdi, %rdx
movq -0x38(%rbp), %r15
movl (%rdx), %esi
cmpl %ecx, %esi
cmovgl %esi, %ecx
addq $0x4, %rdx
cmpq %rax, %rdx
jne 0x17cca
cmpl $0x2, %ecx
setl %bl
jmp 0x17ce8
movb $0x1, %bl
movq -0x38(%rbp), %r15
testq %rdi, %rdi
je 0x17cfc
movq -0x1b0(%rbp), %rsi
subq %rdi, %rsi
callq 0x2a9a0
testb %bl, %bl
je 0x17d24
leaq 0x10(%r15), %rax
movq %rax, (%r15)
movq -0x80(%rbp), %rcx
cmpq %r13, %rcx
je 0x17df5
movq %rcx, (%r15)
movq -0x70(%rbp), %rax
movq %rax, 0x10(%r15)
jmp 0x17dfd
leaq -0x158(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x168(%rbp), %rdi
callq 0x1abf2
leaq -0x138(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
addq %rsi, %rdx
leaq -0x148(%rbp), %rdi
callq 0x1abf2
leaq -0x1a8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
leaq -0x148(%rbp), %r8
movl -0xa4(%rbp), %edx
movl %r12d, %ecx
callq 0x17677
leaq -0x1a8(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x17749
leaq -0x198(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17db9
movq -0x198(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq -0x148(%rbp), %rdi
cmpq %r14, %rdi
je 0x17dd4
movq -0x138(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq -0x168(%rbp), %rdi
cmpq %r15, %rdi
movq -0x38(%rbp), %r15
je 0x17e15
movq -0x158(%rbp), %rsi
incq %rsi
callq 0x2a9a0
jmp 0x17e15
movups (%r13), %xmm0
movups %xmm0, (%rax)
movq -0x78(%rbp), %rax
movq %rax, 0x8(%r15)
movq %r13, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movb $0x0, -0x70(%rbp)
movq -0x80(%rbp), %rdi
cmpq %r13, %rdi
je 0x17e2a
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2a9a0
leaq -0xd0(%rbp), %rdi
callq 0x1a1ee
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x17e51
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x2a9a0
leaq -0x208(%rbp), %rdi
callq 0x1a9f2
movq -0x108(%rbp), %rdi
leaq -0xf8(%rbp), %rax
cmpq %rax, %rdi
je 0x17e7f
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x17e98
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq %r15, %rax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movq -0x188(%rbp), %rdi
cmpq %rbx, %rdi
je 0x17f6b
movq -0x178(%rbp), %rsi
jmp 0x17f28
jmp 0x17f68
movq %rax, %r12
leaq -0x198(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17ef5
movq -0x198(%rbp), %rsi
incq %rsi
callq 0x2a9a0
jmp 0x17ef5
movq %rax, %r12
movq -0x148(%rbp), %rdi
cmpq %r14, %rdi
je 0x17f15
movq -0x138(%rbp), %rsi
incq %rsi
callq 0x2a9a0
jmp 0x17f15
movq %rax, %r12
movq -0x168(%rbp), %rdi
cmpq %r15, %rdi
je 0x17f6b
movq -0x158(%rbp), %rsi
incq %rsi
callq 0x2a9a0
jmp 0x17f6b
movq %rax, %r12
leaq -0x1d8(%rbp), %rdi
callq 0x1a1ee
jmp 0x17f6b
jmp 0x17f68
movq %rax, %r12
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x17f8d
movq -0xd8(%rbp), %rsi
subq %rdi, %rsi
jmp 0x17f7b
jmp 0x17f8a
jmp 0x17f8a
jmp 0x17f8a
jmp 0x17f8a
movq %rax, %r12
movq -0x80(%rbp), %rdi
cmpq %r13, %rdi
je 0x17f8d
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2a9a0
jmp 0x17f8d
jmp 0x17f8a
jmp 0x17f8a
jmp 0x17f8a
jmp 0x17f8a
movq %rax, %r12
leaq -0xd0(%rbp), %rdi
callq 0x1a1ee
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x17fb4
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x2a9a0
leaq -0x208(%rbp), %rdi
callq 0x1a9f2
movq -0x108(%rbp), %rdi
leaq -0xf8(%rbp), %rax
cmpq %rax, %rdi
je 0x17fe2
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x17ffb
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2a9a0
movq %r12, %rdi
callq 0xd5f80