-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInit.shell
More file actions
100 lines (77 loc) · 3.18 KB
/
Init.shell
File metadata and controls
100 lines (77 loc) · 3.18 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
init = () {
"This makes the function slightly faster as we only need to look at the top frame";
globalThis = globalThis;
setGlobal = globalThis.setGlobal;
"Simple true / false constants";
setGlobal("false", globalThis.isTruthy(0));
setGlobal("true", globalThis.isTruthy(1));
"
exports setGlobal function
setGlobal function is used to set global variables from inside of function scopes
";
setGlobal("setGlobal", setGlobal);
"getGlobal can be used to get global variables that have been locally overridden";
setGlobal("getGlobal", globalThis.getGlobal);
"print stuff to the console";
setGlobal("print", (...stuff) {
if(eql((){}(), stuff), () { return(return()); });
globalThis.forEach(stuff, globalThis.console.print);
});
"--- Types ---";
"Shell is the class of this shell object in java";
setGlobal("Shell", globalThis.getClass());
"Range constructor can be used in the forEach loop";
setGlobal("Range", Shell.RangeClass);
"
`return` is a special class which causes a value to be returned from a function
there is a limitation that return must be its own statement
i.e `() { return(0); } ()` will return 0 but
`() { discard = (v) {}; discard(return(0)); } ()` will return nothing
";
setGlobal("return", Shell.ReturnValueClass);
"--- Conditionals ---";
"`if` is a function that executes the second function argument if the first argument evaluates to true";
setGlobal("if", globalThis.ifThen);
"
`ifElse` executes the second function argument if the first argument evaluates to true
`ifElse` executes the third function argument if the first argument evaluates to false
";
setGlobal("ifElse", globalThis.ifElse);
"
`forEach` executes the second function for each iterated value in the first argument
";
setGlobal("forEach", globalThis.forEach);
"
`while` executes the second function argument until the first argument evaluates to false
";
setGlobal("while", globalThis.whileLoop);
"
`tryCatch` executes the first function argument and calls the second if an exception is thrown
";
setGlobal("tryCatch", globalThis.tryCatch);
"--- Conditional logic ---";
setGlobal("and", globalThis.and);
setGlobal("or", globalThis.or);
setGlobal("not", globalThis.not);
setGlobal("eql", .java.util.Objects.deepEquals);
setGlobal("isNull", .java.util.Objects.isNull);
"--- Other ---";
"Returns true if the give object is actually a class type";
setGlobal("isClass", globalThis.isClass);
"Return from n nested scopes, returnN(1, value) is same as return(value)";
setGlobal("returnN", (n, value) {
setGlobal(".value", value);
forEach(Range(1, n), (_) { (_){}(setGlobal(".value", return(getGlobal(".value")))); });
return(getGlobal(".value"));
});
"Returns true if the given variable exists in the current scope";
setGlobal("exists", globalThis.exists);
"Get a value from the parent scope, parent scope must me a function scope, not global";
setGlobal("getParentVar", (key) {
globalThis = globalThis;
frames = globalThis.env.frames;
map = globalThis.ShellMapClass.getMap(frames.get(.java.lang.Integer.sum(frames.size(), -3)));
return(map.get(key));
});
};
init();