// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
leaq 0x109440(%rip), %rdi    # 0x120960
leaq 0xbfc0a(%rip), %rsi     # 0xd7131
callq 0x94210
movq %rax, %rdi
movq %r14, %rsi
callq 0xa6ca0
movq %rax, %rdi
callq 0x93af0
leaq -0x90(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ebx, %ebx
movb %bl, (%rax)
leaq -0x108(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %bl, (%rax)
leaq -0x2c(%rbp), %r15
andl $0x0, (%r15)
leaq -0x140(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq -0xc0(%rbp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
andq $0x0, -0x70(%rbp)
movaps %xmm0, (%rax)
movaps %xmm0, -0x80(%rbp)
xorl %r12d, %r12d
cmpq %rbx, 0x8(%r14)
jbe 0x1762c
movq %r14, %r13
movq (%r14), %rax
movzbl (%rax,%rbx), %eax
cmpl $0x22, %eax
sete %r14b
cmpl $0x5b, %eax
je 0x175d5
cmpl $0x5d, %eax
jne 0x175f2
testb $0x1, %r12b
jne 0x175f2
movl -0x2c(%rbp), %eax
decl %eax
jmp 0x175e0
testb $0x1, %r12b
jne 0x175f2
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
leaq -0xc0(%rbp), %rdi
movq %r15, %rsi
callq 0x18d7e
leaq -0x148(%rbp), %rdi
movq %r15, %rsi
callq 0x23c34
leaq -0x140(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1761e
leaq -0x148(%rbp), %rdi
movq %r15, %rsi
callq 0x18da4
movl %ebx, (%rax)
xorb %r14b, %r12b
incq %rbx
movq %r13, %r14
jmp 0x175a7
testb $0x1, %r12b
je 0x1764e
leaq 0xbfafe(%rip), %rsi     # 0xd7137
leaq -0x58(%rbp), %rdx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x18c74
jmp 0x17a35
movq %r14, %rbx
cmpl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %r14
je 0x17673
leaq 0xbfb10(%rip), %rsi     # 0xd7172
leaq -0x58(%rbp), %rdx
movq %r14, %rdi
callq 0x18c74
jmp 0x17a35
leaq -0xf8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x18e48
leaq -0xf8(%rbp), %rdi
callq 0x17425
leaq -0x58(%rbp), %rsi
movl %eax, (%rsi)
leaq -0x148(%rbp), %rdi
callq 0x18df6
movq %rbx, -0x60(%rbp)
movslq (%rax), %rbx
leaq -0xf8(%rbp), %rdi
callq 0x23a9c
leaq -0x278(%rip), %rax      # 0x17446
movq %rax, -0x68(%rbp)
xorl %r15d, %r15d
movq %rbx, -0xc8(%rbp)
movl $0x0, -0x30(%rbp)
movl %r12d, %r14d
incq %rbx
leaq -0xa0(%rbp), %r12
movl %r15d, %r13d
movq -0x60(%rbp), %rax
cmpq %rbx, 0x8(%rax)
jbe 0x1786d
movq (%rax), %rcx
movzbl (%rcx,%rbx), %eax
cmpl $0x5d, %eax
setne %dl
orb %r14b, %dl
leal 0x1(%rbx), %r15d
testb $0x1, %dl
cmovnel %r13d, %r15d
cmpl $0x20, %eax
je 0x17724
cmpl $0x22, %eax
je 0x177e1
testb $0x1, %dl
movl %r14d, %r13d
jne 0x17737
jmp 0x17747
movl %r14d, %esi
testb $0x1, %r14b
jne 0x17737
movl %esi, %r13d
cmpb $0x22, -0x1(%rcx,%rbx)
jne 0x17747
incq %rbx
movsbl %al, %esi
movq %r12, %rdi
callq 0xa4c00
jmp 0x176e0
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
addq %rdi, %rsi
movb $0x20, -0x58(%rbp)
leaq -0x58(%rbp), %rdx
callq 0x23fef
leaq 0x108184(%rip), %rdi    # 0x11f8f0
leaq -0xa0(%rbp), %r12
movq %r12, %rsi
callq 0x240be
leaq 0x108176(%rip), %rcx    # 0x11f8f8
cmpq %rcx, %rax
sete %al
orb %r13b, %al
testb $0x1, %al
je 0x177f6
leaq 0x10818a(%rip), %rdi    # 0x11f920
movq %r12, %rsi
callq 0x2450e
leaq 0x108183(%rip), %rcx    # 0x11f928
cmpq %rcx, %rax
sete %al
orb %r13b, %al
testb $0x1, %al
je 0x1780e
leaq 0x1081af(%rip), %rdi    # 0x11f968
movq %r12, %rsi
callq 0x2450e
leaq 0x1081a8(%rip), %rcx    # 0x11f970
cmpq %rcx, %rax
sete %al
orb %r13b, %al
testb $0x1, %al
je 0x17824
cmpq $0x0, -0x98(%rbp)
jne 0x17836
jmp 0x17842
movb $0x1, %r12b
movl %r14d, %r13d
testb $0x1, %r14b
je 0x176d3
jmp 0x17765
leaq 0x1080f3(%rip), %rdi    # 0x11f8f0
movq %r12, %rsi
callq 0x18ea0
movq (%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x17842
movb $0x1, %al
movl %eax, -0x30(%rbp)
leaq -0x118(%rbp), %rdi
movq %r12, %rsi
callq 0xa41a0
jmp 0x17842
leaq 0x10813d(%rip), %rdi    # 0x11f968
movq %r12, %rsi
callq 0x18f46
movq %rax, %r12
leaq -0x80(%rbp), %rdi
movq %r12, %rsi
callq 0x18f0a
movq -0x60(%rbp), %rax
movq (%rax), %rax
cmpb $0x5d, (%rax,%rbx)
je 0x1786a
xorl %r12d, %r12d
leaq -0xa0(%rbp), %rdi
leaq 0xbfb0c(%rip), %rsi     # 0xd736c
callq 0xa5520
jmp 0x176d3
movl %r15d, %r13d
leaq -0x48(%rbp), %rbx
movq %rbx, -0x10(%rbx)
andq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
testb $0x1, -0x30(%rbp)
je 0x178f1
leaq 0x1080c6(%rip), %rdi    # 0x11f950
leaq -0x80(%rbp), %rsi
callq 0x19004
leaq 0x108086(%rip), %rdi    # 0x11f920
leaq -0x118(%rbp), %rsi
callq 0x18f46
leaq -0x1c8(%rbp), %rdi
movq %rax, %rsi
callq 0xa6a00
leaq -0x1e8(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x174fe
leaq -0x58(%rbp), %rdi
leaq -0x1e8(%rbp), %r12
movq %r12, %rsi
callq 0xa45c0
movq %r12, %rdi
callq 0xa4570
leaq -0x1c8(%rbp), %rdi
callq 0xa4570
jmp 0x17939
leaq -0xe0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x18fb0
leaq -0x1e8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq *-0x68(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x1e8(%rbp), %r12
movq %r12, %rsi
callq 0xa45c0
movq %r12, %rdi
callq 0xa4570
leaq -0xe0(%rbp), %rdi
callq 0x18cc4
leaq -0x200(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x18e48
leaq -0x200(%rbp), %r12
movq %r12, %rdi
callq 0x17425
movl %eax, %r15d
movq %r12, %rdi
callq 0x23a9c
cmpl $0x1, %r15d
jg 0x17994
movq -0x38(%rbp), %r14
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq -0x58(%rbp), %rcx
cmpq %rbx, %rcx
je 0x17a11
movq %rcx, (%r14)
movq -0x48(%rbp), %rax
movq %rax, 0x10(%r14)
jmp 0x17a17
leaq -0x188(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0xa6a00
leaq -0x168(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xa6a00
leaq -0x1a8(%rbp), %rdi
leaq -0x188(%rbp), %rsi
leaq -0x168(%rbp), %r8
movq -0xc8(%rbp), %rdx
movl %r13d, %ecx
callq 0x17469
movq -0x38(%rbp), %r14
leaq -0x1a8(%rbp), %rsi
movq %r14, %rdi
callq 0x174fe
leaq -0x1a8(%rbp), %rdi
callq 0xa4570
leaq -0x168(%rbp), %rdi
callq 0xa4570
leaq -0x188(%rbp), %rdi
callq 0xa4570
jmp 0x17a2c
movups (%rbx), %xmm0
movups %xmm0, (%rax)
movq -0x50(%rbp), %rax
movq %rax, 0x8(%r14)
movq %rbx, -0x58(%rbp)
andq $0x0, -0x50(%rbp)
movb $0x0, -0x48(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xa4570
leaq -0x80(%rbp), %rdi
callq 0x18cc4
leaq -0xc0(%rbp), %rdi
callq 0x23a9c
leaq -0x148(%rbp), %rdi
callq 0x196ee
leaq -0x118(%rbp), %rdi
callq 0xa4570
leaq -0xa0(%rbp), %rdi
callq 0xa4570
movq %r14, %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17b10
jmp 0x17b10
movq %rax, %rbx
leaq -0x1a8(%rbp), %rdi
callq 0xa4570
jmp 0x17aa1
movq %rax, %rbx
leaq -0x168(%rbp), %rdi
callq 0xa4570
jmp 0x17ab2
movq %rax, %rbx
leaq -0x188(%rbp), %rdi
jmp 0x17ac5
movq %rax, %rbx
leaq -0x1c8(%rbp), %rdi
callq 0xa4570
jmp 0x17aff
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x18cc4
jmp 0x17aff
jmp 0x17afc
jmp 0x17b10
jmp 0x17b10
jmp 0x17afc
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x23a9c
jmp 0x17b13
jmp 0x17b10
jmp 0x17b10
jmp 0x17b10
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0xa4570
jmp 0x17b13
jmp 0x17b10
jmp 0x17b10
jmp 0x17b10
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x18cc4
leaq -0xc0(%rbp), %rdi
callq 0x23a9c
leaq -0x148(%rbp), %rdi
callq 0x196ee
leaq -0x118(%rbp), %rdi
callq 0xa4570
leaq -0xa0(%rbp), %rdi
callq 0xa4570
movq %rbx, %rdi
callq 0xd3a40