From 9eb316c4a02b8e3aedd0e0ecb64cf796e9953b9a Mon Sep 17 00:00:00 2001 From: JFronny Date: Tue, 24 Jan 2023 13:51:58 +0100 Subject: [PATCH] Support concating scripts and add types to StandardLib --- .../gitlab/jfronny/muscript/StandardLib.java | 23 +++++++++---------- .../gitlab/jfronny/muscript/data/Script.java | 9 ++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java index 675bf77..01d9c95 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/StandardLib.java @@ -1,8 +1,7 @@ package io.gitlab.jfronny.muscript; import io.gitlab.jfronny.muscript.data.Scope; -import io.gitlab.jfronny.muscript.data.dynamic.DList; -import io.gitlab.jfronny.muscript.data.dynamic.Dynamic; +import io.gitlab.jfronny.muscript.data.dynamic.*; import java.text.SimpleDateFormat; import java.util.Date; @@ -37,7 +36,7 @@ public class StandardLib { } // Numbers - public static Dynamic round(DList args) { + public static DNumber round(DList args) { if (args.size() == 1) { return of(Math.round(args.get(0).asNumber().getValue())); } 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()); 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()); 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()); 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()); else if (args.size() == 2) { double min = args.get(0).asNumber().getValue(); @@ -75,28 +74,28 @@ public class StandardLib { } // 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()); 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()); 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()); 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()); return of(args.get(0).asString().getValue().replace(args.get(1).asString().getValue(), args.get(2).asString().getValue())); } // Misc - public static Dynamic listOf(DList args) { + public static DList listOf(DList args) { return args; } } diff --git a/muscript/src/main/java/io/gitlab/jfronny/muscript/data/Script.java b/muscript/src/main/java/io/gitlab/jfronny/muscript/data/Script.java index 95eab69..f97d778 100644 --- a/muscript/src/main/java/io/gitlab/jfronny/muscript/data/Script.java +++ b/muscript/src/main/java/io/gitlab/jfronny/muscript/data/Script.java @@ -5,6 +5,7 @@ import io.gitlab.jfronny.muscript.ast.Expr; import io.gitlab.jfronny.muscript.data.dynamic.*; import java.util.List; +import java.util.stream.Stream; public class Script { private final List> steps; @@ -40,4 +41,12 @@ public class Script { public Script optimize() { return new Script(steps.stream().>map(Expr::optimize).toList(), fin.optimize()); } + + public Script concat(Script other) { + return new Script(Stream.concat(this.stream(), other.stream()).toList()); + } + + public Stream> stream() { + return Stream.concat(steps.stream(), Stream.of(fin)); + } }