-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStandardLib.java
More file actions
219 lines (182 loc) · 8.78 KB
/
StandardLib.java
File metadata and controls
219 lines (182 loc) · 8.78 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package org.meteordev.starscript;
import org.meteordev.starscript.value.Value;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.IllegalFormatException;
import java.util.Random;
import java.util.TimeZone;
/** Standard library with some default functions and variables. */
public class StandardLib {
private static final Random rand = new Random();
public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy");
/** Adds the functions and variables to the provided {@link Starscript} instance. */
public static void init(Starscript ss) {
// Variables
ss.set("PI", Math.PI);
ss.set("time", () -> Value.string(timeFormat.format(new Date())));
ss.set("date", () -> Value.string(dateFormat.format(new Date())));
// Numbers
ss.set("round", StandardLib::round);
ss.set("roundToString", StandardLib::roundToString);
ss.set("floor", StandardLib::floor);
ss.set("ceil", StandardLib::ceil);
ss.set("abs", StandardLib::abs);
ss.set("random", StandardLib::random);
// Strings
ss.set("string", StandardLib::string);
ss.set("toUpper", StandardLib::toUpper);
ss.set("toLower", StandardLib::toLower);
ss.set("contains", StandardLib::contains);
ss.set("replace", StandardLib::replace);
ss.set("pad", StandardLib::pad);
// Formatters
ss.set("formatDateTime", StandardLib::formatDateTime);
ss.set("format", StandardLib::format);
}
// Numbers
public static Value round(Starscript ss, int argCount) {
if (argCount == 1) {
double a = ss.popNumber("Argument to round() needs to be a number.");
return Value.number(Math.round(a));
}
else if (argCount == 2) {
double b = ss.popNumber("Second argument to round() needs to be a number.");
double a = ss.popNumber("First argument to round() needs to be a number.");
double x = Math.pow(10, (int) b);
return Value.number(Math.round(a * x) / x);
}
else {
ss.error("round() requires 1 or 2 arguments, got %d.", argCount);
return null;
}
}
public static Value roundToString(Starscript ss, int argCount) {
if (argCount == 1) {
double a = ss.popNumber("Argument to round() needs to be a number.");
return Value.string(Double.toString(Math.round(a)));
}
else if (argCount == 2) {
double b = ss.popNumber("Second argument to round() needs to be a number.");
double a = ss.popNumber("First argument to round() needs to be a number.");
double x = Math.pow(10, (int) b);
return Value.string(Double.toString(Math.round(a * x) / x));
}
else {
ss.error("round() requires 1 or 2 arguments, got %d.", argCount);
return null;
}
}
public static Value floor(Starscript ss, int argCount) {
if (argCount != 1) ss.error("floor() requires 1 argument, got %d.", argCount);
double a = ss.popNumber("Argument to floor() needs to be a number.");
return Value.number(Math.floor(a));
}
public static Value ceil(Starscript ss, int argCount) {
if (argCount != 1) ss.error("ceil() requires 1 argument, got %d.", argCount);
double a = ss.popNumber("Argument to ceil() needs to be a number.");
return Value.number(Math.ceil(a));
}
public static Value abs(Starscript ss, int argCount) {
if (argCount != 1) ss.error("abs() requires 1 argument, got %d.", argCount);
double a = ss.popNumber("Argument to abs() needs to be a number.");
return Value.number(Math.abs(a));
}
public static Value random(Starscript ss, int argCount) {
if (argCount == 0) return Value.number(rand.nextDouble());
else if (argCount == 2) {
double max = ss.popNumber("Second argument to random() needs to be a number.");
double min = ss.popNumber("First argument to random() needs to be a number.");
return Value.number(min + (max - min) * rand.nextDouble());
}
ss.error("random() requires 0 or 2 arguments, got %d.", argCount);
return Value.null_();
}
// Strings
private static Value string(Starscript ss, int argCount) {
if (argCount != 1) ss.error("string() requires 1 argument, got %d.", argCount);
return Value.string(ss.pop().toString());
}
public static Value toUpper(Starscript ss, int argCount) {
if (argCount != 1) ss.error("toUpper() requires 1 argument, got %d.", argCount);
String a = ss.popString("Argument to toUpper() needs to be a string.");
return Value.string(a.toUpperCase());
}
public static Value toLower(Starscript ss, int argCount) {
if (argCount != 1) ss.error("toLower() requires 1 argument, got %d.", argCount);
String a = ss.popString("Argument to toLower() needs to be a string.");
return Value.string(a.toLowerCase());
}
public static Value contains(Starscript ss, int argCount) {
if (argCount != 2) ss.error("replace() requires 2 arguments, got %d.", argCount);
String search = ss.popString("Second argument to contains() needs to be a string.");
String string = ss.popString("First argument to contains() needs to be a string.");
return Value.bool(string.contains(search));
}
public static Value replace(Starscript ss, int argCount) {
if (argCount != 3) ss.error("replace() requires 3 arguments, got %d.", argCount);
String to = ss.popString("Third argument to replace() needs to be a string.");
String from = ss.popString("Second argument to replace() needs to be a string.");
String string = ss.popString("First argument to replace() needs to be a string.");
return Value.string(string.replace(from, to));
}
public static Value pad(Starscript ss, int argCount) {
if (argCount != 2) ss.error("pad() requires 2 arguments, got %d.", argCount);
int width = (int) ss.popNumber("Second argument to pad() needs to be a number.");
String text = ss.pop().toString();
if (text.length() >= Math.abs(width)) return Value.string(text);
char[] padded = new char[Math.max(text.length(), Math.abs(width))];
if (width >= 0) {
int padLength = width - text.length();
for (int i = 0; i < padLength; i++) padded[i] = ' ';
for (int i = 0; i < text.length(); i++) padded[padLength + i] = text.charAt(i);
}
else {
for (int i = 0; i < text.length(); i++) padded[i] = text.charAt(i);
for (int i = 0; i < Math.abs(width) - text.length(); i++) padded[text.length() + i] = ' ';
}
return Value.string(new String(padded));
}
public static Value formatDateTime(Starscript ss, int argCount) {
if (argCount < 1 || argCount > 2) ss.error("formatTime(fmt, timezone) requires 1 to 2 arguments, got %d.", argCount);
try {
String timeZone = null;
if (argCount == 2) timeZone = ss.popString("Argument to formatTime(fmt, timezone) needs to be a string.");
String fmt = ss.popString("Argument to formatTime(fmt, timezone) needs to be a string.");
SimpleDateFormat formatter = new SimpleDateFormat(fmt);
if (timeZone != null && !timeZone.isEmpty()) formatter.setTimeZone(TimeZone.getTimeZone(timeZone));
return Value.string(formatter.format(new Date()));
}
catch (IllegalArgumentException e) {
ss.error(e.toString());
}
return Value.null_();
}
public static Value format(Starscript ss, int argCount) {
if (argCount < 1) ss.error("format(fmt, ...args) requires at least 1 argument, got %d.", argCount);
ArrayList<Object> args = new ArrayList<Object>();
for (int i = 1; i < argCount; i ++) {
Value v = ss.pop();
Object o = null;
switch (v.type) {
case Boolean: o = v.getBool(); break;
case Number: o = v.getNumber(); break;
case String: o = v.getString(); break;
case Function: o = v.getFunction(); break;
case Map: o = v.getMap(); break;
case Null:
default: break;
}
args.add(0, o);
}
String fmt = ss.popString("Argument `fmt` to format() needs to be a string.");
try {
return Value.string(String.format(fmt, args.toArray()));
}
catch (IllegalFormatException e) {
ss.error(e.toString());
}
return Value.null_();
}
}