mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-18 22:30:00 +00:00
reformat all code
This commit is contained in:
@@ -338,9 +338,9 @@ public class Parser {
|
||||
|
||||
Returnable<?> value = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkReturnType(value, returnType);
|
||||
|
||||
|
||||
String id = identifier.getContent();
|
||||
|
||||
|
||||
return switch(value.returnType()) {
|
||||
case NUMBER -> new NumAssignmentNode((Returnable<Number>) value, identifier.getPosition(), scopeBuilder.num(id));
|
||||
case STRING -> new StrAssignmentNode((Returnable<String>) value, identifier.getPosition(), scopeBuilder.str(id));
|
||||
|
||||
@@ -46,9 +46,8 @@ public class Scope {
|
||||
}
|
||||
|
||||
public static final class ScopeBuilder {
|
||||
private int numSize, boolSize, strSize = 0;
|
||||
private final Map<String, Pair<Integer, ReturnType>> indices;
|
||||
|
||||
private int numSize, boolSize, strSize = 0;
|
||||
private ScopeBuilder parent;
|
||||
|
||||
public ScopeBuilder() {
|
||||
@@ -77,6 +76,7 @@ public class Scope {
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public int num(String id) {
|
||||
int num = numSize;
|
||||
indices.put(check(id), Pair.of(num, ReturnType.NUMBER));
|
||||
@@ -107,14 +107,14 @@ public class Scope {
|
||||
parent.updateBoolSize(size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void updateNumSize(int size) {
|
||||
this.numSize = FastMath.max(numSize, size);
|
||||
if(parent != null) {
|
||||
parent.updateNumSize(size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void updateStrSize(int size) {
|
||||
this.strSize = FastMath.max(strSize, size);
|
||||
if(parent != null) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
public class BooleanConstant extends ConstantExpression<Boolean> {
|
||||
private final boolean constant;
|
||||
|
||||
public BooleanConstant(Boolean constant, Position position) {
|
||||
super(constant, position);
|
||||
this.constant = constant;
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
public class NumericConstant extends ConstantExpression<Number> {
|
||||
private final double constant;
|
||||
|
||||
public NumericConstant(Number constant, Position position) {
|
||||
super(constant, position);
|
||||
this.constant = constant.doubleValue();
|
||||
|
||||
@@ -14,6 +14,23 @@ import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public interface Function<T> extends Returnable<T> {
|
||||
Function<?> NULL = new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
default double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return ((Number) apply(implementationArguments, scope)).doubleValue();
|
||||
@@ -23,21 +40,4 @@ public interface Function<T> extends Returnable<T> {
|
||||
default boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return (Boolean) apply(implementationArguments, scope);
|
||||
}
|
||||
|
||||
Function<?> NULL = new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ public class BooleanOrOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
public boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyBoolean(implementationArguments, scope) || right.applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.BOOLEAN;
|
||||
|
||||
@@ -18,16 +18,6 @@ public class ConcatenationOperation extends BinaryOperation<Object, Object> {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return toString(left.apply(implementationArguments, scope)) + toString(right.apply(implementationArguments, scope));
|
||||
}
|
||||
|
||||
private static String toString(Object object) {
|
||||
String s = object.toString();
|
||||
if(object instanceof Double) {
|
||||
@@ -38,4 +28,14 @@ public class ConcatenationOperation extends BinaryOperation<Object, Object> {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return toString(left.apply(implementationArguments, scope)) + toString(right.apply(implementationArguments, scope));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ public class ModuloOperation extends BinaryOperation<Number, Number> {
|
||||
public double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyDouble(implementationArguments, scope) % right.applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
|
||||
@@ -27,6 +27,7 @@ public class MultiplicationOperation extends BinaryOperation<Number, Number> {
|
||||
public double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyDouble(implementationArguments, scope) * right.applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
|
||||
@@ -27,6 +27,7 @@ public class NumberAdditionOperation extends BinaryOperation<Number, Number> {
|
||||
public double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyDouble(implementationArguments, scope) + right.applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
|
||||
@@ -27,6 +27,7 @@ public class SubtractionOperation extends BinaryOperation<Number, Number> {
|
||||
public double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyDouble(implementationArguments, scope) - right.applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
|
||||
@@ -42,7 +42,7 @@ public class EqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
if(leftValue instanceof Number l && rightValue instanceof Number r) {
|
||||
return FastMath.abs(l.doubleValue() - r.doubleValue()) <= EPSILON;
|
||||
}
|
||||
|
||||
|
||||
return leftValue.equals(rightValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public class LessThanOrEqualsStatement extends BinaryOperation<Number, Boolean>
|
||||
public boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyDouble(implementationArguments, scope) <= right.applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
|
||||
@@ -29,6 +29,7 @@ public class LessThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return left.applyDouble(implementationArguments, scope) < right.applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
|
||||
@@ -14,9 +14,8 @@ import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
public abstract class VariableAssignmentNode<T> implements Item<T> {
|
||||
protected final Returnable<T> value;
|
||||
private final Position position;
|
||||
protected final int index;
|
||||
|
||||
private final Position position;
|
||||
|
||||
|
||||
public VariableAssignmentNode(Returnable<T> value, Position position, int index) {
|
||||
|
||||
@@ -12,9 +12,9 @@ import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public abstract class VariableReferenceNode<T> implements Returnable<T> {
|
||||
protected final int index;
|
||||
private final Position position;
|
||||
private final ReturnType type;
|
||||
protected final int index;
|
||||
|
||||
public VariableReferenceNode(Position position, ReturnType type, int index) {
|
||||
this.position = position;
|
||||
|
||||
@@ -30,7 +30,8 @@ import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
public class ParserTest {
|
||||
@Test
|
||||
public void parse() throws IOException, ParseException {
|
||||
Parser parser = new Parser(IOUtils.toString(Objects.requireNonNull(getClass().getResourceAsStream("/test.tesf")), Charset.defaultCharset()));
|
||||
Parser parser = new Parser(
|
||||
IOUtils.toString(Objects.requireNonNull(getClass().getResourceAsStream("/test.tesf")), Charset.defaultCharset()));
|
||||
|
||||
parser.registerFunction("test", new FunctionBuilder<Test1>() {
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user