Skip to content

Commit 57308e2

Browse files
committed
Merge pull request #240 from thomasmulvaney/env
Adds an evironment associative structure
2 parents aef70b1 + f140b48 commit 57308e2

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

pixie/stdlib.pxi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,3 +2340,13 @@ Expands to calls to `extend-type`."
23402340
(def *e)
23412341
(defn -set-*e [e]
23422342
(def *e e))
2343+
2344+
(extend -str Environment
2345+
(fn [v]
2346+
(let [entry->str (map (fn [e] (vector (-repr (key e)) " " (-repr (val e)))))]
2347+
(apply str "#Environment{" (conj (transduce (comp entry->str (interpose [", "]) cat) conj v) "}")))))
2348+
2349+
(extend -repr Environment
2350+
(fn [v]
2351+
(let [entry->str (map (fn [e] (vector (-repr (key e)) " " (-repr (val e)))))]
2352+
(apply str "#Environment{" (conj (transduce (comp entry->str (interpose [", "]) cat) conj v) "}")))))

pixie/vm/libs/env.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from pixie.vm.code import as_var
2+
from pixie.vm.object import Object, Type, runtime_error
3+
from pixie.vm.primitives import nil
4+
from pixie.vm.string import String
5+
import pixie.vm.stdlib as proto
6+
from pixie.vm.code import extend, as_var
7+
import pixie.vm.rt as rt
8+
import os
9+
10+
class Environment(Object):
11+
_type = Type(u"pixie.stdlib.Environment")
12+
13+
def type(self):
14+
return Environment._type
15+
16+
def val_at(self, key, not_found):
17+
if not isinstance(key, String):
18+
runtime_error(u"Environment variables are strings ")
19+
key_str = str(rt.name(key))
20+
try:
21+
var = os.environ[key_str]
22+
return rt.wrap(var)
23+
except KeyError:
24+
return not_found
25+
26+
# TODO: Implement me.
27+
# def dissoc(self):
28+
# def asssoc(self):
29+
30+
def reduce_vars(self, f, init):
31+
for k, v in os.environ.items():
32+
init = f.invoke([init, rt.map_entry(rt.wrap(k), rt.wrap(v))])
33+
if rt.reduced_QMARK_(init):
34+
return init
35+
return init
36+
37+
38+
@extend(proto._val_at, Environment)
39+
def _val_at(self, key, not_found):
40+
assert isinstance(self, Environment)
41+
v = self.val_at(key, not_found)
42+
return v
43+
44+
@extend(proto._reduce, Environment)
45+
def _reduce(self, f, init):
46+
assert isinstance(self, Environment)
47+
val = self.reduce_vars(f, init)
48+
if rt.reduced_QMARK_(val):
49+
return rt.deref(val)
50+
51+
return val
52+
53+
@as_var("pixie.stdlib", "env")
54+
def _env():
55+
return Environment()

pixie/vm/rt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def wrapper(*args):
6565
import pixie.vm.map_entry
6666
import pixie.vm.libs.platform
6767
import pixie.vm.libs.ffi
68+
import pixie.vm.libs.env
6869
import pixie.vm.symbol
6970
import pixie.vm.libs.path
7071
import pixie.vm.libs.string

pixie/vm/stdlib.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,4 +864,3 @@ def _set_current_var_frames(self, frames):
864864
Sets the current var frames. Frames should be a cons list of hashmaps containing mappings of vars to dynamic
865865
values. Setting this value to anything but this data format will cause undefined errors."""
866866
code._dynamic_vars.set_current_frames(frames)
867-

0 commit comments

Comments
 (0)