-
Notifications
You must be signed in to change notification settings - Fork 4
/
os.go
133 lines (107 loc) · 2.67 KB
/
os.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package eval
import (
_ "fmt"
"os"
)
const (
os_name = "os"
)
type Os struct{}
func NewOsObj() Object {
ret := &Os{}
SetGlobalObj(os_name, ret)
return ret
}
func (o *Os) Inspect() string { return "<" + os_name + ">" }
func (o *Os) Type() ObjectType { return OS_OBJ }
func (o *Os) CallMethod(line string, scope *Scope, method string, args ...Object) Object {
switch method {
case "getenv":
return o.getenv(line, args...)
case "setenv":
return o.setenv(line, args...)
case "chdir":
return o.chdir(line, args...)
case "mkdir":
return o.mkdir(line, args...)
case "exit":
return o.exit(line, args...)
}
return newError(line, ERR_NOMETHOD, method, o.Type())
}
func (o *Os) getenv(line string, args ...Object) Object {
if len(args) != 1 {
return newError(line, ERR_ARGUMENT, "1", len(args))
}
key, ok := args[0].(*String)
if !ok {
return newError(line, ERR_PARAMTYPE, "first", "getenv", "*String", args[0].Type())
}
ret := os.Getenv(key.String)
return NewString(ret)
}
func (o *Os) setenv(line string, args ...Object) Object {
if len(args) != 2 {
return newError(line, ERR_ARGUMENT, "2", len(args))
}
key, ok := args[0].(*String)
if !ok {
return newError(line, ERR_PARAMTYPE, "first", "setenv", "*String", args[0].Type())
}
value, ok := args[1].(*String)
if !ok {
return newError(line, ERR_PARAMTYPE, "second", "setenv", "*String", args[1].Type())
}
err := os.Setenv(key.String, value.String)
if err != nil {
return FALSE
}
return TRUE
}
func (o *Os) chdir(line string, args ...Object) Object {
if len(args) != 1 {
return newError(line, ERR_ARGUMENT, "1", len(args))
}
newDir, ok := args[0].(*String)
if !ok {
return newError(line, ERR_PARAMTYPE, "first", "chdir", "*String", args[0].Type())
}
err := os.Chdir(newDir.String)
if err != nil {
return FALSE
}
return TRUE
}
func (o *Os) mkdir(line string, args ...Object) Object {
if len(args) != 2 {
return newError(line, ERR_ARGUMENT, "2", len(args))
}
name, ok := args[0].(*String)
if !ok {
return newError(line, ERR_PARAMTYPE, "first", "mkdir", "*String", args[0].Type())
}
perm, ok := args[1].(*Number)
if !ok {
return newError(line, ERR_PARAMTYPE, "second", "mkdir", "*Number", args[1].Type())
}
err := os.Mkdir(name.String, os.FileMode(int64(perm.Value)))
if err != nil {
return FALSE
}
return TRUE
}
func (o *Os) exit(line string, args ...Object) Object {
if len(args) != 0 && len(args) != 1 {
return newError(line, ERR_ARGUMENT, "0|1", len(args))
}
if len(args) == 0 {
os.Exit(0)
return NIL
}
code, ok := args[0].(*Number)
if !ok {
return newError(line, ERR_PARAMTYPE, "first", "exit", "*Number", args[0].Type())
}
os.Exit(int(code.Value))
return NIL
}