Skip to content

Commit

Permalink
feat: add build-in method 'file.read' to load file (#1034)
Browse files Browse the repository at this point in the history
* feat: add build-in method 'file.read' to load file

Signed-off-by: zongz <[email protected]>

* fix: fix unit test

Signed-off-by: zongz <[email protected]>

* fix: fix test case

Signed-off-by: zongz <[email protected]>

* fix: fix CR comments

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Feb 18, 2024
1 parent a735a37 commit 9829e26
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 9 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 @@ -172,9 +172,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(), 159);
/// assert_eq!(result.symbol_node_map.len(), 159);
/// assert_eq!(result.fully_qualified_name_map.len(), 166);
/// assert_eq!(result.node_symbol_map.len(), 160);
/// assert_eq!(result.symbol_node_map.len(), 160);
/// assert_eq!(result.fully_qualified_name_map.len(), 168);
/// assert_eq!(result.pkg_scope_map.len(), 3);
/// ```
pub fn load_package(&self, args: &LoadPackageArgs) -> anyhow::Result<LoadPackageResult> {
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
@@ -1,4 +1,4 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

// Auto generated, DONOT EDIT!!!

Expand Down Expand Up @@ -300,6 +300,8 @@ void kclvm_dict_update_key_value(kclvm_value_ref_t* p, kclvm_value_ref_t* key, k

kclvm_value_ref_t* kclvm_dict_values(kclvm_context_t* ctx, kclvm_value_ref_t* p);

kclvm_value_ref_t* kclvm_file_read(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);

kclvm_value_ref_t* kclvm_iterator_cur_key(kclvm_iterator_t* p);

kclvm_value_ref_t* kclvm_iterator_cur_value(kclvm_iterator_t* p);
Expand Down
4 changes: 3 additions & 1 deletion kclvm/runtime/src/_kclvm.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; Copyright 2021 The KCL Authors. All rights reserved.
; Copyright The KCL Authors. All rights reserved.

; Auto generated, DONOT EDIT!!!

Expand Down Expand Up @@ -248,6 +248,8 @@ declare void @kclvm_dict_update_key_value(%kclvm_value_ref_t* %p, %kclvm_value_r

declare %kclvm_value_ref_t* @kclvm_dict_values(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %p);

declare %kclvm_value_ref_t* @kclvm_file_read(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

declare %kclvm_value_ref_t* @kclvm_iterator_cur_key(%kclvm_iterator_t* %p);

declare %kclvm_value_ref_t* @kclvm_iterator_cur_value(%kclvm_iterator_t* %p);
Expand Down
3 changes: 2 additions & 1 deletion kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Copyright The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

// Auto generated, DONOT EDIT!!!

Expand Down Expand Up @@ -136,6 +136,7 @@ pub enum ApiFunc {
kclvm_dict_update,
kclvm_dict_update_key_value,
kclvm_dict_values,
kclvm_file_read,
kclvm_iterator_cur_key,
kclvm_iterator_cur_value,
kclvm_iterator_delete,
Expand Down
3 changes: 2 additions & 1 deletion kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Copyright The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

// Auto generated, DONOT EDIT!!!

Expand Down Expand Up @@ -143,6 +143,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_dict_update" => crate::kclvm_dict_update as *const () as u64,
"kclvm_dict_update_key_value" => crate::kclvm_dict_update_key_value as *const () as u64,
"kclvm_dict_values" => crate::kclvm_dict_values as *const () as u64,
"kclvm_file_read" => crate::kclvm_file_read as *const () as u64,
"kclvm_iterator_cur_key" => crate::kclvm_iterator_cur_key as *const () as u64,
"kclvm_iterator_cur_value" => crate::kclvm_iterator_cur_value as *const () as u64,
"kclvm_iterator_delete" => crate::kclvm_iterator_delete as *const () as u64,
Expand Down
6 changes: 5 additions & 1 deletion kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Copyright The KCL Authors. All rights reserved.
// Copyright 2023 The KCL Authors. All rights reserved.

// Auto generated by <make gen-api-spec> command, DONOT EDIT!!!

Expand Down Expand Up @@ -1174,3 +1174,7 @@
// api-spec(c): kclvm_value_ref_t* kclvm_yaml_validate(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_yaml_validate(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

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

24 changes: 24 additions & 0 deletions kclvm/runtime/src/file/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::fs;

use crate::*;

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_read(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let ctx = mut_ptr_as_ref(ctx);

if let Some(x) = args.arg_i_str(0, None) {
let contents =
fs::read_to_string(&x).expect(&format!("failed to access the file in {}", x));

let s = ValueRef::str(contents.as_ref());
return s.into_raw(ctx);
}

panic!("read() takes exactly one argument (0 given)");
}
3 changes: 3 additions & 0 deletions kclvm/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub use self::units::*;
pub mod yaml;
pub use self::yaml::*;

pub mod file;
pub use self::file::*;

pub mod _kcl_run;
pub use self::_kcl_run::*;

Expand Down
36 changes: 35 additions & 1 deletion kclvm/sema/src/builtin/system_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,40 @@ register_collection_member! {
)
}

// ------------------------------
// file system package
// ------------------------------

pub const FILE: &str = "file";
pub const FILE_FUNCTION_NAMES: &[&str] = &["read"];
macro_rules! register_file_member {
($($name:ident => $ty:expr)*) => (
pub const FILE_FUNCTION_TYPES: Lazy<IndexMap<String, Type>> = Lazy::new(|| {
let mut builtin_mapping = IndexMap::default();
$( builtin_mapping.insert(stringify!($name).to_string(), $ty); )*
builtin_mapping
});
)
}
register_file_member! {
read => Type::function(
None,
Type::str_ref(),
&[
Parameter {
name: "filepath".to_string(),
ty: Type::str_ref(),
has_default: false,
},
],
r#"Read the file content from path"#,
false,
None,
)
}

pub const STANDARD_SYSTEM_MODULES: &[&str] = &[
COLLECTION, NET, MANIFESTS, MATH, DATETIME, REGEX, YAML, JSON, CRYPTO, BASE64, UNITS,
COLLECTION, NET, MANIFESTS, MATH, DATETIME, REGEX, YAML, JSON, CRYPTO, BASE64, UNITS, FILE,
];

pub const STANDARD_SYSTEM_MODULE_NAMES_WITH_AT: &[&str] = &[
Expand All @@ -1565,6 +1597,7 @@ pub const STANDARD_SYSTEM_MODULE_NAMES_WITH_AT: &[&str] = &[
"@crypto",
"@base64",
"@units",
"@file",
];

/// Get the system module members
Expand All @@ -1585,6 +1618,7 @@ pub fn get_system_module_members(name: &str) -> Vec<&str> {
members
}
COLLECTION => COLLECTION_FUNCTION_NAMES.to_vec(),
FILE => FILE_FUNCTION_NAMES.to_vec(),
_ => bug!("invalid system module name '{}'", name),
}
}
Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ mod tests {
"crypto",
"base64",
"units",
"file",
]
.iter()
.map(|name| KCLCompletionItem {
Expand Down
8 changes: 8 additions & 0 deletions test/grammar/builtins/file/load_json/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import json
import file

data_string = json.decode(file.read("test.json"))

key1 = data_string.key1
key2 = data_string.key2
data = data_string.data
13 changes: 13 additions & 0 deletions test/grammar/builtins/file/load_json/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data_string:
key1: value1
key2: value2
data:
- 1
- 2
- 3
key1: value1
key2: value2
data:
- 1
- 2
- 3
5 changes: 5 additions & 0 deletions test/grammar/builtins/file/load_json/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key1": "value1",
"key2": "value2",
"data": [1, 2, 3]
}
3 changes: 3 additions & 0 deletions test/grammar/builtins/file/load_txt/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import file

a = file.read("test.txt")
1 change: 1 addition & 0 deletions test/grammar/builtins/file/load_txt/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a: Helloworld
1 change: 1 addition & 0 deletions test/grammar/builtins/file/load_txt/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Helloworld
8 changes: 8 additions & 0 deletions test/grammar/builtins/file/load_yaml/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import yaml
import file

data_string = yaml.decode(file.read("test.yaml"))

key1 = data_string.key1
key2 = data_string.key2
data = data_string.data
13 changes: 13 additions & 0 deletions test/grammar/builtins/file/load_yaml/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data_string:
key1: value1
key2: value2
data:
- 1
- 2
- 3
key1: value1
key2: value2
data:
- 1
- 2
- 3
3 changes: 3 additions & 0 deletions test/grammar/builtins/file/load_yaml/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key1: "value1"
key2: "value2"
data: [1, 2, 3]

0 comments on commit 9829e26

Please sign in to comment.