-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnrs_programs.go
84 lines (76 loc) · 1.41 KB
/
rnrs_programs.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
// The (rnrs programs (6)) library.
//
package scheme
import (
"fmt"
"os"
"github.com/markkurossi/scheme/types"
)
var rnrsProgramsBuiltins = []Builtin{
{
Name: "command-line",
Return: &types.Type{
Enum: types.EnumPair,
Car: types.String,
Cdr: types.Any,
},
Native: func(scm *Scheme, args []Value) (Value, error) {
var head, tail Pair
for _, arg := range os.Args {
pair := NewPair(String(arg), nil)
if tail == nil {
head = pair
} else {
tail.SetCdr(pair)
}
tail = pair
}
return head, nil
},
},
{
Name: "exit",
Args: []string{"[obj]"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
code := 0
if len(args) == 1 {
switch v := args[0].(type) {
case Int:
code = int(v)
case *BigInt:
code = int(v.I.Int64())
case Boolean:
if !v {
code = 1
}
default:
code = 1
}
}
os.Exit(code)
return nil, nil
},
},
{
Name: "getenv",
Args: []string{"name<string>"},
Return: types.Any,
Native: func(scm *Scheme, args []Value) (Value, error) {
name, ok := args[0].(String)
if !ok {
return nil, fmt.Errorf("invalid variable name: %v", args[0])
}
val, found := os.LookupEnv(string(name))
if !found {
return Boolean(false), nil
}
return String(val), nil
},
},
}