Skip to content

Commit

Permalink
feat: support rule statement with index signature (#920)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Nov 27, 2023
1 parent 6245770 commit 7eb2aa2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
24 changes: 22 additions & 2 deletions kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use kclvm_ast::ast::{self, CallExpr, ConfigEntry, NodeRef};
use kclvm_ast::walker::TypedResultWalker;
use kclvm_runtime::{ApiFunc, PKG_PATH_PREFIX};
use kclvm_sema::pkgpath_without_prefix;
use kclvm_sema::ty::{ANY_TYPE_STR, STR_TYPE_STR};

use crate::check_backtrack_stop;
use crate::codegen::error as kcl_error;
Expand Down Expand Up @@ -1087,7 +1088,8 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
);
let fn_ty = self.function_type().ptr_type(AddressSpace::default());
let func_ptr_cast = self.builder.build_bitcast(func_ptr, fn_ty, "");
self.builder
let schema_value = self
.builder
.build_call(
CallableValue::try_from(func_ptr_cast.into_pointer_value())
.expect(kcl_error::INTERNAL_ERROR_MSG),
Expand All @@ -1100,7 +1102,25 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
)
.try_as_basic_value()
.left()
.expect(kcl_error::FUNCTION_RETURN_VALUE_NOT_FOUND_MSG)
.expect(kcl_error::FUNCTION_RETURN_VALUE_NOT_FOUND_MSG);
let protocol_name_native_str =
self.native_global_string_value(&for_host_name.node.get_name());
self.build_void_call(
&ApiFunc::kclvm_schema_value_check.name(),
&[
self.current_runtime_ctx_ptr(),
schema_value,
schema_config,
schema_config_meta,
protocol_name_native_str,
self.undefined_value(),
self.native_global_string("", "").into(),
self.native_global_string(STR_TYPE_STR, "").into(),
self.native_global_string(ANY_TYPE_STR, "").into(),
self.native_i8(1).into(),
],
);
schema_value
} else {
schema_value
};
Expand Down
6 changes: 5 additions & 1 deletion kclvm/sema/src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,10 @@ impl<'ctx> Resolver<'ctx> {
.map(|doc| doc.node.clone())
.unwrap_or_default(),
);
let index_signature = match &protocol_ty {
Some(ty) => ty.index_signature.clone(),
None => None,
};
SchemaType {
name: rule_stmt.name.node.clone(),
pkgpath: self.ctx.pkgpath.clone(),
Expand All @@ -933,7 +937,7 @@ impl<'ctx> Resolver<'ctx> {
is_variadic: false,
kw_only_index: None,
}),
index_signature: None,
index_signature,
decorators,
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/grammar/schema/rule/rule_with_index_signature/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
protocol XProtocol:
[...str]: str
alice: str

rule XRule for XProtocol:
alice == "Alice", "expected Alice, got ${alice}"
bob == "Bob", "expected Bob, got ${bob}"

p = XProtocol {
alice = "Alice"
bob = "Bob"
}

x = XRule {
alice = "Alice"
bob = "Bob"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
p:
alice: Alice
bob: Bob
x:
alice: Alice
bob: Bob

0 comments on commit 7eb2aa2

Please sign in to comment.