Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
commit
0a1a288df6
@ -79,6 +79,14 @@ void Memory::update(string identifier, EvalResult value) {
|
||||
scope.vars[identifier].initialized = true;
|
||||
return;
|
||||
}
|
||||
if (scope.type == ScopeType::Function) {
|
||||
Closure closure = std::get<1>(std::get<Function>(scope.fn->value));
|
||||
if (closure.contains(identifier)) {
|
||||
MemoryVar& var = closure.at(identifier);
|
||||
var.value = value;
|
||||
var.initialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw exception();
|
||||
|
@ -110,5 +110,47 @@ int main() {
|
||||
true
|
||||
);
|
||||
|
||||
_TEST_ASSERT(
|
||||
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
||||
int x = 1;
|
||||
int a() {
|
||||
return x;
|
||||
}
|
||||
int b() {
|
||||
int x = 2;
|
||||
return a();
|
||||
}
|
||||
b();
|
||||
)")) == 1),
|
||||
"Closure capture",
|
||||
true
|
||||
);
|
||||
|
||||
_TEST_ASSERT(
|
||||
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
||||
int x = 1;
|
||||
int a() {
|
||||
return x;
|
||||
}
|
||||
x = 2;
|
||||
a();
|
||||
)")) == 2),
|
||||
"Closure mutation externe",
|
||||
true
|
||||
);
|
||||
|
||||
_TEST_ASSERT(
|
||||
_TEST_NO_EXCEPTION(get<int>(execute(R"(
|
||||
int x = 1;
|
||||
void a() {
|
||||
x = 2;
|
||||
}
|
||||
a();
|
||||
x;
|
||||
)")) == 2),
|
||||
"Closure mutation interne",
|
||||
true
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user