mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-05-20 00:30:20 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 772675639e | |||
| 13300861ee | |||
| 719b9a06f4 | |||
| f5b115e618 | |||
| e1e4a63517 | |||
| 0dc1492c4d | |||
| a184fe40d0 | |||
| f462b8198b | |||
| de3b213deb | |||
| be444f75b7 | |||
| d98238c262 | |||
| 8e96745a85 | |||
| 802bce40c8 | |||
| 76728fe593 | |||
| f3d1751c87 |
+20
-14
@@ -5,18 +5,19 @@
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer;
|
||||
package com.dfsek.terra.addons.terrascript.lexer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public class Char {
|
||||
private final char character;
|
||||
private final int index;
|
||||
private final int line;
|
||||
private final SourcePosition position;
|
||||
|
||||
|
||||
public Char(char character, int index, int line) {
|
||||
public Char(char character, SourcePosition position) {
|
||||
this.character = character;
|
||||
this.index = index;
|
||||
this.line = line;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public boolean is(char... tests) {
|
||||
@@ -33,18 +34,23 @@ public class Char {
|
||||
return Character.toString(character);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(this == o) return true;
|
||||
if(o == null || getClass() != o.getClass()) return false;
|
||||
Char other = (Char) o;
|
||||
return character == other.character && Objects.equals(position, other.position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(character, position);
|
||||
}
|
||||
|
||||
public char getCharacter() {
|
||||
return character;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public boolean isWhitespace() {
|
||||
return Character.isWhitespace(character);
|
||||
}
|
||||
+256
@@ -0,0 +1,256 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.lexer;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token.TokenType;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.exceptions.EOFException;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.exceptions.FormatException;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.exceptions.TokenizerException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
|
||||
|
||||
public class Lexer {
|
||||
public static final Set<Character> syntaxSignificant = Sets.newHashSet(';', '(', ')', '"', ',', '\\', '=', '{', '}', '+', '-', '*', '/',
|
||||
'>', '<', '!'); // Reserved chars
|
||||
private final LookaheadStream reader;
|
||||
private final Stack<Token> bracketStack = new Stack<>();
|
||||
private Token current;
|
||||
|
||||
public Lexer(String data) {
|
||||
reader = new LookaheadStream(data + '\0');
|
||||
current = tokenize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first token.
|
||||
*
|
||||
* @return First token
|
||||
*
|
||||
* @throws ParseException If token does not exist
|
||||
*/
|
||||
public Token current() {
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume (get and remove) the first token.
|
||||
*
|
||||
* @return First token
|
||||
*
|
||||
* @throws ParseException If token does not exist
|
||||
*/
|
||||
public Token consume(String wrongTypeMessage, TokenType expected, TokenType... more) {
|
||||
if(!current.isType(expected) && Arrays.stream(more).noneMatch(t -> t == current.getType())) throw new ParseException(wrongTypeMessage, current.getPosition());
|
||||
return consumeUnchecked();
|
||||
}
|
||||
|
||||
public Token consumeUnchecked() {
|
||||
if(current.getType() == TokenType.END_OF_FILE) return current;
|
||||
Token temp = current;
|
||||
current = tokenize();
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this {@code Tokenizer} contains additional tokens.
|
||||
*
|
||||
* @return {@code true} if more tokens are present, otherwise {@code false}
|
||||
*/
|
||||
public boolean hasNext() {
|
||||
return current.getType() != TokenType.END_OF_FILE;
|
||||
}
|
||||
|
||||
private Token tokenize() throws TokenizerException {
|
||||
consumeWhitespace();
|
||||
SourcePosition position = reader.getPosition();
|
||||
|
||||
// Skip line if comment
|
||||
while(reader.matchesString("//", true)) skipLine();
|
||||
|
||||
// Skip multi line comment
|
||||
if(reader.matchesString("/*", true)) skipTo("*/");
|
||||
|
||||
// Reached end of file
|
||||
if(reader.current().isEOF()) {
|
||||
if(!bracketStack.isEmpty()) throw new ParseException("Dangling open brace", bracketStack.peek().getPosition());
|
||||
return new Token(reader.consume().toString(), TokenType.END_OF_FILE, position);
|
||||
}
|
||||
|
||||
// Check if operator token
|
||||
if(reader.matchesString("==", true))
|
||||
return new Token("==", TokenType.EQUALS_EQUALS, position);
|
||||
if(reader.matchesString("!=", true))
|
||||
return new Token("!=", TokenType.BANG_EQUALS, position);
|
||||
if(reader.matchesString(">=", true))
|
||||
return new Token(">=", TokenType.GREATER_EQUAL, position);
|
||||
if(reader.matchesString("<=", true))
|
||||
return new Token("<=", TokenType.LESS_EQUALS, position);
|
||||
if(reader.matchesString(">", true))
|
||||
return new Token(">", TokenType.GREATER, position);
|
||||
if(reader.matchesString("<", true))
|
||||
return new Token("<", TokenType.LESS, position);
|
||||
|
||||
// Check if logical operator
|
||||
if(reader.matchesString("||", true))
|
||||
return new Token("||", TokenType.BOOLEAN_OR, position);
|
||||
if(reader.matchesString("&&", true))
|
||||
return new Token("&&", TokenType.BOOLEAN_AND, position);
|
||||
|
||||
// Check if number
|
||||
if(isNumberStart()) {
|
||||
StringBuilder num = new StringBuilder();
|
||||
while(!reader.current().isEOF() && isNumberLike()) {
|
||||
num.append(reader.consume().getCharacter());
|
||||
}
|
||||
return new Token(num.toString(), TokenType.NUMBER, position);
|
||||
}
|
||||
|
||||
// Check if string literal
|
||||
if(reader.current().is('"')) {
|
||||
reader.consume(); // Consume first quote
|
||||
StringBuilder string = new StringBuilder();
|
||||
boolean ignoreNext = false;
|
||||
while((!reader.current().is('"')) || ignoreNext) {
|
||||
if(reader.current().is('\\') && !ignoreNext) {
|
||||
ignoreNext = true;
|
||||
reader.consume();
|
||||
continue;
|
||||
} else ignoreNext = false;
|
||||
if(reader.current().isEOF())
|
||||
throw new FormatException("No end of string literal found. ", position);
|
||||
string.append(reader.consume());
|
||||
}
|
||||
reader.consume(); // Consume last quote
|
||||
|
||||
return new Token(string.toString(), TokenType.STRING, position);
|
||||
}
|
||||
|
||||
if(reader.current().is('('))
|
||||
return new Token(reader.consume().toString(), TokenType.OPEN_PAREN, position);
|
||||
if(reader.current().is(')'))
|
||||
return new Token(reader.consume().toString(), TokenType.CLOSE_PAREN, position);
|
||||
if(reader.current().is(';'))
|
||||
return new Token(reader.consume().toString(), TokenType.STATEMENT_END, position);
|
||||
if(reader.current().is(','))
|
||||
return new Token(reader.consume().toString(), TokenType.SEPARATOR, position);
|
||||
|
||||
if(reader.current().is('{')) {
|
||||
Token token = new Token(reader.consume().toString(), TokenType.BLOCK_BEGIN, position);
|
||||
bracketStack.push(token);
|
||||
return token;
|
||||
}
|
||||
if(reader.current().is('}')) {
|
||||
if(bracketStack.isEmpty()) throw new ParseException("Dangling close brace", position);
|
||||
bracketStack.pop();
|
||||
return new Token(reader.consume().toString(), TokenType.BLOCK_END, position);
|
||||
}
|
||||
|
||||
if(reader.current().is('='))
|
||||
return new Token(reader.consume().toString(), TokenType.ASSIGNMENT, position);
|
||||
if(reader.current().is('+'))
|
||||
return new Token(reader.consume().toString(), TokenType.PLUS, position);
|
||||
if(reader.current().is('-'))
|
||||
return new Token(reader.consume().toString(), TokenType.MINUS,
|
||||
position);
|
||||
if(reader.current().is('*'))
|
||||
return new Token(reader.consume().toString(), TokenType.STAR,
|
||||
position);
|
||||
if(reader.current().is('/'))
|
||||
return new Token(reader.consume().toString(), TokenType.FORWARD_SLASH, position);
|
||||
if(reader.current().is('%'))
|
||||
return new Token(reader.consume().toString(), TokenType.MODULO_OPERATOR, position);
|
||||
if(reader.current().is('!'))
|
||||
return new Token(reader.consume().toString(), TokenType.BANG, position);
|
||||
|
||||
// Read word
|
||||
StringBuilder token = new StringBuilder();
|
||||
while(!reader.current().isEOF() && !isSyntaxSignificant(reader.current().getCharacter())) {
|
||||
Char c = reader.consume();
|
||||
if(c.isWhitespace()) break;
|
||||
token.append(c.getCharacter());
|
||||
}
|
||||
String tokenString = token.toString();
|
||||
|
||||
// Check if word is a keyword
|
||||
if(tokenString.equals("true"))
|
||||
return new Token(tokenString, TokenType.BOOLEAN, position);
|
||||
if(tokenString.equals("false"))
|
||||
return new Token(tokenString, TokenType.BOOLEAN, position);
|
||||
|
||||
if(tokenString.equals("num"))
|
||||
return new Token(tokenString, TokenType.TYPE_NUMBER, position);
|
||||
if(tokenString.equals("str"))
|
||||
return new Token(tokenString, TokenType.TYPE_STRING, position);
|
||||
if(tokenString.equals("bool"))
|
||||
return new Token(tokenString, TokenType.TYPE_BOOLEAN, position);
|
||||
if(tokenString.equals("void"))
|
||||
return new Token(tokenString, TokenType.TYPE_VOID, position);
|
||||
|
||||
if(tokenString.equals("if"))
|
||||
return new Token(tokenString, TokenType.IF_STATEMENT, position);
|
||||
if(tokenString.equals("else"))
|
||||
return new Token(tokenString, TokenType.ELSE, position);
|
||||
if(tokenString.equals("while"))
|
||||
return new Token(tokenString, TokenType.WHILE_LOOP, position);
|
||||
if(tokenString.equals("for"))
|
||||
return new Token(tokenString, TokenType.FOR_LOOP, position);
|
||||
|
||||
if(tokenString.equals("return"))
|
||||
return new Token(tokenString, TokenType.RETURN, position);
|
||||
if(tokenString.equals("continue"))
|
||||
return new Token(tokenString, TokenType.CONTINUE, position);
|
||||
if(tokenString.equals("break"))
|
||||
return new Token(tokenString, TokenType.BREAK, position);
|
||||
if(tokenString.equals("fail"))
|
||||
return new Token(tokenString, TokenType.FAIL, position);
|
||||
|
||||
// If not keyword, assume it is an identifier
|
||||
return new Token(tokenString, TokenType.IDENTIFIER, position);
|
||||
}
|
||||
|
||||
private void skipLine() {
|
||||
while(!reader.current().isEOF() && !reader.current().isNewLine()) reader.consume();
|
||||
consumeWhitespace();
|
||||
}
|
||||
|
||||
private void consumeWhitespace() {
|
||||
while(!reader.current().isEOF() && reader.current().isWhitespace()) reader.consume(); // Consume whitespace.
|
||||
}
|
||||
|
||||
private void skipTo(String s) throws EOFException {
|
||||
SourcePosition begin = reader.getPosition();
|
||||
while(!reader.current().isEOF()) {
|
||||
if(reader.matchesString(s, true)) {
|
||||
consumeWhitespace();
|
||||
return;
|
||||
}
|
||||
reader.consume();
|
||||
}
|
||||
throw new EOFException("No end of expression found.", begin);
|
||||
}
|
||||
|
||||
private boolean isNumberLike() {
|
||||
return reader.current().isDigit()
|
||||
|| reader.current().is('_', '.', 'E');
|
||||
}
|
||||
|
||||
private boolean isNumberStart() {
|
||||
return reader.current().isDigit()
|
||||
|| reader.current().is('.') && reader.peek().isDigit();
|
||||
}
|
||||
|
||||
public boolean isSyntaxSignificant(char c) {
|
||||
return syntaxSignificant.contains(c);
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package com.dfsek.terra.addons.terrascript.lexer;
|
||||
|
||||
|
||||
|
||||
public class LookaheadStream {
|
||||
|
||||
private final String source;
|
||||
|
||||
private int index;
|
||||
|
||||
private SourcePosition position = new SourcePosition(1, 1);
|
||||
|
||||
public LookaheadStream(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current character without consuming it.
|
||||
*
|
||||
* @return current character
|
||||
*/
|
||||
public Char current() {
|
||||
return new Char(source.charAt(index), position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume and return one character.
|
||||
*
|
||||
* @return Character that was consumed.
|
||||
*/
|
||||
public Char consume() {
|
||||
Char consumed = current();
|
||||
incrementIndex(1);
|
||||
return consumed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The next character in sequence.
|
||||
*/
|
||||
public Char peek() {
|
||||
int index = this.index + 1;
|
||||
if (index + 1 >= source.length()) return null;
|
||||
return new Char(source.charAt(index), getPositionAfter(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the contained sequence of characters matches the string
|
||||
*
|
||||
* @param check Input string to check against
|
||||
* @param consumeIfMatched Whether to consume the string if there is a match
|
||||
* @return If the string matches
|
||||
*/
|
||||
public boolean matchesString(String check, boolean consumeIfMatched) {
|
||||
boolean matches = check.equals(source.substring(index, Math.min(index + check.length(), source.length())));
|
||||
if (matches && consumeIfMatched) incrementIndex(check.length());
|
||||
return matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Current position within the source file
|
||||
*/
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
private void incrementIndex(int amount) {
|
||||
position = getPositionAfter(amount);
|
||||
index = Math.min(index + amount, source.length() - 1);
|
||||
}
|
||||
|
||||
private SourcePosition getPositionAfter(int chars) {
|
||||
if (chars < 0) throw new IllegalArgumentException("Negative values are not allowed");
|
||||
int line = position.line();
|
||||
int column = position.column();
|
||||
for (int i = index; i < Math.min(index + chars, source.length() - 1); i++) {
|
||||
if (source.charAt(i) == '\n') {
|
||||
line++;
|
||||
column = 0;
|
||||
}
|
||||
column++;
|
||||
}
|
||||
return new SourcePosition(line, column);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.lexer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
public record SourcePosition(int line, int column) {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "line " + line + ", column " + column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(this == o) return true;
|
||||
if(o == null || getClass() != o.getClass()) return false;
|
||||
SourcePosition that = (SourcePosition) o;
|
||||
return line == that.line && column == that.column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(line, column);
|
||||
}
|
||||
}
|
||||
+72
-59
@@ -5,14 +5,14 @@
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer;
|
||||
package com.dfsek.terra.addons.terrascript.lexer;
|
||||
|
||||
public class Token {
|
||||
private final String content;
|
||||
private final Type type;
|
||||
private final Position start;
|
||||
private final TokenType type;
|
||||
private final SourcePosition start;
|
||||
|
||||
public Token(String content, Type type, Position start) {
|
||||
public Token(String content, TokenType type, SourcePosition start) {
|
||||
this.content = content;
|
||||
this.type = type;
|
||||
this.start = start;
|
||||
@@ -23,7 +23,7 @@ public class Token {
|
||||
return type + ": '" + content + "'";
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
public TokenType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -31,63 +31,68 @@ public class Token {
|
||||
return content;
|
||||
}
|
||||
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public boolean isConstant() {
|
||||
return this.type.equals(Type.NUMBER) || this.type.equals(Type.STRING) || this.type.equals(Type.BOOLEAN);
|
||||
return this.type.equals(TokenType.NUMBER) || this.type.equals(TokenType.STRING) || this.type.equals(TokenType.BOOLEAN);
|
||||
}
|
||||
|
||||
public boolean isType(TokenType type) {
|
||||
return type == getType();
|
||||
}
|
||||
|
||||
public boolean isType(TokenType... types) {
|
||||
for (TokenType t : types) if (isType(t)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isBinaryOperator() {
|
||||
return type.equals(Type.ADDITION_OPERATOR)
|
||||
|| type.equals(Type.SUBTRACTION_OPERATOR)
|
||||
|| type.equals(Type.MULTIPLICATION_OPERATOR)
|
||||
|| type.equals(Type.DIVISION_OPERATOR)
|
||||
|| type.equals(Type.EQUALS_OPERATOR)
|
||||
|| type.equals(Type.NOT_EQUALS_OPERATOR)
|
||||
|| type.equals(Type.LESS_THAN_OPERATOR)
|
||||
|| type.equals(Type.GREATER_THAN_OPERATOR)
|
||||
|| type.equals(Type.LESS_THAN_OR_EQUALS_OPERATOR)
|
||||
|| type.equals(Type.GREATER_THAN_OR_EQUALS_OPERATOR)
|
||||
|| type.equals(Type.BOOLEAN_OR)
|
||||
|| type.equals(Type.BOOLEAN_AND)
|
||||
|| type.equals(Type.MODULO_OPERATOR);
|
||||
return type.equals(TokenType.PLUS)
|
||||
|| type.equals(TokenType.MINUS)
|
||||
|| type.equals(TokenType.STAR)
|
||||
|| type.equals(TokenType.FORWARD_SLASH)
|
||||
|| type.equals(TokenType.EQUALS_EQUALS)
|
||||
|| type.equals(TokenType.BANG_EQUALS)
|
||||
|| type.equals(TokenType.LESS)
|
||||
|| type.equals(TokenType.GREATER)
|
||||
|| type.equals(TokenType.LESS_EQUALS)
|
||||
|| type.equals(TokenType.GREATER_EQUAL)
|
||||
|| type.equals(TokenType.BOOLEAN_OR)
|
||||
|| type.equals(TokenType.BOOLEAN_AND)
|
||||
|| type.equals(TokenType.MODULO_OPERATOR);
|
||||
}
|
||||
|
||||
public boolean isStrictNumericOperator() {
|
||||
return type.equals(Type.SUBTRACTION_OPERATOR)
|
||||
|| type.equals(Type.MULTIPLICATION_OPERATOR)
|
||||
|| type.equals(Type.DIVISION_OPERATOR)
|
||||
|| type.equals(Type.GREATER_THAN_OPERATOR)
|
||||
|| type.equals(Type.LESS_THAN_OPERATOR)
|
||||
|| type.equals(Type.LESS_THAN_OR_EQUALS_OPERATOR)
|
||||
|| type.equals(Type.GREATER_THAN_OR_EQUALS_OPERATOR)
|
||||
|| type.equals(Type.MODULO_OPERATOR);
|
||||
return type.equals(TokenType.MINUS)
|
||||
|| type.equals(TokenType.STAR)
|
||||
|| type.equals(TokenType.FORWARD_SLASH)
|
||||
|| type.equals(TokenType.GREATER)
|
||||
|| type.equals(TokenType.LESS)
|
||||
|| type.equals(TokenType.LESS_EQUALS)
|
||||
|| type.equals(TokenType.GREATER_EQUAL)
|
||||
|| type.equals(TokenType.MODULO_OPERATOR);
|
||||
}
|
||||
|
||||
public boolean isStrictBooleanOperator() {
|
||||
return type.equals(Type.BOOLEAN_AND)
|
||||
|| type.equals(Type.BOOLEAN_OR);
|
||||
return type.equals(TokenType.BOOLEAN_AND)
|
||||
|| type.equals(TokenType.BOOLEAN_OR);
|
||||
}
|
||||
|
||||
public boolean isVariableDeclaration() {
|
||||
return type.equals(Type.STRING_VARIABLE)
|
||||
|| type.equals(Type.BOOLEAN_VARIABLE)
|
||||
|| type.equals(Type.NUMBER_VARIABLE);
|
||||
return type.equals(TokenType.TYPE_STRING)
|
||||
|| type.equals(TokenType.TYPE_BOOLEAN)
|
||||
|| type.equals(TokenType.TYPE_NUMBER);
|
||||
}
|
||||
|
||||
public boolean isLoopLike() {
|
||||
return type.equals(Type.IF_STATEMENT)
|
||||
|| type.equals(Type.WHILE_LOOP)
|
||||
|| type.equals(Type.FOR_LOOP);
|
||||
public boolean isControlStructure() {
|
||||
return type.equals(TokenType.IF_STATEMENT)
|
||||
|| type.equals(TokenType.WHILE_LOOP)
|
||||
|| type.equals(TokenType.FOR_LOOP);
|
||||
}
|
||||
|
||||
public boolean isIdentifier() {
|
||||
return type.equals(Type.IDENTIFIER);
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
public enum TokenType {
|
||||
/**
|
||||
* Function identifier or language keyword
|
||||
*/
|
||||
@@ -108,11 +113,11 @@ public class Token {
|
||||
/**
|
||||
* Beginning of group
|
||||
*/
|
||||
GROUP_BEGIN,
|
||||
OPEN_PAREN,
|
||||
/**
|
||||
* Ending of group
|
||||
*/
|
||||
GROUP_END,
|
||||
CLOSE_PAREN,
|
||||
/**
|
||||
* End of statement
|
||||
*/
|
||||
@@ -136,43 +141,43 @@ public class Token {
|
||||
/**
|
||||
* Boolean equals operator
|
||||
*/
|
||||
EQUALS_OPERATOR,
|
||||
EQUALS_EQUALS,
|
||||
/**
|
||||
* Boolean not equals operator
|
||||
*/
|
||||
NOT_EQUALS_OPERATOR,
|
||||
BANG_EQUALS,
|
||||
/**
|
||||
* Boolean greater than operator
|
||||
*/
|
||||
GREATER_THAN_OPERATOR,
|
||||
GREATER,
|
||||
/**
|
||||
* Boolean less than operator
|
||||
*/
|
||||
LESS_THAN_OPERATOR,
|
||||
LESS,
|
||||
/**
|
||||
* Boolean greater than or equal to operator
|
||||
*/
|
||||
GREATER_THAN_OR_EQUALS_OPERATOR,
|
||||
GREATER_EQUAL,
|
||||
/**
|
||||
* Boolean less than or equal to operator
|
||||
*/
|
||||
LESS_THAN_OR_EQUALS_OPERATOR,
|
||||
LESS_EQUALS,
|
||||
/**
|
||||
* Addition/concatenation operator
|
||||
*/
|
||||
ADDITION_OPERATOR,
|
||||
PLUS,
|
||||
/**
|
||||
* Subtraction operator
|
||||
*/
|
||||
SUBTRACTION_OPERATOR,
|
||||
MINUS,
|
||||
/**
|
||||
* Multiplication operator
|
||||
*/
|
||||
MULTIPLICATION_OPERATOR,
|
||||
STAR,
|
||||
/**
|
||||
* Division operator
|
||||
*/
|
||||
DIVISION_OPERATOR,
|
||||
FORWARD_SLASH,
|
||||
/**
|
||||
* Modulo operator.
|
||||
*/
|
||||
@@ -180,7 +185,7 @@ public class Token {
|
||||
/**
|
||||
* Boolean not operator
|
||||
*/
|
||||
BOOLEAN_NOT,
|
||||
BANG,
|
||||
/**
|
||||
* Boolean or
|
||||
*/
|
||||
@@ -192,15 +197,19 @@ public class Token {
|
||||
/**
|
||||
* Numeric variable declaration
|
||||
*/
|
||||
NUMBER_VARIABLE,
|
||||
TYPE_NUMBER,
|
||||
/**
|
||||
* String variable declaration
|
||||
*/
|
||||
STRING_VARIABLE,
|
||||
TYPE_STRING,
|
||||
/**
|
||||
* Boolean variable declaration
|
||||
*/
|
||||
BOOLEAN_VARIABLE,
|
||||
TYPE_BOOLEAN,
|
||||
/**
|
||||
* Void type declaration
|
||||
*/
|
||||
TYPE_VOID,
|
||||
/**
|
||||
* If statement declaration
|
||||
*/
|
||||
@@ -232,6 +241,10 @@ public class Token {
|
||||
/**
|
||||
* Else keyword
|
||||
*/
|
||||
ELSE
|
||||
ELSE,
|
||||
/**
|
||||
* End of file
|
||||
*/
|
||||
END_OF_FILE
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -5,11 +5,11 @@
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer.exceptions;
|
||||
package com.dfsek.terra.addons.terrascript.lexer.exceptions;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
|
||||
|
||||
public class EOFException extends TokenizerException {
|
||||
@@ -17,11 +17,11 @@ public class EOFException extends TokenizerException {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 3980047409902809440L;
|
||||
|
||||
public EOFException(String message, Position position) {
|
||||
public EOFException(String message, SourcePosition position) {
|
||||
super(message, position);
|
||||
}
|
||||
|
||||
public EOFException(String message, Position position, Throwable cause) {
|
||||
public EOFException(String message, SourcePosition position, Throwable cause) {
|
||||
super(message, position, cause);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -5,11 +5,11 @@
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer.exceptions;
|
||||
package com.dfsek.terra.addons.terrascript.lexer.exceptions;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
|
||||
|
||||
public class FormatException extends TokenizerException {
|
||||
@@ -17,11 +17,11 @@ public class FormatException extends TokenizerException {
|
||||
@Serial
|
||||
private static final long serialVersionUID = -791308012940744455L;
|
||||
|
||||
public FormatException(String message, Position position) {
|
||||
public FormatException(String message, SourcePosition position) {
|
||||
super(message, position);
|
||||
}
|
||||
|
||||
public FormatException(String message, Position position, Throwable cause) {
|
||||
public FormatException(String message, SourcePosition position, Throwable cause) {
|
||||
super(message, position, cause);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -5,12 +5,12 @@
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer.exceptions;
|
||||
package com.dfsek.terra.addons.terrascript.lexer.exceptions;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public abstract class TokenizerException extends ParseException {
|
||||
@@ -18,11 +18,11 @@ public abstract class TokenizerException extends ParseException {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 2792384010083575420L;
|
||||
|
||||
public TokenizerException(String message, Position position) {
|
||||
public TokenizerException(String message, SourcePosition position) {
|
||||
super(message, position);
|
||||
}
|
||||
|
||||
public TokenizerException(String message, Position position, Throwable cause) {
|
||||
public TokenizerException(String message, SourcePosition position, Throwable cause) {
|
||||
super(message, position, cause);
|
||||
}
|
||||
}
|
||||
+375
-319
@@ -9,24 +9,26 @@ package com.dfsek.terra.addons.terrascript.parser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Lexer;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token.TokenType;
|
||||
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.Executable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Item;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable.ReturnType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression.ReturnType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope.ScopeBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.BooleanConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.ConstantExpression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.NumericConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.StringConstant;
|
||||
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.functions.UserDefinedFunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow.BreakKeyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow.ContinueKeyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow.FailKeyword;
|
||||
@@ -34,7 +36,6 @@ import com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow.ReturnKeywor
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike.ForKeyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike.IfKeyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike.WhileKeyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BooleanAndOperation;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BooleanNotOperation;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BooleanOrOperation;
|
||||
@@ -58,30 +59,16 @@ import com.dfsek.terra.addons.terrascript.parser.lang.variables.assign.VariableA
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.variables.reference.BoolVariableReferenceNode;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.variables.reference.NumVariableReferenceNode;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.variables.reference.StrVariableReferenceNode;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Token;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Tokenizer;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class Parser {
|
||||
private final String data;
|
||||
private final Map<String, FunctionBuilder<? extends Function<?>>> functions = new HashMap<>();
|
||||
private final List<String> ignoredFunctions = new ArrayList<>();
|
||||
|
||||
public Parser(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
private final Lexer lexer;
|
||||
|
||||
public Parser registerFunction(String name, FunctionBuilder<? extends Function<?>> functionBuilder) {
|
||||
functions.put(name, functionBuilder);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Parser ignoreFunction(String name) {
|
||||
ignoredFunctions.add(name);
|
||||
return this;
|
||||
public Parser(Lexer lexer) {
|
||||
this.lexer = lexer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,378 +78,447 @@ public class Parser {
|
||||
*
|
||||
* @throws ParseException If parsing fails.
|
||||
*/
|
||||
public Executable parse() {
|
||||
ScopeBuilder scopeBuilder = new ScopeBuilder();
|
||||
return new Executable(parseBlock(new Tokenizer(data), false, scopeBuilder), scopeBuilder);
|
||||
public Executable parse(ScopeBuilder scopeBuilder) {
|
||||
return new Executable(parseBlock(scopeBuilder, ReturnType.VOID), scopeBuilder);
|
||||
}
|
||||
|
||||
private Keyword<?> parseLoopLike(Tokenizer tokens, boolean loop, ScopeBuilder scopeBuilder) throws ParseException {
|
||||
|
||||
Token identifier = tokens.consume();
|
||||
ParserUtil.checkType(identifier, Token.Type.IF_STATEMENT, Token.Type.WHILE_LOOP, Token.Type.FOR_LOOP);
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_BEGIN);
|
||||
|
||||
return switch(identifier.getType()) {
|
||||
case FOR_LOOP -> parseForLoop(tokens, identifier.getPosition(), scopeBuilder);
|
||||
case IF_STATEMENT -> parseIfStatement(tokens, identifier.getPosition(), loop, scopeBuilder);
|
||||
case WHILE_LOOP -> parseWhileLoop(tokens, identifier.getPosition(), scopeBuilder);
|
||||
default -> throw new UnsupportedOperationException(
|
||||
"Unknown keyword " + identifier.getContent() + ": " + identifier.getPosition());
|
||||
};
|
||||
private WhileKeyword parseWhileLoop(ScopeBuilder scopeBuilder) {
|
||||
SourcePosition start = lexer.consume("Expected 'while' keyword at beginning of while loop", TokenType.WHILE_LOOP).getPosition();
|
||||
lexer.consume("Expected '(' proceeding 'while' keyword", TokenType.OPEN_PAREN);
|
||||
scopeBuilder = scopeBuilder.innerLoopScope();
|
||||
Expression<?> condition = parseExpression(scopeBuilder);
|
||||
ParserUtil.ensureReturnType(condition, Expression.ReturnType.BOOLEAN);
|
||||
lexer.consume("Expected ')' proceeding while loop condition", TokenType.CLOSE_PAREN);
|
||||
return new WhileKeyword(parseStatementBlock(scopeBuilder, ReturnType.VOID), (Expression<Boolean>) condition,
|
||||
start); // While loop
|
||||
}
|
||||
|
||||
private WhileKeyword parseWhileLoop(Tokenizer tokens, Position start, ScopeBuilder scopeBuilder) {
|
||||
Returnable<?> first = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkReturnType(first, Returnable.ReturnType.BOOLEAN);
|
||||
private IfKeyword parseIfStatement(ScopeBuilder scopeBuilder) {
|
||||
SourcePosition start = lexer.consume("Expected 'if' keyword at beginning of if statement", TokenType.IF_STATEMENT).getPosition();
|
||||
lexer.consume("Expected '(' proceeding 'if' keyword", TokenType.OPEN_PAREN);
|
||||
Expression<?> condition = parseExpression(scopeBuilder);
|
||||
ParserUtil.ensureReturnType(condition, Expression.ReturnType.BOOLEAN);
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_END);
|
||||
|
||||
return new WhileKeyword(parseStatementBlock(tokens, true, scopeBuilder), (Returnable<Boolean>) first, start); // While loop
|
||||
}
|
||||
|
||||
private IfKeyword parseIfStatement(Tokenizer tokens, Position start, boolean loop, ScopeBuilder scopeBuilder) {
|
||||
Returnable<?> condition = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkReturnType(condition, Returnable.ReturnType.BOOLEAN);
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_END);
|
||||
lexer.consume("Expected ')' proceeding if statement condition", TokenType.CLOSE_PAREN);
|
||||
|
||||
Block elseBlock = null;
|
||||
Block statement = parseStatementBlock(tokens, loop, scopeBuilder);
|
||||
Block statement = parseStatementBlock(scopeBuilder, ReturnType.VOID);
|
||||
|
||||
List<Pair<Returnable<Boolean>, Block>> elseIf = new ArrayList<>();
|
||||
List<Pair<Expression<Boolean>, Block>> elseIf = new ArrayList<>();
|
||||
|
||||
while(tokens.hasNext() && tokens.get().getType().equals(Token.Type.ELSE)) {
|
||||
tokens.consume(); // Consume else.
|
||||
if(tokens.get().getType().equals(Token.Type.IF_STATEMENT)) {
|
||||
tokens.consume(); // Consume if.
|
||||
Returnable<?> elseCondition = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkReturnType(elseCondition, Returnable.ReturnType.BOOLEAN);
|
||||
elseIf.add(Pair.of((Returnable<Boolean>) elseCondition, parseStatementBlock(tokens, loop, scopeBuilder)));
|
||||
while(lexer.hasNext() && lexer.current().isType(TokenType.ELSE)) {
|
||||
lexer.consumeUnchecked(); // Consume else.
|
||||
if(lexer.current().isType(TokenType.IF_STATEMENT)) {
|
||||
lexer.consumeUnchecked(); // Consume if.
|
||||
Expression<?> elseCondition = parseExpression(scopeBuilder);
|
||||
ParserUtil.ensureReturnType(elseCondition, Expression.ReturnType.BOOLEAN);
|
||||
elseIf.add(Pair.of((Expression<Boolean>) elseCondition, parseStatementBlock(scopeBuilder, ReturnType.VOID)));
|
||||
} else {
|
||||
elseBlock = parseStatementBlock(tokens, loop, scopeBuilder);
|
||||
elseBlock = parseStatementBlock(scopeBuilder, ReturnType.VOID);
|
||||
break; // Else must be last.
|
||||
}
|
||||
}
|
||||
|
||||
return new IfKeyword(statement, (Returnable<Boolean>) condition, elseIf, elseBlock, start); // If statement
|
||||
return new IfKeyword(statement, (Expression<Boolean>) condition, elseIf, elseBlock, start); // If statement
|
||||
}
|
||||
|
||||
private Block parseStatementBlock(Tokenizer tokens, boolean loop, ScopeBuilder scopeBuilder) {
|
||||
|
||||
if(tokens.get().getType().equals(Token.Type.BLOCK_BEGIN)) {
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.BLOCK_BEGIN);
|
||||
Block block = parseBlock(tokens, loop, scopeBuilder);
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.BLOCK_END);
|
||||
private Block parseStatementBlock(ScopeBuilder scopeBuilder, ReturnType blockReturnType) {
|
||||
if(lexer.current().isType(TokenType.BLOCK_BEGIN)) {
|
||||
lexer.consumeUnchecked();
|
||||
Block block = parseBlock(scopeBuilder, blockReturnType);
|
||||
lexer.consume("Expected block end '}' after block statements", TokenType.BLOCK_END);
|
||||
return block;
|
||||
} else {
|
||||
Position position = tokens.get().getPosition();
|
||||
Block block = new Block(Collections.singletonList(parseItem(tokens, loop, scopeBuilder)), position);
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.STATEMENT_END);
|
||||
return block;
|
||||
SourcePosition position = lexer.current().getPosition();
|
||||
return new Block(Collections.singletonList(parseStatement(scopeBuilder)), position, blockReturnType);
|
||||
}
|
||||
}
|
||||
|
||||
private ForKeyword parseForLoop(Tokenizer tokens, Position start, ScopeBuilder scopeBuilder) {
|
||||
scopeBuilder = scopeBuilder.sub(); // new scope
|
||||
Token f = tokens.get();
|
||||
ParserUtil.checkType(f, Token.Type.NUMBER_VARIABLE, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.IDENTIFIER);
|
||||
Item<?> initializer;
|
||||
if(f.isVariableDeclaration()) {
|
||||
VariableAssignmentNode<?> forVar = parseVariableDeclaration(tokens, scopeBuilder);
|
||||
Token name = tokens.get();
|
||||
if(functions.containsKey(name.getContent()) || scopeBuilder.contains(name.getContent()))
|
||||
throw new ParseException(name.getContent() + " is already defined in this scope", name.getPosition());
|
||||
initializer = forVar;
|
||||
} else initializer = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.STATEMENT_END);
|
||||
Returnable<?> conditional = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkReturnType(conditional, Returnable.ReturnType.BOOLEAN);
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.STATEMENT_END);
|
||||
private ForKeyword parseForLoop(ScopeBuilder scopeBuilder) {
|
||||
SourcePosition start = lexer.consume("Expected 'for' keyword at beginning of for loop", TokenType.FOR_LOOP).getPosition();
|
||||
lexer.consume("Expected '(' after 'for' keyword", TokenType.OPEN_PAREN);
|
||||
scopeBuilder = scopeBuilder.innerLoopScope(); // new scope
|
||||
|
||||
Item<?> incrementer;
|
||||
Token token = tokens.get();
|
||||
if(scopeBuilder.contains(token.getContent())) { // Assume variable assignment
|
||||
incrementer = parseAssignment(tokens, scopeBuilder);
|
||||
} else incrementer = parseFunction(tokens, true, scopeBuilder);
|
||||
Expression<?> initializer = switch(lexer.current().getType()) {
|
||||
case TYPE_NUMBER, TYPE_STRING, TYPE_BOOLEAN -> {
|
||||
Token type = lexer.consume("Expected type before declaration", TokenType.TYPE_STRING, TokenType.TYPE_NUMBER, TokenType.TYPE_BOOLEAN, TokenType.TYPE_VOID);
|
||||
Token identifier = lexer.consume("Expected identifier after type", TokenType.IDENTIFIER);
|
||||
Expression<?> expr = parseVariableDeclaration(scopeBuilder, type, identifier);
|
||||
lexer.consume("Expected ';' after initializer within for loop", TokenType.STATEMENT_END);
|
||||
yield expr;
|
||||
}
|
||||
case IDENTIFIER -> {
|
||||
Expression<?> expr = parseAssignment(scopeBuilder);
|
||||
lexer.consume("Expected ';' after initializer within for loop", TokenType.STATEMENT_END);
|
||||
yield expr;
|
||||
}
|
||||
case STATEMENT_END -> {
|
||||
lexer.consumeUnchecked();
|
||||
yield Expression.NOOP;
|
||||
}
|
||||
default -> throw new ParseException("Unexpected token '" + lexer.current() + "', expected variable declaration or assignment", lexer.current().getPosition());
|
||||
};
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_END);
|
||||
Expression<?> conditional;
|
||||
if (lexer.current().isType(TokenType.STATEMENT_END)) // If no conditional is provided, conditional defaults to true
|
||||
conditional = new BooleanConstant(true, lexer.current().getPosition());
|
||||
else
|
||||
conditional = parseExpression(scopeBuilder);
|
||||
ParserUtil.ensureReturnType(conditional, Expression.ReturnType.BOOLEAN);
|
||||
lexer.consume("Expected ';' separator after conditional within for loop", TokenType.STATEMENT_END);
|
||||
|
||||
return new ForKeyword(parseStatementBlock(tokens, true, scopeBuilder), initializer, (Returnable<Boolean>) conditional, incrementer,
|
||||
Expression<?> incrementer;
|
||||
if(lexer.current().isType(TokenType.CLOSE_PAREN))
|
||||
// If no incrementer is provided, do nothing
|
||||
incrementer = Expression.NOOP;
|
||||
else if(scopeBuilder.containsVariable(lexer.current().getContent())) // Assume variable assignment
|
||||
incrementer = parseAssignment(scopeBuilder);
|
||||
else
|
||||
incrementer = parseFunctionInvocation(lexer.consume("Expected function call within for loop incrementer", TokenType.IDENTIFIER), scopeBuilder);
|
||||
lexer.consume("Expected ')' after for loop incrementer", TokenType.CLOSE_PAREN);
|
||||
|
||||
return new ForKeyword(parseStatementBlock(scopeBuilder, ReturnType.VOID), initializer, (Expression<Boolean>) conditional,
|
||||
incrementer,
|
||||
start);
|
||||
}
|
||||
|
||||
private Returnable<?> parseExpression(Tokenizer tokens, boolean full, ScopeBuilder scopeBuilder) {
|
||||
boolean booleanInverted = false; // Check for boolean not operator
|
||||
boolean negate = false;
|
||||
if(tokens.get().getType().equals(Token.Type.BOOLEAN_NOT)) {
|
||||
booleanInverted = true;
|
||||
tokens.consume();
|
||||
} else if(tokens.get().getType().equals(Token.Type.SUBTRACTION_OPERATOR)) {
|
||||
negate = true;
|
||||
tokens.consume();
|
||||
}
|
||||
|
||||
Token id = tokens.get();
|
||||
|
||||
ParserUtil.checkType(id, Token.Type.IDENTIFIER, Token.Type.BOOLEAN, Token.Type.STRING, Token.Type.NUMBER, Token.Type.GROUP_BEGIN);
|
||||
|
||||
Returnable<?> expression;
|
||||
if(id.isConstant()) {
|
||||
expression = parseConstantExpression(tokens);
|
||||
} else if(id.getType().equals(Token.Type.GROUP_BEGIN)) { // Parse grouped expression
|
||||
expression = parseGroup(tokens, scopeBuilder);
|
||||
} else {
|
||||
if(functions.containsKey(id.getContent()))
|
||||
expression = parseFunction(tokens, false, scopeBuilder);
|
||||
else if(scopeBuilder.contains(id.getContent())) {
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.IDENTIFIER);
|
||||
String varId = id.getContent();
|
||||
ReturnType varType = scopeBuilder.getType(varId);
|
||||
expression = switch(varType) {
|
||||
case NUMBER -> new NumVariableReferenceNode(id.getPosition(), varType, scopeBuilder.getIndex(varId));
|
||||
case STRING -> new StrVariableReferenceNode(id.getPosition(), varType, scopeBuilder.getIndex(varId));
|
||||
case BOOLEAN -> new BoolVariableReferenceNode(id.getPosition(), varType, scopeBuilder.getIndex(varId));
|
||||
default -> throw new ParseException("Illegal type for variable reference: " + varType, id.getPosition());
|
||||
};
|
||||
|
||||
} else throw new ParseException("Unexpected token \" " + id.getContent() + "\"", id.getPosition());
|
||||
}
|
||||
|
||||
if(booleanInverted) { // Invert operation if boolean not detected
|
||||
ParserUtil.checkReturnType(expression, Returnable.ReturnType.BOOLEAN);
|
||||
expression = new BooleanNotOperation((Returnable<Boolean>) expression, expression.getPosition());
|
||||
} else if(negate) {
|
||||
ParserUtil.checkReturnType(expression, Returnable.ReturnType.NUMBER);
|
||||
expression = new NegationOperation((Returnable<Number>) expression, expression.getPosition());
|
||||
}
|
||||
|
||||
if(full && tokens.get().isBinaryOperator()) { // Parse binary operations
|
||||
return parseBinaryOperation(expression, tokens, scopeBuilder);
|
||||
}
|
||||
return expression;
|
||||
private Expression<?> parseExpression(ScopeBuilder scopeBuilder) {
|
||||
return parseLogicOr(scopeBuilder);
|
||||
}
|
||||
|
||||
private ConstantExpression<?> parseConstantExpression(Tokenizer tokens) {
|
||||
Token constantToken = tokens.consume();
|
||||
Position position = constantToken.getPosition();
|
||||
switch(constantToken.getType()) {
|
||||
case NUMBER:
|
||||
String content = constantToken.getContent();
|
||||
return new NumericConstant(content.contains(".") ? Double.parseDouble(content) : Integer.parseInt(content), position);
|
||||
case STRING:
|
||||
return new StringConstant(constantToken.getContent(), position);
|
||||
case BOOLEAN:
|
||||
return new BooleanConstant(Boolean.parseBoolean(constantToken.getContent()), position);
|
||||
default:
|
||||
throw new UnsupportedOperationException(
|
||||
"Unsupported constant token: " + constantToken.getType() + " at position: " + position);
|
||||
}
|
||||
private Expression<?> parseLogicOr(ScopeBuilder scopeBuilder) {
|
||||
return parseLeftAssociativeBinaryOperation(this::parseLogicAnd, scopeBuilder, (op) -> {
|
||||
ParserUtil.ensureReturnType(op.left, ReturnType.BOOLEAN);
|
||||
ParserUtil.ensureReturnType(op.right, ReturnType.BOOLEAN);
|
||||
}, Map.of(TokenType.BOOLEAN_OR, (op) -> new BooleanOrOperation((Expression<Boolean>) op.left, (Expression<Boolean>) op.right, op.operator.getPosition())));
|
||||
}
|
||||
|
||||
private Returnable<?> parseGroup(Tokenizer tokens, ScopeBuilder scopeBuilder) {
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_BEGIN);
|
||||
Returnable<?> expression = parseExpression(tokens, true, scopeBuilder); // Parse inside of group as a separate expression
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_END);
|
||||
return expression;
|
||||
private Expression<?> parseLogicAnd(ScopeBuilder scopeBuilder) {
|
||||
return parseLeftAssociativeBinaryOperation(this::parseEquality, scopeBuilder, (op) -> {
|
||||
ParserUtil.ensureReturnType(op.left, ReturnType.BOOLEAN);
|
||||
ParserUtil.ensureReturnType(op.right, ReturnType.BOOLEAN);
|
||||
}, Map.of(TokenType.BOOLEAN_AND, (op) -> new BooleanAndOperation((Expression<Boolean>) op.left, (Expression<Boolean>) op.right, op.operator.getPosition())));
|
||||
}
|
||||
|
||||
private BinaryOperation<?, ?> parseBinaryOperation(Returnable<?> left, Tokenizer tokens,
|
||||
ScopeBuilder scopeBuilder) {
|
||||
Token binaryOperator = tokens.consume();
|
||||
ParserUtil.checkBinaryOperator(binaryOperator);
|
||||
|
||||
Returnable<?> right = parseExpression(tokens, false, scopeBuilder);
|
||||
|
||||
Token other = tokens.get();
|
||||
if(ParserUtil.hasPrecedence(binaryOperator.getType(), other.getType())) {
|
||||
return assemble(left, parseBinaryOperation(right, tokens, scopeBuilder), binaryOperator);
|
||||
} else if(other.isBinaryOperator()) {
|
||||
return parseBinaryOperation(assemble(left, right, binaryOperator), tokens, scopeBuilder);
|
||||
}
|
||||
return assemble(left, right, binaryOperator);
|
||||
private Expression<?> parseEquality(ScopeBuilder scopeBuilder) {
|
||||
return parseLeftAssociativeBinaryOperation(this::parseComparison, scopeBuilder, Map.of(
|
||||
TokenType.EQUALS_EQUALS, (op) -> new EqualsStatement((Expression<Object>) op.left, (Expression<Object>) op.right, op.operator.getPosition()),
|
||||
TokenType.BANG_EQUALS, (op) -> new NotEqualsStatement((Expression<Object>) op.left, (Expression<Object>) op.right, op.operator.getPosition())
|
||||
));
|
||||
}
|
||||
|
||||
private BinaryOperation<?, ?> assemble(Returnable<?> left, Returnable<?> right, Token binaryOperator) {
|
||||
if(binaryOperator.isStrictNumericOperator())
|
||||
ParserUtil.checkArithmeticOperation(left, right, binaryOperator); // Numeric type checking
|
||||
if(binaryOperator.isStrictBooleanOperator()) ParserUtil.checkBooleanOperation(left, right, binaryOperator); // Boolean type checking
|
||||
switch(binaryOperator.getType()) {
|
||||
case ADDITION_OPERATOR:
|
||||
if(left.returnType().equals(Returnable.ReturnType.NUMBER) && right.returnType().equals(Returnable.ReturnType.NUMBER)) {
|
||||
return new NumberAdditionOperation((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
private Expression<?> parseComparison(ScopeBuilder scopeBuilder) {
|
||||
return parseLeftAssociativeBinaryOperation(this::parseTerm, scopeBuilder, (op) -> {
|
||||
ParserUtil.ensureReturnType(op.left, ReturnType.NUMBER);
|
||||
ParserUtil.ensureReturnType(op.right, ReturnType.NUMBER);
|
||||
}, Map.of(
|
||||
TokenType.LESS, (op) -> new LessThanStatement((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition()),
|
||||
TokenType.LESS_EQUALS, (op) -> new LessThanOrEqualsStatement((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition()),
|
||||
TokenType.GREATER, (op) -> new GreaterThanStatement((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition()),
|
||||
TokenType.GREATER_EQUAL, (op) -> new GreaterOrEqualsThanStatement((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition())
|
||||
));
|
||||
}
|
||||
|
||||
private Expression<?> parseTerm(ScopeBuilder scopeBuilder) {
|
||||
return parseLeftAssociativeBinaryOperation(this::parseFactor, scopeBuilder, Map.of(
|
||||
TokenType.MINUS, (op) -> {
|
||||
ParserUtil.ensureReturnType(op.left, ReturnType.NUMBER);
|
||||
ParserUtil.ensureReturnType(op.right, ReturnType.NUMBER);
|
||||
return new SubtractionOperation((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition());
|
||||
},
|
||||
TokenType.PLUS, (op) -> {
|
||||
if (op.left.returnType() == ReturnType.NUMBER && op.right.returnType() == ReturnType.NUMBER)
|
||||
return new NumberAdditionOperation((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition());
|
||||
else
|
||||
return new ConcatenationOperation((Expression<Object>) op.left, (Expression<Object>) op.right, op.operator.getPosition());
|
||||
}));
|
||||
}
|
||||
|
||||
private Expression<?> parseFactor(ScopeBuilder scopeBuilder) {
|
||||
return parseLeftAssociativeBinaryOperation(this::parseUnary, scopeBuilder, (op) -> {
|
||||
ParserUtil.ensureReturnType(op.left, ReturnType.NUMBER);
|
||||
ParserUtil.ensureReturnType(op.right, ReturnType.NUMBER);
|
||||
}, Map.of(
|
||||
TokenType.STAR, (op) -> new MultiplicationOperation((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition()),
|
||||
TokenType.FORWARD_SLASH, (op) -> new DivisionOperation((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition()),
|
||||
TokenType.MODULO_OPERATOR, (op) -> new ModuloOperation((Expression<Number>) op.left, (Expression<Number>) op.right, op.operator.getPosition())
|
||||
));
|
||||
}
|
||||
|
||||
private Expression<?> parseUnary(ScopeBuilder scopeBuilder) {
|
||||
if (lexer.current().isType(TokenType.BANG, TokenType.MINUS)) {
|
||||
Token operator = lexer.consumeUnchecked();
|
||||
Expression<?> right = parseUnary(scopeBuilder);
|
||||
return switch(operator.getType()) {
|
||||
case BANG -> {
|
||||
ParserUtil.ensureReturnType(right, ReturnType.BOOLEAN);
|
||||
yield new BooleanNotOperation((Expression<Boolean>) right, operator.getPosition());
|
||||
}
|
||||
return new ConcatenationOperation((Returnable<Object>) left, (Returnable<Object>) right, binaryOperator.getPosition());
|
||||
case SUBTRACTION_OPERATOR:
|
||||
return new SubtractionOperation((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
case MULTIPLICATION_OPERATOR:
|
||||
return new MultiplicationOperation((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
case DIVISION_OPERATOR:
|
||||
return new DivisionOperation((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
case EQUALS_OPERATOR:
|
||||
return new EqualsStatement((Returnable<Object>) left, (Returnable<Object>) right, binaryOperator.getPosition());
|
||||
case NOT_EQUALS_OPERATOR:
|
||||
return new NotEqualsStatement((Returnable<Object>) left, (Returnable<Object>) right, binaryOperator.getPosition());
|
||||
case GREATER_THAN_OPERATOR:
|
||||
return new GreaterThanStatement((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
case LESS_THAN_OPERATOR:
|
||||
return new LessThanStatement((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
case GREATER_THAN_OR_EQUALS_OPERATOR:
|
||||
return new GreaterOrEqualsThanStatement((Returnable<Number>) left, (Returnable<Number>) right,
|
||||
binaryOperator.getPosition());
|
||||
case LESS_THAN_OR_EQUALS_OPERATOR:
|
||||
return new LessThanOrEqualsStatement((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
case BOOLEAN_AND:
|
||||
return new BooleanAndOperation((Returnable<Boolean>) left, (Returnable<Boolean>) right, binaryOperator.getPosition());
|
||||
case BOOLEAN_OR:
|
||||
return new BooleanOrOperation((Returnable<Boolean>) left, (Returnable<Boolean>) right, binaryOperator.getPosition());
|
||||
case MODULO_OPERATOR:
|
||||
return new ModuloOperation((Returnable<Number>) left, (Returnable<Number>) right, binaryOperator.getPosition());
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unsupported binary operator: " + binaryOperator.getType());
|
||||
case MINUS -> {
|
||||
ParserUtil.ensureReturnType(right, ReturnType.NUMBER);
|
||||
yield new NegationOperation((Expression<Number>) right, operator.getPosition());
|
||||
}
|
||||
default -> throw new IllegalStateException();
|
||||
};
|
||||
}
|
||||
return parsePrimary(scopeBuilder);
|
||||
}
|
||||
|
||||
private VariableAssignmentNode<?> parseVariableDeclaration(Tokenizer tokens, ScopeBuilder scopeBuilder) {
|
||||
Token type = tokens.consume();
|
||||
ParserUtil.checkType(type, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.NUMBER_VARIABLE);
|
||||
private Expression<?> parsePrimary(ScopeBuilder scopeBuilder) {
|
||||
Token token = lexer.consumeUnchecked();
|
||||
return switch(token.getType()) {
|
||||
case NUMBER -> {
|
||||
String content = token.getContent();
|
||||
yield new NumericConstant(content.contains(".") ? Double.parseDouble(content) : Integer.parseInt(content), token.getPosition());
|
||||
}
|
||||
case STRING -> new StringConstant(token.getContent(), token.getPosition());
|
||||
case BOOLEAN -> new BooleanConstant(Boolean.parseBoolean(token.getContent()), token.getPosition());
|
||||
case OPEN_PAREN -> {
|
||||
Expression<?> expr = parseExpression(scopeBuilder);
|
||||
lexer.consume("Missing ')' at end of expression group", TokenType.CLOSE_PAREN);
|
||||
yield expr;
|
||||
}
|
||||
case IDENTIFIER -> {
|
||||
if (scopeBuilder.containsFunction(token.getContent()))
|
||||
yield parseFunctionInvocation(token, scopeBuilder);
|
||||
else if (scopeBuilder.containsVariable(token.getContent())) {
|
||||
ReturnType variableType = scopeBuilder.getVaraibleType(token.getContent());
|
||||
yield switch(variableType) {
|
||||
case NUMBER -> new NumVariableReferenceNode(token.getPosition(), variableType, scopeBuilder.getIndex(token.getContent()));
|
||||
case BOOLEAN -> new BoolVariableReferenceNode(token.getPosition(), variableType, scopeBuilder.getIndex(token.getContent()));
|
||||
case STRING -> new StrVariableReferenceNode(token.getPosition(), variableType, scopeBuilder.getIndex(token.getContent()));
|
||||
default -> throw new ParseException("Illegal type for variable reference: " + variableType, token.getPosition());
|
||||
};
|
||||
}
|
||||
throw new ParseException("Identifier '" + token.getContent() + "' is not defined in this scope", token.getPosition());
|
||||
}
|
||||
default -> throw new ParseException("Unexpected token '" + token.getContent() + "' when parsing expression", token.getPosition());
|
||||
};
|
||||
}
|
||||
|
||||
private Expression<?> parseLeftAssociativeBinaryOperation(Function<ScopeBuilder, Expression<?>> higherPrecedence, ScopeBuilder scopeBuilder,
|
||||
Consumer<BinaryOperationInfo> init,
|
||||
Map<TokenType, Function<BinaryOperationInfo, Expression<?>>> operators) {
|
||||
Expression<?> expr = higherPrecedence.apply(scopeBuilder);
|
||||
TokenType[] opTypes = operators.keySet().toArray(new TokenType[0]);
|
||||
while (lexer.current().isType(opTypes)) {
|
||||
Token operator = lexer.consumeUnchecked();
|
||||
Expression<?> right = higherPrecedence.apply(scopeBuilder);
|
||||
BinaryOperationInfo op = new BinaryOperationInfo(expr, operator, right);
|
||||
init.accept(op);
|
||||
expr = operators.get(operator.getType()).apply(op);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
private Expression<?> parseLeftAssociativeBinaryOperation(Function<ScopeBuilder, Expression<?>> higherPrecedence, ScopeBuilder scopeBuilder, Map<TokenType, Function<BinaryOperationInfo, Expression<?>>> operators) {
|
||||
return parseLeftAssociativeBinaryOperation(higherPrecedence, scopeBuilder, (op) -> {}, operators);
|
||||
}
|
||||
|
||||
private record BinaryOperationInfo(Expression<?> left, Token operator, Expression<?> right) {}
|
||||
|
||||
private Expression<?> parseDeclaration(ScopeBuilder scopeBuilder) {
|
||||
Token type = lexer.consume("Expected type before declaration", TokenType.TYPE_STRING, TokenType.TYPE_NUMBER, TokenType.TYPE_BOOLEAN, TokenType.TYPE_VOID);
|
||||
Token identifier = lexer.consume("Expected identifier after type", TokenType.IDENTIFIER);
|
||||
|
||||
Returnable.ReturnType returnType = ParserUtil.getVariableReturnType(type);
|
||||
return switch(lexer.current().getType()) {
|
||||
case ASSIGNMENT -> parseVariableDeclaration(scopeBuilder, type, identifier);
|
||||
case OPEN_PAREN -> parseFunctionDeclaration(scopeBuilder, type, identifier);
|
||||
default -> throw new ParseException("Expected '=' for variable assignment or '(' for function declaration after identifier '" + identifier.getContent() + "'", lexer.current().getPosition());
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private Expression<?> parseVariableDeclaration(ScopeBuilder scopeBuilder, Token type, Token identifier) {
|
||||
lexer.consume("Expected '=' after identifier '" + identifier.getContent() + "' for variable declaration", TokenType.ASSIGNMENT);
|
||||
|
||||
ParserUtil.checkVarType(type, returnType); // Check for type mismatch
|
||||
Token identifier = tokens.consume();
|
||||
ParserUtil.checkType(identifier, Token.Type.IDENTIFIER);
|
||||
if(functions.containsKey(identifier.getContent()) || scopeBuilder.contains(identifier.getContent()))
|
||||
if (!type.isVariableDeclaration()) throw new ParseException("Expected type specification at beginning of variable declaration", type.getPosition());
|
||||
|
||||
if(scopeBuilder.containsVariable(identifier.getContent()))
|
||||
throw new ParseException(identifier.getContent() + " is already defined in this scope", identifier.getPosition());
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.ASSIGNMENT);
|
||||
|
||||
Returnable<?> value = parseExpression(tokens, true, scopeBuilder);
|
||||
ParserUtil.checkReturnType(value, returnType);
|
||||
|
||||
String id = identifier.getContent();
|
||||
|
||||
Expression<?> value = parseExpression(scopeBuilder);
|
||||
ParserUtil.ensureReturnType(value, ParserUtil.getVariableReturnType(type));
|
||||
|
||||
String variableName = 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));
|
||||
case BOOLEAN -> new BoolAssignmentNode((Returnable<Boolean>) value, identifier.getPosition(), scopeBuilder.bool(id));
|
||||
case NUMBER -> new NumAssignmentNode((Expression<Number>) value, identifier.getPosition(),
|
||||
scopeBuilder.declareNum(variableName));
|
||||
case STRING -> new StrAssignmentNode((Expression<String>) value, identifier.getPosition(),
|
||||
scopeBuilder.declareStr(variableName));
|
||||
case BOOLEAN -> new BoolAssignmentNode((Expression<Boolean>) value, identifier.getPosition(),
|
||||
scopeBuilder.declareBool(variableName));
|
||||
default -> throw new ParseException("Illegal type for variable declaration: " + type, value.getPosition());
|
||||
};
|
||||
}
|
||||
|
||||
private Block parseBlock(Tokenizer tokens, boolean loop, ScopeBuilder scopeBuilder) {
|
||||
List<Item<?>> parsedItems = new ArrayList<>();
|
||||
private Expression<?> parseFunctionDeclaration(ScopeBuilder scopeBuilder, Token type, Token identifier) {
|
||||
lexer.consume("Expected '(' after identifier '" + identifier.getContent() + "' for function declaration", TokenType.OPEN_PAREN);
|
||||
|
||||
scopeBuilder = scopeBuilder.sub();
|
||||
if(!(type.isType(TokenType.TYPE_STRING, TokenType.TYPE_BOOLEAN, TokenType.TYPE_NUMBER, TokenType.TYPE_VOID)))
|
||||
throw new ParseException("Invalid function declaration return type specification " + type.getType(), type.getPosition());
|
||||
|
||||
Token first = tokens.get();
|
||||
if(scopeBuilder.containsVariable(identifier.getContent()))
|
||||
throw new ParseException(identifier.getContent() + " is already defined in this scope", identifier.getPosition());
|
||||
|
||||
while(tokens.hasNext()) {
|
||||
Token token = tokens.get();
|
||||
if(token.getType().equals(Token.Type.BLOCK_END)) break; // Stop parsing at block end.
|
||||
Item<?> parsedItem = parseItem(tokens, loop, scopeBuilder);
|
||||
if(parsedItem != Function.NULL) {
|
||||
parsedItems.add(parsedItem);
|
||||
}
|
||||
if(tokens.hasNext() && !token.isLoopLike()) ParserUtil.checkType(tokens.consume(), Token.Type.STATEMENT_END);
|
||||
ReturnType returnType = ParserUtil.getVariableReturnType(type);
|
||||
|
||||
ScopeBuilder functionBodyScope = scopeBuilder.functionScope();
|
||||
|
||||
// Declare parameter names into function body scope
|
||||
List<Pair<Integer, ReturnType>> parameterInfo = getFunctionParameterDeclaration().stream().map(
|
||||
arg -> Pair.of(switch(arg.getRight()) {
|
||||
case NUMBER -> functionBodyScope.declareNum(arg.getLeft());
|
||||
case BOOLEAN -> functionBodyScope.declareBool(arg.getLeft());
|
||||
case STRING -> functionBodyScope.declareStr(arg.getLeft());
|
||||
default -> throw new IllegalArgumentException("Unsupported parameter type: " + arg.getRight());
|
||||
}, arg.getRight())).toList();
|
||||
|
||||
Block body = parseStatementBlock(functionBodyScope, returnType);
|
||||
|
||||
FunctionBuilder<?> functionBuilder = new UserDefinedFunctionBuilder<>(returnType, parameterInfo, body, functionBodyScope);
|
||||
|
||||
scopeBuilder.registerFunction(identifier.getContent(), functionBuilder);
|
||||
return Expression.NOOP;
|
||||
}
|
||||
|
||||
private List<Pair<String, ReturnType>> getFunctionParameterDeclaration() {
|
||||
List<Pair<String, ReturnType>> parameters = new ArrayList<>();
|
||||
while(lexer.current().getType() != TokenType.CLOSE_PAREN) {
|
||||
// Parse parameter type
|
||||
Token typeToken = lexer.consume("Expected function parameter type declaration", TokenType.TYPE_BOOLEAN, TokenType.TYPE_STRING, TokenType.TYPE_NUMBER);
|
||||
ReturnType type = ParserUtil.getVariableReturnType(typeToken);
|
||||
|
||||
// Parse parameter name
|
||||
Token identifierToken = lexer.consume("Expected function parameter identifier", TokenType.IDENTIFIER);
|
||||
String name = identifierToken.getContent();
|
||||
|
||||
parameters.add(Pair.of(name, type));
|
||||
|
||||
// Consume separator if present, trailing separators are allowed
|
||||
if(lexer.current().isType(TokenType.SEPARATOR)) lexer.consumeUnchecked();
|
||||
}
|
||||
return new Block(parsedItems, first.getPosition());
|
||||
lexer.consume("Expected ')' after function parameter declaration", TokenType.CLOSE_PAREN);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private Item<?> parseItem(Tokenizer tokens, boolean loop, ScopeBuilder scopeBuilder) {
|
||||
Token token = tokens.get();
|
||||
if(loop) ParserUtil.checkType(token, Token.Type.IDENTIFIER, Token.Type.IF_STATEMENT, Token.Type.WHILE_LOOP, Token.Type.FOR_LOOP,
|
||||
Token.Type.NUMBER_VARIABLE, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE,
|
||||
Token.Type.RETURN, Token.Type.BREAK, Token.Type.CONTINUE, Token.Type.FAIL);
|
||||
else ParserUtil.checkType(token, Token.Type.IDENTIFIER, Token.Type.IF_STATEMENT, Token.Type.WHILE_LOOP, Token.Type.FOR_LOOP,
|
||||
Token.Type.NUMBER_VARIABLE, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.RETURN,
|
||||
Token.Type.FAIL);
|
||||
private Block parseBlock(ScopeBuilder scopeBuilder, ReturnType blockReturnType) {
|
||||
List<Expression<?>> expressions = new ArrayList<>();
|
||||
scopeBuilder = scopeBuilder.innerScope(); // Create new inner scope for the block
|
||||
SourcePosition startPosition = lexer.current().getPosition();
|
||||
|
||||
if(token.isLoopLike()) { // Parse loop-like tokens (if, while, etc)
|
||||
return parseLoopLike(tokens, loop, scopeBuilder);
|
||||
} else if(token.isIdentifier()) { // Parse identifiers
|
||||
if(scopeBuilder.contains(token.getContent())) { // Assume variable assignment
|
||||
return parseAssignment(tokens, scopeBuilder);
|
||||
} else return parseFunction(tokens, true, scopeBuilder);
|
||||
} else if(token.isVariableDeclaration()) {
|
||||
|
||||
return parseVariableDeclaration(tokens, scopeBuilder);
|
||||
|
||||
} else if(token.getType().equals(Token.Type.RETURN)) return new ReturnKeyword(tokens.consume().getPosition());
|
||||
else if(token.getType().equals(Token.Type.BREAK)) return new BreakKeyword(tokens.consume().getPosition());
|
||||
else if(token.getType().equals(Token.Type.CONTINUE)) return new ContinueKeyword(tokens.consume().getPosition());
|
||||
else if(token.getType().equals(Token.Type.FAIL)) return new FailKeyword(tokens.consume().getPosition());
|
||||
else throw new UnsupportedOperationException("Unexpected token " + token.getType() + ": " + token.getPosition());
|
||||
boolean hasReturn = false;
|
||||
|
||||
// Parse each statement
|
||||
while(lexer.hasNext() && !lexer.current().isType(TokenType.BLOCK_END)) {
|
||||
Expression<?> expression = parseStatement(scopeBuilder);
|
||||
if(expression != Expression.NOOP) {
|
||||
expressions.add(expression);
|
||||
}
|
||||
if(expression instanceof ReturnKeyword returnKeyword) {
|
||||
hasReturn = true;
|
||||
if(returnKeyword.dataReturnType() != blockReturnType)
|
||||
throw new ParseException(
|
||||
"Invalid return type, expected " + blockReturnType + ", found " + returnKeyword.dataReturnType(),
|
||||
expression.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
if(blockReturnType != ReturnType.VOID && !hasReturn)
|
||||
throw new ParseException("Block does not contain a return statement, must return type " + blockReturnType, startPosition);
|
||||
|
||||
return new Block(expressions, startPosition, blockReturnType);
|
||||
}
|
||||
|
||||
private VariableAssignmentNode<?> parseAssignment(Tokenizer tokens, ScopeBuilder scopeBuilder) {
|
||||
Token identifier = tokens.consume();
|
||||
private Expression<?> parseStatement(ScopeBuilder scopeBuilder) {
|
||||
Token token = lexer.current();
|
||||
Expression<?> expression = switch(token.getType()) {
|
||||
case FOR_LOOP -> parseForLoop(scopeBuilder);
|
||||
case IF_STATEMENT -> parseIfStatement(scopeBuilder);
|
||||
case WHILE_LOOP -> parseWhileLoop(scopeBuilder);
|
||||
case IDENTIFIER -> {
|
||||
if(scopeBuilder.containsVariable(token.getContent())) yield parseAssignment(scopeBuilder); // Assume variable assignment
|
||||
else yield parseFunctionInvocation(lexer.consumeUnchecked(), scopeBuilder);
|
||||
}
|
||||
case TYPE_NUMBER, TYPE_STRING, TYPE_BOOLEAN, TYPE_VOID -> parseDeclaration(scopeBuilder);
|
||||
case RETURN -> parseReturn(scopeBuilder);
|
||||
case BREAK -> {
|
||||
if (!scopeBuilder.isInLoop()) throw new ParseException("Break statements can only be defined inside loops", token.getPosition());
|
||||
yield new BreakKeyword(lexer.consumeUnchecked().getPosition());
|
||||
}
|
||||
case CONTINUE -> {
|
||||
if (!scopeBuilder.isInLoop()) throw new ParseException("Continue statements can only be defined inside loops", token.getPosition());
|
||||
yield new ContinueKeyword(lexer.consumeUnchecked().getPosition());
|
||||
}
|
||||
case FAIL -> new FailKeyword(lexer.consumeUnchecked().getPosition());
|
||||
case STATEMENT_END -> Expression.NOOP;
|
||||
default -> throw new UnsupportedOperationException("Unexpected token " + token.getType() + ": " + token.getPosition());
|
||||
};
|
||||
if(!token.isControlStructure() && expression != Expression.NOOP) lexer.consume("Expected ';' at end of statement", TokenType.STATEMENT_END);
|
||||
return expression;
|
||||
}
|
||||
|
||||
private ReturnKeyword parseReturn(ScopeBuilder scopeBuilder) {
|
||||
Token returnToken = lexer.consume("Expected 'return' keyword at beginning of return statement", TokenType.RETURN);
|
||||
Expression<?> data = null;
|
||||
if(!lexer.current().isType(TokenType.STATEMENT_END)) {
|
||||
data = parseExpression(scopeBuilder);
|
||||
}
|
||||
return new ReturnKeyword(data, returnToken.getPosition());
|
||||
}
|
||||
|
||||
private VariableAssignmentNode<?> parseAssignment(ScopeBuilder scopeBuilder) {
|
||||
Token identifier = lexer.consume("Expected identifier at beginning of assignment", TokenType.IDENTIFIER);
|
||||
|
||||
ParserUtil.checkType(identifier, Token.Type.IDENTIFIER);
|
||||
lexer.consume("Expected '=' after identifier for variable assignment", TokenType.ASSIGNMENT);
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.ASSIGNMENT);
|
||||
|
||||
Returnable<?> value = parseExpression(tokens, true, scopeBuilder);
|
||||
Expression<?> value = parseExpression(scopeBuilder);
|
||||
|
||||
String id = identifier.getContent();
|
||||
|
||||
ParserUtil.checkReturnType(value, scopeBuilder.getType(id));
|
||||
ParserUtil.ensureReturnType(value, scopeBuilder.getVaraibleType(id));
|
||||
|
||||
ReturnType type = value.returnType();
|
||||
|
||||
return switch(type) {
|
||||
case NUMBER -> new NumAssignmentNode((Returnable<Number>) value, identifier.getPosition(), scopeBuilder.getIndex(id));
|
||||
case STRING -> new StrAssignmentNode((Returnable<String>) value, identifier.getPosition(), scopeBuilder.getIndex(id));
|
||||
case BOOLEAN -> new BoolAssignmentNode((Returnable<Boolean>) value, identifier.getPosition(), scopeBuilder.getIndex(id));
|
||||
case NUMBER -> new NumAssignmentNode((Expression<Number>) value, identifier.getPosition(), scopeBuilder.getIndex(id));
|
||||
case STRING -> new StrAssignmentNode((Expression<String>) value, identifier.getPosition(), scopeBuilder.getIndex(id));
|
||||
case BOOLEAN -> new BoolAssignmentNode((Expression<Boolean>) value, identifier.getPosition(), scopeBuilder.getIndex(id));
|
||||
default -> throw new ParseException("Illegal type for variable assignment: " + type, value.getPosition());
|
||||
};
|
||||
}
|
||||
|
||||
private Function<?> parseFunction(Tokenizer tokens, boolean fullStatement, ScopeBuilder scopeBuilder) {
|
||||
Token identifier = tokens.consume();
|
||||
ParserUtil.checkType(identifier, Token.Type.IDENTIFIER); // First token must be identifier
|
||||
private Expression<?> parseFunctionInvocation(Token identifier, ScopeBuilder scopeBuilder) {
|
||||
if(!scopeBuilder.containsFunction(identifier.getContent()))
|
||||
throw new ParseException("Function '" + identifier.getContent() + "' is not defined in this scope", identifier.getPosition());
|
||||
|
||||
if(!functions.containsKey(identifier.getContent()))
|
||||
throw new ParseException("No such function \"" + identifier.getContent() + "\"", identifier.getPosition());
|
||||
FunctionBuilder<?> builder = scopeBuilder.getFunction(identifier.getContent());
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_BEGIN); // Second is body begin
|
||||
lexer.consume("Expected '(' after identifier " + identifier.getContent(), TokenType.OPEN_PAREN); // Invocation starts with open paren
|
||||
|
||||
|
||||
List<Returnable<?>> args = getArgs(tokens, scopeBuilder); // Extract arguments, consume the rest.
|
||||
|
||||
ParserUtil.checkType(tokens.consume(), Token.Type.GROUP_END); // Remove body end
|
||||
|
||||
if(fullStatement) ParserUtil.checkType(tokens.get(), Token.Type.STATEMENT_END);
|
||||
List<Expression<?>> args = new ArrayList<>();
|
||||
while(!lexer.current().isType(TokenType.CLOSE_PAREN)) {
|
||||
args.add(parseExpression(scopeBuilder));
|
||||
if (lexer.current().isType(TokenType.CLOSE_PAREN)) break;
|
||||
lexer.consume("Expected ',' between function arguments", TokenType.SEPARATOR);
|
||||
}
|
||||
lexer.consume("Expected ')' after function arguments", TokenType.CLOSE_PAREN);
|
||||
|
||||
if(ignoredFunctions.contains(identifier.getContent())) {
|
||||
return Function.NULL;
|
||||
return Expression.NOOP;
|
||||
}
|
||||
|
||||
if(functions.containsKey(identifier.getContent())) {
|
||||
FunctionBuilder<?> builder = functions.get(identifier.getContent());
|
||||
|
||||
if(builder.argNumber() != -1 && args.size() != builder.argNumber())
|
||||
throw new ParseException("Expected " + builder.argNumber() + " arguments, found " + args.size(), identifier.getPosition());
|
||||
|
||||
for(int i = 0; i < args.size(); i++) {
|
||||
Returnable<?> argument = args.get(i);
|
||||
if(builder.getArgument(i) == null)
|
||||
throw new ParseException("Unexpected argument at position " + i + " in function " + identifier.getContent(),
|
||||
identifier.getPosition());
|
||||
ParserUtil.checkReturnType(argument, builder.getArgument(i));
|
||||
}
|
||||
return builder.build(args, identifier.getPosition());
|
||||
if(builder.argNumber() != -1 && args.size() != builder.argNumber())
|
||||
throw new ParseException("Expected " + builder.argNumber() + " arguments, found " + args.size(), identifier.getPosition());
|
||||
|
||||
for(int i = 0; i < args.size(); i++) {
|
||||
Expression<?> argument = args.get(i);
|
||||
if(builder.getArgument(i) == null)
|
||||
throw new ParseException("Unexpected argument at position " + i + " in function " + identifier.getContent(),
|
||||
identifier.getPosition());
|
||||
ParserUtil.ensureReturnType(argument, builder.getArgument(i));
|
||||
}
|
||||
throw new UnsupportedOperationException("Unsupported function: " + identifier.getContent());
|
||||
return builder.build(args, identifier.getPosition());
|
||||
}
|
||||
|
||||
private List<Returnable<?>> getArgs(Tokenizer tokens, ScopeBuilder scopeBuilder) {
|
||||
List<Returnable<?>> args = new ArrayList<>();
|
||||
|
||||
while(!tokens.get().getType().equals(Token.Type.GROUP_END)) {
|
||||
args.add(parseExpression(tokens, true, scopeBuilder));
|
||||
ParserUtil.checkType(tokens.get(), Token.Type.SEPARATOR, Token.Type.GROUP_END);
|
||||
if(tokens.get().getType().equals(Token.Type.SEPARATOR)) tokens.consume();
|
||||
}
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
+15
-87
@@ -12,104 +12,32 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Token.TokenType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Token;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public class ParserUtil {
|
||||
|
||||
private static final Map<Token.Type, Map<Token.Type, Boolean>> PRECEDENCE = new HashMap<>(); // If second has precedence, true.
|
||||
private static final List<Token.Type> ARITHMETIC = Arrays.asList(Token.Type.ADDITION_OPERATOR, Token.Type.SUBTRACTION_OPERATOR,
|
||||
Token.Type.MULTIPLICATION_OPERATOR, Token.Type.DIVISION_OPERATOR,
|
||||
Token.Type.MODULO_OPERATOR);
|
||||
private static final List<Token.Type> COMPARISON = Arrays.asList(Token.Type.EQUALS_OPERATOR, Token.Type.NOT_EQUALS_OPERATOR,
|
||||
Token.Type.LESS_THAN_OPERATOR, Token.Type.LESS_THAN_OR_EQUALS_OPERATOR,
|
||||
Token.Type.GREATER_THAN_OPERATOR,
|
||||
Token.Type.GREATER_THAN_OR_EQUALS_OPERATOR);
|
||||
// public static void ensureType(Token token, TokenType... expected) {
|
||||
// for(TokenType type : expected) if(token.getType().equals(type)) return;
|
||||
// throw new ParseException("Expected " + Arrays.toString(expected) + " but found " + token.getType(), token.getPosition());
|
||||
// }
|
||||
|
||||
static { // Setup precedence
|
||||
Map<Token.Type, Boolean> add = new HashMap<>(); // Addition/subtraction before Multiplication/division.
|
||||
add.put(Token.Type.MULTIPLICATION_OPERATOR, true);
|
||||
add.put(Token.Type.DIVISION_OPERATOR, true);
|
||||
|
||||
PRECEDENCE.put(Token.Type.ADDITION_OPERATOR, add);
|
||||
PRECEDENCE.put(Token.Type.SUBTRACTION_OPERATOR, add);
|
||||
|
||||
Map<Token.Type, Boolean> numericBoolean = new HashMap<>();
|
||||
|
||||
ARITHMETIC.forEach(op -> numericBoolean.put(op, true)); // Numbers before comparison
|
||||
COMPARISON.forEach(op -> PRECEDENCE.put(op, numericBoolean));
|
||||
|
||||
|
||||
Map<Token.Type, Boolean> booleanOps = new HashMap<>();
|
||||
ARITHMETIC.forEach(op -> booleanOps.put(op, true)); // Everything before boolean
|
||||
COMPARISON.forEach(op -> booleanOps.put(op, true));
|
||||
|
||||
|
||||
PRECEDENCE.put(Token.Type.BOOLEAN_AND, booleanOps);
|
||||
PRECEDENCE.put(Token.Type.BOOLEAN_OR, booleanOps);
|
||||
public static void ensureReturnType(Expression<?> returnable, Expression.ReturnType... types) {
|
||||
for(Expression.ReturnType type : types) if(returnable.returnType().equals(type)) return;
|
||||
throw new ParseException("Invalid type " + returnable.returnType() + ", expected " + (types.length == 1 ? types[0].toString() : "one of " + Arrays.toString(types)), returnable.getPosition());
|
||||
}
|
||||
|
||||
public static void checkType(Token token, Token.Type... expected) {
|
||||
for(Token.Type type : expected) if(token.getType().equals(type)) return;
|
||||
throw new ParseException("Expected " + Arrays.toString(expected) + " but found " + token.getType(), token.getPosition());
|
||||
}
|
||||
|
||||
public static void checkReturnType(Returnable<?> returnable, Returnable.ReturnType... types) {
|
||||
for(Returnable.ReturnType type : types) if(returnable.returnType().equals(type)) return;
|
||||
throw new ParseException("Expected " + Arrays.toString(types) + " but found " + returnable.returnType(), returnable.getPosition());
|
||||
}
|
||||
|
||||
public static void checkArithmeticOperation(Returnable<?> left, Returnable<?> right, Token operation) {
|
||||
if(!left.returnType().equals(Returnable.ReturnType.NUMBER) || !right.returnType().equals(Returnable.ReturnType.NUMBER)) {
|
||||
throw new ParseException(
|
||||
"Operation " + operation.getType() + " not supported between " + left.returnType() + " and " + right.returnType(),
|
||||
operation.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkBooleanOperation(Returnable<?> left, Returnable<?> right, Token operation) {
|
||||
if(!left.returnType().equals(Returnable.ReturnType.BOOLEAN) || !right.returnType().equals(Returnable.ReturnType.BOOLEAN)) {
|
||||
throw new ParseException(
|
||||
"Operation " + operation.getType() + " not supported between " + left.returnType() + " and " + right.returnType(),
|
||||
operation.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkVarType(Token token, Returnable.ReturnType returnType) {
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if token is a binary operator
|
||||
*
|
||||
* @param token Token to check
|
||||
*
|
||||
* @throws ParseException If token isn't a binary operator
|
||||
*/
|
||||
public static void checkBinaryOperator(Token token) {
|
||||
if(!token.isBinaryOperator())
|
||||
throw new ParseException("Expected binary operator, found " + token.getType(), token.getPosition());
|
||||
}
|
||||
|
||||
public static Returnable.ReturnType getVariableReturnType(Token varToken) {
|
||||
public static Expression.ReturnType getVariableReturnType(Token varToken) {
|
||||
return switch(varToken.getType()) {
|
||||
case NUMBER_VARIABLE -> Returnable.ReturnType.NUMBER;
|
||||
case STRING_VARIABLE -> Returnable.ReturnType.STRING;
|
||||
case BOOLEAN_VARIABLE -> Returnable.ReturnType.BOOLEAN;
|
||||
case TYPE_NUMBER -> Expression.ReturnType.NUMBER;
|
||||
case TYPE_STRING -> Expression.ReturnType.STRING;
|
||||
case TYPE_BOOLEAN -> Expression.ReturnType.BOOLEAN;
|
||||
case TYPE_VOID -> Expression.ReturnType.VOID;
|
||||
default -> throw new ParseException("Unexpected token " + varToken.getType() + "; expected variable declaration",
|
||||
varToken.getPosition());
|
||||
};
|
||||
}
|
||||
|
||||
public static boolean hasPrecedence(Token.Type first, Token.Type second) {
|
||||
if(!PRECEDENCE.containsKey(first)) return false;
|
||||
Map<Token.Type, Boolean> pre = PRECEDENCE.get(first);
|
||||
if(!pre.containsKey(second)) return false;
|
||||
return pre.get(second);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -9,30 +9,30 @@ package com.dfsek.terra.addons.terrascript.parser.exceptions;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
|
||||
|
||||
public class ParseException extends RuntimeException {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 6744390543046766386L;
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
|
||||
public ParseException(String message, Position position) {
|
||||
public ParseException(String message, SourcePosition position) {
|
||||
super(message);
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public ParseException(String message, Position position, Throwable cause) {
|
||||
public ParseException(String message, SourcePosition position, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return super.getMessage() + ": " + position;
|
||||
return "Error at " + position + ": " + super.getMessage();
|
||||
}
|
||||
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+23
-30
@@ -9,35 +9,43 @@ package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationInfo;
|
||||
|
||||
|
||||
public class Block implements Item<Block.ReturnInfo<?>> {
|
||||
private final List<Item<?>> items;
|
||||
private final Position position;
|
||||
public class Block implements Expression<EvaluationInfo<?>> {
|
||||
private final List<Expression<?>> items;
|
||||
private final SourcePosition position;
|
||||
private final ReturnType returnType;
|
||||
|
||||
public Block(List<Item<?>> items, Position position) {
|
||||
public Block(List<Expression<?>> items, SourcePosition position, ReturnType returnType) {
|
||||
this.items = items;
|
||||
this.position = position;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
for(Item<?> item : items) {
|
||||
Object result = item.apply(implementationArguments, scope);
|
||||
if(result instanceof ReturnInfo<?> level) {
|
||||
if(!level.getLevel().equals(ReturnLevel.NONE)) return level;
|
||||
public ReturnType returnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
for(Expression<?> item : items) {
|
||||
Object result = item.evaluate(implementationArguments, scope);
|
||||
if(result instanceof EvaluationInfo<?> evalInfo) {
|
||||
if(!evalInfo.level().equals(EvaluationLevel.NONE)) return evalInfo;
|
||||
}
|
||||
}
|
||||
return new ReturnInfo<>(ReturnLevel.NONE, null);
|
||||
return new EvaluationInfo<>(EvaluationLevel.NONE, Expression.NOOP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public enum ReturnLevel {
|
||||
public enum EvaluationLevel {
|
||||
NONE(false),
|
||||
BREAK(false),
|
||||
CONTINUE(false),
|
||||
@@ -46,7 +54,7 @@ public class Block implements Item<Block.ReturnInfo<?>> {
|
||||
|
||||
private final boolean returnFast;
|
||||
|
||||
ReturnLevel(boolean returnFast) {
|
||||
EvaluationLevel(boolean returnFast) {
|
||||
this.returnFast = returnFast;
|
||||
}
|
||||
|
||||
@@ -56,21 +64,6 @@ public class Block implements Item<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
|
||||
public static class ReturnInfo<T> {
|
||||
private final ReturnLevel level;
|
||||
private final T data;
|
||||
|
||||
public ReturnInfo(ReturnLevel level, T data) {
|
||||
this.level = level;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ReturnLevel getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
public record EvaluationInfo<T extends Expression<?>>(EvaluationLevel level, T data) {
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -7,13 +7,13 @@ import com.dfsek.terra.addons.terrascript.parser.lang.Scope.ScopeBuilder;
|
||||
public class Executable {
|
||||
private final Block script;
|
||||
private final ThreadLocal<Scope> scope;
|
||||
|
||||
|
||||
public Executable(Block script, ScopeBuilder scopeBuilder) {
|
||||
this.script = script;
|
||||
this.scope = ThreadLocal.withInitial(scopeBuilder::build);
|
||||
}
|
||||
|
||||
|
||||
public boolean execute(ImplementationArguments arguments) {
|
||||
return script.apply(arguments, scope.get()).getLevel() != Block.ReturnLevel.FAIL;
|
||||
return script.evaluate(arguments, scope.get()).level() != Block.EvaluationLevel.FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
|
||||
|
||||
public interface Expression<T> {
|
||||
Expression<Void> NOOP = new Expression<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.VOID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourcePosition getPosition() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
ReturnType returnType();
|
||||
|
||||
T evaluate(ImplementationArguments implementationArguments, Scope scope);
|
||||
|
||||
default double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
throw new UnsupportedOperationException("Cannot apply " + this + " as double");
|
||||
}
|
||||
|
||||
default boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
throw new UnsupportedOperationException("Cannot apply " + this + " as double");
|
||||
}
|
||||
|
||||
SourcePosition getPosition();
|
||||
|
||||
|
||||
enum ReturnType {
|
||||
NUMBER(true),
|
||||
STRING(true),
|
||||
BOOLEAN(false),
|
||||
VOID(false),
|
||||
OBJECT(false);
|
||||
|
||||
private final boolean comparable;
|
||||
|
||||
ReturnType(boolean comparable) {
|
||||
this.comparable = comparable;
|
||||
}
|
||||
|
||||
public boolean isComparable() {
|
||||
return comparable;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -8,7 +8,7 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
/**
|
||||
* Arguments passed to {@link Item}s by the implementation
|
||||
* Arguments passed to {@link Expression}s by the implementation
|
||||
*/
|
||||
public interface ImplementationArguments {
|
||||
}
|
||||
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public interface Item<T> {
|
||||
T apply(ImplementationArguments implementationArguments, Scope scope);
|
||||
|
||||
default double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
throw new UnsupportedOperationException("Cannot apply " + this + " as double");
|
||||
}
|
||||
|
||||
default boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
throw new UnsupportedOperationException("Cannot apply " + this + " as double");
|
||||
}
|
||||
|
||||
Position getPosition();
|
||||
}
|
||||
+1
-1
@@ -7,5 +7,5 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
public interface Keyword<T> extends Returnable<T> {
|
||||
public interface Keyword<T> extends Expression<T> {
|
||||
}
|
||||
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
public interface Returnable<T> extends Item<T> {
|
||||
ReturnType returnType();
|
||||
|
||||
enum ReturnType {
|
||||
NUMBER(true),
|
||||
STRING(true),
|
||||
BOOLEAN(false),
|
||||
VOID(false),
|
||||
OBJECT(false);
|
||||
|
||||
private final boolean comparable;
|
||||
|
||||
ReturnType(boolean comparable) {
|
||||
this.comparable = comparable;
|
||||
}
|
||||
|
||||
public boolean isComparable() {
|
||||
return comparable;
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
-13
@@ -6,7 +6,9 @@ import net.jafama.FastMath;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable.ReturnType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression.ReturnType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
|
||||
@@ -46,28 +48,57 @@ public class Scope {
|
||||
}
|
||||
|
||||
public static final class ScopeBuilder {
|
||||
|
||||
private final Map<String, FunctionBuilder<? extends Function<?>>> functions;
|
||||
private final Map<String, Pair<Integer, ReturnType>> indices;
|
||||
private int numSize, boolSize, strSize = 0;
|
||||
private ScopeBuilder parent;
|
||||
|
||||
private boolean inLoop;
|
||||
|
||||
public ScopeBuilder() {
|
||||
this.functions = new HashMap<>();
|
||||
this.indices = new HashMap<>();
|
||||
}
|
||||
|
||||
private ScopeBuilder(ScopeBuilder parent) {
|
||||
private ScopeBuilder(ScopeBuilder parent, boolean inLoop) {
|
||||
this.parent = parent;
|
||||
this.numSize = parent.numSize;
|
||||
this.boolSize = parent.boolSize;
|
||||
this.strSize = parent.strSize;
|
||||
this.functions = new HashMap<>(parent.functions);
|
||||
this.indices = new HashMap<>(parent.indices);
|
||||
this.inLoop = inLoop;
|
||||
}
|
||||
|
||||
private ScopeBuilder(Map<String, FunctionBuilder<? extends Function<?>>> functions) {
|
||||
this.functions = new HashMap<>(functions);
|
||||
this.indices = new HashMap<>();
|
||||
}
|
||||
|
||||
public Scope build() {
|
||||
return new Scope(numSize, boolSize, strSize);
|
||||
}
|
||||
|
||||
public ScopeBuilder sub() {
|
||||
return new ScopeBuilder(this);
|
||||
public ScopeBuilder innerScope() {
|
||||
return new ScopeBuilder(this, inLoop);
|
||||
}
|
||||
|
||||
public ScopeBuilder innerLoopScope() { return new ScopeBuilder(this, true); }
|
||||
|
||||
public ScopeBuilder functionScope() { return new ScopeBuilder(functions); }
|
||||
|
||||
public ScopeBuilder registerFunction(String name, FunctionBuilder<? extends Function<?>> functionBuilder) {
|
||||
functions.put(name, functionBuilder);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean containsFunction(String functionName) {
|
||||
return functions.containsKey(functionName);
|
||||
}
|
||||
|
||||
public FunctionBuilder<?> getFunction(String functionName) {
|
||||
return functions.get(functionName);
|
||||
}
|
||||
|
||||
private String check(String id) {
|
||||
@@ -76,8 +107,12 @@ public class Scope {
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
public int num(String id) {
|
||||
|
||||
public boolean isInLoop() {
|
||||
return inLoop;
|
||||
}
|
||||
|
||||
public int declareNum(String id) {
|
||||
int num = numSize;
|
||||
indices.put(check(id), Pair.of(num, ReturnType.NUMBER));
|
||||
numSize++;
|
||||
@@ -85,7 +120,7 @@ public class Scope {
|
||||
return num;
|
||||
}
|
||||
|
||||
public int str(String id) {
|
||||
public int declareStr(String id) {
|
||||
int str = strSize;
|
||||
indices.put(check(id), Pair.of(str, ReturnType.STRING));
|
||||
strSize++;
|
||||
@@ -93,7 +128,7 @@ public class Scope {
|
||||
return str;
|
||||
}
|
||||
|
||||
public int bool(String id) {
|
||||
public int declareBool(String id) {
|
||||
int bool = boolSize;
|
||||
indices.put(check(id), Pair.of(bool, ReturnType.BOOLEAN));
|
||||
boolSize++;
|
||||
@@ -107,14 +142,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) {
|
||||
@@ -126,12 +161,11 @@ public class Scope {
|
||||
return indices.get(id).getLeft();
|
||||
}
|
||||
|
||||
public ReturnType getType(String id) {
|
||||
public ReturnType getVaraibleType(String id) {
|
||||
return indices.get(id).getRight();
|
||||
}
|
||||
|
||||
|
||||
public boolean contains(String id) {
|
||||
public boolean containsVariable(String id) {
|
||||
return indices.containsKey(id);
|
||||
}
|
||||
}
|
||||
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang;
|
||||
|
||||
public interface Statement extends Item<Boolean> {
|
||||
}
|
||||
+3
-3
@@ -7,15 +7,15 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.constants;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BooleanConstant extends ConstantExpression<Boolean> {
|
||||
private final boolean constant;
|
||||
|
||||
public BooleanConstant(Boolean constant, Position position) {
|
||||
|
||||
public BooleanConstant(Boolean constant, SourcePosition position) {
|
||||
super(constant, position);
|
||||
this.constant = constant;
|
||||
}
|
||||
|
||||
+7
-7
@@ -7,28 +7,28 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.constants;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public abstract class ConstantExpression<T> implements Returnable<T> {
|
||||
public abstract class ConstantExpression<T> implements Expression<T> {
|
||||
private final T constant;
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
|
||||
public ConstantExpression(T constant, Position position) {
|
||||
public ConstantExpression(T constant, SourcePosition position) {
|
||||
this.constant = constant;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public T evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return constant;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -7,16 +7,16 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.constants;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class NumericConstant extends ConstantExpression<Number> {
|
||||
private final double constant;
|
||||
|
||||
public NumericConstant(Number constant, Position position) {
|
||||
|
||||
public NumericConstant(Number constant, SourcePosition position) {
|
||||
super(constant, position);
|
||||
this.constant = constant.doubleValue();
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class NumericConstant extends ConstantExpression<Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.NUMBER;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.NUMBER;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -7,17 +7,17 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.constants;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public class StringConstant extends ConstantExpression<String> {
|
||||
public StringConstant(String constant, Position position) {
|
||||
public StringConstant(String constant, SourcePosition position) {
|
||||
super(constant, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.STRING;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.STRING;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-21
@@ -7,37 +7,20 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.functions;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
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;
|
||||
}
|
||||
};
|
||||
public interface Function<T> extends Expression<T> {
|
||||
|
||||
@Override
|
||||
default double applyDouble(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return ((Number) apply(implementationArguments, scope)).doubleValue();
|
||||
return ((Number) evaluate(implementationArguments, scope)).doubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return (Boolean) apply(implementationArguments, scope);
|
||||
return (Boolean) evaluate(implementationArguments, scope);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-4
@@ -9,14 +9,17 @@ package com.dfsek.terra.addons.terrascript.parser.lang.functions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public interface FunctionBuilder<T extends Function<?>> {
|
||||
T build(List<Returnable<?>> argumentList, Position position);
|
||||
T build(List<Expression<?>> argumentList, SourcePosition position);
|
||||
|
||||
/**
|
||||
* @return Number of function arguments, -1 if the function uses a vararg at the end
|
||||
*/
|
||||
int argNumber();
|
||||
|
||||
Returnable.ReturnType getArgument(int position);
|
||||
Expression.ReturnType getArgument(int position);
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.functions;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression.ReturnType;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
|
||||
public record FunctionSignature(ReturnType returnType, Pair<String, ReturnType>[] arguments) {
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.functions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression.ReturnType;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope.ScopeBuilder;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
|
||||
public class UserDefinedFunctionBuilder<T extends Function<?>> implements FunctionBuilder<T> {
|
||||
|
||||
private final ReturnType returnType;
|
||||
private final List<Pair<Integer, ReturnType>> parameterInfo;
|
||||
private final ScopeBuilder bodyScopeBuilder;
|
||||
private final Block body;
|
||||
|
||||
public UserDefinedFunctionBuilder(ReturnType returnType, List<Pair<Integer, ReturnType>> parameterInfo, Block body,
|
||||
ScopeBuilder functionBodyScope) {
|
||||
this.returnType = returnType;
|
||||
this.bodyScopeBuilder = functionBodyScope;
|
||||
this.body = body;
|
||||
this.parameterInfo = parameterInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
//noinspection unchecked
|
||||
return (T) new Function() {
|
||||
|
||||
private final ThreadLocal<Scope> threadLocalScope = ThreadLocal.withInitial(bodyScopeBuilder::build);
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
Scope bodyScope = threadLocalScope.get();
|
||||
// Pass arguments into scope of function body
|
||||
for(int i = 0; i < argumentList.size(); i++) {
|
||||
Pair<Integer, ReturnType> paramInfo = parameterInfo.get(i);
|
||||
Expression<?> argExpression = argumentList.get(i);
|
||||
switch(paramInfo.getRight()) {
|
||||
case NUMBER -> bodyScope.setNum(paramInfo.getLeft(), argExpression.applyDouble(implementationArguments, scope));
|
||||
case BOOLEAN -> bodyScope.setBool(paramInfo.getLeft(), argExpression.applyBoolean(implementationArguments, scope));
|
||||
case STRING -> bodyScope.setStr(paramInfo.getLeft(), (String) argExpression.evaluate(implementationArguments, scope));
|
||||
}
|
||||
}
|
||||
return body.evaluate(implementationArguments, bodyScope).data().evaluate(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int argNumber() {
|
||||
return parameterInfo.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType getArgument(int position) {
|
||||
return parameterInfo.get(position).getRight();
|
||||
}
|
||||
}
|
||||
+9
-8
@@ -7,27 +7,28 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationInfo;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BreakKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
public class BreakKeyword implements Keyword<EvaluationInfo<?>> {
|
||||
private final SourcePosition position;
|
||||
|
||||
public BreakKeyword(Position position) {
|
||||
public BreakKeyword(SourcePosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.BREAK, null);
|
||||
public EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new EvaluationInfo<>(EvaluationLevel.BREAK, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+9
-8
@@ -7,27 +7,28 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationInfo;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class ContinueKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
public class ContinueKeyword implements Keyword<EvaluationInfo<?>> {
|
||||
private final SourcePosition position;
|
||||
|
||||
public ContinueKeyword(Position position) {
|
||||
public ContinueKeyword(SourcePosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.CONTINUE, null);
|
||||
public EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new EvaluationInfo<>(EvaluationLevel.CONTINUE, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+9
-8
@@ -7,27 +7,28 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationInfo;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class FailKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
public class FailKeyword implements Keyword<EvaluationInfo<?>> {
|
||||
private final SourcePosition position;
|
||||
|
||||
public FailKeyword(Position position) {
|
||||
public FailKeyword(SourcePosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.FAIL, null);
|
||||
public EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new EvaluationInfo<>(EvaluationLevel.FAIL, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+23
-8
@@ -7,27 +7,34 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationInfo;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class ReturnKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
private final Position position;
|
||||
public class ReturnKeyword implements Keyword<EvaluationInfo<?>> {
|
||||
private final SourcePosition position;
|
||||
|
||||
public ReturnKeyword(Position position) {
|
||||
private final Expression<?> data;
|
||||
|
||||
public ReturnKeyword(@Nullable Expression<?> data, SourcePosition position) {
|
||||
this.data = data;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.RETURN, null);
|
||||
public EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return new EvaluationInfo<>(EvaluationLevel.RETURN, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@@ -35,4 +42,12 @@ public class ReturnKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.VOID;
|
||||
}
|
||||
|
||||
public ReturnType dataReturnType() {
|
||||
if(data != null) {
|
||||
return data.returnType();
|
||||
} else {
|
||||
return ReturnType.VOID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-18
@@ -7,23 +7,24 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Item;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class ForKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
public class ForKeyword implements Keyword<Block.EvaluationInfo<?>> {
|
||||
private final Block conditional;
|
||||
private final Item<?> initializer;
|
||||
private final Returnable<Boolean> statement;
|
||||
private final Item<?> incrementer;
|
||||
private final Position position;
|
||||
private final Expression<?> initializer;
|
||||
private final Expression<Boolean> statement;
|
||||
private final Expression<?> incrementer;
|
||||
private final SourcePosition position;
|
||||
|
||||
public ForKeyword(Block conditional, Item<?> initializer, Returnable<Boolean> statement, Item<?> incrementer, Position position) {
|
||||
public ForKeyword(Block conditional, Expression<?> initializer, Expression<Boolean> statement, Expression<?> incrementer,
|
||||
SourcePosition position) {
|
||||
this.conditional = conditional;
|
||||
this.initializer = initializer;
|
||||
this.statement = statement;
|
||||
@@ -32,19 +33,19 @@ public class ForKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
for(initializer.apply(implementationArguments, scope);
|
||||
statement.apply(implementationArguments, scope);
|
||||
incrementer.apply(implementationArguments, scope)) {
|
||||
Block.ReturnInfo<?> level = conditional.apply(implementationArguments, scope);
|
||||
if(level.getLevel().equals(Block.ReturnLevel.BREAK)) break;
|
||||
if(level.getLevel().isReturnFast()) return level;
|
||||
public Block.EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
for(initializer.evaluate(implementationArguments, scope);
|
||||
statement.evaluate(implementationArguments, scope);
|
||||
incrementer.evaluate(implementationArguments, scope)) {
|
||||
Block.EvaluationInfo<?> level = conditional.evaluate(implementationArguments, scope);
|
||||
if(level.level().equals(EvaluationLevel.BREAK)) break;
|
||||
if(level.level().isReturnFast()) return level;
|
||||
}
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.NONE, null);
|
||||
return new Block.EvaluationInfo<>(EvaluationLevel.NONE, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+17
-16
@@ -11,24 +11,25 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
|
||||
public class IfKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
public class IfKeyword implements Keyword<Block.EvaluationInfo<?>> {
|
||||
private final Block conditional;
|
||||
private final Returnable<Boolean> statement;
|
||||
private final Position position;
|
||||
private final List<Pair<Returnable<Boolean>, Block>> elseIf;
|
||||
private final Expression<Boolean> statement;
|
||||
private final SourcePosition position;
|
||||
private final List<Pair<Expression<Boolean>, Block>> elseIf;
|
||||
private final Block elseBlock;
|
||||
|
||||
public IfKeyword(Block conditional, Returnable<Boolean> statement, List<Pair<Returnable<Boolean>, Block>> elseIf,
|
||||
@Nullable Block elseBlock, Position position) {
|
||||
public IfKeyword(Block conditional, Expression<Boolean> statement, List<Pair<Expression<Boolean>, Block>> elseIf,
|
||||
@Nullable Block elseBlock, SourcePosition position) {
|
||||
this.conditional = conditional;
|
||||
this.statement = statement;
|
||||
this.position = position;
|
||||
@@ -37,21 +38,21 @@ public class IfKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
if(statement.apply(implementationArguments, scope)) return conditional.apply(implementationArguments, scope);
|
||||
public Block.EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
if(statement.evaluate(implementationArguments, scope)) return conditional.evaluate(implementationArguments, scope);
|
||||
else {
|
||||
for(Pair<Returnable<Boolean>, Block> pair : elseIf) {
|
||||
if(pair.getLeft().apply(implementationArguments, scope)) {
|
||||
return pair.getRight().apply(implementationArguments, scope);
|
||||
for(Pair<Expression<Boolean>, Block> pair : elseIf) {
|
||||
if(pair.getLeft().evaluate(implementationArguments, scope)) {
|
||||
return pair.getRight().evaluate(implementationArguments, scope);
|
||||
}
|
||||
}
|
||||
if(elseBlock != null) return elseBlock.apply(implementationArguments, scope);
|
||||
if(elseBlock != null) return elseBlock.evaluate(implementationArguments, scope);
|
||||
}
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.NONE, null);
|
||||
return new Block.EvaluationInfo<>(EvaluationLevel.NONE, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+15
-13
@@ -7,37 +7,39 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationInfo;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Block.EvaluationLevel;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class WhileKeyword implements Keyword<Block.ReturnInfo<?>> {
|
||||
public class WhileKeyword implements Keyword<EvaluationInfo<?>> {
|
||||
private final Block conditional;
|
||||
private final Returnable<Boolean> statement;
|
||||
private final Position position;
|
||||
private final Expression<Boolean> statement;
|
||||
private final SourcePosition position;
|
||||
|
||||
public WhileKeyword(Block conditional, Returnable<Boolean> statement, Position position) {
|
||||
public WhileKeyword(Block conditional, Expression<Boolean> statement, SourcePosition position) {
|
||||
this.conditional = conditional;
|
||||
this.statement = statement;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
while(statement.apply(implementationArguments, scope)) {
|
||||
Block.ReturnInfo<?> level = conditional.apply(implementationArguments, scope);
|
||||
if(level.getLevel().equals(Block.ReturnLevel.BREAK)) break;
|
||||
if(level.getLevel().isReturnFast()) return level;
|
||||
public EvaluationInfo<?> evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
while(statement.evaluate(implementationArguments, scope)) {
|
||||
EvaluationInfo<?> level = conditional.evaluate(implementationArguments, scope);
|
||||
if(level.level().equals(EvaluationLevel.BREAK)) break;
|
||||
if(level.level().isReturnFast()) return level;
|
||||
}
|
||||
return new Block.ReturnInfo<>(Block.ReturnLevel.NONE, null);
|
||||
return new EvaluationInfo<>(EvaluationLevel.NONE, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -7,23 +7,23 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public abstract class BinaryOperation<I, O> implements Returnable<O> {
|
||||
protected final Returnable<I> left;
|
||||
protected final Returnable<I> right;
|
||||
private final Position start;
|
||||
public abstract class BinaryOperation<I, O> implements Expression<O> {
|
||||
protected final Expression<I> left;
|
||||
protected final Expression<I> right;
|
||||
private final SourcePosition start;
|
||||
|
||||
public BinaryOperation(Returnable<I> left, Returnable<I> right, Position start) {
|
||||
public BinaryOperation(Expression<I> left, Expression<I> right, SourcePosition start) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return start;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -7,14 +7,14 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BooleanAndOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
public BooleanAndOperation(Returnable<Boolean> left, Returnable<Boolean> right, Position start) {
|
||||
public BooleanAndOperation(Expression<Boolean> left, Expression<Boolean> right, SourcePosition start) {
|
||||
super(left, right, start);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class BooleanAndOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BooleanNotOperation extends UnaryOperation<Boolean> {
|
||||
public BooleanNotOperation(Returnable<Boolean> input, Position position) {
|
||||
public BooleanNotOperation(Expression<Boolean> input, SourcePosition position) {
|
||||
super(input, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BooleanOrOperation extends BinaryOperation<Boolean, Boolean> {
|
||||
public BooleanOrOperation(Returnable<Boolean> left, Returnable<Boolean> right, Position start) {
|
||||
public BooleanOrOperation(Expression<Boolean> left, Expression<Boolean> right, SourcePosition start) {
|
||||
super(left, right, start);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -27,7 +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;
|
||||
|
||||
+7
-7
@@ -7,14 +7,14 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class ConcatenationOperation extends BinaryOperation<Object, Object> {
|
||||
public ConcatenationOperation(Returnable<Object> left, Returnable<Object> right, Position position) {
|
||||
public ConcatenationOperation(Expression<Object> left, Expression<Object> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@@ -30,12 +30,12 @@ public class ConcatenationOperation extends BinaryOperation<Object, Object> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.STRING;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return toString(left.apply(implementationArguments, scope)) + toString(right.apply(implementationArguments, scope));
|
||||
public Object evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return toString(left.evaluate(implementationArguments, scope)) + toString(right.evaluate(implementationArguments, scope));
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -7,24 +7,24 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class DivisionOperation extends BinaryOperation<Number, Number> {
|
||||
public DivisionOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public DivisionOperation(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.NUMBER;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.NUMBER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class ModuloOperation extends BinaryOperation<Number, Number> {
|
||||
public ModuloOperation(Returnable<Number> left, Returnable<Number> right, Position start) {
|
||||
public ModuloOperation(Expression<Number> left, Expression<Number> right, SourcePosition start) {
|
||||
super(left, right, start);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -27,7 +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;
|
||||
|
||||
+5
-5
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class MultiplicationOperation extends BinaryOperation<Number, Number> {
|
||||
public MultiplicationOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public MultiplicationOperation(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -27,7 +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;
|
||||
|
||||
+4
-4
@@ -7,14 +7,14 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class NegationOperation extends UnaryOperation<Number> {
|
||||
public NegationOperation(Returnable<Number> input, Position position) {
|
||||
public NegationOperation(Expression<Number> input, SourcePosition position) {
|
||||
super(input, position);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class NegationOperation extends UnaryOperation<Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class NumberAdditionOperation extends BinaryOperation<Number, Number> {
|
||||
public NumberAdditionOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public NumberAdditionOperation(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -27,7 +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;
|
||||
|
||||
+5
-5
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class SubtractionOperation extends BinaryOperation<Number, Number> {
|
||||
public SubtractionOperation(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public SubtractionOperation(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -27,7 +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;
|
||||
|
||||
+7
-7
@@ -7,21 +7,21 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public abstract class UnaryOperation<T> implements Returnable<T> {
|
||||
protected final Returnable<T> input;
|
||||
private final Position position;
|
||||
public abstract class UnaryOperation<T> implements Expression<T> {
|
||||
protected final Expression<T> input;
|
||||
private final SourcePosition position;
|
||||
|
||||
public UnaryOperation(Returnable<T> input, Position position) {
|
||||
public UnaryOperation(Expression<T> input, SourcePosition position) {
|
||||
this.input = input;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-9
@@ -9,40 +9,40 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations.statements;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import static com.dfsek.terra.api.util.MathUtil.EPSILON;
|
||||
|
||||
|
||||
public class EqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
|
||||
public EqualsStatement(Returnable<Object> left, Returnable<Object> right, Position position) {
|
||||
public EqualsStatement(Expression<Object> left, Expression<Object> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
Object leftValue = left.apply(implementationArguments, scope);
|
||||
Object rightValue = right.apply(implementationArguments, scope);
|
||||
Object leftValue = left.evaluate(implementationArguments, scope);
|
||||
Object rightValue = right.evaluate(implementationArguments, scope);
|
||||
if(leftValue instanceof Number l && rightValue instanceof Number r) {
|
||||
return FastMath.abs(l.doubleValue() - r.doubleValue()) <= EPSILON;
|
||||
}
|
||||
|
||||
|
||||
return leftValue.equals(rightValue);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -7,25 +7,25 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations.statements;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class GreaterOrEqualsThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public GreaterOrEqualsThanStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public GreaterOrEqualsThanStatement(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -7,21 +7,21 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations.statements;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class GreaterThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public GreaterThanStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public GreaterThanStatement(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class GreaterThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -7,21 +7,21 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations.statements;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class LessThanOrEqualsStatement extends BinaryOperation<Number, Boolean> {
|
||||
public LessThanOrEqualsStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public LessThanOrEqualsStatement(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -29,9 +29,9 @@ 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;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -7,21 +7,21 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.operations.statements;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class LessThanStatement extends BinaryOperation<Number, Boolean> {
|
||||
public LessThanStatement(Returnable<Number> left, Returnable<Number> right, Position position) {
|
||||
public LessThanStatement(Expression<Number> left, Expression<Number> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@@ -29,9 +29,9 @@ 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;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,29 +9,29 @@ package com.dfsek.terra.addons.terrascript.parser.lang.operations.statements;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.operations.BinaryOperation;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
import static com.dfsek.terra.api.util.MathUtil.EPSILON;
|
||||
|
||||
|
||||
public class NotEqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
public NotEqualsStatement(Returnable<Object> left, Returnable<Object> right, Position position) {
|
||||
public NotEqualsStatement(Expression<Object> left, Expression<Object> right, SourcePosition position) {
|
||||
super(left, right, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean applyBoolean(ImplementationArguments implementationArguments, Scope scope) {
|
||||
Object leftValue = left.apply(implementationArguments, scope);
|
||||
Object rightValue = right.apply(implementationArguments, scope);
|
||||
Object leftValue = left.evaluate(implementationArguments, scope);
|
||||
Object rightValue = right.evaluate(implementationArguments, scope);
|
||||
if(leftValue instanceof Number l && rightValue instanceof Number r) {
|
||||
return FastMath.abs(l.doubleValue() - r.doubleValue()) > EPSILON;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class NotEqualsStatement extends BinaryOperation<Object, Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType returnType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
public Expression.ReturnType returnType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -7,15 +7,15 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public class BooleanVariable implements Variable<Boolean> {
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
private Boolean value;
|
||||
|
||||
public BooleanVariable(Boolean value, Position position) {
|
||||
public BooleanVariable(Boolean value, SourcePosition position) {
|
||||
this.value = value;
|
||||
this.position = position;
|
||||
}
|
||||
@@ -31,12 +31,12 @@ public class BooleanVariable implements Variable<Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getType() {
|
||||
return Returnable.ReturnType.BOOLEAN;
|
||||
public Expression.ReturnType getType() {
|
||||
return Expression.ReturnType.BOOLEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -7,15 +7,15 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public class NumberVariable implements Variable<Number> {
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
private Number value;
|
||||
|
||||
public NumberVariable(Number value, Position position) {
|
||||
public NumberVariable(Number value, SourcePosition position) {
|
||||
this.value = value;
|
||||
this.position = position;
|
||||
}
|
||||
@@ -31,12 +31,12 @@ public class NumberVariable implements Variable<Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getType() {
|
||||
return Returnable.ReturnType.NUMBER;
|
||||
public Expression.ReturnType getType() {
|
||||
return Expression.ReturnType.NUMBER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -7,15 +7,15 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public class StringVariable implements Variable<String> {
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
private String value;
|
||||
|
||||
public StringVariable(String value, Position position) {
|
||||
public StringVariable(String value, SourcePosition position) {
|
||||
this.value = value;
|
||||
this.position = position;
|
||||
}
|
||||
@@ -31,12 +31,12 @@ public class StringVariable implements Variable<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getType() {
|
||||
return Returnable.ReturnType.STRING;
|
||||
public Expression.ReturnType getType() {
|
||||
return Expression.ReturnType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -7,8 +7,8 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public interface Variable<T> {
|
||||
@@ -16,7 +16,7 @@ public interface Variable<T> {
|
||||
|
||||
void setValue(T value);
|
||||
|
||||
Returnable.ReturnType getType();
|
||||
Expression.ReturnType getType();
|
||||
|
||||
Position getPosition();
|
||||
SourcePosition getPosition();
|
||||
}
|
||||
|
||||
+9
-4
@@ -1,18 +1,23 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.assign;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BoolAssignmentNode extends VariableAssignmentNode<Boolean> {
|
||||
public BoolAssignmentNode(Returnable<Boolean> value, Position position, int index) {
|
||||
public BoolAssignmentNode(Expression<Boolean> value, SourcePosition position, int index) {
|
||||
super(value, position, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.BOOLEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyBoolean(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+9
-4
@@ -1,18 +1,23 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.assign;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class NumAssignmentNode extends VariableAssignmentNode<Number> {
|
||||
public NumAssignmentNode(Returnable<Number> value, Position position, int index) {
|
||||
public NumAssignmentNode(Expression<Number> value, SourcePosition position, int index) {
|
||||
super(value, position, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return applyDouble(implementationArguments, scope);
|
||||
}
|
||||
|
||||
|
||||
+10
-5
@@ -1,19 +1,24 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.assign;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class StrAssignmentNode extends VariableAssignmentNode<String> {
|
||||
public StrAssignmentNode(Returnable<String> value, Position position, int index) {
|
||||
public StrAssignmentNode(Expression<String> value, SourcePosition position, int index) {
|
||||
super(value, position, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
String val = value.apply(implementationArguments, scope);
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
String val = value.evaluate(implementationArguments, scope);
|
||||
scope.setStr(index, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
+7
-8
@@ -7,25 +7,24 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.assign;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Item;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public abstract class VariableAssignmentNode<T> implements Item<T> {
|
||||
protected final Returnable<T> value;
|
||||
public abstract class VariableAssignmentNode<T> implements Expression<T> {
|
||||
protected final Expression<T> value;
|
||||
protected final int index;
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
|
||||
|
||||
public VariableAssignmentNode(Returnable<T> value, Position position, int index) {
|
||||
public VariableAssignmentNode(Expression<T> value, SourcePosition position, int index) {
|
||||
this.value = value;
|
||||
this.index = index;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,17 +1,17 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.reference;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class BoolVariableReferenceNode extends VariableReferenceNode<Boolean> {
|
||||
public BoolVariableReferenceNode(Position position, ReturnType type, int index) {
|
||||
public BoolVariableReferenceNode(SourcePosition position, ReturnType type, int index) {
|
||||
super(position, type, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return scope.getBool(index);
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,17 +1,17 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.reference;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class NumVariableReferenceNode extends VariableReferenceNode<Number> {
|
||||
public NumVariableReferenceNode(Position position, ReturnType type, int index) {
|
||||
public NumVariableReferenceNode(SourcePosition position, ReturnType type, int index) {
|
||||
super(position, type, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return scope.getNum(index);
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,17 +1,17 @@
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.reference;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class StrVariableReferenceNode extends VariableReferenceNode<String> {
|
||||
public StrVariableReferenceNode(Position position, ReturnType type, int index) {
|
||||
public StrVariableReferenceNode(SourcePosition position, ReturnType type, int index) {
|
||||
super(position, type, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public String evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return scope.getStr(index);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -7,16 +7,16 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.parser.lang.variables.reference;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
|
||||
|
||||
public abstract class VariableReferenceNode<T> implements Returnable<T> {
|
||||
public abstract class VariableReferenceNode<T> implements Expression<T> {
|
||||
protected final int index;
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
private final ReturnType type;
|
||||
|
||||
public VariableReferenceNode(Position position, ReturnType type, int index) {
|
||||
public VariableReferenceNode(SourcePosition position, ReturnType type, int index) {
|
||||
this.position = position;
|
||||
this.type = type;
|
||||
this.index = index;
|
||||
@@ -28,7 +28,7 @@ public abstract class VariableReferenceNode<T> implements Returnable<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+24
-23
@@ -7,6 +7,8 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.script;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Lexer;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -19,7 +21,8 @@ import java.util.Random;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.Parser;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Executable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope.ScopeBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.builders.BinaryNumberFunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.builders.BiomeFunctionBuilder;
|
||||
@@ -51,32 +54,34 @@ import com.dfsek.terra.api.world.WritableWorld;
|
||||
|
||||
public class StructureScript implements Structure, Keyed<StructureScript> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(StructureScript.class);
|
||||
private final Executable block;
|
||||
private final Executable executable;
|
||||
private final RegistryKey id;
|
||||
|
||||
private final String profile;
|
||||
private final Platform platform;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public StructureScript(InputStream inputStream, RegistryKey id, Platform platform, Registry<Structure> registry,
|
||||
public StructureScript(InputStream source, RegistryKey id, Platform platform, Registry<Structure> structureRegistry,
|
||||
Registry<LootTable> lootRegistry,
|
||||
Registry<FunctionBuilder> functionRegistry) {
|
||||
Parser parser;
|
||||
Lexer lexer;
|
||||
try {
|
||||
parser = new Parser(IOUtils.toString(inputStream, Charset.defaultCharset()));
|
||||
lexer = new Lexer(IOUtils.toString(source, Charset.defaultCharset()));
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Parser parser = new Parser(lexer);
|
||||
this.id = id;
|
||||
this.profile = "terrascript_direct:" + id;
|
||||
|
||||
//noinspection unchecked
|
||||
functionRegistry.forEach((key, function) -> parser.registerFunction(key.getID(), function)); // Register registry functions.
|
||||
ScopeBuilder scope = new ScopeBuilder();
|
||||
|
||||
parser
|
||||
functionRegistry.forEach((key, function) -> scope.registerFunction(key.getID(), function)); // Register registry functions.
|
||||
|
||||
scope
|
||||
.registerFunction("block", new BlockFunctionBuilder(platform))
|
||||
.registerFunction("debugBlock", new BlockFunctionBuilder(platform))
|
||||
.registerFunction("structure", new StructureFunctionBuilder(registry, platform))
|
||||
.registerFunction("structure", new StructureFunctionBuilder(structureRegistry, platform))
|
||||
.registerFunction("randomInt", new RandomFunctionBuilder())
|
||||
.registerFunction("recursions", new RecursionsFunctionBuilder())
|
||||
.registerFunction("setMark", new SetMarkFunctionBuilder())
|
||||
@@ -89,15 +94,15 @@ public class StructureScript implements Structure, Keyed<StructureScript> {
|
||||
.registerFunction("state", new StateFunctionBuilder(platform))
|
||||
.registerFunction("setWaterlog", new UnaryBooleanFunctionBuilder((waterlog, args) -> args.setWaterlog(waterlog)))
|
||||
.registerFunction("originX", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getX(),
|
||||
Returnable.ReturnType.NUMBER))
|
||||
Expression.ReturnType.NUMBER))
|
||||
.registerFunction("originY", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getY(),
|
||||
Returnable.ReturnType.NUMBER))
|
||||
Expression.ReturnType.NUMBER))
|
||||
.registerFunction("originZ", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getZ(),
|
||||
Returnable.ReturnType.NUMBER))
|
||||
Expression.ReturnType.NUMBER))
|
||||
.registerFunction("rotation", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().toString(),
|
||||
Returnable.ReturnType.STRING))
|
||||
Expression.ReturnType.STRING))
|
||||
.registerFunction("rotationDegrees", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().getDegrees(),
|
||||
Returnable.ReturnType.NUMBER))
|
||||
Expression.ReturnType.NUMBER))
|
||||
.registerFunction("print",
|
||||
new UnaryStringFunctionBuilder(string -> LOGGER.info("[TerraScript:{}] {}", id, string)))
|
||||
.registerFunction("abs", new UnaryNumberFunctionBuilder(number -> FastMath.abs(number.doubleValue())))
|
||||
@@ -120,11 +125,7 @@ public class StructureScript implements Structure, Keyed<StructureScript> {
|
||||
.registerFunction("min", new BinaryNumberFunctionBuilder(
|
||||
(number, number2) -> FastMath.min(number.doubleValue(), number2.doubleValue())));
|
||||
|
||||
if(!platform.getTerraConfig().isDebugScript()) {
|
||||
parser.ignoreFunction("debugBlock");
|
||||
}
|
||||
|
||||
block = parser.parse();
|
||||
executable = parser.parse(scope);
|
||||
this.platform = platform;
|
||||
}
|
||||
|
||||
@@ -132,21 +133,21 @@ public class StructureScript implements Structure, Keyed<StructureScript> {
|
||||
@SuppressWarnings("try")
|
||||
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
|
||||
platform.getProfiler().push(profile);
|
||||
boolean result = applyBlock(new TerraImplementationArguments(location, rotation, random, world, 0));
|
||||
boolean result = execute(new TerraImplementationArguments(location, rotation, random, world, 0));
|
||||
platform.getProfiler().pop(profile);
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation, int recursions) {
|
||||
platform.getProfiler().push(profile);
|
||||
boolean result = applyBlock(new TerraImplementationArguments(location, rotation, random, world, recursions));
|
||||
boolean result = execute(new TerraImplementationArguments(location, rotation, random, world, recursions));
|
||||
platform.getProfiler().pop(profile);
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean applyBlock(TerraImplementationArguments arguments) {
|
||||
private boolean execute(TerraImplementationArguments arguments) {
|
||||
try {
|
||||
return block.execute(arguments);
|
||||
return executable.execute(arguments);
|
||||
} catch(RuntimeException e) {
|
||||
LOGGER.error("Failed to generate structure at {}", arguments.getOrigin(), e);
|
||||
return false;
|
||||
|
||||
+9
-9
@@ -10,12 +10,12 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
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.tokenizer.Position;
|
||||
|
||||
|
||||
public class BinaryNumberFunctionBuilder implements FunctionBuilder<Function<Number>> {
|
||||
@@ -27,7 +27,7 @@ public class BinaryNumberFunctionBuilder implements FunctionBuilder<Function<Num
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Number> build(List<Returnable<?>> argumentList, Position position) {
|
||||
public Function<Number> build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
@@ -36,13 +36,13 @@ public class BinaryNumberFunctionBuilder implements FunctionBuilder<Function<Num
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments, scope),
|
||||
((Returnable<Number>) argumentList.get(1)).apply(implementationArguments, scope));
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return function.apply(((Expression<Number>) argumentList.get(0)).evaluate(implementationArguments, scope),
|
||||
((Expression<Number>) argumentList.get(1)).evaluate(implementationArguments, scope));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
@@ -54,8 +54,8 @@ public class BinaryNumberFunctionBuilder implements FunctionBuilder<Function<Num
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0 || position == 1) return Returnable.ReturnType.NUMBER;
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
if(position == 0 || position == 1) return Expression.ReturnType.NUMBER;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -9,10 +9,10 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.BiomeFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ public class BiomeFunctionBuilder implements FunctionBuilder<BiomeFunction> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public BiomeFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new BiomeFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), position);
|
||||
public BiomeFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new BiomeFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,9 +36,9 @@ public class BiomeFunctionBuilder implements FunctionBuilder<BiomeFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+13
-13
@@ -9,13 +9,13 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.BooleanConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.StringConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.BlockFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
|
||||
|
||||
@@ -28,17 +28,17 @@ public class BlockFunctionBuilder implements FunctionBuilder<BlockFunction> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public BlockFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
public BlockFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
if(argumentList.size() < 4) throw new ParseException("Expected data", position);
|
||||
Returnable<Boolean> booleanReturnable = new BooleanConstant(true, position);
|
||||
if(argumentList.size() == 5) booleanReturnable = (Returnable<Boolean>) argumentList.get(4);
|
||||
Expression<Boolean> booleanReturnable = new BooleanConstant(true, position);
|
||||
if(argumentList.size() == 5) booleanReturnable = (Expression<Boolean>) argumentList.get(4);
|
||||
if(argumentList.get(3) instanceof StringConstant) {
|
||||
return new BlockFunction.Constant((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (StringConstant) argumentList.get(3),
|
||||
return new BlockFunction.Constant((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (StringConstant) argumentList.get(3),
|
||||
booleanReturnable, platform, position);
|
||||
}
|
||||
return new BlockFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), booleanReturnable,
|
||||
return new BlockFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (Expression<String>) argumentList.get(3), booleanReturnable,
|
||||
platform, position);
|
||||
}
|
||||
|
||||
@@ -48,11 +48,11 @@ public class BlockFunctionBuilder implements FunctionBuilder<BlockFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 3 -> Returnable.ReturnType.STRING;
|
||||
case 4 -> Returnable.ReturnType.BOOLEAN;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
case 3 -> Expression.ReturnType.STRING;
|
||||
case 4 -> Expression.ReturnType.BOOLEAN;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+7
-7
@@ -9,18 +9,18 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.CheckBlockFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class CheckBlockFunctionBuilder implements FunctionBuilder<CheckBlockFunction> {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public CheckBlockFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new CheckBlockFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), position);
|
||||
public CheckBlockFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new CheckBlockFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -29,9 +29,9 @@ public class CheckBlockFunctionBuilder implements FunctionBuilder<CheckBlockFunc
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,10 +9,10 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.EntityFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ public class EntityFunctionBuilder implements FunctionBuilder<EntityFunction> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public EntityFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new EntityFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), platform, position);
|
||||
public EntityFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new EntityFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (Expression<String>) argumentList.get(3), platform, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,10 +36,10 @@ public class EntityFunctionBuilder implements FunctionBuilder<EntityFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 3 -> Returnable.ReturnType.STRING;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
case 3 -> Expression.ReturnType.STRING;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+7
-7
@@ -9,10 +9,10 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.GetMarkFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class GetMarkFunctionBuilder implements FunctionBuilder<GetMarkFunction> {
|
||||
@@ -22,9 +22,9 @@ public class GetMarkFunctionBuilder implements FunctionBuilder<GetMarkFunction>
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public GetMarkFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new GetMarkFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), position);
|
||||
public GetMarkFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new GetMarkFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,9 +33,9 @@ public class GetMarkFunctionBuilder implements FunctionBuilder<GetMarkFunction>
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,11 +9,11 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.StructureScript;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.LootFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.LootTable;
|
||||
@@ -32,9 +32,9 @@ public class LootFunctionBuilder implements FunctionBuilder<LootFunction> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public LootFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new LootFunction(registry, (Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), platform, position,
|
||||
public LootFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new LootFunction(registry, (Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (Expression<String>) argumentList.get(3), platform, position,
|
||||
script);
|
||||
}
|
||||
|
||||
@@ -44,10 +44,10 @@ public class LootFunctionBuilder implements FunctionBuilder<LootFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 3 -> Returnable.ReturnType.STRING;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
case 3 -> Expression.ReturnType.STRING;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,10 +9,10 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.PullFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
|
||||
|
||||
@@ -25,9 +25,9 @@ public class PullFunctionBuilder implements FunctionBuilder<PullFunction> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public PullFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new PullFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), platform, position);
|
||||
public PullFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new PullFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (Expression<String>) argumentList.get(3), platform, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,10 +36,10 @@ public class PullFunctionBuilder implements FunctionBuilder<PullFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 3 -> Returnable.ReturnType.STRING;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
case 3 -> Expression.ReturnType.STRING;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+6
-6
@@ -9,17 +9,17 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.RandomFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class RandomFunctionBuilder implements FunctionBuilder<RandomFunction> {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public RandomFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new RandomFunction((Returnable<Number>) argumentList.get(0), position);
|
||||
public RandomFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new RandomFunction((Expression<Number>) argumentList.get(0), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -28,8 +28,8 @@ public class RandomFunctionBuilder implements FunctionBuilder<RandomFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Returnable.ReturnType.NUMBER;
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Expression.ReturnType.NUMBER;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -9,15 +9,15 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.RecursionsFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class RecursionsFunctionBuilder implements FunctionBuilder<RecursionsFunction> {
|
||||
@Override
|
||||
public RecursionsFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
public RecursionsFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new RecursionsFunction(position);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class RecursionsFunctionBuilder implements FunctionBuilder<RecursionsFunc
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,10 +9,10 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.SetMarkFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class SetMarkFunctionBuilder implements FunctionBuilder<SetMarkFunction> {
|
||||
@@ -22,9 +22,9 @@ public class SetMarkFunctionBuilder implements FunctionBuilder<SetMarkFunction>
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public SetMarkFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
return new SetMarkFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), position);
|
||||
public SetMarkFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new SetMarkFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (Expression<String>) argumentList.get(3), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,10 +33,10 @@ public class SetMarkFunctionBuilder implements FunctionBuilder<SetMarkFunction>
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 3 -> Returnable.ReturnType.STRING;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
case 3 -> Expression.ReturnType.STRING;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,11 +9,11 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.StateFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ public class StateFunctionBuilder implements FunctionBuilder<StateFunction> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public StateFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
public StateFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
if(argumentList.size() < 4) throw new ParseException("Expected data", position);
|
||||
return new StateFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
|
||||
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), position);
|
||||
return new StateFunction((Expression<Number>) argumentList.get(0), (Expression<Number>) argumentList.get(1),
|
||||
(Expression<Number>) argumentList.get(2), (Expression<String>) argumentList.get(3), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,10 +38,10 @@ public class StateFunctionBuilder implements FunctionBuilder<StateFunction> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
case 3 -> Returnable.ReturnType.STRING;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
case 3 -> Expression.ReturnType.STRING;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
+9
-9
@@ -10,11 +10,11 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.functions.StructureFunction;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
@@ -31,12 +31,12 @@ public class StructureFunctionBuilder implements FunctionBuilder<StructureFuncti
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public StructureFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||
public StructureFunction build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
if(argumentList.size() < 5) throw new ParseException("Expected rotations", position);
|
||||
|
||||
return new StructureFunction((Returnable<Number>) argumentList.remove(0), (Returnable<Number>) argumentList.remove(0),
|
||||
(Returnable<Number>) argumentList.remove(0), (Returnable<String>) argumentList.remove(0),
|
||||
argumentList.stream().map(item -> ((Returnable<String>) item)).collect(Collectors.toList()), registry,
|
||||
return new StructureFunction((Expression<Number>) argumentList.remove(0), (Expression<Number>) argumentList.remove(0),
|
||||
(Expression<Number>) argumentList.remove(0), (Expression<String>) argumentList.remove(0),
|
||||
argumentList.stream().map(item -> ((Expression<String>) item)).collect(Collectors.toList()), registry,
|
||||
position, platform);
|
||||
}
|
||||
|
||||
@@ -46,10 +46,10 @@ public class StructureFunctionBuilder implements FunctionBuilder<StructureFuncti
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
return switch(position) {
|
||||
case 0, 1, 2 -> Returnable.ReturnType.NUMBER;
|
||||
default -> Returnable.ReturnType.STRING;
|
||||
case 0, 1, 2 -> Expression.ReturnType.NUMBER;
|
||||
default -> Expression.ReturnType.STRING;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -10,13 +10,13 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
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.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class UnaryBooleanFunctionBuilder implements FunctionBuilder<Function<Void>> {
|
||||
@@ -28,7 +28,7 @@ public class UnaryBooleanFunctionBuilder implements FunctionBuilder<Function<Voi
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Void> build(List<Returnable<?>> argumentList, Position position) {
|
||||
public Function<Void> build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
@@ -37,14 +37,14 @@ public class UnaryBooleanFunctionBuilder implements FunctionBuilder<Function<Voi
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
function.accept(((Returnable<Boolean>) argumentList.get(0)).apply(implementationArguments, scope),
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
function.accept(((Expression<Boolean>) argumentList.get(0)).evaluate(implementationArguments, scope),
|
||||
(TerraImplementationArguments) implementationArguments);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
@@ -56,8 +56,8 @@ public class UnaryBooleanFunctionBuilder implements FunctionBuilder<Function<Voi
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Returnable.ReturnType.BOOLEAN;
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Expression.ReturnType.BOOLEAN;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,12 +9,12 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
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.tokenizer.Position;
|
||||
|
||||
|
||||
public class UnaryNumberFunctionBuilder implements FunctionBuilder<Function<Number>> {
|
||||
@@ -26,7 +26,7 @@ public class UnaryNumberFunctionBuilder implements FunctionBuilder<Function<Numb
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Number> build(List<Returnable<?>> argumentList, Position position) {
|
||||
public Function<Number> build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
@@ -35,12 +35,12 @@ public class UnaryNumberFunctionBuilder implements FunctionBuilder<Function<Numb
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments, scope));
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return function.apply(((Expression<Number>) argumentList.get(0)).evaluate(implementationArguments, scope));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
@@ -52,8 +52,8 @@ public class UnaryNumberFunctionBuilder implements FunctionBuilder<Function<Numb
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Returnable.ReturnType.NUMBER;
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Expression.ReturnType.NUMBER;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,12 +9,12 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
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.tokenizer.Position;
|
||||
|
||||
|
||||
public class UnaryStringFunctionBuilder implements FunctionBuilder<Function<Void>> {
|
||||
@@ -26,7 +26,7 @@ public class UnaryStringFunctionBuilder implements FunctionBuilder<Function<Void
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<Void> build(List<Returnable<?>> argumentList, Position position) {
|
||||
public Function<Void> build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
@@ -35,13 +35,13 @@ public class UnaryStringFunctionBuilder implements FunctionBuilder<Function<Void
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
function.accept(((Returnable<String>) argumentList.get(0)).apply(implementationArguments, scope));
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
function.accept(((Expression<String>) argumentList.get(0)).evaluate(implementationArguments, scope));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
@@ -53,8 +53,8 @@ public class UnaryStringFunctionBuilder implements FunctionBuilder<Function<Void
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Returnable.ReturnType.STRING;
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
if(position == 0) return Expression.ReturnType.STRING;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -9,26 +9,26 @@ package com.dfsek.terra.addons.terrascript.script.builders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
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.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class ZeroArgFunctionBuilder<T> implements FunctionBuilder<Function<T>> {
|
||||
private final java.util.function.Function<TerraImplementationArguments, T> function;
|
||||
private final Returnable.ReturnType type;
|
||||
private final Expression.ReturnType type;
|
||||
|
||||
public ZeroArgFunctionBuilder(java.util.function.Function<TerraImplementationArguments, T> function, Returnable.ReturnType type) {
|
||||
public ZeroArgFunctionBuilder(java.util.function.Function<TerraImplementationArguments, T> function, Expression.ReturnType type) {
|
||||
this.function = function;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<T> build(List<Returnable<?>> argumentList, Position position) {
|
||||
public Function<T> build(List<Expression<?>> argumentList, SourcePosition position) {
|
||||
return new Function<>() {
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
@@ -36,12 +36,12 @@ public class ZeroArgFunctionBuilder<T> implements FunctionBuilder<Function<T>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public T evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return function.apply((TerraImplementationArguments) implementationArguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
};
|
||||
@@ -53,7 +53,7 @@ public class ZeroArgFunctionBuilder<T> implements FunctionBuilder<Function<T>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
public Expression.ReturnType getArgument(int position) {
|
||||
if(position == 0) return type;
|
||||
return null;
|
||||
}
|
||||
|
||||
+10
-10
@@ -9,12 +9,12 @@ package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
@@ -22,11 +22,11 @@ import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
|
||||
public class BiomeFunction implements Function<String> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
|
||||
|
||||
public BiomeFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) {
|
||||
public BiomeFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, SourcePosition position) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
@@ -35,11 +35,11 @@ public class BiomeFunction implements Function<String> {
|
||||
|
||||
|
||||
@Override
|
||||
public String apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public String evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()),
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
@@ -49,12 +49,12 @@ public class BiomeFunction implements Function<String> {
|
||||
.toVector3()
|
||||
.mutable()
|
||||
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, scope).intValue(),
|
||||
y.evaluate(implementationArguments, scope).intValue(),
|
||||
FastMath.roundToInt(xz.getZ()))).immutable(), arguments.getWorld().getSeed()).getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+18
-17
@@ -14,13 +14,13 @@ import org.slf4j.LoggerFactory;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.StringConstant;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
@@ -30,15 +30,15 @@ import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
public class BlockFunction implements Function<Void> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(BlockFunction.class);
|
||||
protected final Returnable<Number> x, y, z;
|
||||
protected final Returnable<String> blockData;
|
||||
protected final Expression<Number> x, y, z;
|
||||
protected final Expression<String> blockData;
|
||||
protected final Platform platform;
|
||||
private final Map<String, BlockState> data = new HashMap<>();
|
||||
private final Returnable<Boolean> overwrite;
|
||||
private final Position position;
|
||||
private final Expression<Boolean> overwrite;
|
||||
private final SourcePosition position;
|
||||
|
||||
public BlockFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> blockData,
|
||||
Returnable<Boolean> overwrite, Platform platform, Position position) {
|
||||
public BlockFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, Expression<String> blockData,
|
||||
Expression<Boolean> overwrite, Platform platform, SourcePosition position) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
@@ -49,7 +49,7 @@ public class BlockFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
BlockState rot = getBlockState(implementationArguments, scope);
|
||||
setBlock(implementationArguments, scope, arguments, rot);
|
||||
@@ -57,7 +57,7 @@ public class BlockFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@@ -68,14 +68,15 @@ public class BlockFunction implements Function<Void> {
|
||||
|
||||
void setBlock(ImplementationArguments implementationArguments, Scope scope,
|
||||
TerraImplementationArguments arguments, BlockState rot) {
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
try {
|
||||
Vector3.Mutable set = Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, scope).doubleValue(),
|
||||
y.evaluate(implementationArguments, scope).doubleValue(),
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());
|
||||
BlockState current = arguments.getWorld().getBlockState(set);
|
||||
if(overwrite.apply(implementationArguments, scope) || current.isAir()) {
|
||||
if(overwrite.evaluate(implementationArguments, scope) || current.isAir()) {
|
||||
arguments.getWorld().setBlockState(set, rot);
|
||||
}
|
||||
} catch(RuntimeException e) {
|
||||
@@ -84,15 +85,15 @@ public class BlockFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
protected BlockState getBlockState(ImplementationArguments arguments, Scope scope) {
|
||||
return data.computeIfAbsent(blockData.apply(arguments, scope), platform.getWorldHandle()::createBlockState);
|
||||
return data.computeIfAbsent(blockData.evaluate(arguments, scope), platform.getWorldHandle()::createBlockState);
|
||||
}
|
||||
|
||||
|
||||
public static class Constant extends BlockFunction {
|
||||
private final BlockState state;
|
||||
|
||||
public Constant(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, StringConstant blockData,
|
||||
Returnable<Boolean> overwrite, Platform platform, Position position) {
|
||||
public Constant(Expression<Number> x, Expression<Number> y, Expression<Number> z, StringConstant blockData,
|
||||
Expression<Boolean> overwrite, Platform platform, SourcePosition position) {
|
||||
super(x, y, z, blockData, overwrite, platform, position);
|
||||
this.state = platform.getWorldHandle().createBlockState(blockData.getConstant());
|
||||
}
|
||||
|
||||
+10
-10
@@ -9,22 +9,22 @@ package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
|
||||
public class CheckBlockFunction implements Function<String> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
|
||||
public CheckBlockFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) {
|
||||
public CheckBlockFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, SourcePosition position) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
@@ -33,11 +33,11 @@ public class CheckBlockFunction implements Function<String> {
|
||||
|
||||
|
||||
@Override
|
||||
public String apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public String evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()),
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public class CheckBlockFunction implements Function<String> {
|
||||
.toVector3()
|
||||
.mutable()
|
||||
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, scope)
|
||||
y.evaluate(implementationArguments, scope)
|
||||
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
|
||||
.getAsString();
|
||||
if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties
|
||||
@@ -54,7 +54,7 @@ public class CheckBlockFunction implements Function<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+12
-11
@@ -7,14 +7,14 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.ConstantExpression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.EntityType;
|
||||
@@ -26,12 +26,12 @@ import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
public class EntityFunction implements Function<Void> {
|
||||
private final EntityType data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
private final Platform platform;
|
||||
|
||||
public EntityFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data, Platform platform,
|
||||
Position position) {
|
||||
public EntityFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, Expression<String> data, Platform platform,
|
||||
SourcePosition position) {
|
||||
this.position = position;
|
||||
this.platform = platform;
|
||||
if(!(data instanceof ConstantExpression)) throw new ParseException("Entity data must be constant", data.getPosition());
|
||||
@@ -43,12 +43,13 @@ public class EntityFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, scope).doubleValue(),
|
||||
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.evaluate(implementationArguments, scope).doubleValue(),
|
||||
xz.getZ())
|
||||
.mutable()
|
||||
.add(arguments.getOrigin())
|
||||
@@ -59,7 +60,7 @@ public class EntityFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+11
-10
@@ -9,22 +9,22 @@ package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
|
||||
public class GetMarkFunction implements Function<String> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
|
||||
public GetMarkFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) {
|
||||
public GetMarkFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, SourcePosition position) {
|
||||
this.position = position;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@@ -32,13 +32,14 @@ public class GetMarkFunction implements Function<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public String evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
|
||||
y.apply(implementationArguments, scope).doubleValue()),
|
||||
y.evaluate(implementationArguments, scope).doubleValue()),
|
||||
FastMath.floorToInt(xz.getZ()))
|
||||
.mutable()
|
||||
.add(arguments.getOrigin())
|
||||
@@ -47,7 +48,7 @@ public class GetMarkFunction implements Function<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+16
-16
@@ -13,13 +13,13 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.StructureScript;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.block.entity.BlockEntity;
|
||||
import com.dfsek.terra.api.block.entity.Container;
|
||||
@@ -35,14 +35,14 @@ import com.dfsek.terra.api.util.vector.Vector3;
|
||||
public class LootFunction implements Function<Void> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(LootFunction.class);
|
||||
private final Registry<LootTable> registry;
|
||||
private final Returnable<String> data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<String> data;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
private final Platform platform;
|
||||
private final StructureScript script;
|
||||
|
||||
public LootFunction(Registry<LootTable> registry, Returnable<Number> x, Returnable<Number> y, Returnable<Number> z,
|
||||
Returnable<String> data, Platform platform, Position position, StructureScript script) {
|
||||
public LootFunction(Registry<LootTable> registry, Expression<Number> x, Expression<Number> y, Expression<Number> z,
|
||||
Expression<String> data, Platform platform, SourcePosition position, StructureScript script) {
|
||||
this.registry = registry;
|
||||
this.position = position;
|
||||
this.data = data;
|
||||
@@ -54,23 +54,23 @@ public class LootFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()),
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
String id = data.apply(implementationArguments, scope);
|
||||
String id = data.evaluate(implementationArguments, scope);
|
||||
|
||||
|
||||
registry.get(RegistryKey.parse(id))
|
||||
.ifPresentOrElse(table -> {
|
||||
Vector3 apply = Vector3.of(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, scope)
|
||||
y.evaluate(implementationArguments, scope)
|
||||
.intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable();
|
||||
|
||||
|
||||
try {
|
||||
BlockEntity data = arguments.getWorld().getBlockEntity(apply);
|
||||
if(!(data instanceof Container container)) {
|
||||
@@ -78,12 +78,12 @@ public class LootFunction implements Function<Void> {
|
||||
apply, data);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
LootPopulateEvent event = new LootPopulateEvent(container, table,
|
||||
arguments.getWorld().getPack(), script);
|
||||
platform.getEventManager().callEvent(event);
|
||||
if(event.isCancelled()) return;
|
||||
|
||||
|
||||
event.getTable().fillInventory(container.getInventory(),
|
||||
new Random(apply.hashCode()));
|
||||
data.update(false);
|
||||
@@ -97,7 +97,7 @@ public class LootFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+12
-11
@@ -9,14 +9,14 @@ package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.constants.ConstantExpression;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
@@ -26,11 +26,11 @@ import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
public class PullFunction implements Function<Void> {
|
||||
private final BlockState data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
|
||||
public PullFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data, Platform platform,
|
||||
Position position) {
|
||||
public PullFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, Expression<String> data, Platform platform,
|
||||
SourcePosition position) {
|
||||
this.position = position;
|
||||
if(!(data instanceof ConstantExpression)) throw new ParseException("Block data must be constant", data.getPosition());
|
||||
|
||||
@@ -41,12 +41,13 @@ public class PullFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
Vector3.Mutable mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, scope).intValue(),
|
||||
Vector3.Mutable mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.evaluate(implementationArguments, scope).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());
|
||||
while(mutable.getY() > arguments.getWorld().getMinHeight()) {
|
||||
if(!arguments.getWorld().getBlockState(mutable).isAir()) {
|
||||
@@ -59,7 +60,7 @@ public class PullFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -7,19 +7,19 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class RandomFunction implements Function<Integer> {
|
||||
private final Returnable<Number> numberReturnable;
|
||||
private final Position position;
|
||||
private final Expression<Number> numberReturnable;
|
||||
private final SourcePosition position;
|
||||
|
||||
public RandomFunction(Returnable<Number> numberReturnable, Position position) {
|
||||
public RandomFunction(Expression<Number> numberReturnable, SourcePosition position) {
|
||||
this.numberReturnable = numberReturnable;
|
||||
this.position = position;
|
||||
}
|
||||
@@ -31,13 +31,13 @@ public class RandomFunction implements Function<Integer> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Integer evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return ((TerraImplementationArguments) implementationArguments).getRandom().nextInt(
|
||||
numberReturnable.apply(implementationArguments, scope).intValue());
|
||||
numberReturnable.evaluate(implementationArguments, scope).intValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -7,17 +7,17 @@
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
|
||||
|
||||
public class RecursionsFunction implements Function<Number> {
|
||||
private final Position position;
|
||||
private final SourcePosition position;
|
||||
|
||||
public RecursionsFunction(Position position) {
|
||||
public RecursionsFunction(SourcePosition position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@@ -27,12 +27,12 @@ public class RecursionsFunction implements Function<Number> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Number evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
return ((TerraImplementationArguments) implementationArguments).getRecursions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+14
-12
@@ -9,23 +9,24 @@ package com.dfsek.terra.addons.terrascript.script.functions;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
|
||||
public class SetMarkFunction implements Function<Void> {
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Returnable<String> mark;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
private final Expression<String> mark;
|
||||
|
||||
public SetMarkFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> mark, Position position) {
|
||||
public SetMarkFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, Expression<String> mark,
|
||||
SourcePosition position) {
|
||||
this.position = position;
|
||||
this.mark = mark;
|
||||
this.x = x;
|
||||
@@ -34,22 +35,23 @@ public class SetMarkFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
|
||||
FastMath.floorToInt(
|
||||
y.apply(implementationArguments, scope).doubleValue()),
|
||||
y.evaluate(implementationArguments, scope).doubleValue()),
|
||||
FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable(),
|
||||
mark.apply(implementationArguments, scope));
|
||||
mark.evaluate(implementationArguments, scope));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+14
-13
@@ -11,12 +11,12 @@ import net.jafama.FastMath;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.block.entity.BlockEntity;
|
||||
import com.dfsek.terra.api.util.RotationUtil;
|
||||
import com.dfsek.terra.api.util.vector.Vector2;
|
||||
@@ -25,12 +25,12 @@ import com.dfsek.terra.api.util.vector.Vector3;
|
||||
|
||||
public class StateFunction implements Function<Void> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(StateFunction.class);
|
||||
private final Returnable<String> data;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<String> data;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
|
||||
public StateFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data,
|
||||
Position position) {
|
||||
public StateFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, Expression<String> data,
|
||||
SourcePosition position) {
|
||||
this.position = position;
|
||||
this.data = data;
|
||||
this.x = x;
|
||||
@@ -39,17 +39,18 @@ public class StateFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Void evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, scope).intValue(),
|
||||
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.evaluate(implementationArguments, scope).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable();
|
||||
try {
|
||||
BlockEntity state = arguments.getWorld().getBlockEntity(origin);
|
||||
state.applyState(data.apply(implementationArguments, scope));
|
||||
state.applyState(data.evaluate(implementationArguments, scope));
|
||||
state.update(false);
|
||||
} catch(Exception e) {
|
||||
LOGGER.warn("Could not apply BlockState at {}", origin, e);
|
||||
@@ -59,7 +60,7 @@ public class StateFunction implements Function<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
+17
-16
@@ -13,13 +13,13 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.Expression;
|
||||
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.Scope;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||
import com.dfsek.terra.addons.terrascript.script.StructureScript;
|
||||
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
@@ -31,14 +31,14 @@ import com.dfsek.terra.api.util.vector.Vector2;
|
||||
public class StructureFunction implements Function<Boolean> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(StructureFunction.class);
|
||||
private final Registry<Structure> registry;
|
||||
private final Returnable<String> id;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
private final Expression<String> id;
|
||||
private final Expression<Number> x, y, z;
|
||||
private final SourcePosition position;
|
||||
private final Platform platform;
|
||||
private final List<Returnable<String>> rotations;
|
||||
private final List<Expression<String>> rotations;
|
||||
|
||||
public StructureFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> id,
|
||||
List<Returnable<String>> rotations, Registry<Structure> registry, Position position, Platform platform) {
|
||||
public StructureFunction(Expression<Number> x, Expression<Number> y, Expression<Number> z, Expression<String> id,
|
||||
List<Expression<String>> rotations, Registry<Structure> registry, SourcePosition position, Platform platform) {
|
||||
this.registry = registry;
|
||||
this.id = id;
|
||||
this.position = position;
|
||||
@@ -55,20 +55,21 @@ public class StructureFunction implements Function<Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||
public Boolean evaluate(ImplementationArguments implementationArguments, Scope scope) {
|
||||
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
|
||||
|
||||
if(arguments.getRecursions() > platform.getTerraConfig().getMaxRecursion())
|
||||
throw new RuntimeException("Structure recursion too deep: " + arguments.getRecursions());
|
||||
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
|
||||
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
|
||||
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.evaluate(implementationArguments, scope).doubleValue(),
|
||||
z.evaluate(implementationArguments, scope).doubleValue()),
|
||||
arguments.getRotation());
|
||||
|
||||
|
||||
String app = id.apply(implementationArguments, scope);
|
||||
String app = id.evaluate(implementationArguments, scope);
|
||||
return registry.getByID(app).map(script -> {
|
||||
Rotation rotation1;
|
||||
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments, scope);
|
||||
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).evaluate(implementationArguments, scope);
|
||||
try {
|
||||
rotation1 = Rotation.valueOf(rotString);
|
||||
} catch(IllegalArgumentException e) {
|
||||
@@ -80,7 +81,7 @@ public class StructureFunction implements Function<Boolean> {
|
||||
return structureScript.generate(arguments.getOrigin(),
|
||||
arguments.getWorld()
|
||||
.buffer(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, scope).intValue(),
|
||||
y.evaluate(implementationArguments, scope).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())),
|
||||
arguments.getRandom(),
|
||||
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
|
||||
@@ -88,7 +89,7 @@ public class StructureFunction implements Function<Boolean> {
|
||||
return script.generate(arguments.getOrigin(),
|
||||
arguments.getWorld()
|
||||
.buffer(FastMath.roundToInt(xz.getX()),
|
||||
y.apply(implementationArguments, scope).intValue(),
|
||||
y.evaluate(implementationArguments, scope).intValue(),
|
||||
FastMath.roundToInt(xz.getZ())),
|
||||
arguments.getRandom(),
|
||||
arguments.getRotation().rotate(rotation1));
|
||||
@@ -99,7 +100,7 @@ public class StructureFunction implements Function<Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
public SourcePosition getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
-130
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Stream-like data structure that allows viewing future elements without consuming current.
|
||||
*/
|
||||
public class Lookahead {
|
||||
private final List<Char> buffer = new ArrayList<>();
|
||||
private final Reader input;
|
||||
private int index = 0;
|
||||
private int line = 0;
|
||||
private boolean end = false;
|
||||
|
||||
public Lookahead(Reader r) {
|
||||
this.input = r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current character without consuming it.
|
||||
*
|
||||
* @return current character
|
||||
*/
|
||||
public Char current() {
|
||||
return next(0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Consume and return one character.
|
||||
*
|
||||
* @return Character that was consumed.
|
||||
*/
|
||||
public Char consume() {
|
||||
Char consumed = current();
|
||||
consume(1);
|
||||
return consumed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a future character without consuming it.
|
||||
*
|
||||
* @param ahead Distance ahead to peek
|
||||
*
|
||||
* @return Character
|
||||
*/
|
||||
public Char next(int ahead) {
|
||||
if(ahead < 0) throw new IllegalArgumentException();
|
||||
|
||||
while(buffer.size() <= ahead && !end) {
|
||||
Char item = fetch();
|
||||
if(item != null) {
|
||||
buffer.add(item);
|
||||
} else end = true;
|
||||
}
|
||||
|
||||
if(ahead >= buffer.size()) {
|
||||
return null;
|
||||
} else return buffer.get(ahead);
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume an amount of characters
|
||||
*
|
||||
* @param amount Number of characters to consume
|
||||
*/
|
||||
public void consume(int amount) {
|
||||
if(amount < 0) throw new IllegalArgumentException();
|
||||
while(amount-- > 0) {
|
||||
if(!buffer.isEmpty()) buffer.remove(0); // Remove top item from buffer.
|
||||
else {
|
||||
if(end) return;
|
||||
Char item = fetch();
|
||||
if(item == null) end = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean matches(String check, boolean consume) {
|
||||
if(check == null) return false;
|
||||
|
||||
for(int i = 0; i < check.length(); i++) {
|
||||
if(!next(i).is(check.charAt(i))) return false;
|
||||
}
|
||||
|
||||
if(consume) consume(check.length()); // Consume string
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the next character.
|
||||
*
|
||||
* @return Next character
|
||||
*/
|
||||
private Char fetch() {
|
||||
try {
|
||||
int c = input.read();
|
||||
if(c == -1) return null;
|
||||
if(c == '\n') {
|
||||
line++;
|
||||
index = 0;
|
||||
}
|
||||
index++;
|
||||
return new Char((char) c, line, index);
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer;
|
||||
|
||||
public class Position {
|
||||
private final int line;
|
||||
private final int index;
|
||||
|
||||
public Position(int line, int index) {
|
||||
this.line = line;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return (line + 1) + ":" + index;
|
||||
}
|
||||
}
|
||||
-247
@@ -1,247 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.terrascript.tokenizer;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.exceptions.EOFException;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.exceptions.FormatException;
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.exceptions.TokenizerException;
|
||||
|
||||
|
||||
public class Tokenizer {
|
||||
public static final Set<Character> syntaxSignificant = Sets.newHashSet(';', '(', ')', '"', ',', '\\', '=', '{', '}', '+', '-', '*', '/',
|
||||
'>', '<', '!'); // Reserved chars
|
||||
private final Lookahead reader;
|
||||
private final Stack<Token> brackets = new Stack<>();
|
||||
private Token current;
|
||||
private Token last;
|
||||
|
||||
public Tokenizer(String data) {
|
||||
reader = new Lookahead(new StringReader(data + '\0'));
|
||||
current = fetchCheck();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first token.
|
||||
*
|
||||
* @return First token
|
||||
*
|
||||
* @throws ParseException If token does not exist
|
||||
*/
|
||||
public Token get() {
|
||||
if(!hasNext()) throw new ParseException("Unexpected end of input", last.getPosition());
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume (get and remove) the first token.
|
||||
*
|
||||
* @return First token
|
||||
*
|
||||
* @throws ParseException If token does not exist
|
||||
*/
|
||||
public Token consume() {
|
||||
if(!hasNext()) throw new ParseException("Unexpected end of input", last.getPosition());
|
||||
Token temp = current;
|
||||
current = fetchCheck();
|
||||
return temp;
|
||||
}
|
||||
|
||||
private Token fetchCheck() {
|
||||
Token fetch = fetch();
|
||||
if(fetch != null) {
|
||||
last = fetch;
|
||||
if(fetch.getType() == Token.Type.BLOCK_BEGIN) brackets.push(fetch); // Opening bracket
|
||||
else if(fetch.getType() == Token.Type.BLOCK_END) {
|
||||
if(!brackets.isEmpty()) brackets.pop();
|
||||
else throw new ParseException("Dangling opening brace", new Position(0, 0));
|
||||
}
|
||||
} else if(!brackets.isEmpty()) {
|
||||
throw new ParseException("Dangling closing brace", brackets.peek().getPosition());
|
||||
}
|
||||
return fetch;
|
||||
}
|
||||
|
||||
private Token fetch() throws TokenizerException {
|
||||
while(!reader.current().isEOF() && reader.current().isWhitespace()) reader.consume();
|
||||
|
||||
while(reader.matches("//", true)) skipLine(); // Skip line if comment
|
||||
|
||||
if(reader.matches("/*", true)) skipTo("*/"); // Skip multi line comment
|
||||
|
||||
if(reader.current().isEOF()) return null; // EOF
|
||||
|
||||
if(reader.matches("==", true))
|
||||
return new Token("==", Token.Type.EQUALS_OPERATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.matches("!=", true))
|
||||
return new Token("!=", Token.Type.NOT_EQUALS_OPERATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.matches(">=", true))
|
||||
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.GREATER_THAN_OPERATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.matches("<", true))
|
||||
return new Token("<", Token.Type.LESS_THAN_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(isNumberStart()) {
|
||||
StringBuilder num = new StringBuilder();
|
||||
while(!reader.current().isEOF() && isNumberLike()) {
|
||||
num.append(reader.consume());
|
||||
}
|
||||
return new Token(num.toString(), Token.Type.NUMBER, new Position(reader.getLine(), reader.getIndex()));
|
||||
}
|
||||
|
||||
if(reader.current().is('"')) {
|
||||
reader.consume(); // Consume first quote
|
||||
StringBuilder string = new StringBuilder();
|
||||
boolean ignoreNext = false;
|
||||
while((!reader.current().is('"')) || ignoreNext) {
|
||||
if(reader.current().is('\\') && !ignoreNext) {
|
||||
ignoreNext = true;
|
||||
reader.consume();
|
||||
continue;
|
||||
} else ignoreNext = false;
|
||||
if(reader.current().isEOF())
|
||||
throw new FormatException("No end of string literal found. ", new Position(reader.getLine(), reader.getIndex()));
|
||||
string.append(reader.consume());
|
||||
}
|
||||
reader.consume(); // Consume last quote
|
||||
|
||||
return new Token(string.toString(), Token.Type.STRING, new Position(reader.getLine(), reader.getIndex()));
|
||||
}
|
||||
|
||||
if(reader.current().is('('))
|
||||
return new Token(reader.consume().toString(), Token.Type.GROUP_BEGIN, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is(')'))
|
||||
return new Token(reader.consume().toString(), Token.Type.GROUP_END, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is(';'))
|
||||
return new Token(reader.consume().toString(), Token.Type.STATEMENT_END, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is(','))
|
||||
return new Token(reader.consume().toString(), Token.Type.SEPARATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('{'))
|
||||
return new Token(reader.consume().toString(), Token.Type.BLOCK_BEGIN, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('}'))
|
||||
return new Token(reader.consume().toString(), Token.Type.BLOCK_END, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('='))
|
||||
return new Token(reader.consume().toString(), Token.Type.ASSIGNMENT, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('+'))
|
||||
return new Token(reader.consume().toString(), Token.Type.ADDITION_OPERATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('-'))
|
||||
return new Token(reader.consume().toString(), Token.Type.SUBTRACTION_OPERATOR,
|
||||
new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('*'))
|
||||
return new Token(reader.consume().toString(), Token.Type.MULTIPLICATION_OPERATOR,
|
||||
new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('/'))
|
||||
return new Token(reader.consume().toString(), Token.Type.DIVISION_OPERATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('%'))
|
||||
return new Token(reader.consume().toString(), Token.Type.MODULO_OPERATOR, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(reader.current().is('!'))
|
||||
return new Token(reader.consume().toString(), Token.Type.BOOLEAN_NOT, new Position(reader.getLine(), reader.getIndex()));
|
||||
|
||||
StringBuilder token = new StringBuilder();
|
||||
while(!reader.current().isEOF() && !isSyntaxSignificant(reader.current().getCharacter())) {
|
||||
Char c = reader.consume();
|
||||
if(c.isWhitespace()) break;
|
||||
token.append(c);
|
||||
}
|
||||
|
||||
String tokenString = token.toString();
|
||||
|
||||
if(tokenString.equals("true"))
|
||||
return new Token(tokenString, Token.Type.BOOLEAN, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("false"))
|
||||
return new Token(tokenString, Token.Type.BOOLEAN, new Position(reader.getLine(), reader.getIndex()));
|
||||
|
||||
if(tokenString.equals("num"))
|
||||
return new Token(tokenString, Token.Type.NUMBER_VARIABLE, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("str"))
|
||||
return new Token(tokenString, Token.Type.STRING_VARIABLE, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("bool"))
|
||||
return new Token(tokenString, Token.Type.BOOLEAN_VARIABLE, new Position(reader.getLine(), reader.getIndex()));
|
||||
|
||||
if(tokenString.equals("if"))
|
||||
return new Token(tokenString, Token.Type.IF_STATEMENT, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("else"))
|
||||
return new Token(tokenString, Token.Type.ELSE, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("while"))
|
||||
return new Token(tokenString, Token.Type.WHILE_LOOP, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("for"))
|
||||
return new Token(tokenString, Token.Type.FOR_LOOP, new Position(reader.getLine(), reader.getIndex()));
|
||||
|
||||
if(tokenString.equals("return"))
|
||||
return new Token(tokenString, Token.Type.RETURN, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("continue"))
|
||||
return new Token(tokenString, Token.Type.CONTINUE, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("break"))
|
||||
return new Token(tokenString, Token.Type.BREAK, new Position(reader.getLine(), reader.getIndex()));
|
||||
if(tokenString.equals("fail"))
|
||||
return new Token(tokenString, Token.Type.FAIL, new Position(reader.getLine(), reader.getIndex()));
|
||||
|
||||
return new Token(tokenString, Token.Type.IDENTIFIER, new Position(reader.getLine(), reader.getIndex()));
|
||||
}
|
||||
|
||||
private void skipLine() {
|
||||
while(!reader.current().isEOF() && !reader.current().isNewLine()) reader.consume();
|
||||
consumeWhitespace();
|
||||
}
|
||||
|
||||
private void consumeWhitespace() {
|
||||
while(!reader.current().isEOF() && reader.current().isWhitespace()) reader.consume(); // Consume whitespace.
|
||||
}
|
||||
|
||||
private void skipTo(String s) throws EOFException {
|
||||
Position begin = new Position(reader.getLine(), reader.getIndex());
|
||||
while(!reader.current().isEOF()) {
|
||||
if(reader.matches(s, true)) {
|
||||
consumeWhitespace();
|
||||
return;
|
||||
}
|
||||
reader.consume();
|
||||
}
|
||||
throw new EOFException("No end of expression found.", begin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this {@code Tokenizer} contains additional tokens.
|
||||
*
|
||||
* @return {@code true} if more tokens are present, otherwise {@code false}
|
||||
*/
|
||||
public boolean hasNext() {
|
||||
return !(current == null);
|
||||
}
|
||||
|
||||
private boolean isNumberLike() {
|
||||
return reader.current().isDigit()
|
||||
|| reader.current().is('_', '.', 'E');
|
||||
}
|
||||
|
||||
private boolean isNumberStart() {
|
||||
return reader.current().isDigit()
|
||||
|| reader.current().is('.') && reader.next(1).isDigit();
|
||||
}
|
||||
|
||||
public boolean isSyntaxSignificant(char c) {
|
||||
return syntaxSignificant.contains(c);
|
||||
}
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package structure;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.LookaheadStream;
|
||||
import com.dfsek.terra.addons.terrascript.lexer.Char;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.lexer.SourcePosition;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class LookaheadStreamTest {
|
||||
@Test
|
||||
public void lookahead() {
|
||||
String testString = "Test string...\nNew line";
|
||||
|
||||
LookaheadStream lookahead = new LookaheadStream(testString);
|
||||
|
||||
Char first = new Char('T', new SourcePosition(1, 1));
|
||||
Char second = new Char('e', new SourcePosition(1, 2));
|
||||
Char third = new Char('s', new SourcePosition(1, 3));
|
||||
Char space = new Char(' ', new SourcePosition(1, 5));
|
||||
Char newline = new Char('\n', new SourcePosition(1, 15));
|
||||
Char lineTwoColOne = new Char('N', new SourcePosition(2, 1));
|
||||
String lineTwo = "New line";
|
||||
|
||||
assertTrue(lookahead.matchesString("Test", false));
|
||||
assertTrue(lookahead.matchesString(testString, false));
|
||||
assertFalse(lookahead.matchesString(testString + "asdf", false));
|
||||
assertFalse(lookahead.matchesString("Foo", false));
|
||||
|
||||
assertEquals(first, lookahead.current());
|
||||
assertEquals(first, lookahead.current());
|
||||
|
||||
assertEquals(new SourcePosition(1, 1), lookahead.getPosition());
|
||||
assertEquals(new SourcePosition(1, 1), lookahead.getPosition());
|
||||
|
||||
assertEquals(second, lookahead.peek());
|
||||
assertEquals(second, lookahead.peek());
|
||||
|
||||
assertEquals(first, lookahead.consume());
|
||||
|
||||
assertFalse(lookahead.matchesString(testString, false));
|
||||
|
||||
assertEquals(second, lookahead.current());
|
||||
|
||||
assertEquals(second, lookahead.consume());
|
||||
|
||||
assertEquals(third, lookahead.current());
|
||||
|
||||
assertTrue(lookahead.matchesString("st", true));
|
||||
|
||||
assertEquals(space, lookahead.current());
|
||||
|
||||
assertEquals(space, lookahead.consume());
|
||||
|
||||
assertTrue(lookahead.matchesString("string...", false));
|
||||
assertTrue(lookahead.matchesString("string...", true));
|
||||
assertFalse(lookahead.matchesString("string...", false));
|
||||
|
||||
assertEquals(newline, lookahead.current());
|
||||
assertEquals(newline, lookahead.consume());
|
||||
|
||||
assertEquals(lineTwoColOne, lookahead.current());
|
||||
|
||||
assertTrue(lookahead.matchesString(lineTwo, false));
|
||||
assertFalse(lookahead.matchesString(lineTwo + "asdf", false));
|
||||
assertTrue(lookahead.matchesString(lineTwo, true));
|
||||
|
||||
assertEquals(new SourcePosition(2, 8), lookahead.getPosition());
|
||||
|
||||
assertDoesNotThrow(lookahead::consume);
|
||||
assertEquals(new SourcePosition(2, 8), lookahead.getPosition());
|
||||
|
||||
assertDoesNotThrow(lookahead::consume);
|
||||
assertEquals(new SourcePosition(2, 8), lookahead.getPosition());
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package structure;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import com.dfsek.terra.addons.terrascript.tokenizer.Lookahead;
|
||||
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user