Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more functions and parameters for the datetime module #1605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), 179);
/// assert_eq!(result.symbol_node_map.len(), 179);
/// assert_eq!(result.fully_qualified_name_map.len(), 189);
/// 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.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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -291,6 +284,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -443,6 +436,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -685,6 +678,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -927,6 +920,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -765,6 +758,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -1007,6 +1000,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
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: 138,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 139,
Expand Down Expand Up @@ -1249,6 +1242,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 167,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down
Binary file modified kclvm/runtime/src/_kclvm.bc
Binary file not shown.
4 changes: 3 additions & 1 deletion kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,14 @@ kclvm_value_ref_t* kclvm_crypto_uuid(kclvm_context_t* ctx, kclvm_value_ref_t* _a

kclvm_value_ref_t* kclvm_datetime_date(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);

kclvm_value_ref_t* kclvm_datetime_now(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
kclvm_value_ref_t* kclvm_datetime_now(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);

kclvm_value_ref_t* kclvm_datetime_ticks(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);

kclvm_value_ref_t* kclvm_datetime_today(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);

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

void kclvm_default_collection_insert_int_pointer(kclvm_value_ref_t* p, kclvm_char_t* key, uint64_t* ptr);

void kclvm_default_collection_insert_value(kclvm_value_ref_t* p, kclvm_char_t* key, kclvm_value_ref_t* value);
Expand Down
4 changes: 3 additions & 1 deletion kclvm/runtime/src/_kclvm.ll
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ declare %kclvm_value_ref_t* @kclvm_crypto_uuid(%kclvm_context_t* %ctx, %kclvm_va

declare %kclvm_value_ref_t* @kclvm_datetime_date(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

declare %kclvm_value_ref_t* @kclvm_datetime_now(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);
declare %kclvm_value_ref_t* @kclvm_datetime_now(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

declare %kclvm_value_ref_t* @kclvm_datetime_ticks(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

declare %kclvm_value_ref_t* @kclvm_datetime_today(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

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

declare void @kclvm_default_collection_insert_int_pointer(%kclvm_value_ref_t* %p, %kclvm_char_t* %key, i64* %ptr);

declare void @kclvm_default_collection_insert_value(%kclvm_value_ref_t* %p, %kclvm_char_t* %key, %kclvm_value_ref_t* %value);
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 @@ -116,6 +116,7 @@ pub enum ApiFunc {
kclvm_datetime_now,
kclvm_datetime_ticks,
kclvm_datetime_today,
kclvm_datetime_validate,
kclvm_default_collection_insert_int_pointer,
kclvm_default_collection_insert_value,
kclvm_dict_clear,
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 @@ -117,6 +117,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_datetime_now" => crate::kclvm_datetime_now as *const () as u64,
"kclvm_datetime_ticks" => crate::kclvm_datetime_ticks as *const () as u64,
"kclvm_datetime_today" => crate::kclvm_datetime_today as *const () as u64,
"kclvm_datetime_validate" => crate::kclvm_datetime_validate as *const () as u64,
"kclvm_default_collection_insert_int_pointer" => {
crate::kclvm_default_collection_insert_int_pointer as *const () as u64
}
Expand Down
8 changes: 6 additions & 2 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,8 @@
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_datetime_today(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

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

// api-spec: kclvm_datetime_ticks
// api-spec(c): kclvm_value_ref_t* kclvm_datetime_ticks(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
Expand All @@ -818,6 +818,10 @@
// api-spec(c): kclvm_value_ref_t* kclvm_datetime_date(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_datetime_date(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

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

// api-spec: kclvm_json_encode
// api-spec(c): kclvm_value_ref_t* kclvm_json_encode(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_json_encode(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down
55 changes: 50 additions & 5 deletions kclvm/runtime/src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate chrono;

use chrono::prelude::Local;
use chrono::{prelude::Local, NaiveDate, NaiveDateTime, NaiveTime};

use crate::*;

Expand All @@ -21,18 +21,23 @@ pub extern "C" fn kclvm_datetime_today(
.into_raw(ctx)
}

/// Return the local time. e.g. 'Sat Jun 06 16:26:11 1998'
/// Return the local time format. e.g. 'Sat Jun 06 16:26:11 1998' or format the combined date and time per the specified format string,
/// and the default date format is "%a %b %d %H:%M:%S %Y".
/// `now() -> str`
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_datetime_now(
ctx: *mut kclvm_context_t,
_args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let s = Local::now();
let ctx = mut_ptr_as_ref(ctx);
ValueRef::str(&s.format("%a %b %d %H:%M:%S %Y").to_string()).into_raw(ctx)
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let format = get_call_arg_str(args, kwargs, 0, Some("format"))
.unwrap_or_else(|| "%a %b %d %H:%M:%S %Y".to_string());
ValueRef::str(&s.format(&format).to_string()).into_raw(ctx)
}

/// Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them.
Expand Down Expand Up @@ -62,3 +67,43 @@ pub extern "C" fn kclvm_datetime_date(
let ctx = mut_ptr_as_ref(ctx);
ValueRef::str(&s.format("%Y-%m-%d %H:%M:%S").to_string()).into_raw(ctx)
}

/// Validates whether the provided date string matches the specified format.
/// `validate(str, str) -> bool`
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_datetime_validate(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const 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(date) = get_call_arg_str(args, kwargs, 0, Some("date")) {
if let Some(format) = get_call_arg_str(args, kwargs, 1, Some("format")) {
let result = validate_date(&date, &format);
return ValueRef::bool(result).into_raw(ctx);
}
panic!("validate() takes 2 positional arguments (1 given)");
}
panic!("validate() takes 2 positional arguments (0 given)");
}

/// Validates whether the provided date string matches the specified format.
///
/// # Parameters
/// - `date`: A string slice representing the date to be validated.
/// - `format`: A string slice representing the expected format for the date.
///
/// # Returns
/// - Returns `true` if the date string successfully parses according to the specified format,
/// otherwise, returns `false`.
#[inline]
fn validate_date(date: &str, format: &str) -> bool {
NaiveDateTime::parse_from_str(date, format)
.map(|_| true)
.or_else(|_| NaiveDate::parse_from_str(date, format).map(|_| true))
.or_else(|_| NaiveTime::parse_from_str(date, format).map(|_| true))
.is_ok()
}
4 changes: 2 additions & 2 deletions kclvm/runtime/src/value/val_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub fn get_call_arg(
}

#[inline]
pub(crate) fn get_call_arg_str(
pub fn get_call_arg_str(
args: &ValueRef,
kwargs: &ValueRef,
index: usize,
Expand All @@ -244,7 +244,7 @@ pub(crate) fn get_call_arg_str(
}

#[inline]
pub(crate) fn get_call_arg_bool(
pub fn get_call_arg_bool(
args: &ValueRef,
kwargs: &ValueRef,
index: usize,
Expand Down
Loading
Loading