Transpiled version (8439L) is out of date.
1 | // Always thread-safe |
2 | sclass FixedVarContext > VarContext { |
3 | S[] names; // always sorted, may be null |
4 | O[] values; // same length as names, may also be null |
5 | |
6 | *() {} |
7 | *(S[] *names) { this(null, names); } |
8 | *(VarContext parent, S[] *names) { |
9 | super(parent); |
10 | if (names != null) values = new O[names.length]; |
11 | } |
12 | |
13 | int indexOfVar(S name) { |
14 | ret indexOfInSortedArray(names, name); |
15 | } |
16 | |
17 | int indexOfVarMandatory(S name) { |
18 | int idx = indexOfVar(name); |
19 | if (idx < 0) fail("Variable not found: " + name + ", known: " + joinWithComma(names)); |
20 | ret idx; |
21 | } |
22 | |
23 | O get(int idx) { |
24 | ret values[idx]; |
25 | } |
26 | |
27 | O get(S name) { |
28 | int idx = indexOfVar(name); |
29 | if (idx >= 0) ret values[idx]; |
30 | if (parent != null) |
31 | ret parent.get(name); |
32 | null; |
33 | } |
34 | |
35 | void set(int idx, O value) { |
36 | values[idx] = value; |
37 | } |
38 | |
39 | void set(S name, O value) { |
40 | int idx = indexOfVar(name); |
41 | if (idx >= 0) |
42 | values[idx] = value; |
43 | else |
44 | fail("Variable " + name + " not defined in context"); |
45 | } |
46 | |
47 | AutoCloseable tempSet(S name, O value) { |
48 | int idx = indexOfVar(name); |
49 | if (idx >= 0) { |
50 | O old = values[idx]; |
51 | values[idx] = value; |
52 | ret -> values[idx] = old; |
53 | } else |
54 | fail("Variable " + name + " not defined in context"); |
55 | } |
56 | |
57 | void unset(S name) { |
58 | set(name, null); |
59 | } |
60 | |
61 | MapSO varMap() { |
62 | new MapSO map; |
63 | int n = l(names); |
64 | for i to n: |
65 | map.put(names[i], values[i]); |
66 | ret map; |
67 | } |
68 | } |
Began life as a copy of #1033977
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035284 |
Snippet name: | FixedVarContext - a VarContext with a fixed number of variables |
Eternal ID of this version: | #1035284/4 |
Text MD5: | 0a34952a70be15bb30ce0922bd459212 |
Author: | stefan |
Category: | javax / parsing |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-04-29 00:32:06 |
Source code size: | 1536 bytes / 68 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 121 / 198 |
Version history: | 3 change(s) |
Referenced in: | [show references] |