Skip to content

Commit

Permalink
feat: added mkdir and delete module functions
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

corrected formatting

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

updated formatting

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

feat: added mkdir and delete module functions

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

updated linker.rs

feat: added mkdir and delete module functions

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

format checking

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

`get_schema_type` function will now return `SchemaType` instances with base schema information included (#1272)

* `get_schema_type` and `get_full_schema_type` will now return `SchemaType` instance

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

* added  the recursive function

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

* fixed the types

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

---------

Signed-off-by: utnim2 <mintugogoi567@gmail.com>

feat: extend file module functions mkdir and delete

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

feat: added mkdir and delete module functions

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

tests added

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

tests updated

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>

feat: added mkdir and delete module functions

Signed-off-by: shruti2522 <shruti.apc01@gmail.com>
  • Loading branch information
shruti2522 committed May 4, 2024
1 parent 5f8ccd3 commit 24673ce
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 13 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 @@ -176,9 +176,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(), 169);
/// assert_eq!(result.symbol_node_map.len(), 169);
/// assert_eq!(result.fully_qualified_name_map.len(), 178);
/// assert_eq!(result.node_symbol_map.len(), 171);
/// assert_eq!(result.symbol_node_map.len(), 171);
/// assert_eq!(result.fully_qualified_name_map.len(), 180);
/// assert_eq!(result.pkg_scope_map.len(), 3);
/// ```
#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 157,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 158,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,20 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 157,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 158,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -240,7 +240,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 158,
index: 160,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -337,7 +337,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -585,6 +585,20 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 157,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 158,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -617,7 +631,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 159,
index: 161,
generation: 0,
},
kind: Value,
Expand Down Expand Up @@ -827,6 +841,20 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 157,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 158,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -1069,6 +1097,20 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
},
kind: Value,
},
SymbolRef {
id: Index {
index: 157,
generation: 0,
},
kind: Value,
},
SymbolRef {
id: Index {
index: 158,
generation: 0,
},
kind: Value,
},
],
is_global: false,
},
Expand Down Expand Up @@ -1101,7 +1143,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 160,
index: 162,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression: "format!(\"{:#?}\", p.symbols.values())"
def: Some(
SymbolRef {
id: Index {
index: 157,
index: 159,
generation: 0,
},
kind: Value,
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ kclvm_value_ref_t* kclvm_file_read(kclvm_context_t* ctx, kclvm_value_ref_t* args

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

kclvm_value_ref_t* kclvm_file_mkdir(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_iterator_cur_key(kclvm_iterator_t* p);

kclvm_value_ref_t* kclvm_iterator_cur_value(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 @@ -264,6 +264,10 @@ declare %kclvm_value_ref_t* @kclvm_file_read(%kclvm_context_t* %ctx, %kclvm_valu

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

declare %kclvm_value_ref_t* @kclvm_file_mkdir(%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_iterator_cur_key(%kclvm_iterator_t* %p);

declare %kclvm_value_ref_t* @kclvm_iterator_cur_value(%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 @@ -143,6 +143,8 @@ pub enum ApiFunc {
kclvm_file_modpath,
kclvm_file_read,
kclvm_file_workdir,
kclvm_file_mkdir,
kclvm_file_delete,
kclvm_iterator_cur_key,
kclvm_iterator_cur_value,
kclvm_iterator_delete,
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 @@ -144,6 +144,8 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_dict_values" => crate::kclvm_dict_values as *const () as u64,
"kclvm_file_abs" => crate::kclvm_file_abs as *const () as u64,
"kclvm_file_exists" => crate::kclvm_file_exists as *const () as u64,
"kclvm_file_mkdir" => crate::kclvm_file_mkdir as *const () as u64,
"kclvm_file_delete" => crate::kclvm_file_delete 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,
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 @@ -1222,6 +1222,14 @@
// api-spec(c): kclvm_value_ref_t* kclvm_file_abs(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_file_abs(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);

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

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

// api-spec: kclvm_template_execute
// api-spec(c): kclvm_value_ref_t* kclvm_template_execute(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_template_execute(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %kwargs);
Expand Down
52 changes: 52 additions & 0 deletions kclvm/runtime/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,55 @@ pub extern "C" fn kclvm_file_abs(

panic!("read() takes exactly one argument (0 given)");
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_mkdir(
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 kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);

if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("directory")) {
if let Err(e) = fs::create_dir(&path) {
panic!("Failed to create directory '{}': {}", path, e);
}
return ValueRef::none().into_raw(ctx);
}

panic!("mkdir() takes exactly one argument (0 given)");
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_file_delete(
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 kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);

if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) {
if let Err(e) = fs::remove_file(&path) {
match e.kind() {
std::io::ErrorKind::NotFound => {
// if file not found, try to remove it as a directory
if let Err(e) = fs::remove_dir(&path) {
panic!("failed to delete '{}': {}", path, e);
}
}
_ => {
panic!("failed to delete '{}': {}", path, e);
}
}
}
return ValueRef::none().into_raw(ctx);
}

panic!("delete() takes exactly one argument (0 given)");
}
28 changes: 28 additions & 0 deletions kclvm/sema/src/builtin/system_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,34 @@ register_file_member! {
false,
None,
)
mkdir => Type::function(
None,
Type::str_ref(),
&[
Parameter {
name: "directory".to_string(),
ty: Type::str_ref(),
has_default: false,
},
],
r#"Create a new directory at the specified path if it doesn't already exist."#,
false,
None,
)
delete => Type::function(
None,
Type::str_ref(),
&[
Parameter {
name: "filepath".to_string(),
ty: Type::str_ref(),
has_default: false,
},
],
r#"Delete a file or an empty directory at the specified path."#,
false,
None,
)
}

// ------------------------------
Expand Down
6 changes: 6 additions & 0 deletions test/grammar/builtins/file/delete/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import file

file.delete("test_dir")



1 change: 1 addition & 0 deletions test/grammar/builtins/file/delete/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete: ''
3 changes: 3 additions & 0 deletions test/grammar/builtins/file/mkdir/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import file

file.mkdir("test_dir")
1 change: 1 addition & 0 deletions test/grammar/builtins/file/mkdir/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mkdir: ''

0 comments on commit 24673ce

Please sign in to comment.