move modules to better directory structure

This commit is contained in:
dfsek
2021-07-08 23:08:56 -07:00
parent b609a0ba63
commit 40e28c5e4b
298 changed files with 19 additions and 14 deletions

View File

@@ -0,0 +1,20 @@
package structure;
import com.dfsek.terra.addons.terrascript.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());
}
}
}

View File

@@ -0,0 +1,89 @@
package structure;
import com.dfsek.terra.addons.terrascript.parser.Parser;
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.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;
}
}
}

View File

@@ -0,0 +1,99 @@
id "testScript";
bool thing1 = 2 > (2+2) || false;
if(2 > 2 || 3 + 4 <= 2 && 4 + 5 > 2 / 3) {
test("ok", 2);
}
test("minecraft:green_w" + "ool", (2 * (3+1) * (2 * (1+1))));
//
num testVar = 3.4;
bool boolean = true;
str stringVar = "hello!";
num precedence = 3 + 2 * 2 + 3;
test("precedence: " + precedence, 2);
num precedence2 = 3 * 2 + 2 * 3;
test("precedence 2: " + precedence2, 2);
bool iftest = false;
bool truetest = false;
num iterator = 0;
num thing = 4 - 2-2+2-2+2;
test("4 - 2 = " + thing, 2);
thing = -2;
test("-2 = " + thing, 2);
thing = -thing;
test("--2 = " + thing, 2);
for(num i = 0; i < 5; i = i + 1) {
test("i = " + i, iterator);
if(i > 1 + 1) {
test("more than 2", iterator);
continue;
}
}
for(num i = 0; i < 5; i = i + 1) {
test("i = " + i, iterator);
}
for(num j = 0; j < 5; j = j + 1) test("single statement j = " + j, iterator);
if(4 + 2 == 2 + 4) {
test("new thing " + 2, iterator);
}
while(iterator < 5) {
test("always, even after " + 2, iterator);
iterator = iterator + 1;
if(iterator > 2) {
continue;
}
test("not after " + 2, iterator);
}
if(true) test("single statement" + 2, iterator);
else if(true) test("another single statement" + 2, iterator);
if(true) {
test("true!" + 2, iterator);
} else {
test("false!" + 2, iterator);
}
if(false) {
test("true!" + 2, iterator);
} else {
test("false!" + 2, iterator);
}
if(false) {
test("true again!" + 2, iterator);
} else if(true == true) {
test("false again!" + 2, iterator);
} else {
test("not logged!" + 2, iterator);
}
// comment
/*
fsdfsd
*/
test("fdsgdf" + 2, 1 + testVar);
if(true && !(boolean && false) && true) {
num scopedVar = 2;
test("if statement" + 2 + stringVar, 1 + testVar + scopedVar);
}