Skip to content

Commit

Permalink
feat: add file.current function
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jul 30, 2024
1 parent 2ed0c55 commit 341f8e9
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 52 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(), 178);
/// assert_eq!(result.symbol_node_map.len(), 178);
/// assert_eq!(result.fully_qualified_name_map.len(), 188);
/// 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.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: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -291,6 +284,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
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 @@ -180,13 +180,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -383,6 +376,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -422,13 +422,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -625,6 +618,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -664,13 +664,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -867,6 +860,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
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 @@ -382,13 +382,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -585,6 +578,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -624,13 +624,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -827,6 +820,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
Expand Down Expand Up @@ -866,13 +866,6 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
),
attrs: [
SymbolRef {
id: Index {
index: 137,
generation: 0,
},
kind: Function,
},
SymbolRef {
id: Index {
index: 138,
Expand Down Expand Up @@ -1069,6 +1062,13 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Function,
},
SymbolRef {
id: Index {
index: 166,
generation: 0,
},
kind: Function,
},
],
is_global: false,
},
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 @@ -295,6 +295,8 @@ kclvm_value_ref_t* kclvm_file_append(kclvm_context_t* ctx, kclvm_value_ref_t* ar

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

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

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

kclvm_value_ref_t* kclvm_file_exists(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 @@ -258,6 +258,8 @@ declare %kclvm_value_ref_t* @kclvm_file_append(%kclvm_context_t* %ctx, %kclvm_va

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

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

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

declare %kclvm_value_ref_t* @kclvm_file_exists(%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 @@ -140,6 +140,7 @@ pub enum ApiFunc {
kclvm_file_abs,
kclvm_file_append,
kclvm_file_cp,
kclvm_file_current,
kclvm_file_delete,
kclvm_file_exists,
kclvm_file_glob,
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 @@ -145,6 +145,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_file_abs" => crate::kclvm_file_abs as *const () as u64,
"kclvm_file_append" => crate::kclvm_file_append as *const () as u64,
"kclvm_file_cp" => crate::kclvm_file_cp as *const () as u64,
"kclvm_file_current" => crate::kclvm_file_current as *const () as u64,
"kclvm_file_delete" => crate::kclvm_file_delete as *const () as u64,
"kclvm_file_exists" => crate::kclvm_file_exists as *const () as u64,
"kclvm_file_glob" => crate::kclvm_file_glob 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 @@ -1218,6 +1218,10 @@
// api-spec(c): kclvm_value_ref_t* kclvm_file_workdir(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_file_workdir(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);

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

// api-spec: kclvm_file_exists
// api-spec(c): kclvm_value_ref_t* kclvm_file_exists(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_file_exists(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down
13 changes: 13 additions & 0 deletions kclvm/runtime/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ pub extern "C" fn kclvm_file_workdir(
s.into_raw(ctx)
}

/// Read the path of the current script or module that is being executed
#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_current(
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.panic_info.kcl_file.as_ref());
s.into_raw(ctx)
}

/// Whether this file path exists. Returns true if the path points at
/// an existing entity. This function will traverse symbolic links to
/// query information about the destination file.
Expand Down
8 changes: 8 additions & 0 deletions kclvm/sema/src/builtin/system_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,14 @@ register_file_member! {
false,
None,
)
current => Type::function(
None,
Type::str_ref(),
&[],
r#"Read the path of the current script or module that is being executed"#,
false,
None,
)
exists => Type::function(
None,
Type::bool_ref(),
Expand Down
3 changes: 3 additions & 0 deletions test/grammar/builtins/file/current/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import file

a = file.current().endswith("main.k")
1 change: 1 addition & 0 deletions test/grammar/builtins/file/current/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a: true

0 comments on commit 341f8e9

Please sign in to comment.