[muscript] Standard library from starscript

This commit is contained in:
Johannes Frohnmeyer 2022-06-03 20:30:25 +02:00
parent a4e9a3425e
commit c7b9320dea
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.optic.*;
import java.util.*;
public record ExpressionParameter(Map<String, OAny<?>> value) implements OObject {
public ExpressionParameter() {
this(new HashMap<>());
}
@Override
public Map<String, OAny<?>> getValue() {
return value;
}
public ExpressionParameter set(String key, OAny<?> value) {
this.value.put(key, value);
return this;
}
}

View File

@ -0,0 +1,94 @@
package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.optic.*;
import java.text.*;
import java.util.*;
import static io.gitlab.jfronny.muscript.optic.OFinal.*;
public class StandardLib {
private static final Random rnd = new Random();
public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy");
public static ExpressionParameter addTo(ExpressionParameter parameter) {
return parameter
.set("PI", of(Math.PI))
.set("time", of(timeFormat.format(new Date())))
.set("date", of(dateFormat.format(new Date())))
.set("round", of(StandardLib::round))
.set("floor", of(StandardLib::floor))
.set("ceil", of(StandardLib::ceil))
.set("abs", of(StandardLib::abs))
.set("random", of(StandardLib::random))
.set("toUpper", of(StandardLib::toUpper))
.set("toLower", of(StandardLib::toLower))
.set("contains", of(StandardLib::contains))
.set("replace", of(StandardLib::replace));
}
// Numbers
public static OAny<?> round(OList args) {
if (args.size() == 1) {
return of(Math.round(args.get(0).asNumber().getValue()));
}
else if (args.size() == 2) {
double x = Math.pow(10, (int) (double) args.get(1).asNumber().getValue());
return of(Math.round(args.get(0).asNumber().getValue() * x) / x);
}
else {
throw new IllegalArgumentException("Invalid number of arguments for round: expected 1 or 2 but got " + args.size());
}
}
public static OAny<?> floor(OList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for floor: expected 1 but got " + args.size());
return of(Math.floor(args.get(0).asNumber().getValue()));
}
public static OAny<?> ceil(OList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for ceil: expected 1 but got " + args.size());
return of(Math.ceil(args.get(0).asNumber().getValue()));
}
public static OAny<?> abs(OList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for abs: expected 1 but got " + args.size());
return of(Math.abs(args.get(0).asNumber().getValue()));
}
public static OAny<?> random(OList args) {
if (args.size() == 0) return of(rnd.nextDouble());
else if (args.size() == 2) {
double min = args.get(0).asNumber().getValue();
double max = args.get(1).asNumber().getValue();
return of(min + (max - min) * rnd.nextDouble());
}
throw new IllegalArgumentException("Invalid number of arguments for random: expected 0 or 2 but got " + args.size());
}
// Strings
public static OAny<?> toUpper(OList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toUpper: expected 1 but got " + args.size());
return of(args.get(0).asString().getValue().toUpperCase());
}
public static OAny<?> toLower(OList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toLower: expected 1 but got " + args.size());
return of(args.get(0).asString().getValue().toLowerCase());
}
public static OAny<?> contains(OList args) {
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for contains: expected 2 but got " + args.size());
return of(args.get(0).asString().getValue().contains(args.get(1).asString().getValue()));
}
public static OAny<?> replace(OList args) {
if (args.size() != 3) throw new IllegalArgumentException("Invalid number of arguments for replace: expected 3 but got " + args.size());
return of(args.get(0).asString().getValue().replace(args.get(1).asString().getValue(), args.get(2).asString().getValue()));
}
}