git @ Cat's Eye Technologies Robin / a9800ac
Add beginnings of `env` module. Cat's Eye Technologies 13 years ago
4 changed file(s) with 124 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
118118
119119 * Document the `list` module, add `length` and alist functions to it.
120120
121 * An `env` module, with macros for manipulating the environment, like
122 `unbind` and `sandbox`.
121 * Document the `env` module, and add some more macros to it.
123122
124123 * A macro for asserting that the correct number of arguments have been
125124 given to a macro. (Right now the `small` macros don't complain if
0 -> encoding: UTF-8
1
2 Module `env`
3 ============
4
5 -> Functionality "Interpret Robin Program" is implemented by
6 -> shell command "bin/robin %(test-file)"
7
8 -> Tests for functionality "Interpret Robin Program"
9
10 ### `unbind` ###
11
12 `unbind` removes the given symbol from the environment and evaluates its
13 second argument in that reduced environment.
14
15 | (robin (0 . 1) (small (0 . 1) env (0 . 1))
16 | (unbind if (if #t (literal x) (literal y))))
17 ? robin: uncaught exception: (unbound-identifier . if)
18
19 `unbind` removes all trace of binding from the given symbol; if it has
20 several definitions that are shadowed, none of them will be in effect.
21
22 | (robin (0 . 1) (small (0 . 1) env (0 . 1))
23 | (let ((x 7))
24 | (let ((x 8))
25 | (unbind x
26 | x))))
27 ? robin: uncaught exception: (unbound-identifier . x)
28
29 `unbind` could be implemented in Robin.
30
31 | (robin (0 . 1) (small (0 . 1) env (0 . 1))
32 | ((bind del-env
33 | (fun (self name env)
34 | (if (equal? env (literal ()))
35 | env
36 | (if (equal? name (head (head env)))
37 | (self self name (tail env))
38 | (pair (head env) (self self name (tail env))))))
39 | (macro (self args env)
40 | (eval (del-env del-env (head args) env) (head (tail args)))))
41 | if (if #t (literal x) (literal y))))
42 ? robin: uncaught exception: (unbound-identifier . if)
43
44 ### `sandbox` ###
45
46 `sandbox` takes a list of symbols as its first argument, and evaluates its
47 second argument in an environment where all bindings *except* those for the
48 listed symbols have been unbound.
49
50 | (robin (0 . 1) (small (0 . 1) env (0 . 1))
51 | (sandbox (pair tail)
52 | (tail (pair 8 9))))
53 = 9
54
55 | (robin (0 . 1) (small (0 . 1) env (0 . 1))
56 | (sandbox (pair tail)
57 | (head (pair 8 9))))
58 ? robin: uncaught exception: (unbound-identifier . head)
59
60 Could be useful to prevent some bindings from being exported from a module.
61
62 `sandbox` could be implemented in Robin.
63
64 | (robin (0 . 1) (small (0 . 1) env (0 . 1))
65 | ((bind elem
66 | (fun (self x xs)
67 | (if (equal? xs ())
68 | #f
69 | (if (equal? x (head xs))
70 | #t
71 | (self self x (tail xs)))))
72 | (bind keep-env
73 | (fun (self names env)
74 | (if (equal? env ())
75 | env
76 | (if (elem elem (head (head env)) names)
77 | (pair (head env) (self self names (tail env)))
78 | (self self names (tail env)))))
79 | (macro (self args env)
80 | (eval (keep-env keep-env (head args) env) (head (tail args))))))
81 | (pair tail)
82 | (head (pair 8 9))))
83 ? robin: uncaught exception: (unbound-identifier . head)
0 (robin (0 . 1) (small (0 . 1) list (0 . 1))
1 (let (
2 (unbind
3 (bind del-env
4 (fun (self name env)
5 (if (equal? env ())
6 env
7 (if (equal? name (head (head env)))
8 (self self name (tail env))
9 (pair (head env) (self self name (tail env))))))
10 (macro (self args env)
11 (eval (del-env del-env (head args) env) (head (tail args))))))
12 (sandbox
13 (bind elem
14 (fun (self x xs)
15 (if (equal? xs ())
16 #f
17 (if (equal? x (head xs))
18 #t
19 (self self x (tail xs)))))
20 (bind keep-env
21 (fun (self names env)
22 (if (equal? env ())
23 env
24 (if (elem elem (head (head env)) names)
25 (pair (head env) (self self names (tail env)))
26 (self self names (tail env)))))
27 (macro (self args env)
28 (eval (keep-env keep-env (head args) env) (head (tail args)))))))
29 )
30 (list
31 (pair (literal unbind) unbind)
32 (pair (literal sandbox) sandbox)
33 )
34 )
35 )
36
88 doc/module/Small.falderal \
99 doc/module/Exception.falderal \
1010 doc/module/Concurrency.falderal \
11 doc/module/List.falderal
11 doc/module/List.falderal \
12 doc/module/Environment.falderal