Fixes and functions

This commit is contained in:
2025-10-01 13:43:08 +10:00
parent d137a623ed
commit 2a977707ee
8 changed files with 97 additions and 6 deletions

View File

@@ -137,9 +137,42 @@ std::vector<Instruction> parse(std::string program) {
std::string then_content = line.substr(block_start + 1, block_end - block_start - 1);
while_inst.args.push_back(Value(parse(then_content))); // Recursive call
instructions.push_back(while_inst);
} else if (line.rfind("fun", 0) == 0) {
Instruction function_inst;
function_inst.instruction = InstructionType::Function;
size_t block_start = line.find('{');
if (block_start == std::string::npos) {
error("Expected '{' for function statement");
continue;
}
// 1. Function name
std::string name = line.substr(3, block_start - 3);
function_inst.args.push_back(Value(Instruction(split(trim(name)))));
// 2. Find 'then' block
size_t block_end = 0;
int brace_level = 0;
for (size_t i = block_start; i < line.length(); ++i) {
if (line[i] == '{') brace_level++;
else if (line[i] == '}') brace_level--;
if (brace_level == 0) {
block_end = i;
break;
}
}
if (block_end == 0) {
error("Mismatched braces in function statement");
continue;
}
std::string then_content = line.substr(block_start + 1, block_end - block_start - 1);
function_inst.args.push_back(Value(parse(then_content))); // Recursive call
instructions.push_back(function_inst);
} else {
instructions.push_back(Instruction(split(line)));
}
}
return instructions;
}
}