forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.py
More file actions
55 lines (45 loc) · 1.45 KB
/
env.py
File metadata and controls
55 lines (45 loc) · 1.45 KB
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
from pixie.vm.code import as_var
from pixie.vm.object import Object, Type, runtime_error
from pixie.vm.primitives import nil
from pixie.vm.string import String
import pixie.vm.stdlib as proto
from pixie.vm.code import extend, as_var
import pixie.vm.rt as rt
import os
class Environment(Object):
_type = Type(u"pixie.stdlib.Environment")
def type(self):
return Environment._type
def val_at(self, key, not_found):
if not isinstance(key, String):
runtime_error(u"Environment variables are strings ")
key_str = str(rt.name(key))
try:
var = os.environ[key_str]
return rt.wrap(var)
except KeyError:
return not_found
# TODO: Implement me.
# def dissoc(self):
# def asssoc(self):
def reduce_vars(self, f, init):
for k, v in os.environ.items():
init = f.invoke([init, rt.map_entry(rt.wrap(k), rt.wrap(v))])
if rt.reduced_QMARK_(init):
return init
return init
@extend(proto._val_at, Environment)
def _val_at(self, key, not_found):
assert isinstance(self, Environment)
v = self.val_at(key, not_found)
return v
@extend(proto._reduce, Environment)
def _reduce(self, f, init):
assert isinstance(self, Environment)
val = self.reduce_vars(f, init)
if rt.reduced_QMARK_(val):
return rt.deref(val)
return val
@as_var("pixie.stdlib", "env")
def _env():
return Environment()