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 (kcl-lang#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>
  • Loading branch information
shruti2522 committed May 4, 2024
1 parent e023195 commit 143f196
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions kclvm/api/src/service/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub(crate) fn kcl_schema_ty_to_pb_ty(schema_ty: &SchemaType) -> KclType {
filename: schema_ty.filename.clone(),
pkg_path: schema_ty.pkgpath.clone(),
description: schema_ty.doc.clone(),
base_schema: schema_ty
.base
.as_ref()
.map(|base| Box::new(kcl_schema_ty_to_pb_ty(&**base))),
..Default::default()
}
}
Expand Down
13 changes: 12 additions & 1 deletion kclvm/query/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ pub fn get_full_schema_type(
let scope = resolve_file(&opts)?;
for (name, o) in &scope.borrow().elems {
if o.borrow().ty.is_schema() {
let schema_ty = o.borrow().ty.into_schema_type();
let mut schema_ty = o.borrow().ty.into_schema_type();
if let Some(base) = &schema_ty.base {
schema_ty.base = Some(Box::new(get_full_schema_type_recursive(*base.clone())?));
}
if opts.get_schema_opts == GetSchemaOption::All
|| (opts.get_schema_opts == GetSchemaOption::Definitions && !schema_ty.is_instance)
|| (opts.get_schema_opts == GetSchemaOption::Instances && schema_ty.is_instance)
Expand All @@ -186,6 +189,14 @@ pub fn get_full_schema_type(
Ok(result)
}

fn get_full_schema_type_recursive(schema_ty: SchemaType) -> Result<SchemaType> {
let mut result = schema_ty;
if let Some(base) = result.base {
result.base = Some(Box::new(get_full_schema_type_recursive(*base)?));
}
Ok(result)
}

fn resolve_file(opts: &CompilationOptions) -> Result<Rc<RefCell<Scope>>> {
let sess = Arc::new(ParseSession::default());
let mut program = match load_program(
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)");
}
1 change: 1 addition & 0 deletions kclvm/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ message KclType {
string pkg_path = 13; // `pkg_path` represents the path name of the package where the attribute is located.
string description = 14; // `description` represents the document of the attribute.
map<string, Example> examples = 15; // A map object to hold examples, the key is the example name.
KclType base_schema = 16;
}

message Decorator {
Expand Down

0 comments on commit 143f196

Please sign in to comment.