From 4f40bcbe5e186d806005bcb6d8c313f64b93a6ad Mon Sep 17 00:00:00 2001 From: dfsek Date: Tue, 22 Dec 2020 02:00:40 -0700 Subject: [PATCH] Implement variables --- .../terra/api/structures/parser/Parser.java | 121 ++++++++++++++---- .../parser/lang/variables/Assignment.java | 38 ++++++ .../lang/variables/BooleanVariable.java | 34 +++++ .../parser/lang/variables/Getter.java | 34 +++++ .../parser/lang/variables/NumberVariable.java | 34 +++++ .../parser/lang/variables/StringVariable.java | 34 +++++ .../parser/lang/variables/Variable.java | 14 ++ .../terra/api/structures/tokenizer/Token.java | 8 +- .../api/structures/tokenizer/Tokenizer.java | 15 ++- .../src/test/java/structure/ParserTest.java | 28 ++-- common/src/test/resources/test.tesf | 13 +- 11 files changed, 320 insertions(+), 53 deletions(-) create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Assignment.java create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/BooleanVariable.java create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Getter.java create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/NumberVariable.java create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/StringVariable.java create mode 100644 common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Variable.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 cb2f20115..d2461e48a 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 @@ -26,6 +26,12 @@ import com.dfsek.terra.api.structures.parser.lang.operations.statements.GreaterT import com.dfsek.terra.api.structures.parser.lang.operations.statements.LessThanOrEqualsStatement; import com.dfsek.terra.api.structures.parser.lang.operations.statements.LessThanStatement; import com.dfsek.terra.api.structures.parser.lang.operations.statements.NotEqualsStatement; +import com.dfsek.terra.api.structures.parser.lang.variables.Assignment; +import com.dfsek.terra.api.structures.parser.lang.variables.BooleanVariable; +import com.dfsek.terra.api.structures.parser.lang.variables.Getter; +import com.dfsek.terra.api.structures.parser.lang.variables.NumberVariable; +import com.dfsek.terra.api.structures.parser.lang.variables.StringVariable; +import com.dfsek.terra.api.structures.parser.lang.variables.Variable; import com.dfsek.terra.api.structures.tokenizer.Position; import com.dfsek.terra.api.structures.tokenizer.Token; import com.dfsek.terra.api.structures.tokenizer.Tokenizer; @@ -42,7 +48,7 @@ import java.util.Set; public class Parser { private final String data; private final Map>> functions = new HashMap<>(); - private final Set keywords = Sets.newHashSet("if"); + private final Set keywords = Sets.newHashSet("if", "return", "var"); Set allowedArguments = Sets.newHashSet(Token.Type.STRING, Token.Type.NUMBER, Token.Type.IDENTIFIER); @@ -73,12 +79,12 @@ public class Parser { } if(blockLevel != 0) throw new ParseException("Dangling opening brace"); - return parseBlock(tokens); + return parseBlock(tokens, new HashMap<>()); } @SuppressWarnings("unchecked") - private Keyword parseKeyword(List tokens) throws ParseException { + private Keyword parseKeyword(List tokens, Map> variableMap) throws ParseException { Token identifier = tokens.remove(0); checkType(identifier, Token.Type.KEYWORD); @@ -89,24 +95,24 @@ public class Parser { checkType(tokens.remove(0), Token.Type.GROUP_BEGIN); - Returnable comparator = parseExpression(tokens, true); + Returnable comparator = parseExpression(tokens, true, variableMap); checkReturnType(comparator, Returnable.ReturnType.BOOLEAN); checkType(tokens.remove(0), Token.Type.GROUP_END); checkType(tokens.remove(0), Token.Type.BLOCK_BEGIN); - k = new IfKeyword(parseBlock(tokens), (Returnable) comparator, identifier.getPosition()); + k = new IfKeyword(parseBlock(tokens, variableMap), (Returnable) comparator, identifier.getPosition()); } return k; } @SuppressWarnings("unchecked") - private Returnable parseExpression(List tokens, boolean full) throws ParseException { + private Returnable parseExpression(List tokens, boolean full, Map> variableMap) throws ParseException { Token first = tokens.get(0); - if(first.getType().equals(Token.Type.GROUP_BEGIN)) return parseGroup(tokens); + if(first.getType().equals(Token.Type.GROUP_BEGIN)) return parseGroup(tokens, variableMap); checkType(first, Token.Type.IDENTIFIER, Token.Type.BOOLEAN, Token.Type.STRING, Token.Type.NUMBER, Token.Type.BOOLEAN_NOT, Token.Type.GROUP_BEGIN); @@ -116,8 +122,9 @@ public class Parser { tokens.remove(0); } + Token id = tokens.get(0); Returnable expression; - if(tokens.get(0).isConstant()) { + if(id.isConstant()) { Token constantToken = tokens.remove(0); Position position = constantToken.getPosition(); switch(constantToken.getType()) { @@ -134,7 +141,13 @@ public class Parser { default: throw new UnsupportedOperationException("Unsupported constant token: " + constantToken.getType() + " at position: " + position); } - } else expression = parseFunction(tokens, false); + } else { + if(functions.containsKey(id.getContent())) expression = parseFunction(tokens, false, variableMap); + else if(variableMap.containsKey(id.getContent())) { + checkType(tokens.remove(0), Token.Type.IDENTIFIER); + expression = new Getter(variableMap.get(id.getContent())); + } else throw new ParseException("Unexpected token: " + id.getContent() + " at " + id.getPosition()); + } if(not) { @@ -142,32 +155,32 @@ public class Parser { expression = new BooleanNotOperation((Returnable) expression, expression.getPosition()); } if(full && tokens.get(0).isBinaryOperator()) { - return parseBinaryOperation(expression, tokens); + return parseBinaryOperation(expression, tokens, variableMap); } return expression; } - private Returnable parseGroup(List tokens) throws ParseException { + private Returnable parseGroup(List tokens, Map> variableMap) throws ParseException { checkType(tokens.remove(0), Token.Type.GROUP_BEGIN); - Returnable expression = parseExpression(tokens, true); + Returnable expression = parseExpression(tokens, true, variableMap); checkType(tokens.remove(0), Token.Type.GROUP_END); return expression; } - private BinaryOperation parseBinaryOperation(Returnable left, List tokens) throws ParseException { + private BinaryOperation parseBinaryOperation(Returnable left, List tokens, Map> variableMap) throws ParseException { Token binaryOperator = tokens.remove(0); checkType(binaryOperator, Token.Type.ADDITION_OPERATOR, Token.Type.MULTIPLICATION_OPERATOR, Token.Type.DIVISION_OPERATOR, Token.Type.SUBTRACTION_OPERATOR, Token.Type.GREATER_THAN_OPERATOR, Token.Type.LESS_THAN_OPERATOR, Token.Type.LESS_THAN_OR_EQUALS_OPERATOR, Token.Type.GREATER_THAN_OR_EQUALS_OPERATOR, Token.Type.EQUALS_OPERATOR, Token.Type.NOT_EQUALS_OPERATOR, Token.Type.BOOLEAN_AND, Token.Type.BOOLEAN_OR); - Returnable right = parseExpression(tokens, false); + Returnable right = parseExpression(tokens, false, variableMap); Token other = tokens.get(0); if(other.isBinaryOperator() && (other.getType().equals(Token.Type.MULTIPLICATION_OPERATOR) || other.getType().equals(Token.Type.DIVISION_OPERATOR))) { - return assemble(left, parseBinaryOperation(right, tokens), binaryOperator); + return assemble(left, parseBinaryOperation(right, tokens, variableMap), binaryOperator); } else if(other.isBinaryOperator()) { - return parseBinaryOperation(assemble(left, right, binaryOperator), tokens); + return parseBinaryOperation(assemble(left, right, binaryOperator), tokens, variableMap); } return assemble(left, right, binaryOperator); } @@ -209,35 +222,84 @@ public class Parser { } } - private Block parseBlock(List tokens) throws ParseException { + private Variable parseVariableDeclaration(List tokens, Returnable.ReturnType type) throws ParseException { + checkVarType(tokens.get(0), type); // Check for type mismatch + switch(type) { + case NUMBER: + return new NumberVariable(0d, tokens.get(0).getPosition()); + case STRING: + return new StringVariable("", tokens.get(0).getPosition()); + case BOOLEAN: + return new BooleanVariable(false, tokens.get(0).getPosition()); + } + throw new UnsupportedOperationException("Unsupported variable type: " + type); + } + + private Block parseBlock(List tokens, Map> superVars) throws ParseException { List> parsedItems = new GlueList<>(); + + Map> parsedVariables = new HashMap<>(superVars); // New hashmap as to not mutate parent scope's declarations. + Token first = tokens.get(0); - checkType(tokens.get(0), Token.Type.IDENTIFIER, Token.Type.KEYWORD); + checkType(tokens.get(0), Token.Type.IDENTIFIER, Token.Type.KEYWORD, Token.Type.NUMBER_VARIABLE, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE); main: while(tokens.size() > 0) { Token token = tokens.get(0); - checkType(token, Token.Type.IDENTIFIER, Token.Type.KEYWORD, Token.Type.BLOCK_END); + checkType(token, Token.Type.IDENTIFIER, Token.Type.KEYWORD, Token.Type.BLOCK_END, Token.Type.NUMBER_VARIABLE, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE); switch(token.getType()) { case KEYWORD: - parsedItems.add(parseKeyword(tokens)); + parsedItems.add(parseKeyword(tokens, parsedVariables)); if(tokens.isEmpty()) break; checkType(tokens.get(0), Token.Type.IDENTIFIER, Token.Type.KEYWORD, Token.Type.BLOCK_END); break; case IDENTIFIER: - parsedItems.add(parseFunction(tokens, true)); + parsedItems.add(parseFunction(tokens, true, parsedVariables)); if(tokens.isEmpty()) break; checkType(tokens.remove(0), Token.Type.STATEMENT_END, Token.Type.BLOCK_END); break; case BLOCK_END: - tokens.remove(0); + tokens.remove(0); // Remove block end. break main; + case NUMBER_VARIABLE: + case BOOLEAN_VARIABLE: + case STRING_VARIABLE: + Variable temp; + if(token.getType().equals(Token.Type.NUMBER_VARIABLE)) + temp = parseVariableDeclaration(tokens, Returnable.ReturnType.NUMBER); + else if(token.getType().equals(Token.Type.STRING_VARIABLE)) + temp = parseVariableDeclaration(tokens, Returnable.ReturnType.STRING); + else temp = parseVariableDeclaration(tokens, Returnable.ReturnType.BOOLEAN); + Token name = tokens.get(1); + + checkType(name, Token.Type.IDENTIFIER); + + parsedVariables.put(name.getContent(), temp); + parsedItems.add(parseAssignment(temp, tokens, parsedVariables)); + checkType(tokens.remove(0), Token.Type.STATEMENT_END); } } return new Block(parsedItems, first.getPosition()); } - private Function parseFunction(List tokens, boolean fullStatement) throws ParseException { + @SuppressWarnings("unchecked") + private Assignment parseAssignment(Variable variable, List tokens, Map> variableMap) throws ParseException { + checkType(tokens.remove(0), Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.NUMBER_VARIABLE); + + Token name = tokens.get(0); + + checkType(tokens.remove(0), Token.Type.IDENTIFIER); + + checkType(tokens.remove(0), Token.Type.ASSIGNMENT); + + Returnable expression = parseExpression(tokens, true, variableMap); + + checkReturnType(expression, variable.getType()); + + return new Assignment<>((Variable) variable, (Returnable) expression, name.getPosition()); + } + + private Function parseFunction(List tokens, boolean fullStatement, Map> variableMap) throws ParseException { Token identifier = tokens.remove(0); checkType(identifier, Token.Type.IDENTIFIER); // First token must be identifier @@ -247,7 +309,7 @@ public class Parser { checkType(tokens.remove(0), Token.Type.GROUP_BEGIN); // Second is body begin - List> args = getArgs(tokens); // Extract arguments, consume the rest. + List> args = getArgs(tokens, variableMap); // Extract arguments, consume the rest. tokens.remove(0); // Remove body end @@ -268,11 +330,11 @@ public class Parser { } - private List> getArgs(List tokens) throws ParseException { + private List> getArgs(List tokens, Map> variableMap) throws ParseException { List> args = new GlueList<>(); while(!tokens.get(0).getType().equals(Token.Type.GROUP_END)) { - args.add(parseExpression(tokens, true)); + args.add(parseExpression(tokens, true, variableMap)); checkType(tokens.get(0), Token.Type.SEPARATOR, Token.Type.GROUP_END); if(tokens.get(0).getType().equals(Token.Type.SEPARATOR)) tokens.remove(0); } @@ -300,4 +362,11 @@ public class Parser { throw new ParseException("Operation " + operation.getType() + " not supported between " + left.returnType() + " and " + right.returnType() + ": " + operation.getPosition()); } } + + private void checkVarType(Token token, Returnable.ReturnType returnType) throws ParseException { + if(returnType.equals(Returnable.ReturnType.STRING) && token.getType().equals(Token.Type.STRING_VARIABLE)) return; + if(returnType.equals(Returnable.ReturnType.NUMBER) && token.getType().equals(Token.Type.NUMBER_VARIABLE)) return; + if(returnType.equals(Returnable.ReturnType.BOOLEAN) && token.getType().equals(Token.Type.BOOLEAN_VARIABLE)) return; + throw new ParseException("Type mismatch, cannot convert from " + returnType + " to " + token.getType() + ": " + token.getPosition()); + } } diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Assignment.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Assignment.java new file mode 100644 index 000000000..08f71a834 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Assignment.java @@ -0,0 +1,38 @@ +package com.dfsek.terra.api.structures.parser.lang.variables; + +import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.platform.world.Chunk; +import com.dfsek.terra.api.structures.parser.lang.Item; +import com.dfsek.terra.api.structures.parser.lang.Returnable; +import com.dfsek.terra.api.structures.tokenizer.Position; + +public class Assignment implements Item { + private final Variable delegate; + private final Returnable value; + private final Position position; + + public Assignment(Variable delegate, Returnable value, Position position) { + this.delegate = delegate; + this.value = value; + this.position = position; + } + + @Override + public T apply(Location location) { + T val = value.apply(location); + delegate.setValue(val); + return val; + } + + @Override + public T apply(Location location, Chunk chunk) { + T val = value.apply(location, chunk); + delegate.setValue(val); + return val; + } + + @Override + public Position getPosition() { + return position; + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/BooleanVariable.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/BooleanVariable.java new file mode 100644 index 000000000..ad0ed5400 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/BooleanVariable.java @@ -0,0 +1,34 @@ +package com.dfsek.terra.api.structures.parser.lang.variables; + +import com.dfsek.terra.api.structures.parser.lang.Returnable; +import com.dfsek.terra.api.structures.tokenizer.Position; + +public class BooleanVariable implements Variable { + private final Position position; + private Boolean value; + + public BooleanVariable(Boolean value, Position position) { + this.value = value; + this.position = position; + } + + @Override + public Boolean getValue() { + return value; + } + + @Override + public void setValue(Boolean value) { + this.value = value; + } + + @Override + public Returnable.ReturnType getType() { + return Returnable.ReturnType.BOOLEAN; + } + + @Override + public Position getPosition() { + return position; + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Getter.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Getter.java new file mode 100644 index 000000000..b7fe3eedd --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Getter.java @@ -0,0 +1,34 @@ +package com.dfsek.terra.api.structures.parser.lang.variables; + +import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.platform.world.Chunk; +import com.dfsek.terra.api.structures.parser.lang.Returnable; +import com.dfsek.terra.api.structures.tokenizer.Position; + +public class Getter implements Returnable { + private final Variable delegate; + + public Getter(Variable delegate) { + this.delegate = delegate; + } + + @Override + public ReturnType returnType() { + return delegate.getType(); + } + + @Override + public Object apply(Location location) { + return delegate.getValue(); + } + + @Override + public Object apply(Location location, Chunk chunk) { + return delegate.getValue(); + } + + @Override + public Position getPosition() { + return delegate.getPosition(); + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/NumberVariable.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/NumberVariable.java new file mode 100644 index 000000000..6f7355cb4 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/NumberVariable.java @@ -0,0 +1,34 @@ +package com.dfsek.terra.api.structures.parser.lang.variables; + +import com.dfsek.terra.api.structures.parser.lang.Returnable; +import com.dfsek.terra.api.structures.tokenizer.Position; + +public class NumberVariable implements Variable { + private final Position position; + private Number value; + + public NumberVariable(Number value, Position position) { + this.value = value; + this.position = position; + } + + @Override + public Number getValue() { + return value; + } + + @Override + public void setValue(Number value) { + this.value = value; + } + + @Override + public Returnable.ReturnType getType() { + return Returnable.ReturnType.NUMBER; + } + + @Override + public Position getPosition() { + return position; + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/StringVariable.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/StringVariable.java new file mode 100644 index 000000000..a2b48e73e --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/StringVariable.java @@ -0,0 +1,34 @@ +package com.dfsek.terra.api.structures.parser.lang.variables; + +import com.dfsek.terra.api.structures.parser.lang.Returnable; +import com.dfsek.terra.api.structures.tokenizer.Position; + +public class StringVariable implements Variable { + private final Position position; + private String value; + + public StringVariable(String value, Position position) { + this.value = value; + this.position = position; + } + + @Override + public String getValue() { + return value; + } + + @Override + public void setValue(String value) { + this.value = value; + } + + @Override + public Returnable.ReturnType getType() { + return Returnable.ReturnType.STRING; + } + + @Override + public Position getPosition() { + return position; + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Variable.java b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Variable.java new file mode 100644 index 000000000..4db60a3bf --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/structures/parser/lang/variables/Variable.java @@ -0,0 +1,14 @@ +package com.dfsek.terra.api.structures.parser.lang.variables; + +import com.dfsek.terra.api.structures.parser.lang.Returnable; +import com.dfsek.terra.api.structures.tokenizer.Position; + +public interface Variable { + T getValue(); + + void setValue(T value); + + Returnable.ReturnType getType(); + + Position getPosition(); +} 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 d6cea01a3..ab1d9bea5 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 @@ -163,6 +163,12 @@ public class Token { /** * Boolean and */ - BOOLEAN_AND + BOOLEAN_AND, + /** + * Variable declaration + */ + NUMBER_VARIABLE, + STRING_VARIABLE, + BOOLEAN_VARIABLE } } diff --git a/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Tokenizer.java b/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Tokenizer.java index 526f6c3dd..18a62bf46 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Tokenizer.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/tokenizer/Tokenizer.java @@ -12,7 +12,7 @@ public class Tokenizer { private final Lookahead reader; private final Set syntaxSignificant = Sets.newHashSet(';', '(', ')', '"', ',', '\\', '=', '{', '}', '+', '-', '*', '/', '>', '<', '!'); // Reserved chars - private final Set keywords = Sets.newHashSet("if", "return"); + private final Set keywords = Sets.newHashSet("if", "return", "num", "bool", "str"); public Tokenizer(String data) { @@ -20,7 +20,7 @@ public class Tokenizer { } public boolean hasNext() { - while(!reader.current().isEOF() && reader.current().isWhitespace()) reader.consume(); // Consume whitespace. + //while(!reader.current().isEOF() && reader.current().isWhitespace()) reader.consume(); // Consume whitespace. return !reader.current().isEOF(); } @@ -50,11 +50,19 @@ public class Tokenizer { return new Token(">=", Token.Type.GREATER_THAN_OR_EQUALS_OPERATOR, new Position(reader.getLine(), reader.getIndex())); if(reader.matches("<=", true)) return new Token("<=", Token.Type.LESS_THAN_OR_EQUALS_OPERATOR, new Position(reader.getLine(), reader.getIndex())); + if(reader.matches("||", true)) return new Token("||", Token.Type.BOOLEAN_OR, new Position(reader.getLine(), reader.getIndex())); if(reader.matches("&&", true)) return new Token("&&", Token.Type.BOOLEAN_AND, new Position(reader.getLine(), reader.getIndex())); + if(reader.matches("num ", true)) + return new Token("num ", Token.Type.NUMBER_VARIABLE, new Position(reader.getLine(), reader.getIndex())); + if(reader.matches("str ", true)) + return new Token("str ", Token.Type.STRING_VARIABLE, new Position(reader.getLine(), reader.getIndex())); + if(reader.matches("bool ", true)) + return new Token("bool ", Token.Type.BOOLEAN_VARIABLE, new Position(reader.getLine(), reader.getIndex())); + if(isNumberStart()) { StringBuilder num = new StringBuilder(); @@ -111,7 +119,8 @@ public class Tokenizer { StringBuilder token = new StringBuilder(); while(!reader.current().isEOF() && !isSyntaxSignificant(reader.current().getCharacter())) { Char c = reader.consume(); - if(!c.isWhitespace()) token.append(c); + if(c.isWhitespace()) break; + token.append(c); } String tokenString = token.toString(); diff --git a/common/src/test/java/structure/ParserTest.java b/common/src/test/java/structure/ParserTest.java index f69936b5e..dadd2a9bd 100644 --- a/common/src/test/java/structure/ParserTest.java +++ b/common/src/test/java/structure/ParserTest.java @@ -4,7 +4,7 @@ import com.dfsek.terra.api.math.vector.Location; import com.dfsek.terra.api.platform.world.Chunk; 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.Item; +import com.dfsek.terra.api.structures.parser.lang.Block; 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; @@ -23,7 +23,7 @@ public class ParserTest { parser.addFunction("test", new FunctionBuilder() { @Override public Test1 build(List> argumentList, Position position) throws ParseException { - return new Test1(argumentList.get(0).apply(new Location(null, 0, 0, 0)).toString(), Double.parseDouble(argumentList.get(1).apply(new Location(null, 0, 0, 0)).toString()), position); + return new Test1(argumentList.get(0), argumentList.get(1), position); } @Override @@ -46,34 +46,27 @@ public class ParserTest { }); long l = System.nanoTime(); - List> functions = parser.parse().getItems(); + Block block = parser.parse(); long t = System.nanoTime() - l; System.out.println("Took " + (double) t / 1000000); - for(Item f : functions) System.out.println(f); + block.apply(null); } private static class Test1 implements Function { - private final String a; - private final double b; + private final Returnable a; + private final Returnable b; private final Position position; - public Test1(String a, double b, Position position) { + public Test1(Returnable a, Returnable b, Position position) { this.a = a; this.b = b; this.position = position; } - public String getA() { - return a; - } - - public double getB() { - return b; - } - @Override public Void apply(Location location) { + System.out.println("string: " + a.apply(location) + ", double: " + b.apply(location)); return null; } @@ -92,11 +85,6 @@ public class ParserTest { return null; } - @Override - public String toString() { - return "string: " + a + ", double: " + b; - } - @Override public ReturnType returnType() { return ReturnType.VOID; diff --git a/common/src/test/resources/test.tesf b/common/src/test/resources/test.tesf index 1136218f8..76de53504 100644 --- a/common/src/test/resources/test.tesf +++ b/common/src/test/resources/test.tesf @@ -1,5 +1,12 @@ test("hello" + 3 + "gdfg", (2 * (3+1) * (2 * (1+1)))); -if(true || false) { - test("fdsgdf" + 2, 1); -} \ No newline at end of file +num testVar = 3.4; +bool boolean = true; +str stringVar = "hello!"; + +if(true && boolean) { + num scopedVar = 2; + test("fdsgdf" + 2 + stringVar, 1 + testVar + scopedVar); +} + +test("fdsgdf" + 2, 1 + testVar); \ No newline at end of file