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 6b4458b commit f9d77dd
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 67 deletions.
6 changes: 3 additions & 3 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ impl KclvmServiceImpl {
/// assert_eq!(result.type_errors.len(), 0);
/// assert_eq!(result.symbols.len(), 12);
/// assert_eq!(result.scopes.len(), 3);
/// assert_eq!(result.node_symbol_map.len(), 180);
/// assert_eq!(result.symbol_node_map.len(), 180);
/// assert_eq!(result.fully_qualified_name_map.len(), 190);
/// assert_eq!(result.node_symbol_map.len(), 181);
/// assert_eq!(result.symbol_node_map.len(), 181);
/// assert_eq!(result.fully_qualified_name_map.len(), 191);
/// assert_eq!(result.pkg_scope_map.len(), 3);
/// ```
#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -291,6 +284,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down
42 changes: 21 additions & 21 deletions kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_1.snap
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -443,6 +436,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -482,13 +482,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -685,6 +678,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -724,13 +724,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -927,6 +920,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down
42 changes: 21 additions & 21 deletions kclvm/loader/src/snapshots/kclvm_loader__tests__builtin_call_2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -765,6 +758,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -804,13 +804,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -1007,6 +1000,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -1046,13 +1046,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 139,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 140,
Expand Down Expand Up @@ -1249,6 +1242,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 168,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down
30 changes: 15 additions & 15 deletions kclvm/loader/src/snapshots/kclvm_loader__tests__import_stmt_0.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 45,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 46,
Expand Down Expand Up @@ -157,6 +150,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 61,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -246,13 +246,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 45,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 46,
Expand Down Expand Up @@ -358,6 +351,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 61,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -457,7 +457,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 55,
index: 56,
generation: 0,
},
kind: Function,
Expand Down
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 @@ -1114,6 +1114,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)
Loading

0 comments on commit f9d77dd

Please sign in to comment.