Skip to content

Commit

Permalink
fix: unification stmt eval scope
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Sep 10, 2024
1 parent e3792bc commit 235cfc9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 42 deletions.
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

0 comments on commit 235cfc9

Please sign in to comment.