Skip to content

Commit

Permalink
feat: add isnullish builtin function
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 27, 2024
1 parent d02868d commit eed9922
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 0 deletions.
Binary file modified kclvm/runtime/src/_kclvm.bc
Binary file not shown.
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ kclvm_value_ref_t* kclvm_builtin_hex(kclvm_context_t* ctx, kclvm_value_ref_t* ar

kclvm_value_ref_t* kclvm_builtin_int(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_builtin_isnullish(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_builtin_isunique(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_builtin_len(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.ll
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ declare %kclvm_value_ref_t* @kclvm_builtin_hex(%kclvm_context_t* %ctx, %kclvm_va

declare %kclvm_value_ref_t* @kclvm_builtin_int(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_builtin_isnullish(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_builtin_isunique(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_builtin_len(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down
1 change: 1 addition & 0 deletions kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum ApiFunc {
kclvm_builtin_float,
kclvm_builtin_hex,
kclvm_builtin_int,
kclvm_builtin_isnullish,
kclvm_builtin_isunique,
kclvm_builtin_len,
kclvm_builtin_list,
Expand Down
1 change: 1 addition & 0 deletions kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_builtin_float" => crate::kclvm_builtin_float as *const () as u64,
"kclvm_builtin_hex" => crate::kclvm_builtin_hex as *const () as u64,
"kclvm_builtin_int" => crate::kclvm_builtin_int as *const () as u64,
"kclvm_builtin_isnullish" => crate::kclvm_builtin_isnullish as *const () as u64,
"kclvm_builtin_isunique" => crate::kclvm_builtin_isunique as *const () as u64,
"kclvm_builtin_len" => crate::kclvm_builtin_len as *const () as u64,
"kclvm_builtin_list" => crate::kclvm_builtin_list as *const () as u64,
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,10 @@
// api-spec(c): kclvm_value_ref_t* kclvm_builtin_range(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_builtin_range(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

// api-spec: kclvm_builtin_isnullish
// api-spec(c): kclvm_value_ref_t* kclvm_builtin_isnullish(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_builtin_isnullish(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

// api-spec: kclvm_plugin_init
// api-spec(c): void kclvm_plugin_init(void* fn_ptr);
// api-spec(llvm): declare void @kclvm_plugin_init(i8* %fn_ptr);
Expand Down
18 changes: 18 additions & 0 deletions kclvm/runtime/src/stdlib/builtin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,21 @@ pub unsafe extern "C" fn kclvm_builtin_range(
_ => kclvm_value_Undefined(ctx),
}
}

/// Return `True` if the input value is `None` or `Undefined`, and `False` otherwise.
#[no_mangle]
#[runtime_fn]
pub unsafe extern "C" fn kclvm_builtin_isnullish(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *mut kclvm_value_ref_t {
let ctx = mut_ptr_as_ref(ctx);
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);

if let Some(arg0) = get_call_arg(args, kwargs, 0, Some("inval")) {
return ValueRef::bool(arg0.is_none_or_undefined()).into_raw(ctx);
}
panic!("is_nullable() takes exactly one argument (0 given)");
}
15 changes: 15 additions & 0 deletions kclvm/sema/src/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ end: string appended after the last value, default a newline."#,
false,
None,
)
isnullish => Type::function(
None,
Arc::new(Type::BOOL),
&[
Parameter {
name: "inval".to_string(),
ty: Type::any_ref(),
has_default: false,
range: dummy_range(),
},
],
"Return `True` if the input value is `None` or `Undefined`, and `False` otherwise.",
false,
None,
)
len => Type::function(
None,
Arc::new(Type::INT),
Expand Down
14 changes: 14 additions & 0 deletions test/grammar/builtins/default/isnullable/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a = [100, 10, 100]
b = [100, 10]
c = ["Hello", "world", "world"]
d = ["Hello", "world"]
e = None
f = Undefined
g = [None]
A = isnullish(a)
B = isnullish(b)
C = isnullish(c)
D = isnullish(d)
E = isnullish(e)
F = isnullish(f)
G = isnullish(g)
24 changes: 24 additions & 0 deletions test/grammar/builtins/default/isnullable/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
a:
- 100
- 10
- 100
b:
- 100
- 10
c:
- Hello
- world
- world
d:
- Hello
- world
e: null
g:
- null
A: false
B: false
C: false
D: false
E: true
F: true
G: false

0 comments on commit eed9922

Please sign in to comment.