feat: implement for-loops with proper scope management

- Add support for C-style for loops: for (init; condition; increment) { body }
- Implement three-level scoping: loop context + per-iteration contexts
- Add semicolon disabler for increment statements using RAII guards
- Support break/continue/return control flow with scope validation
- Fix semicolon handling in parser for flexible for-loop syntax
This commit is contained in:
2025-12-21 22:55:46 +08:00
parent acc2d33dbc
commit 014803b705
13 changed files with 327 additions and 59 deletions

View File

@@ -2,7 +2,7 @@
#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <memory>
#include <context_forward.hpp>
#include <fig_string.hpp>
@@ -10,7 +10,7 @@
namespace Fig
{
struct Context
class Context : public std::enable_shared_from_this<Context>
{
private:
FString scopeName;
@@ -145,6 +145,33 @@ namespace Fig
}
throw RuntimeError(FStringView(std::format("Variable '{}' not defined", name.toBasicString())));
}
bool isInFunctionContext()
{
ContextPtr ctx = shared_from_this();
while (ctx)
{
if (ctx->getScopeName().find(u8"<Function ") == 0)
{
return true;
}
ctx = ctx->parent;
}
return false;
}
bool isInLoopContext()
{
ContextPtr ctx = shared_from_this();
while (ctx)
{
if (ctx->getScopeName().find(u8"<While ") == 0 or
ctx->getScopeName().find(u8"<For ") == 0)
{
return true;
}
ctx = ctx->parent;
}
return false;
}
void printStackTrace(std::ostream &os = std::cerr, int indent = 0) const
{
const Context *ctx = this;