@@ -2,25 +2,31 @@ package os
2
2
3
3
import (
4
4
"github.com/vknabel/lithia/ast"
5
- . "github.com/vknabel/lithia/runtime"
5
+ "github.com/vknabel/lithia/runtime"
6
6
"github.com/vknabel/lithia/world"
7
7
)
8
8
9
- var _ ExternalDefinition = ExternalOS {}
9
+ var _ runtime. ExternalDefinition = ExternalOS {}
10
10
11
- type ExternalOS struct {}
11
+ type ExternalOS struct {
12
+ inter * runtime.Interpreter
13
+ }
14
+
15
+ func New (inter * runtime.Interpreter ) ExternalOS {
16
+ return ExternalOS {inter }
17
+ }
12
18
13
- func (e ExternalOS ) Lookup (name string , env * Environment , decl ast.Decl ) (RuntimeValue , bool ) {
19
+ func (e ExternalOS ) Lookup (name string , env * runtime. Environment , decl ast.Decl ) (runtime. RuntimeValue , bool ) {
14
20
switch name {
15
21
case "exit" :
16
22
return builtinOsExit (decl ), true
17
23
case "env" :
18
24
return builtinOsEnv (env , decl ), true
19
25
case "args" :
20
26
relevantArgs := world .Current .Args
21
- runtimeArgs := make ([]RuntimeValue , len (relevantArgs ))
27
+ runtimeArgs := make ([]runtime. RuntimeValue , len (relevantArgs ))
22
28
for i , arg := range relevantArgs {
23
- runtimeArgs [i ] = PreludeString (arg )
29
+ runtimeArgs [i ] = runtime . PreludeString (arg )
24
30
}
25
31
list , err := env .MakeEagerList (runtimeArgs )
26
32
if err != nil {
@@ -32,44 +38,44 @@ func (e ExternalOS) Lookup(name string, env *Environment, decl ast.Decl) (Runtim
32
38
}
33
39
}
34
40
35
- func builtinOsExit (decl ast.Decl ) PreludeExternFunction {
36
- return MakeExternFunction (
41
+ func builtinOsExit (decl ast.Decl ) runtime. PreludeExternFunction {
42
+ return runtime . MakeExternFunction (
37
43
decl ,
38
- func (args []Evaluatable ) (RuntimeValue , * RuntimeError ) {
44
+ func (args []runtime. Evaluatable ) (runtime. RuntimeValue , * runtime. RuntimeError ) {
39
45
value , err := args [0 ].Evaluate ()
40
46
if err != nil {
41
47
return nil , err
42
48
}
43
- if code , ok := value .(PreludeInt ); ok {
49
+ if code , ok := value .(runtime. PreludeInt ); ok {
44
50
world .Current .Env .Exit (int (code ))
45
51
return value , nil
46
52
} else {
47
- return nil , NewRuntimeErrorf ("%s is not an int" , value ).CascadeDecl (decl )
53
+ return nil , runtime . NewRuntimeErrorf ("%s is not an int" , value ).CascadeDecl (decl )
48
54
}
49
55
},
50
56
)
51
57
}
52
58
53
- func builtinOsEnv (prelude * Environment , decl ast.Decl ) PreludeExternFunction {
54
- return MakeExternFunction (
59
+ func builtinOsEnv (prelude * runtime. Environment , decl ast.Decl ) runtime. PreludeExternFunction {
60
+ return runtime . MakeExternFunction (
55
61
decl ,
56
- func (args []Evaluatable ) (RuntimeValue , * RuntimeError ) {
62
+ func (args []runtime. Evaluatable ) (runtime. RuntimeValue , * runtime. RuntimeError ) {
57
63
value , err := args [0 ].Evaluate ()
58
64
if err != nil {
59
65
return nil , err .CascadeDecl (decl )
60
66
}
61
- if key , ok := value .(PreludeString ); ok {
67
+ if key , ok := value .(runtime. PreludeString ); ok {
62
68
if env , ok := world .Current .Env .LookupEnv (string (key )); ok && env != "" {
63
- value , err := prelude .MakeDataRuntimeValue ("Some" , map [string ]Evaluatable {
64
- "value" : NewConstantRuntimeValue (PreludeString (env )),
69
+ value , err := prelude .MakeDataRuntimeValue ("Some" , map [string ]runtime. Evaluatable {
70
+ "value" : runtime . NewConstantRuntimeValue (runtime . PreludeString (env )),
65
71
})
66
72
return value , err .CascadeDecl (decl )
67
73
} else {
68
74
value , err := prelude .MakeEmptyDataRuntimeValue ("None" )
69
75
return value , err .CascadeDecl (decl )
70
76
}
71
77
} else {
72
- return nil , NewRuntimeErrorf ("%s is not a string" , value ).CascadeDecl (decl )
78
+ return nil , runtime . NewRuntimeErrorf ("%s is not a string" , value ).CascadeDecl (decl )
73
79
}
74
80
},
75
81
)
0 commit comments