Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions crates/my-lang/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,37 @@ fn register_fs_functions(define: &mut impl FnMut(String, Value)) {
},
}),
);

// fs_list_dir(path) -> Array<String> - entry names (not full paths) in
// `path`, sorted for deterministic tooling output. Errors if `path` is not
// a readable directory. See hyperpolymath/my-lang#55.
define(
"fs_list_dir".to_string(),
Value::NativeFunction(NativeFunction {
name: "fs_list_dir".to_string(),
arity: 1,
func: |args| match &args[0] {
Value::String(path) => {
let rd = std::fs::read_dir(path).map_err(|e| {
RuntimeError::Custom(format!("fs_list_dir({}) failed: {}", path, e))
})?;
let mut names: Vec<String> = Vec::new();
for entry in rd {
let entry = entry.map_err(|e| {
RuntimeError::Custom(format!("fs_list_dir({}) failed: {}", path, e))
})?;
names.push(entry.file_name().to_string_lossy().into_owned());
}
names.sort();
Ok(Value::Array(names.into_iter().map(Value::String).collect()))
}
_ => Err(RuntimeError::TypeError {
expected: "string".to_string(),
got: format!("{:?}", args[0]),
}),
},
}),
);
}

// ============================================================================
Expand Down Expand Up @@ -1880,6 +1911,7 @@ pub fn stdlib_functions() -> Vec<&'static str> {
"fs_read_file",
"fs_create_dir_all",
"fs_exists",
"fs_list_dir",
// Env extras
"env_args",
// Map / dict
Expand Down
12 changes: 12 additions & 0 deletions examples/fs_list_dir.my
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Solo fs_list_dir() builtin (hyperpolymath/my-lang#55).
//
// my run examples/fs_list_dir.my
//
// Lists this repo's examples/ directory, sorted. Output includes the other
// example files (date.my, json.my, map.my, fs_list_dir.my, ...).

fn main() -> Int {
let entries = fs_list_dir("examples");
println(str_join(entries, "\n"));
return 0;
}
21 changes: 21 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,27 @@ fn test_eval_date_today() {
}
}

// Solo fs_list_dir() stdlib builtin (hyperpolymath/my-lang#55): enumerate a
// directory, sorted entry names. Self-contained: builds a dir with fs_* builtins
// then lists it (exercises fs_create_dir_all/fs_write_file/fs_list_dir/str_join).
#[test]
fn test_eval_fs_list_dir() {
let source = r#"
fn main() -> String {
let dir = "target/.it_fs_list_dir";
fs_create_dir_all(dir);
fs_write_file("target/.it_fs_list_dir/b.txt", "x");
fs_write_file("target/.it_fs_list_dir/a.txt", "y");
let names = fs_list_dir(dir);
return str_join(names, ",");
}
"#;
match eval(source) {
Ok(Value::String(s)) => assert_eq!(s, "a.txt,b.txt"),
other => panic!("expected sorted dir listing, got {:?}", other),
}
}

// AI Runtime tests (require API keys, so just test initialization)
#[test]
fn test_ai_runtime_creation() {
Expand Down
Loading