Skip to content

Commit

Permalink
fix: lambda in schema fucntion calling for the evaluator
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Apr 20, 2024
1 parent a02d6b0 commit 5815a73
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-test-macos-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ jobs:
working-directory: ./kclvm
run: export PATH=$PATH:$PWD/../_build/dist/Darwin/kclvm/bin:/opt/homebrew/opt/llvm@12/bin/ && make && make test-grammar
shell: bash
- name: Evaluator Grammar test
working-directory: ./kclvm
run: export PATH=$PATH:$PWD/../_build/dist/Darwin/kclvm/bin:/opt/homebrew/opt/llvm@12/bin/ && make test-grammar-evaluator
shell: bash

- uses: actions/upload-artifact@v3
with:
Expand Down
16 changes: 14 additions & 2 deletions kclvm/evaluator/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ use generational_arena::Index;
use kclvm_ast::ast;
use kclvm_runtime::ValueRef;

use crate::error as kcl_error;
use crate::proxy::Proxy;
use crate::Evaluator;
use crate::{error as kcl_error, EvalContext};

pub type FunctionHandler =
Arc<dyn Fn(&Evaluator, &FunctionEvalContext, &ValueRef, &ValueRef) -> ValueRef>;

#[derive(Clone)]
pub struct FunctionEvalContext {
pub node: ast::LambdaExpr,
pub this: Option<EvalContext>,
}

/// Proxy functions represent the saved functions of the runtime itself,
Expand Down Expand Up @@ -59,7 +60,18 @@ impl<'ctx> Evaluator<'ctx> {
self.push_pkgpath(&frame.pkgpath);
self.push_backtrace(&frame);
let value = match &frame.proxy {
Proxy::Lambda(lambda) => (lambda.body)(self, &lambda.ctx, args, kwargs),
Proxy::Lambda(lambda) => {
// Capture function schema this reference.
if let Some(this) = lambda.ctx.this.clone() {
self.push_schema(this);
}
let value = (lambda.body)(self, &lambda.ctx, args, kwargs);
// Release function schema this reference.
if lambda.ctx.this.is_some() {
self.pop_schema()
}
value
}
Proxy::Schema(schema) => (schema.body)(
self,
&schema
Expand Down
1 change: 1 addition & 0 deletions kclvm/evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct Evaluator<'ctx> {
pub backtrack_meta: RefCell<Vec<BacktrackMeta>>,
}

#[derive(Clone)]
pub enum EvalContext {
Schema(SchemaEvalContextRef),
Rule(RuleEvalContextRef),
Expand Down
2 changes: 2 additions & 0 deletions kclvm/evaluator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,9 +1000,11 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {

fn walk_lambda_expr(&self, lambda_expr: &'ctx ast::LambdaExpr) -> Self::Result {
let func = Arc::new(func_body);
// Capture schema self
let proxy = FunctionCaller::new(
FunctionEvalContext {
node: lambda_expr.clone(),
this: self.schema_stack.borrow().last().cloned(),
},
func,
);
Expand Down
22 changes: 22 additions & 0 deletions test/grammar/lambda/in_schema_10/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
schema Job:
name: str

createResource: () -> CronJob = lambda {
lambda {
CronJob {
name = name
}
}()
}
getName: () -> str = lambda {
lambda {name}()
}

schema CronJob:
name?: str

myJob = Job {
name = "myJob"
}
myCronJob = myJob.createResource()
name = myJob.getName()
5 changes: 5 additions & 0 deletions test/grammar/lambda/in_schema_10/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
myJob:
name: myJob
myCronJob:
name: myJob
name: myJob
20 changes: 20 additions & 0 deletions test/grammar/lambda/in_schema_9/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
schema Job:
name: str

createResource: () -> CronJob = lambda {
CronJob {
name = name
}
}
getName: () -> str = lambda {
name
}

schema CronJob:
name?: str

myJob = Job {
name = "myJob"
}
myCronJob = myJob.createResource()
name = myJob.getName()
5 changes: 5 additions & 0 deletions test/grammar/lambda/in_schema_9/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
myJob:
name: myJob
myCronJob:
name: myJob
name: myJob

0 comments on commit 5815a73

Please sign in to comment.