-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnrs_files.go
49 lines (45 loc) · 988 Bytes
/
rnrs_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Copyright (c) 2023 Markku Rossi
//
// All rights reserved.
//
// The (rnrs files (6)) library.
//
package scheme
import (
"errors"
"fmt"
"os"
"github.com/markkurossi/scheme/types"
)
var rnrsFilesBuiltins = []Builtin{
{
Name: "file-exists?",
Args: []string{"filename<string>"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
filename, ok := args[0].(String)
if !ok {
return nil, fmt.Errorf("invalid filename: %v", args[0])
}
_, err := os.Stat(string(filename))
return Boolean(!errors.Is(err, os.ErrNotExist)), nil
},
},
{
Name: "delete-file",
Args: []string{"filename<string>"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
filename, ok := args[0].(String)
if !ok {
return nil, fmt.Errorf("invalid filename: %v", args[0])
}
err := os.Remove(string(filename))
if err != nil {
return nil, err
}
return Boolean(true), nil
},
},
}