From 8b97d74e0a4e59c81b04532605aa157b9b4fc6d4 Mon Sep 17 00:00:00 2001 From: dfsek Date: Sun, 20 Dec 2020 01:50:41 -0700 Subject: [PATCH] allow constant expressions in if statements --- .../terra/api/structures/parser/Parser.java | 12 ++++++++-- .../parser/lang/ConstantExpression.java | 22 +++++++++++++++++++ .../terra/api/structures/tokenizer/Token.java | 4 ++++ common/src/test/resources/test.tesf | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/ConstantExpression.java diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java index d7f0700f2..a971995c6 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/Parser.java @@ -2,6 +2,8 @@ package com.dfsek.terra.api.structures.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.ConstantExpression; +import com.dfsek.terra.api.structures.parser.lang.Expression; import com.dfsek.terra.api.structures.parser.lang.Function; import com.dfsek.terra.api.structures.parser.lang.Item; import com.dfsek.terra.api.structures.parser.lang.Keyword; @@ -70,13 +72,13 @@ public class Parser { checkType(functionAndArguments.remove(0), Token.Type.BODY_BEGIN); - Function left = parseFunction(functionAndArguments, false); + Expression left = parseExpression(functionAndArguments); Statement statement = null; Token comparator = functionAndArguments.remove(0); checkType(comparator, Token.Type.BOOLEAN_OPERATOR); - Function right = parseFunction(functionAndArguments, false); + Expression right = parseExpression(functionAndArguments); checkType(functionAndArguments.remove(0), Token.Type.BODY_END); if(comparator.getContent().equals("==")) { @@ -89,6 +91,12 @@ public class Parser { return k; } + private Expression parseExpression(List tokens) throws ParseException { + if(tokens.get(0).isConstant()) { + return new ConstantExpression(tokens.remove(0)); + } else return parseFunction(tokens, false); + } + private Block parseBlock(List tokens) throws ParseException { List> parsedItems = new GlueList<>(); List functionArgs = new GlueList<>(); diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/ConstantExpression.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/ConstantExpression.java new file mode 100644 index 000000000..bf82fe7af --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/ConstantExpression.java @@ -0,0 +1,22 @@ +package com.dfsek.terra.api.structures.parser.lang; + +import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.platform.world.Chunk; + +public class ConstantExpression implements Expression { + private final Object constant; + + public ConstantExpression(Object constant) { + this.constant = constant; + } + + @Override + public Object apply(Location location) { + return constant; + } + + @Override + public Object apply(Location location, Chunk chunk) { + return constant; + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Token.java b/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Token.java index 34f5f9eb1..9696943bf 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Token.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Token.java @@ -28,6 +28,10 @@ public class Token { return type + ": '" + content + "'"; } + public boolean isConstant() { + return this.type.equals(Type.NUMBER) || this.type.equals(Type.STRING) || this.type.equals(Type.BOOLEAN); + } + public enum Type { /** * Function identifier or language keyword diff --git a/common/src/test/resources/test.tesf b/common/src/test/resources/test.tesf index 916c3537c..8fe2f603a 100644 --- a/common/src/test/resources/test.tesf +++ b/common/src/test/resources/test.tesf @@ -1,7 +1,7 @@ test("hello", 1); test("ghgj{}()\"\\hgjhgj", 3.4); -if(test("hello", 1) == test("hello", 1)) { +if(test("hello", 1) == "a string literal") { test("hello", 1); }