mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-21 07:40:27 +00:00
split common
This commit is contained in:
236
common/implementation/src/test/java/command/CommandTest.java
Normal file
236
common/implementation/src/test/java/command/CommandTest.java
Normal file
@@ -0,0 +1,236 @@
|
||||
package command;
|
||||
|
||||
import com.dfsek.terra.api.command.CommandManager;
|
||||
import com.dfsek.terra.api.command.CommandTemplate;
|
||||
import com.dfsek.terra.api.command.TerraCommandManager;
|
||||
import com.dfsek.terra.api.command.annotation.Argument;
|
||||
import com.dfsek.terra.api.command.annotation.Command;
|
||||
import com.dfsek.terra.api.command.annotation.Subcommand;
|
||||
import com.dfsek.terra.api.command.annotation.Switch;
|
||||
import com.dfsek.terra.api.command.annotation.inject.ArgumentTarget;
|
||||
import com.dfsek.terra.api.command.annotation.inject.SwitchTarget;
|
||||
import com.dfsek.terra.api.command.arg.DoubleArgumentParser;
|
||||
import com.dfsek.terra.api.command.arg.IntegerArgumentParser;
|
||||
import com.dfsek.terra.api.command.exception.CommandException;
|
||||
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
|
||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||
import com.dfsek.terra.api.platform.CommandSender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class CommandTest {
|
||||
@Test
|
||||
public void subcommand() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager(null);
|
||||
manager.register("test", DemoParentCommand.class);
|
||||
|
||||
manager.execute("test", null, Arrays.asList("subcommand1", "first", "2"));
|
||||
manager.execute("test", null, Arrays.asList("subcommand2", "first", "2"));
|
||||
manager.execute("test", null, Arrays.asList("s1", "first", "2", "3.4"));
|
||||
manager.execute("test", null, Arrays.asList("s2", "first", "2"));
|
||||
manager.execute("test", null, Arrays.asList("sub1", "first", "2", "3.4"));
|
||||
manager.execute("test", null, Arrays.asList("sub2", "first", "2"));
|
||||
manager.execute("test", null, Arrays.asList("first", "2")); // Parent command args
|
||||
|
||||
System.out.println("ARGS: " + manager.getMaxArgumentDepth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void args() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager(null);
|
||||
manager.register("test", DemoCommand.class);
|
||||
|
||||
manager.execute("test", null, Arrays.asList("first", "2"));
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "3.4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void argsBeforeFlags() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager(null);
|
||||
manager.register("test", DemoCommand.class);
|
||||
|
||||
try {
|
||||
manager.execute("test", null, Arrays.asList("first", "-flag", "2"));
|
||||
fail();
|
||||
} catch(InvalidArgumentsException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requiredArgsFirst() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager(null);
|
||||
manager.register("test", DemoInvalidCommand.class);
|
||||
|
||||
try {
|
||||
manager.execute("test", null, Arrays.asList("first", "2"));
|
||||
fail();
|
||||
} catch(MalformedCommandException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void switches() throws CommandException {
|
||||
CommandManager manager = new TerraCommandManager(null);
|
||||
manager.register("test", DemoSwitchCommand.class);
|
||||
|
||||
manager.execute("test", null, Arrays.asList("first", "2"));
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "3.4"));
|
||||
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "-a"));
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "3.4", "-b"));
|
||||
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "-aSwitch"));
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "3.4", "-bSwitch"));
|
||||
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "-aSwitch", "-b"));
|
||||
manager.execute("test", null, Arrays.asList("first", "2", "3.4", "-bSwitch", "-a"));
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1", argumentParser = IntegerArgumentParser.class),
|
||||
@Argument(value = "arg2", required = false, argumentParser = DoubleArgumentParser.class, defaultValue = "0")
|
||||
}
|
||||
)
|
||||
public static final class DemoCommand implements CommandTemplate {
|
||||
|
||||
@ArgumentTarget("arg0")
|
||||
private String arg0;
|
||||
|
||||
@ArgumentTarget("arg1")
|
||||
private Integer arg1;
|
||||
|
||||
@ArgumentTarget("arg2")
|
||||
private Double arg2;
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender) {
|
||||
System.out.println(arg0);
|
||||
System.out.println(arg1);
|
||||
System.out.println(arg2);
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1"),
|
||||
@Argument(value = "arg2", required = false)
|
||||
},
|
||||
switches = {
|
||||
@Switch(value = "a", aliases = {"aSwitch"}),
|
||||
@Switch(value = "b", aliases = {"bSwitch"})
|
||||
}
|
||||
)
|
||||
public static final class DemoSwitchCommand implements CommandTemplate {
|
||||
@ArgumentTarget("arg0")
|
||||
private String arg0;
|
||||
|
||||
@ArgumentTarget("arg1")
|
||||
private String arg1;
|
||||
|
||||
@ArgumentTarget("arg2")
|
||||
private String arg2;
|
||||
|
||||
@SwitchTarget("a")
|
||||
private boolean a;
|
||||
|
||||
@SwitchTarget("b")
|
||||
private boolean b;
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender) {
|
||||
System.out.println(arg0);
|
||||
System.out.println(arg1);
|
||||
System.out.println(arg2);
|
||||
|
||||
System.out.println("A: " + a);
|
||||
System.out.println("B: " + b);
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg2", required = false), // optional arguments must be last. this command is invalid.
|
||||
@Argument(value = "arg1")
|
||||
}
|
||||
)
|
||||
public static final class DemoInvalidCommand implements CommandTemplate {
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender) {
|
||||
throw new Error("this should never be reached");
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1"),
|
||||
@Argument(value = "arg2", required = false),
|
||||
},
|
||||
subcommands = {
|
||||
@Subcommand(
|
||||
value = "subcommand1",
|
||||
aliases = {"s1", "sub1"},
|
||||
clazz = DemoChildCommand.class
|
||||
),
|
||||
@Subcommand(
|
||||
value = "subcommand2",
|
||||
aliases = {"s2", "sub2"},
|
||||
clazz = DemoChildCommand.class // Duplicate command intentional.
|
||||
)
|
||||
}
|
||||
)
|
||||
public static final class DemoParentCommand implements CommandTemplate {
|
||||
@ArgumentTarget("arg0")
|
||||
private String arg0;
|
||||
|
||||
@ArgumentTarget("arg1")
|
||||
private String arg1;
|
||||
|
||||
@ArgumentTarget("arg2")
|
||||
private String arg2;
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender) {
|
||||
System.out.println(arg0);
|
||||
System.out.println(arg1);
|
||||
System.out.println(arg2);
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
arguments = {
|
||||
@Argument(value = "arg0"),
|
||||
@Argument(value = "arg1"),
|
||||
@Argument(value = "arg2", required = false),
|
||||
}
|
||||
)
|
||||
public static final class DemoChildCommand implements CommandTemplate {
|
||||
@ArgumentTarget("arg0")
|
||||
private String arg0;
|
||||
|
||||
@ArgumentTarget("arg1")
|
||||
private String arg1;
|
||||
|
||||
@ArgumentTarget("arg2")
|
||||
private String arg2;
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender) {
|
||||
System.out.println(arg0);
|
||||
System.out.println(arg1);
|
||||
System.out.println(arg2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package noise;
|
||||
|
||||
import com.dfsek.tectonic.annotations.Default;
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.util.collections.ProbabilityCollection;
|
||||
|
||||
public class ColorConfigTemplate implements ConfigTemplate {
|
||||
@Value("colors")
|
||||
private ProbabilityCollection<Integer> colors;
|
||||
|
||||
@Value("enable")
|
||||
@Default
|
||||
private boolean enable = false;
|
||||
|
||||
public boolean enable() {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public ProbabilityCollection<Integer> getColors() {
|
||||
return colors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package noise;
|
||||
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class NoiseConfigTemplate implements ConfigTemplate {
|
||||
@Value(".")
|
||||
private NoiseSeeded builder;
|
||||
|
||||
public NoiseSeeded getBuilder() {
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
205
common/implementation/src/test/java/noise/NoiseTool.java
Normal file
205
common/implementation/src/test/java/noise/NoiseTool.java
Normal file
@@ -0,0 +1,205 @@
|
||||
package noise;
|
||||
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.util.collections.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import com.dfsek.terra.config.GenericLoaders;
|
||||
import com.dfsek.terra.config.fileloaders.FolderLoader;
|
||||
import com.dfsek.terra.config.loaders.ProbabilityCollectionLoader;
|
||||
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
|
||||
import com.dfsek.terra.config.loaders.config.sampler.NoiseSamplerBuilderLoader;
|
||||
import com.dfsek.terra.registry.config.NoiseRegistry;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class NoiseTool {
|
||||
public static void main(String... args) throws ConfigException, IOException {
|
||||
JFrame frame = new JFrame("Noise Viewer");
|
||||
|
||||
AtomicInteger seed = new AtomicInteger(2403);
|
||||
JLabel label = new JLabel(new ImageIcon(load(seed.get(), false, false)));
|
||||
frame.add(label);
|
||||
frame.pack();
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
|
||||
frame.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
if(e.getKeyChar() == 'r') {
|
||||
try {
|
||||
label.setIcon(new ImageIcon(load(seed.get(), false, false)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
} else if(e.getKeyChar() == 's') {
|
||||
try {
|
||||
seed.set(ThreadLocalRandom.current().nextInt());
|
||||
label.setIcon(new ImageIcon(load(seed.get(), false, false)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
} else if(e.getKeyChar() == 'd') {
|
||||
try {
|
||||
label.setIcon(new ImageIcon(load(seed.get(), true, false)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
} else if(e.getKeyChar() == 'c') {
|
||||
try {
|
||||
label.setIcon(new ImageIcon(load(seed.get(), false, true)));
|
||||
} catch(ConfigException | IOException configException) {
|
||||
configException.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
});
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private static int normal(double in, double out, double min, double max) {
|
||||
double range = max - min;
|
||||
return (int) (((in - min) * out) / range);
|
||||
}
|
||||
|
||||
private static int buildRGBA(int in) {
|
||||
return (255 << 24)
|
||||
+ (in << 16)
|
||||
+ (in << 8)
|
||||
+ in;
|
||||
}
|
||||
|
||||
private static BufferedImage load(int seed, boolean distribution, boolean chunk) throws ConfigException, IOException {
|
||||
long s = System.nanoTime();
|
||||
|
||||
FolderLoader folderLoader = new FolderLoader(Paths.get("./"));
|
||||
|
||||
ConfigLoader loader = new ConfigLoader();
|
||||
loader.registerLoader(NoiseSeeded.class, new NoiseSamplerBuilderLoader(new NoiseRegistry()))
|
||||
.registerLoader(BufferedImage.class, new BufferedImageLoader(folderLoader))
|
||||
.registerLoader(ProbabilityCollection.class, new ProbabilityCollectionLoader());
|
||||
|
||||
new GenericLoaders(null).register(loader);
|
||||
NoiseConfigTemplate template = new NoiseConfigTemplate();
|
||||
|
||||
File file = new File("./config.yml");
|
||||
|
||||
System.out.println(file.getAbsolutePath());
|
||||
|
||||
File colorFile = new File("./color.yml");
|
||||
|
||||
|
||||
System.out.println(file.getAbsolutePath());
|
||||
if(!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
FileUtils.copyInputStreamToFile(NoiseTool.class.getResourceAsStream("/config.yml"), file);
|
||||
}
|
||||
|
||||
|
||||
boolean colors = false;
|
||||
ColorConfigTemplate color = new ColorConfigTemplate();
|
||||
if(colorFile.exists()) {
|
||||
loader.load(color, new FileInputStream(colorFile));
|
||||
colors = color.enable();
|
||||
}
|
||||
ProbabilityCollection<Integer> colorCollection = color.getColors();
|
||||
|
||||
loader.load(template, new FileInputStream(file));
|
||||
System.out.println(template.getBuilder().getDimensions());
|
||||
NoiseSampler noise = template.getBuilder().apply((long) seed);
|
||||
|
||||
|
||||
int size = 1024;
|
||||
|
||||
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
double[][] noiseVals = new double[size][size];
|
||||
int[][] rgbVals = new int[size][size];
|
||||
double max = Double.MIN_VALUE;
|
||||
double min = Double.MAX_VALUE;
|
||||
|
||||
int[] buckets = new int[1024];
|
||||
|
||||
for(int x = 0; x < noiseVals.length; x++) {
|
||||
for(int z = 0; z < noiseVals[x].length; z++) {
|
||||
double n = noise.getNoise(x, z);
|
||||
noiseVals[x][z] = n;
|
||||
max = Math.max(n, max);
|
||||
min = Math.min(n, min);
|
||||
if(colors) rgbVals[x][z] = colorCollection.get(noise, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
for(int x = 0; x < noiseVals.length; x++) {
|
||||
for(int z = 0; z < noiseVals[x].length; z++) {
|
||||
if(colors) image.setRGB(x, z, rgbVals[x][z] + (255 << 24));
|
||||
else image.setRGB(x, z, buildRGBA(normal(noiseVals[x][z], 255, min, max)));
|
||||
buckets[normal(noiseVals[x][z], size - 1, min, max)]++;
|
||||
}
|
||||
}
|
||||
|
||||
long time = System.nanoTime() - s;
|
||||
|
||||
double ms = time / 1000000d;
|
||||
|
||||
|
||||
if(chunk) {
|
||||
for(int x = 0; x < image.getWidth(); x += 16) {
|
||||
for(int y = 0; y < image.getHeight(); y++) image.setRGB(x, y, buildRGBA(0));
|
||||
}
|
||||
for(int y = 0; y < image.getWidth(); y += 16) {
|
||||
for(int x = 0; x < image.getHeight(); x++) image.setRGB(x, y, buildRGBA(0));
|
||||
}
|
||||
}
|
||||
|
||||
Graphics graphics = image.getGraphics();
|
||||
graphics.setColor(Color.WHITE);
|
||||
graphics.fillRect(0, 0, 325, 90);
|
||||
graphics.setColor(Color.BLACK);
|
||||
graphics.setFont(new Font("Monospace", Font.BOLD, 20));
|
||||
graphics.drawString("min: " + min, 0, 20);
|
||||
graphics.drawString("max: " + max, 0, 40);
|
||||
graphics.drawString("seed: " + seed, 0, 60);
|
||||
graphics.drawString("time: " + ms + "ms", 0, 80);
|
||||
|
||||
if(distribution) {
|
||||
graphics.setColor(Color.WHITE);
|
||||
graphics.fillRect(0, size - (size / 4) - 1, size, (size / 4) - 1);
|
||||
int highestBucket = Integer.MIN_VALUE;
|
||||
for(int i : buckets) highestBucket = Math.max(highestBucket, i);
|
||||
graphics.setColor(Color.BLACK);
|
||||
graphics.drawString("" + highestBucket, 0, size - (size / 4) - 1 + 20);
|
||||
|
||||
for(int x = 0; x < size; x++) {
|
||||
for(int y = 0; y < ((double) buckets[x] / highestBucket) * ((double) size / 4); y++) {
|
||||
image.setRGB(x, size - y - 1, buildRGBA(0));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package profiler;
|
||||
|
||||
import com.dfsek.terra.profiler.Profiler;
|
||||
import com.dfsek.terra.profiler.ProfilerImpl;
|
||||
|
||||
public class ProfilerTest {
|
||||
private static final Profiler PROFILER = new ProfilerImpl();
|
||||
//@Test
|
||||
public static void main(String... a) throws InterruptedException {
|
||||
//PROFILER.start();
|
||||
for(int i = 0; i < 1000; i++) {
|
||||
doThing();
|
||||
}
|
||||
|
||||
for(int i = 0; i < 100; i++) {
|
||||
doThirdOtherThing();
|
||||
}
|
||||
|
||||
for(int i = 0; i < 100; i++) {
|
||||
doOtherThing();
|
||||
}
|
||||
PROFILER.stop();
|
||||
PROFILER.push("thing");
|
||||
PROFILER.push("thing2");
|
||||
PROFILER.start();
|
||||
PROFILER.pop("thing2");
|
||||
PROFILER.pop("thing");
|
||||
PROFILER.push("thing4");
|
||||
PROFILER.pop("thing4");
|
||||
|
||||
PROFILER.getTimings().forEach((id, timings) -> {
|
||||
System.out.println(id + ": " + timings.toString());
|
||||
});
|
||||
}
|
||||
|
||||
private static void doThing() throws InterruptedException {
|
||||
PROFILER.push("thing");
|
||||
Thread.sleep(1);
|
||||
doOtherThing();
|
||||
thing4();
|
||||
PROFILER.pop("thing");
|
||||
}
|
||||
|
||||
private static void doOtherThing() throws InterruptedException {
|
||||
PROFILER.push("thing2");
|
||||
Thread.sleep(2);
|
||||
doThirdOtherThing();
|
||||
thing4();
|
||||
PROFILER.pop("thing2");
|
||||
}
|
||||
|
||||
private static void doThirdOtherThing() throws InterruptedException {
|
||||
PROFILER.push("thing3");
|
||||
Thread.sleep(2);
|
||||
PROFILER.pop("thing3");
|
||||
}
|
||||
|
||||
private static void thing4() throws InterruptedException {
|
||||
PROFILER.push("thing4");
|
||||
Thread.sleep(2);
|
||||
PROFILER.pop("thing4");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package structure;
|
||||
|
||||
import com.dfsek.terra.api.structures.tokenizer.Lookahead;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
public class LookaheadTest {
|
||||
@Test
|
||||
public void lookahead() {
|
||||
Lookahead lookahead = new Lookahead(new StringReader("Test string..."));
|
||||
|
||||
for(int i = 0; lookahead.next(i) != null; i++) {
|
||||
System.out.print(lookahead.next(i).getCharacter());
|
||||
}
|
||||
while(lookahead.next(0) != null) {
|
||||
System.out.print(lookahead.consume().getCharacter());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package structure;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.Parser;
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||
import com.dfsek.terra.api.structures.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.parser.lang.variables.Variable;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParserTest {
|
||||
@Test
|
||||
public void parse() throws IOException, ParseException {
|
||||
Parser parser = new Parser(IOUtils.toString(getClass().getResourceAsStream("/test.tesf"), Charset.defaultCharset()));
|
||||
|
||||
parser.registerFunction("test", new FunctionBuilder<Test1>() {
|
||||
@Override
|
||||
public Test1 build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new Test1(argumentList.get(0), argumentList.get(1), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int argNumber() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
switch(position) {
|
||||
case 0:
|
||||
return Returnable.ReturnType.STRING;
|
||||
case 1:
|
||||
return Returnable.ReturnType.NUMBER;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
long l = System.nanoTime();
|
||||
Block block = parser.parse();
|
||||
long t = System.nanoTime() - l;
|
||||
System.out.println("Took " + (double) t / 1000000);
|
||||
|
||||
block.apply(null, new HashMap<>());
|
||||
|
||||
block.apply(null, new HashMap<>());
|
||||
}
|
||||
|
||||
private static class Test1 implements Function<Void> {
|
||||
private final Returnable<?> a;
|
||||
private final Returnable<?> b;
|
||||
private final Position position;
|
||||
|
||||
public Test1(Returnable<?> a, Returnable<?> b, Position position) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
|
||||
System.out.println("string: " + a.apply(implementationArguments, variableMap) + ", double: " + b.apply(implementationArguments, variableMap));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.VOID;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user