Support concating scripts and add types to StandardLib
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2023-01-24 13:51:58 +01:00
parent 201db9e6cf
commit 9eb316c4a0
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 20 additions and 12 deletions

View File

@ -1,8 +1,7 @@
package io.gitlab.jfronny.muscript; package io.gitlab.jfronny.muscript;
import io.gitlab.jfronny.muscript.data.Scope; import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.data.dynamic.DList; import io.gitlab.jfronny.muscript.data.dynamic.*;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -37,7 +36,7 @@ public class StandardLib {
} }
// Numbers // Numbers
public static Dynamic<?> round(DList args) { public static DNumber round(DList args) {
if (args.size() == 1) { if (args.size() == 1) {
return of(Math.round(args.get(0).asNumber().getValue())); return of(Math.round(args.get(0).asNumber().getValue()));
} else if (args.size() == 2) { } else if (args.size() == 2) {
@ -48,22 +47,22 @@ public class StandardLib {
} }
} }
public static Dynamic<?> floor(DList args) { public static DNumber floor(DList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for floor: expected 1 but got " + args.size()); 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())); return of(Math.floor(args.get(0).asNumber().getValue()));
} }
public static Dynamic<?> ceil(DList args) { public static DNumber ceil(DList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for ceil: expected 1 but got " + args.size()); 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())); return of(Math.ceil(args.get(0).asNumber().getValue()));
} }
public static Dynamic<?> abs(DList args) { public static DNumber abs(DList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for abs: expected 1 but got " + args.size()); 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())); return of(Math.abs(args.get(0).asNumber().getValue()));
} }
public static Dynamic<?> random(DList args) { public static DNumber random(DList args) {
if (args.size() == 0) return of(rnd.nextDouble()); if (args.size() == 0) return of(rnd.nextDouble());
else if (args.size() == 2) { else if (args.size() == 2) {
double min = args.get(0).asNumber().getValue(); double min = args.get(0).asNumber().getValue();
@ -75,28 +74,28 @@ public class StandardLib {
} }
// Strings // Strings
public static Dynamic<?> toUpper(DList args) { public static DString toUpper(DList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toUpper: expected 1 but got " + args.size()); 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()); return of(args.get(0).asString().getValue().toUpperCase());
} }
public static Dynamic<?> toLower(DList args) { public static DString toLower(DList args) {
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for toLower: expected 1 but got " + args.size()); 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()); return of(args.get(0).asString().getValue().toLowerCase());
} }
public static Dynamic<?> contains(DList args) { public static DBool contains(DList args) {
if (args.size() != 2) throw new IllegalArgumentException("Invalid number of arguments for contains: expected 2 but got " + args.size()); 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())); return of(args.get(0).asString().getValue().contains(args.get(1).asString().getValue()));
} }
public static Dynamic<?> replace(DList args) { public static DString replace(DList args) {
if (args.size() != 3) throw new IllegalArgumentException("Invalid number of arguments for replace: expected 3 but got " + args.size()); 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())); return of(args.get(0).asString().getValue().replace(args.get(1).asString().getValue(), args.get(2).asString().getValue()));
} }
// Misc // Misc
public static Dynamic<?> listOf(DList args) { public static DList listOf(DList args) {
return args; return args;
} }
} }

View File

@ -5,6 +5,7 @@ import io.gitlab.jfronny.muscript.ast.Expr;
import io.gitlab.jfronny.muscript.data.dynamic.*; import io.gitlab.jfronny.muscript.data.dynamic.*;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
public class Script { public class Script {
private final List<Expr<?>> steps; private final List<Expr<?>> steps;
@ -40,4 +41,12 @@ public class Script {
public Script optimize() { public Script optimize() {
return new Script(steps.stream().<Expr<?>>map(Expr::optimize).toList(), fin.optimize()); return new Script(steps.stream().<Expr<?>>map(Expr::optimize).toList(), fin.optimize());
} }
public Script concat(Script other) {
return new Script(Stream.concat(this.stream(), other.stream()).toList());
}
public Stream<Expr<?>> stream() {
return Stream.concat(steps.stream(), Stream.of(fin));
}
} }