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 builtin io function modpath() #1056

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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(), 161);
/// assert_eq!(result.symbol_node_map.len(), 161);
/// assert_eq!(result.fully_qualified_name_map.len(), 169);
/// assert_eq!(result.node_symbol_map.len(), 162);
/// assert_eq!(result.symbol_node_map.len(), 162);
/// assert_eq!(result.fully_qualified_name_map.len(), 170);
/// assert_eq!(result.pkg_scope_map.len(), 3);
/// ```
pub fn load_package(&self, args: &LoadPackageArgs) -> anyhow::Result<LoadPackageResult> {
Expand Down
8 changes: 8 additions & 0 deletions kclvm/compiler/src/codegen/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,14 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
self.pkgpath_stack.borrow_mut().push(pkgpath.clone());
}
}
// Set the kcl workdir to the runtime context
self.build_void_call(
&ApiFunc::kclvm_context_set_kcl_modpath.name(),
&[
self.current_runtime_ctx_ptr(),
self.native_global_string_value(&self.program.root),
],
);
if !self.import_names.is_empty() {
let import_names = self.dict_value();
for (k, v) in &self.import_names {
Expand Down
Binary file modified kclvm/runtime/src/_kclvm.bc
Binary file not shown.
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ void kclvm_context_set_kcl_line_col(kclvm_context_t* ctx, int32_t line, int32_t

void kclvm_context_set_kcl_location(kclvm_context_t* p, char* filename, int32_t line, int32_t col);

void kclvm_context_set_kcl_modpath(kclvm_context_t* p, char* module_path);

void kclvm_context_set_kcl_pkgpath(kclvm_context_t* p, char* pkgpath);

void kclvm_context_set_list_option_mode(kclvm_context_t* p, kclvm_bool_t v);
Expand Down Expand Up @@ -302,6 +304,8 @@ kclvm_value_ref_t* kclvm_dict_values(kclvm_context_t* ctx, kclvm_value_ref_t* p)

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

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

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);
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm.ll
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ declare void @kclvm_context_set_kcl_line_col(%kclvm_context_t* %ctx, i32 %line,

declare void @kclvm_context_set_kcl_location(%kclvm_context_t* %p, i8* %filename, i32 %line, i32 %col);

declare void @kclvm_context_set_kcl_modpath(%kclvm_context_t* %p, i8* %module_path);

declare void @kclvm_context_set_kcl_pkgpath(%kclvm_context_t* %p, i8* %pkgpath);

declare void @kclvm_context_set_list_option_mode(%kclvm_context_t* %p, %kclvm_bool_t %v);
Expand Down Expand Up @@ -250,6 +252,8 @@ declare %kclvm_value_ref_t* @kclvm_dict_values(%kclvm_context_t* %ctx, %kclvm_va

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

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

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);
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub enum ApiFunc {
kclvm_context_set_kcl_filename,
kclvm_context_set_kcl_line_col,
kclvm_context_set_kcl_location,
kclvm_context_set_kcl_modpath,
kclvm_context_set_kcl_pkgpath,
kclvm_context_set_list_option_mode,
kclvm_context_set_strict_range_check,
Expand Down Expand Up @@ -137,6 +138,7 @@ pub enum ApiFunc {
kclvm_dict_update_key_value,
kclvm_dict_values,
kclvm_file_glob,
kclvm_file_modpath,
kclvm_file_read,
kclvm_iterator_cur_key,
kclvm_iterator_cur_value,
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_context_set_kcl_location" => {
crate::kclvm_context_set_kcl_location as *const () as u64
}
"kclvm_context_set_kcl_modpath" => crate::kclvm_context_set_kcl_modpath as *const () as u64,
"kclvm_context_set_kcl_pkgpath" => crate::kclvm_context_set_kcl_pkgpath as *const () as u64,
"kclvm_context_set_list_option_mode" => {
crate::kclvm_context_set_list_option_mode as *const () as u64
Expand Down Expand Up @@ -144,6 +145,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> 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_glob" => crate::kclvm_file_glob as *const () as u64,
"kclvm_file_modpath" => crate::kclvm_file_modpath 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,
Expand Down
8 changes: 8 additions & 0 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
// api-spec(c): void kclvm_context_set_kcl_pkgpath(kclvm_context_t* p, char* pkgpath);
// api-spec(llvm): declare void @kclvm_context_set_kcl_pkgpath(%kclvm_context_t* %p, i8* %pkgpath);

// api-spec: kclvm_context_set_kcl_modpath
// api-spec(c): void kclvm_context_set_kcl_modpath(kclvm_context_t* p, char* module_path);
// api-spec(llvm): declare void @kclvm_context_set_kcl_modpath(%kclvm_context_t* %p, i8* %module_path);

// api-spec: kclvm_context_set_kcl_filename
// api-spec(c): void kclvm_context_set_kcl_filename(kclvm_context_t* ctx, char* filename);
// api-spec(llvm): declare void @kclvm_context_set_kcl_filename(%kclvm_context_t* %ctx, i8* %filename);
Expand Down Expand Up @@ -1182,3 +1186,7 @@
// api-spec(c): kclvm_value_ref_t* kclvm_file_glob(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_file_glob(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

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

1 change: 1 addition & 0 deletions kclvm/runtime/src/api/kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ pub struct Context {
pub output: ContextOutput,
pub panic_info: PanicInfo,

pub module_path: String,
pub main_pkg_path: String,
pub main_pkg_files: Vec<String>,
pub backtrace: Vec<BacktraceFrame>,
Expand Down
12 changes: 12 additions & 0 deletions kclvm/runtime/src/context/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ pub unsafe extern "C" fn kclvm_context_set_kcl_pkgpath(
}
}

#[no_mangle]
#[runtime_fn]
pub unsafe extern "C" fn kclvm_context_set_kcl_modpath(
p: *mut kclvm_context_t,
module_path: *const c_char,
) {
let p = mut_ptr_as_ref(p);
if !module_path.is_null() {
p.set_kcl_module_path(c2str(module_path));
}
}

#[no_mangle]
#[runtime_fn]
pub unsafe extern "C" fn kclvm_context_set_kcl_filename(
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl crate::Context {
self.panic_info.kcl_pkgpath = pkgpath.to_string();
}

pub fn set_kcl_module_path(&mut self, module_path: &str) {
self.module_path = module_path.to_string();
}

pub fn set_kcl_filename(&mut self, file: &str) {
if !file.is_empty() {
self.panic_info.kcl_file = file.to_string();
Expand Down
12 changes: 12 additions & 0 deletions kclvm/runtime/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ pub extern "C" fn kclvm_file_glob(

return ValueRef::list_str(matched_paths.as_slice()).into_raw(ctx);
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_modpath(
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 s = ValueRef::str(&ctx.module_path.as_ref());
return s.into_raw(ctx);
}
10 changes: 9 additions & 1 deletion kclvm/sema/src/builtin/system_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ register_collection_member! {
// ------------------------------

pub const FILE: &str = "file";
pub const FILE_FUNCTION_NAMES: &[&str] = &["read", "glob"];
pub const FILE_FUNCTION_NAMES: &[&str] = &["read", "glob", "modpath"];
macro_rules! register_file_member {
($($name:ident => $ty:expr)*) => (
pub const FILE_FUNCTION_TYPES: Lazy<IndexMap<String, Type>> = Lazy::new(|| {
Expand Down Expand Up @@ -1593,6 +1593,14 @@ register_file_member! {
false,
None,
)
modpath => Type::function(
None,
Type::str_ref(),
&[],
r#"return the module root path (kcl.mod file path or a single *.k file path)"#,
false,
None,
)
}

pub const STANDARD_SYSTEM_MODULES: &[&str] = &[
Expand Down
8 changes: 8 additions & 0 deletions test/grammar/builtins/file/mod_root/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import file
import json

data_string = json.decode(file.read(file.modpath()+"/test.json"))

key1 = data_string.key1
key2 = data_string.key2
data = data_string.data
13 changes: 13 additions & 0 deletions test/grammar/builtins/file/mod_root/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/mod_root/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key1": "value1",
"key2": "value2",
"data": [1, 2, 3]
}
5 changes: 5 additions & 0 deletions test/grammar/builtins/file/mod_root_sub/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "workdir_sub"
edition = "0.0.1"
version = "0.0.1"

13 changes: 13 additions & 0 deletions test/grammar/builtins/file/mod_root_sub/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
8 changes: 8 additions & 0 deletions test/grammar/builtins/file/mod_root_sub/sub/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import file
import json

data_string = json.decode(file.read(file.modpath()+"/test.json"))

key1 = data_string.key1
key2 = data_string.key2
data = data_string.data
5 changes: 5 additions & 0 deletions test/grammar/builtins/file/mod_root_sub/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key1": "value1",
"key2": "value2",
"data": [1, 2, 3]
}
Loading