Skip to content

Commit

Permalink
fix: evaluator schema scope context set value (#1356)
Browse files Browse the repository at this point in the history
* fix: evaluator schema scope context set value

Signed-off-by: peefy <[email protected]>

* chore: remove file.append grammar tests

Signed-off-by: peefy <[email protected]>

---------

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored May 24, 2024
1 parent 1a83c61 commit 7c20bc3
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 20 deletions.
20 changes: 16 additions & 4 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,10 +1208,22 @@ impl<'ctx> Evaluator<'ctx> {
// Lambda local variables.
} else if self.is_in_lambda() {
let value = right_value.clone().expect(kcl_error::INTERNAL_ERROR_MSG);
// If variable exists in the scope and update it, if not, add it to the scope.
if !self.store_variable_in_current_scope(name, value.clone()) {
self.add_variable(name, self.undefined_value());
self.store_variable(name, value);
// schema frame in the lambda
if self.is_schema_scope() {
let is_local_var = self.is_local_var(name);
let value = right_value.clone().expect(kcl_error::INTERNAL_ERROR_MSG);
match (is_local_var, is_in_schema) {
(false, true) => {
self.update_schema_or_rule_scope_value(name, Some(&value))
}
_ => self.add_variable(name, value),
}
} else {
// If variable exists in the scope and update it, if not, add it to the scope.
if !self.store_variable_in_current_scope(name, value.clone()) {
self.add_variable(name, self.undefined_value());
self.store_variable(name, value);
}
}
} else {
let is_local_var = self.is_local_var(name);
Expand Down
2 changes: 1 addition & 1 deletion kclvm/evaluator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ pub(crate) fn schema_body(
};
let schema_name = { ctx.borrow().node.name.node.to_string() };
s.push_schema(crate::EvalContext::Schema(ctx.clone()));
s.enter_scope();
s.enter_schema_scope(true);
// Evaluate arguments and keyword arguments and store values to local variables.
s.walk_arguments(&ctx.borrow().node.args, args, kwargs);
// Eval schema body and record schema instances.
Expand Down
28 changes: 25 additions & 3 deletions kclvm/evaluator/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Scope {
pub scalars: Vec<ValueRef>,
/// schema_scalar_idx denotes whether a schema exists in the scalar list.
pub schema_scalar_idx: usize,
/// is_schema denotes whether the scope is a schema.
pub is_schema: bool,
/// Scope normal variables
pub variables: IndexMap<String, ValueRef>,
/// Potential arguments in the current scope, such as schema/lambda arguments.
Expand Down Expand Up @@ -107,16 +109,36 @@ impl<'ctx> Evaluator<'ctx> {
scopes.len() - 1
}

/// Enter scope
pub(crate) fn enter_scope(&self) {
/// Get the scope level
pub(crate) fn is_schema_scope(&self) -> bool {
let current_pkgpath = self.current_pkgpath();
let pkg_scopes = &self.pkg_scopes.borrow();
let msg = format!("pkgpath {} is not found", current_pkgpath);
let scopes = pkg_scopes.get(&current_pkgpath).expect(&msg);
if let Some(last_scope) = scopes.last() {
last_scope.is_schema
} else {
false
}
}

/// Enter a schema scope
pub(crate) fn enter_schema_scope(&self, is_schema: bool) {
let current_pkgpath = self.current_pkgpath();
let pkg_scopes = &mut self.pkg_scopes.borrow_mut();
let msg = format!("pkgpath {} is not found", current_pkgpath);
let scopes = pkg_scopes.get_mut(&current_pkgpath).expect(&msg);
let scope = Scope::default();
let mut scope = Scope::default();
scope.is_schema = is_schema;
scopes.push(scope);
}

/// Enter scope
#[inline]
pub(crate) fn enter_scope(&self) {
self.enter_schema_scope(false);
}

/// Leave scope
pub(crate) fn leave_scope(&self) {
let current_pkgpath = self.current_pkgpath();
Expand Down
1 change: 0 additions & 1 deletion test/grammar/builtins/file/append/file_append.txt

This file was deleted.

5 changes: 0 additions & 5 deletions test/grammar/builtins/file/append/main.k

This file was deleted.

6 changes: 0 additions & 6 deletions test/grammar/builtins/file/append/stderr.golden

This file was deleted.

13 changes: 13 additions & 0 deletions test/grammar/schema/optional_attr/inherit_4/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
schema Name:
n1: str = "aa"
n2: str

schema Sub[a: str](Name):
n2 = a

schema Phase:
a: Sub

a = lambda {
[[Phase{a = Sub("a")}.a]][0][0] | {}
}()
3 changes: 3 additions & 0 deletions test/grammar/schema/optional_attr/inherit_4/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a:
n1: aa
n2: a
16 changes: 16 additions & 0 deletions test/grammar/schema/optional_attr/inherit_5/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
schema Name:
n1: str = "aa"
n2: str

schema Sub[a: str](Name):
n2 = a

schema Phase:
a: Sub

schema Schema:
f = lambda {
[[Phase{a = Sub("a")}.a]][0][0] | {}
}()

a = Schema{}
4 changes: 4 additions & 0 deletions test/grammar/schema/optional_attr/inherit_5/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a:
f:
n1: aa
n2: a
31 changes: 31 additions & 0 deletions test/grammar/schema/optional_attr/inherit_6/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
schema Name:
n1: str = "aa"
n2: str

schema Sub[a: str](Name):
n2 = a

schema Phase:
a: Sub

schema Schema:
f0: () = lambda {
[[Phase{a = Sub("a")}.a]][0][0]
}
f1: () = lambda {
[[Phase{a = Sub("a")}.a]][0][0] | {}
}
f2: () = lambda {
s = [[Phase{a = Sub("a")}.a]][0][0]
s | {}
}
ff: () -> () = lambda {
lambda {
[[Phase{a = Sub("a")}.a]][0] | [{}]
}
}

a = Schema().f0()
b = Schema().f1()
c = Schema().f2()
d = Schema().ff()()
12 changes: 12 additions & 0 deletions test/grammar/schema/optional_attr/inherit_6/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
a:
n1: aa
n2: a
b:
n1: aa
n2: a
c:
n1: aa
n2: a
d:
- n1: aa
n2: a
31 changes: 31 additions & 0 deletions test/grammar/schema/optional_attr/inherit_7/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
schema Name:
n1: str = "aa"
n2: str

schema Sub[a: str](Name):
n2: str = a

schema Phase:
a: Sub

schema Schema:
f0: () = lambda {
[[Phase{a = Sub("a")}.a]][0][0]
}
f1: () = lambda {
[[Phase{a = Sub("a")}.a]][0][0] | {}
}
f2: () = lambda {
s = [[Phase{a = Sub("a")}.a]][0][0]
s | {}
}
ff: () -> () = lambda {
lambda {
[[Phase{a = Sub("a")}.a]][0] | [{}]
}
}

a = Schema().f0()
b = Schema().f1()
c = Schema().f2()
d = Schema().ff()()
12 changes: 12 additions & 0 deletions test/grammar/schema/optional_attr/inherit_7/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
a:
n1: aa
n2: a
b:
n1: aa
n2: a
c:
n1: aa
n2: a
d:
- n1: aa
n2: a

0 comments on commit 7c20bc3

Please sign in to comment.