Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unification stmt eval scope #1638

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 11 additions & 42 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,48 +78,17 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
self.add_target_var(name);
// The right value of the unification_stmt is a schema_expr.
let value = self.walk_schema_expr(&unification_stmt.value.node)?;
if self.scope_level() == GLOBAL_LEVEL || self.is_in_lambda() {
if self.resolve_variable(name) {
let mut org_value = self.walk_identifier_with_ctx(
&unification_stmt.target.node,
&ast::ExprContext::Load,
None,
)?;
let value = org_value.bin_aug_bit_or(&mut self.runtime_ctx.borrow_mut(), &value);
// Store the identifier value
self.walk_identifier_with_ctx(
&unification_stmt.target.node,
&ast::ExprContext::Store,
Some(value.clone()),
)?;
return Ok(value.clone());
} else {
self.walk_identifier_with_ctx(
&unification_stmt.target.node,
&unification_stmt.target.node.ctx,
Some(value.clone()),
)?;
return Ok(value);
}
// Local variables including schema/rule/lambda
} else if self.is_in_schema() {
// Load the identifier value
let org_value = self
.walk_identifier_with_ctx(
&unification_stmt.target.node,
&ast::ExprContext::Load,
None,
)
.unwrap_or(self.undefined_value());
let value = self.bit_or(org_value, value);
// Store the identifier value
self.walk_identifier_with_ctx(
&unification_stmt.target.node,
&ast::ExprContext::Store,
Some(value.clone()),
)?;
return Ok(value);
}
// Load the identifier value
let org_value = self
.walk_identifier_with_ctx(&unification_stmt.target.node, &ast::ExprContext::Load, None)
.unwrap_or(self.undefined_value());
let value = self.bit_or(org_value, value);
// Store the identifier value
self.walk_identifier_with_ctx(
&unification_stmt.target.node,
&ast::ExprContext::Store,
Some(value.clone()),
)?;
self.pop_target_var();
Ok(value)
}
Expand Down
62 changes: 62 additions & 0 deletions test/grammar/schema/config_op/union/union_3/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
schema ResourceMapping:
[str]: any

schema BaseFrontend:
name: str

protocol MixinProtocol:
count: int
resources: ResourceMapping

mixin Mixin1Mixin for MixinProtocol:
resources: ResourceMapping {
resource1 += [{
a = "a"
}]
}
count += 1

mixin Mixin2Mixin for MixinProtocol:
resources: ResourceMapping {
resource2 += [{
b = "b"
}]
}
count += 1

mixin Mixin3Mixin for MixinProtocol:
resources: ResourceMapping {
resource3 += [{
c = "c"
}]
}
count += 1

schema BaseBackend[a: BaseFrontend]:
mixin [
Mixin1Mixin,
Mixin2Mixin,
Mixin3Mixin,
Mixin3Mixin,
]
resources: ResourceMapping
count: int = 0

render = lambda a: BaseFrontend {
impl = BaseBackend(a)
impl
}
frontEnd = BaseFrontend {name = "app"}
res1 = [render(a) for a in BaseFrontend.instances()]
res2 = [BaseBackend(a) for a in BaseFrontend.instances()]
resources: ResourceMapping {}
resources: ResourceMapping {
resource1 += [{
a = "a"
}]
}
resources: ResourceMapping {
resource2 += [{
b = "b"
}]
}
27 changes: 27 additions & 0 deletions test/grammar/schema/config_op/union/union_3/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
frontEnd:
name: app
res1:
- resources:
resource1:
- a: a
resource2:
- b: b
resource3:
- c: c
- c: c
count: 4
res2:
- resources:
resource1:
- a: a
resource2:
- b: b
resource3:
- c: c
- c: c
count: 4
resources:
resource1:
- a: a
resource2:
- b: b
Loading