add some directory functions and constants

This commit is contained in:
Pekka Laiho 2020-06-17 18:28:25 +07:00
parent 85ccdcdbf1
commit 90a9b3aa53
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -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)
));