diff --git a/README.md b/README.md index 1d271f2..471ad83 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,8 @@ Name | Example | Example result | Description Name | Example | Example result | Description ------- | ------- | -------------- | ----------- +wd | `(wd)` | `"/home/pekka/code/madlisp/"` | Get the current working directory. +chdir | `(chdir "/tmp")` | `true` | Change the current working directory. file? | `(file? "test.txt")` | `true` | Return true if the file exists. fread | `(fread "test.txt")` | `"content"` | Read the contents of a file. fwrite | `(fwrite "test.txt" "content")` | `true` | Write string to file. Give optional third parameter as `true` to append. @@ -284,8 +286,10 @@ The following constants are defined by default: Name | PHP constant ------- | ------------ -pi | M_PI +dirsep | DIRECTORY_SEPARATOR +homedir | $_SERVER['HOME'] ln | PHP_EOL +pi | M_PI ## Extending diff --git a/src/Lib/IO.php b/src/Lib/IO.php index f197d9f..e15859e 100644 --- a/src/Lib/IO.php +++ b/src/Lib/IO.php @@ -8,6 +8,17 @@ class IO implements ILib { public function register(Env $env): void { + $env->set('dirsep', \DIRECTORY_SEPARATOR); + $env->set('homedir', $_SERVER['HOME'] . \DIRECTORY_SEPARATOR); + + $env->set('wd', new CoreFunc('wd', 'Get the current working directory.', 0, 0, + fn () => getcwd() . \DIRECTORY_SEPARATOR + )); + + $env->set('chdir', new CoreFunc('chdir', 'Change working directory.', 1, 1, + fn (string $dir) => chdir($dir) + )); + $env->set('file?', new CoreFunc('file?', 'Check if file exists.', 1, 1, fn (string $filename) => file_exists($filename) ));