From d7123aff1e22b2e1f0d923b19111fbe5b119c186 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Thu, 2 Oct 2025 12:51:15 +1000 Subject: [PATCH] Return --- src/defs/defs.cpp | 1 + src/defs/defs.h | 3 ++- src/executor/executor.cpp | 14 ++++++++++++++ src/modules/if/if.cpp | 6 ++++++ src/modules/while/while.cpp | 3 +++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/defs/defs.cpp b/src/defs/defs.cpp index f6a4734..ba7f042 100644 --- a/src/defs/defs.cpp +++ b/src/defs/defs.cpp @@ -15,6 +15,7 @@ InstructionType strToInstructionType(std::string in) { else if (in == "while") return InstructionType::While; else if (in == "compare") return InstructionType::Compare; else if (in == "input") return InstructionType::Input; + else if (in == "return") return InstructionType::Return; else return InstructionType::Variable; } diff --git a/src/defs/defs.h b/src/defs/defs.h index b3cf323..099ba71 100644 --- a/src/defs/defs.h +++ b/src/defs/defs.h @@ -6,7 +6,7 @@ enum class InstructionType { - None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function + None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Return }; enum class ValueType { @@ -33,6 +33,7 @@ struct Value { std::unique_ptr processed; std::string real = ""; InstructionGroup instructionGroup; + bool is_return = false; std::string toString() const; Varname varName = Varname(); Value(std::string stringval); diff --git a/src/executor/executor.cpp b/src/executor/executor.cpp index e4f90fb..5f60e87 100644 --- a/src/executor/executor.cpp +++ b/src/executor/executor.cpp @@ -89,10 +89,14 @@ Value execute(Instruction inst) { Value return_val; for (const auto& body_inst : func.body) { return_val = execute(body_inst); + if (return_val.is_return) { + break; + } } data::popScope(); + return_val.is_return = false; return return_val; } } @@ -104,6 +108,16 @@ Value execute(Instruction inst) { } break; } + case InstructionType::Return: { + if (inst.args.empty()) { + Value val; + val.is_return = true; + return val; + } + Value return_val = inst.args[0]; + return_val.is_return = true; + return return_val; + } default: // Note: 'If' and 'Let' are already handled. error("Unknown instruction"); diff --git a/src/modules/if/if.cpp b/src/modules/if/if.cpp index a7e9a57..fbed957 100644 --- a/src/modules/if/if.cpp +++ b/src/modules/if/if.cpp @@ -30,12 +30,18 @@ Value modules::ifs(std::vector args) { if (is_true) { for (const auto& inst : args[1].instructionGroup) { return_val = execute(inst); + if (return_val.is_return) { + return return_val; + } } } else { if (args.size() > 2) { if (args[2].valtype == ValueType::InstructionGroup) { for (const auto& inst : args[2].instructionGroup) { return_val = execute(inst); + if (return_val.is_return) { + return return_val; + } } } else { error("Syntax error: 'else' body must be an instruction group"); diff --git a/src/modules/while/while.cpp b/src/modules/while/while.cpp index 3f7e44f..22b451c 100644 --- a/src/modules/while/while.cpp +++ b/src/modules/while/while.cpp @@ -30,6 +30,9 @@ Value modules::whiles(std::vector args) { while (is_true) { for (const auto& inst : args[1].instructionGroup) { return_val = execute(inst); + if (return_val.is_return) { + return return_val; + } } condition_result = execute(*args[0].processed);