-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstdlib_env.go
81 lines (69 loc) · 2 KB
/
stdlib_env.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
package evaluator
import (
"os"
"github.com/skx/monkey/object"
)
// os.getenv() -> ( Hash )
func envFun(args ...object.Object) object.Object {
env := os.Environ()
newHash := make(map[object.HashKey]object.HashPair)
//
// If we get a match then the output is an array
// First entry is the match, any additional parts
// are the capture-groups.
//
for i := 1; i < len(env); i++ {
// Capture groups start at index 0.
k := &object.String{Value: env[i]}
v := &object.String{Value: os.Getenv(env[i])}
newHashPair := object.HashPair{Key: k, Value: v}
newHash[k.HashKey()] = newHashPair
}
return &object.Hash{Pairs: newHash}
}
// os.getenv( "PATH" ) -> string
func getEnvFun(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if args[0].Type() != object.STRING_OBJ {
return newError("argument must be a string, got=%s",
args[0].Type())
}
input := args[0].(*object.String).Value
return &object.String{Value: os.Getenv(input)}
}
// os.setenv( "PATH", "/home/skx/bin:/usr/bin" );
func setEnvFun(args ...object.Object) object.Object {
if len(args) != 2 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if args[0].Type() != object.STRING_OBJ {
return newError("argument must be a string, got=%s",
args[0].Type())
}
if args[1].Type() != object.STRING_OBJ {
return newError("argument must be a string, got=%s",
args[1].Type())
}
name := args[0].(*object.String).Value
value := args[1].(*object.String).Value
os.Setenv(name, value)
return NULL
}
func init() {
RegisterBuiltin("os.getenv",
func(env *object.Environment, args ...object.Object) object.Object {
return (getEnvFun(args...))
})
RegisterBuiltin("os.setenv",
func(env *object.Environment, args ...object.Object) object.Object {
return (setEnvFun(args...))
})
RegisterBuiltin("os.environment",
func(env *object.Environment, args ...object.Object) object.Object {
return (envFun(args...))
})
}