Skip to content

Commit

Permalink
feat: enhance system package sematic definitions and sync spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy committed Sep 5, 2023
1 parent 0f8a2a4 commit e71498c
Show file tree
Hide file tree
Showing 17 changed files with 1,488 additions and 214 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.6
0.6.0-alpha.1
4 changes: 0 additions & 4 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,6 @@ kclvm_value_ref_t* kclvm_schema_value_new(kclvm_context_t* ctx, kclvm_value_ref_

kclvm_size_t kclvm_strlen(uint8_t* ptr);

void kclvm_testing_arguments(kclvm_context_t* _ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);

void kclvm_testing_setting_file(kclvm_context_t* _ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);

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

kclvm_value_ref_t* kclvm_units_to_Gi(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
Expand Down
2 changes: 0 additions & 2 deletions kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ pub enum ApiFunc {
kclvm_schema_value_check,
kclvm_schema_value_new,
kclvm_strlen,
kclvm_testing_arguments,
kclvm_testing_setting_file,
kclvm_units_to_G,
kclvm_units_to_Gi,
kclvm_units_to_K,
Expand Down
2 changes: 0 additions & 2 deletions kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_schema_value_check" => crate::kclvm_schema_value_check as *const () as u64,
"kclvm_schema_value_new" => crate::kclvm_schema_value_new as *const () as u64,
"kclvm_strlen" => crate::kclvm_strlen as *const () as u64,
"kclvm_testing_arguments" => crate::kclvm_testing_arguments as *const () as u64,
"kclvm_testing_setting_file" => crate::kclvm_testing_setting_file as *const () as u64,
"kclvm_units_to_G" => crate::kclvm_units_to_G as *const () as u64,
"kclvm_units_to_Gi" => crate::kclvm_units_to_Gi as *const () as u64,
"kclvm_units_to_K" => crate::kclvm_units_to_K as *const () as u64,
Expand Down
8 changes: 0 additions & 8 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,14 +1190,6 @@
// api-spec(c): char* kclvm_plugin_invoke_json(int8_t* method, char* args, char* kwargs);
// api-spec(llvm): declare i8* @kclvm_plugin_invoke_json(i8* %method, i8* %args, i8* %kwargs);

// api-spec: kclvm_testing_arguments
// api-spec(c): void kclvm_testing_arguments(kclvm_context_t* _ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare void @kclvm_testing_arguments(%kclvm_context_t* %_ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

// api-spec: kclvm_testing_setting_file
// api-spec(c): void kclvm_testing_setting_file(kclvm_context_t* _ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare void @kclvm_testing_setting_file(%kclvm_context_t* %_ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

// api-spec: kclvm_units_to_n
// api-spec(c): kclvm_value_ref_t* kclvm_units_to_n(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_units_to_n(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down
8 changes: 4 additions & 4 deletions kclvm/runtime/src/datetime/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::*;
#[allow(non_camel_case_types)]
type kclvm_value_ref_t = ValueRef;

// def KMANGLED_today() -> str:
// today() -> str:

#[no_mangle]
#[runtime_fn]
Expand All @@ -25,7 +25,7 @@ pub unsafe extern "C" fn kclvm_datetime_today(
return ValueRef::str(s.as_ref()).into_raw();
}

// def KMANGLED_now() -> str:
// now() -> str:

#[no_mangle]
#[runtime_fn]
Expand All @@ -38,7 +38,7 @@ pub unsafe extern "C" fn kclvm_datetime_now(
return ValueRef::str(s.as_ref()).into_raw();
}

// def KMANGLED_ticks() -> float:
// ticks() -> float:

#[no_mangle]
#[runtime_fn]
Expand All @@ -51,7 +51,7 @@ pub unsafe extern "C" fn kclvm_datetime_ticks(
ValueRef::float(x as f64).into_raw()
}

// def KMANGLED_date() -> str:
// date() -> str:

#[no_mangle]
#[runtime_fn]
Expand Down
44 changes: 25 additions & 19 deletions kclvm/runtime/src/json/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,11 @@ pub unsafe extern "C" fn kclvm_json_encode(
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);

let mut opt = JsonEncodeOptions::default();
if let Some(sort_keys) = kwargs.kwarg_bool("sort_keys", None) {
opt.sort_keys = sort_keys;
}
if let Some(indent) = kwargs.kwarg_int("indent", None) {
opt.indent = indent;
}
if let Some(ignore_private) = kwargs.kwarg_bool("ignore_private", None) {
opt.ignore_private = ignore_private;
}
if let Some(ignore_none) = kwargs.kwarg_bool("ignore_none", None) {
opt.ignore_none = ignore_none;
}

if let Some(arg0) = args.arg_i(0) {
let s = ValueRef::str(arg0.to_json_string_with_option(&opt).as_ref());
let s = ValueRef::str(
arg0.to_json_string_with_option(&kwargs_to_opts(kwargs))
.as_ref(),
);
return s.into_raw();
}
panic!("encode() missing 1 required positional argument: 'value'")
Expand Down Expand Up @@ -64,17 +53,34 @@ pub unsafe extern "C" fn kclvm_json_decode(
pub unsafe extern "C" fn kclvm_json_dump_to_file(
_ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);

if let Some(data) = args.arg_i(0) {
if let Some(filename) = args.arg_i(0) {
let yaml = data.to_json_string();
let filename = filename.as_str();

std::fs::write(filename, yaml).expect("Unable to write file");
let json = data.to_json_string_with_option(&kwargs_to_opts(kwargs));
std::fs::write(filename, json).expect("Unable to write file");
}
}
panic!("dump_to_file() missing 2 required positional arguments: 'data' and 'filename'")
}

fn kwargs_to_opts(kwargs: &ValueRef) -> JsonEncodeOptions {
let mut opts = JsonEncodeOptions::default();
if let Some(sort_keys) = kwargs.kwarg_bool("sort_keys", None) {
opts.sort_keys = sort_keys;
}
if let Some(indent) = kwargs.kwarg_int("indent", None) {
opts.indent = indent;
}
if let Some(ignore_private) = kwargs.kwarg_bool("ignore_private", None) {
opts.ignore_private = ignore_private;
}
if let Some(ignore_none) = kwargs.kwarg_bool("ignore_none", None) {
opts.ignore_none = ignore_none;
}
opts
}
3 changes: 0 additions & 3 deletions kclvm/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ pub use self::regex::*;
pub mod stdlib;
pub use self::stdlib::*;

pub mod testing;
pub use self::testing::*;

pub mod units;
pub use self::units::*;

Expand Down
41 changes: 24 additions & 17 deletions kclvm/runtime/src/math/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub unsafe extern "C" fn kclvm_math_ceil(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Int(x);
}
if let Some(x) = args.arg_i_float(0, None) {
Expand Down Expand Up @@ -55,7 +55,7 @@ pub unsafe extern "C" fn kclvm_math_factorial(

let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
if x >= 0 {
return kclvm_value_Int(factorial(x));
}
Expand All @@ -65,6 +65,7 @@ pub unsafe extern "C" fn kclvm_math_factorial(
return kclvm_value_Float(factorial(x as i64) as f64);
}
}

if args.args_len() > 0 {
panic!("factorial() only accepts integral values")
}
Expand All @@ -80,7 +81,7 @@ pub unsafe extern "C" fn kclvm_math_floor(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Int(x);
}
if let Some(x) = args.arg_i_float(0, None) {
Expand Down Expand Up @@ -120,12 +121,15 @@ pub unsafe extern "C" fn kclvm_math_isfinite(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(_x) = args.arg_i_int(0, None) {
if let Some(_) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Bool(true as i8);
}
if let Some(x) = args.arg_i_float(0, None) {
return kclvm_value_Bool(x.is_finite() as i8);
}
if let Some(_) = args.arg_i_bool(0, None) {
return kclvm_value_Bool(true as i8);
}

panic!("isfinite() takes exactly one argument (0 given)");
}
Expand All @@ -139,12 +143,15 @@ pub unsafe extern "C" fn kclvm_math_isinf(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(_x) = args.arg_i_int(0, None) {
if let Some(_x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Bool(false as i8);
}
if let Some(x) = args.arg_i_float(0, None) {
return kclvm_value_Bool(x.is_infinite() as i8);
}
if let Some(_) = args.arg_i_bool(0, None) {
return kclvm_value_Bool(false as i8);
}

panic!("isinf() takes exactly one argument (0 given)");
}
Expand All @@ -158,7 +165,7 @@ pub unsafe extern "C" fn kclvm_math_isnan(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(_x) = args.arg_i_int(0, None) {
if let Some(_x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Bool(false as i8);
}
if let Some(x) = args.arg_i_float(0, None) {
Expand All @@ -177,7 +184,7 @@ pub unsafe extern "C" fn kclvm_math_modf(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
let list = ValueRef::list_float(&[0.0, x as f64]);
return list.into_raw();
}
Expand Down Expand Up @@ -207,7 +214,7 @@ pub unsafe extern "C" fn kclvm_math_exp(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Float((x as f64).exp());
}
if let Some(x) = args.arg_i_float(0, None) {
Expand All @@ -225,7 +232,7 @@ pub unsafe extern "C" fn kclvm_math_expm1(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Float((x as f64).exp_m1());
}
if let Some(x) = args.arg_i_float(0, None) {
Expand All @@ -243,7 +250,7 @@ pub unsafe extern "C" fn kclvm_math_log(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
if let Some(base) = args.arg_i_float(1, Some(std::f64::consts::E)) {
return kclvm_value_Int((x as f64).log(base) as i64);
}
Expand All @@ -265,7 +272,7 @@ pub unsafe extern "C" fn kclvm_math_log1p(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Float(((x + 1) as f64).ln_1p());
}
if let Some(x) = args.arg_i_float(0, None) {
Expand All @@ -283,7 +290,7 @@ pub unsafe extern "C" fn kclvm_math_log2(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Int((x as f64).log2() as i64);
}
if let Some(x) = args.arg_i_float(0, None) {
Expand All @@ -301,7 +308,7 @@ pub unsafe extern "C" fn kclvm_math_log10(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Float((x as f64).log10());
}
if let Some(x) = args.arg_i_float(0, None) {
Expand All @@ -319,8 +326,8 @@ pub unsafe extern "C" fn kclvm_math_pow(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(n) = args.arg_i_int(1, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
if let Some(n) = args.arg_i_int_or_bool(1, None) {
if n < 0 {
return kclvm_value_Float((x as f64).powf(n as f64));
} else {
Expand All @@ -332,7 +339,7 @@ pub unsafe extern "C" fn kclvm_math_pow(
}
}
if let Some(x) = args.arg_i_float(0, None) {
if let Some(n) = args.arg_i_int(1, None) {
if let Some(n) = args.arg_i_int_or_bool(1, None) {
return kclvm_value_Float(x.powi(n as i32));
}
if let Some(n) = args.arg_i_float(1, None) {
Expand All @@ -351,7 +358,7 @@ pub unsafe extern "C" fn kclvm_math_sqrt(
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);

if let Some(x) = args.arg_i_int(0, None) {
if let Some(x) = args.arg_i_int_or_bool(0, None) {
return kclvm_value_Float((x as f64).sqrt());
}
if let Some(x) = args.arg_i_float(0, None) {
Expand Down
2 changes: 1 addition & 1 deletion kclvm/runtime/src/net/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub unsafe extern "C" fn kclvm_net_IP_string(
return ValueRef::str(s.as_ref()).into_raw();
}

return kclvm_value_False();
return ValueRef::str("").into_raw();
}

panic!("IP_string() missing 1 required positional argument: 'ip'");
Expand Down
12 changes: 6 additions & 6 deletions kclvm/runtime/src/regex/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::*;
#[allow(non_camel_case_types)]
type kclvm_value_ref_t = ValueRef;

// def KMANGLED_match(string: str, pattern: str) -> bool:
// match(string: str, pattern: str) -> bool:

#[no_mangle]
#[runtime_fn]
Expand Down Expand Up @@ -46,7 +46,7 @@ pub unsafe extern "C" fn kclvm_regex_match(
panic!("match() missing 2 required positional arguments: 'string' and 'pattern'")
}

// def KMANGLED_replace(string: str, pattern: str, replace: str, count: int = 0):
// replace(string: str, pattern: str, replace: str, count: int = 0):

#[no_mangle]
#[runtime_fn]
Expand All @@ -71,7 +71,7 @@ pub unsafe extern "C" fn kclvm_regex_replace(
panic!("replace() missing 3 required positional arguments: 'string', 'pattern', and 'replace");
}

// def KMANGLED_compile(pattern: str) -> bool:
// compile(pattern: str) -> bool:

#[no_mangle]
#[runtime_fn]
Expand All @@ -91,7 +91,7 @@ pub unsafe extern "C" fn kclvm_regex_compile(
panic!("compile() missing 2 required positional arguments: 'string' and 'pattern'")
}

// def KMANGLED_findall(string: str, pattern: str) -> [str]:
// findall(string: str, pattern: str) -> [str]:

#[no_mangle]
#[runtime_fn]
Expand Down Expand Up @@ -130,7 +130,7 @@ pub unsafe extern "C" fn kclvm_regex_findall(
panic!("findall() missing 2 required positional arguments: 'string' and 'pattern'")
}

// def KMANGLED_search(string: str, pattern: str):
// search(string: str, pattern: str):

#[no_mangle]
#[runtime_fn]
Expand All @@ -154,7 +154,7 @@ pub unsafe extern "C" fn kclvm_regex_search(
panic!("search() missing 2 required positional arguments: 'string' and 'pattern'");
}

// def KMANGLED_split(string: str, pattern: str, maxsplit: int = 0):
// split(string: str, pattern: str, maxsplit: int = 0):

#[no_mangle]
#[runtime_fn]
Expand Down
4 changes: 0 additions & 4 deletions kclvm/runtime/src/testing/mod.rs

This file was deleted.

Loading

0 comments on commit e71498c

Please sign in to comment.