From e1c765aadea9007b4c24fe652eedaa409e725483 Mon Sep 17 00:00:00 2001 From: PuqiAR Date: Sun, 8 Feb 2026 09:18:31 +0800 Subject: [PATCH] =?UTF-8?q?For=E5=BE=AA=E7=8E=AF=E7=95=A5=E5=BE=AE?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=8F=90=E5=8D=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExampleCodes/SpeedTest/fib.py | 2 +- src/Evaluator/Context/context.hpp | 7 +++++++ src/Evaluator/Core/EvalStatement.cpp | 17 ++++++++++++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ExampleCodes/SpeedTest/fib.py b/ExampleCodes/SpeedTest/fib.py index 0d1d315..88d9d7a 100644 --- a/ExampleCodes/SpeedTest/fib.py +++ b/ExampleCodes/SpeedTest/fib.py @@ -6,6 +6,6 @@ def fib(x:int) -> int: if __name__ == '__main__': t0 = tt() - result = fib(25) + result = fib(30) t1 = tt() print('cost: ',t1-t0, 'result:', result) diff --git a/src/Evaluator/Context/context.hpp b/src/Evaluator/Context/context.hpp index ac4762a..6317c78 100644 --- a/src/Evaluator/Context/context.hpp +++ b/src/Evaluator/Context/context.hpp @@ -80,6 +80,13 @@ namespace Fig // c.structTypeNames.end()); } + void clear() + { + variables.clear(); + implRegistry.clear(); + opRegistry.clear(); + } + std::unordered_map getFunctions() const { std::unordered_map result; diff --git a/src/Evaluator/Core/EvalStatement.cpp b/src/Evaluator/Core/EvalStatement.cpp index 629b2be..e2399e0 100644 --- a/src/Evaluator/Core/EvalStatement.cpp +++ b/src/Evaluator/Core/EvalStatement.cpp @@ -327,7 +327,7 @@ namespace Fig FString(std::format("", opFnName.toBasicString())), ctx); const auto &fillOpFnParas = [this, structType, implMethod, opFnName, fnCtx, ctx, paraCnt]( - const std::vector &args) { + const std::vector &args) -> StatementResult { const Ast::FunctionParameters ¶s = implMethod.paras; for (size_t i = 0; i < paraCnt; ++i) { @@ -345,6 +345,7 @@ namespace Fig } fnCtx->def(paras.posParas[i].first, paraType, AccessModifier::Normal, args[i]); } + return StatementResult::normal(); }; if (paraCnt == 1) @@ -536,6 +537,11 @@ namespace Fig loopContext); // ignore init statement result size_t iteration = 0; + ContextPtr iterationContext = std::make_shared( + FString(std::format( + "", forSt->getAAI().line, forSt->getAAI().column, iteration)), + loopContext); // every loop has its own context + while (true) // use while loop to simulate for loop, cause we // need to check condition type every iteration { @@ -549,11 +555,12 @@ namespace Fig } if (!condVal->as()) { break; } iteration++; - ContextPtr iterationContext = std::make_shared( - FString(std::format( - "", forSt->getAAI().line, forSt->getAAI().column, iteration)), - loopContext); // every loop has its own context + StatementResult sr = evalBlockStatement(forSt->body, iterationContext); + iterationContext->clear(); + iterationContext->setScopeName(FString(std::format( + "", forSt->getAAI().line, forSt->getAAI().column, iteration))); + if (sr.shouldReturn()) { return sr; } if (sr.shouldBreak()) { break; } if (sr.shouldContinue())