drastically optimise terrascript scoping

This commit is contained in:
dfsek 2022-02-16 15:55:46 -07:00
parent aa39dc4b81
commit a02847e3ed
39 changed files with 226 additions and 289 deletions

View File

@ -48,9 +48,9 @@ import com.dfsek.terra.addons.terrascript.parser.lang.operations.statements.Grea
import com.dfsek.terra.addons.terrascript.parser.lang.operations.statements.LessThanOrEqualsStatement;
import com.dfsek.terra.addons.terrascript.parser.lang.operations.statements.LessThanStatement;
import com.dfsek.terra.addons.terrascript.parser.lang.operations.statements.NotEqualsStatement;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Assignment;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Declaration;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Getter;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.VariableAssignmentNode;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.VariableDeclarationNode;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.VariableReferenceNode;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.addons.terrascript.tokenizer.Token;
import com.dfsek.terra.addons.terrascript.tokenizer.Tokenizer;
@ -160,7 +160,7 @@ public class Parser {
ParserUtil.checkType(f, Token.Type.NUMBER_VARIABLE, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.IDENTIFIER);
Item<?> initializer;
if(f.isVariableDeclaration()) {
Declaration<?> forVar = parseVariableDeclaration(tokens, variableMap);
VariableDeclarationNode<?> forVar = parseVariableDeclaration(tokens, variableMap);
Token name = tokens.get();
if(functions.containsKey(name.getContent()) || variableMap.containsKey(name.getContent()))
throw new ParseException(name.getContent() + " is already defined in this scope", name.getPosition());
@ -208,7 +208,7 @@ public class Parser {
expression = parseFunction(tokens, false, variableMap);
else if(variableMap.containsKey(id.getContent())) {
ParserUtil.checkType(tokens.consume(), Token.Type.IDENTIFIER);
expression = new Getter(id.getContent(), id.getPosition(), variableMap.get(id.getContent()));
expression = new VariableReferenceNode(id.getContent(), id.getPosition(), variableMap.get(id.getContent()));
} else throw new ParseException("Unexpected token \" " + id.getContent() + "\"", id.getPosition());
}
@ -306,7 +306,7 @@ public class Parser {
}
}
private Declaration<?> parseVariableDeclaration(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) {
private VariableDeclarationNode<?> parseVariableDeclaration(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) {
Token type = tokens.consume();
ParserUtil.checkType(type, Token.Type.STRING_VARIABLE, Token.Type.BOOLEAN_VARIABLE, Token.Type.NUMBER_VARIABLE);
@ -324,7 +324,7 @@ public class Parser {
variableMap.put(identifier.getContent(), returnType);
return new Declaration<>(tokens.get().getPosition(), identifier.getContent(), value, returnType);
return new VariableDeclarationNode<>(tokens.get().getPosition(), identifier.getContent(), value, returnType);
}
private Block parseBlock(Tokenizer tokens, Map<String, Returnable.ReturnType> superVars, boolean loop) {
@ -373,7 +373,7 @@ public class Parser {
else throw new UnsupportedOperationException("Unexpected token " + token.getType() + ": " + token.getPosition());
}
private Assignment<?> parseAssignment(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) {
private VariableAssignmentNode<?> parseAssignment(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) {
Token identifier = tokens.consume();
ParserUtil.checkType(identifier, Token.Type.IDENTIFIER);
@ -384,7 +384,7 @@ public class Parser {
ParserUtil.checkReturnType(value, variableMap.get(identifier.getContent()));
return new Assignment<>(value, identifier.getContent(), identifier.getPosition());
return new VariableAssignmentNode<>(value, identifier.getContent(), identifier.getPosition());
}
private Function<?> parseFunction(Tokenizer tokens, boolean fullStatement, Map<String, Returnable.ReturnType> variableMap) {

View File

@ -7,11 +7,8 @@
package com.dfsek.terra.addons.terrascript.parser.lang;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -25,12 +22,22 @@ public class Block implements Item<Block.ReturnInfo<?>> {
}
public ReturnInfo<?> apply(ImplementationArguments implementationArguments) {
return apply(implementationArguments, new HashMap<>());
return apply(implementationArguments, new Scope());
}
@Override
public ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
Map<String, Variable<?>> scope = new HashMap<>(variableMap);
public ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
Scope sub = scope.sub();
for(Item<?> item : items) {
Object result = item.apply(implementationArguments, sub);
if(result instanceof ReturnInfo<?> level) {
if(!level.getLevel().equals(ReturnLevel.NONE)) return level;
}
}
return new ReturnInfo<>(ReturnLevel.NONE, null);
}
public ReturnInfo<?> applyNoNewScope(ImplementationArguments implementationArguments, Scope scope) {
for(Item<?> item : items) {
Object result = item.apply(implementationArguments, scope);
if(result instanceof ReturnInfo<?> level) {

View File

@ -7,14 +7,11 @@
package com.dfsek.terra.addons.terrascript.parser.lang;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
public interface Item<T> {
T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap);
T apply(ImplementationArguments implementationArguments, Scope scope);
Position getPosition();
}

View File

@ -0,0 +1,46 @@
package com.dfsek.terra.addons.terrascript.parser.lang;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import java.util.HashMap;
import java.util.Map;
public class Scope {
private static final Scope NULL = new Scope() {
@Override
public Variable<?> get(String id) {
throw new IllegalStateException("Cannot get variable from null scope: " + id);
}
@Override
public void put(String id, Variable<?> variable) {
throw new IllegalStateException("Cannot set variable in null scope: " + id);
}
};
private final Scope parent;
private final Map<String, Variable<?>> variableMap = new HashMap<>();
public Scope(Scope parent) {
this.parent = parent;
}
public Scope() {
this.parent = NULL;
}
public Variable<?> get(String id) {
Variable<?> var = variableMap.get(id);
return var == null ? parent.get(id) : var;
}
public void put(String id, Variable<?> variable) {
variableMap.put(id, variable);
}
public Scope sub() {
return new Scope(this);
}
}

View File

@ -7,11 +7,9 @@
package com.dfsek.terra.addons.terrascript.parser.lang.constants;
import java.util.Map;
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.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -25,7 +23,7 @@ public abstract class ConstantExpression<T> implements Returnable<T> {
}
@Override
public T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public T apply(ImplementationArguments implementationArguments, Scope scope) {
return constant;
}

View File

@ -7,11 +7,9 @@
package com.dfsek.terra.addons.terrascript.parser.lang.functions;
import java.util.Map;
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.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -23,7 +21,7 @@ public interface Function<T> extends Returnable<T> {
}
@Override
public Object apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Object apply(ImplementationArguments implementationArguments, Scope scope) {
return null;
}

View File

@ -1,54 +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.functions.def;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Item;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
public class FunctionBlock<T> implements Item<T> {
private final List<Item<?>> items;
private final Position position;
private final T defaultVal;
public FunctionBlock(List<Item<?>> items, T defaultVal, Position position) {
this.items = items;
this.position = position;
this.defaultVal = defaultVal;
}
@SuppressWarnings("unchecked")
@Override
public synchronized T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
Map<String, Variable<?>> scope = new HashMap<>(variableMap);
for(Item<?> item : items) {
Object result = item.apply(implementationArguments, variableMap);
if(result instanceof Block.ReturnInfo) {
Block.ReturnInfo<T> level = (Block.ReturnInfo<T>) result;
if(level.getLevel().equals(Block.ReturnLevel.RETURN)) return level.getData();
}
}
return defaultVal;
}
@Override
public Position getPosition() {
return position;
}
public List<Item<?>> getItems() {
return items;
}
}

View File

@ -7,12 +7,10 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -24,7 +22,7 @@ public class BreakKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
return new Block.ReturnInfo<>(Block.ReturnLevel.BREAK, null);
}

View File

@ -7,12 +7,10 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -24,7 +22,7 @@ public class ContinueKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
return new Block.ReturnInfo<>(Block.ReturnLevel.CONTINUE, null);
}

View File

@ -7,12 +7,10 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -24,7 +22,7 @@ public class FailKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
return new Block.ReturnInfo<>(Block.ReturnLevel.FAIL, null);
}

View File

@ -7,12 +7,10 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.flow;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -24,7 +22,7 @@ public class ReturnKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
return new Block.ReturnInfo<>(Block.ReturnLevel.RETURN, null);
}

View File

@ -7,14 +7,12 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.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.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -34,11 +32,12 @@ public class ForKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
for(initializer.apply(implementationArguments, variableMap);
statement.apply(implementationArguments, variableMap);
incrementer.apply(implementationArguments, variableMap)) {
Block.ReturnInfo<?> level = conditional.apply(implementationArguments, variableMap);
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
Scope sub = scope.sub();
for(initializer.apply(implementationArguments, sub);
statement.apply(implementationArguments, sub);
incrementer.apply(implementationArguments, sub)) {
Block.ReturnInfo<?> level = conditional.applyNoNewScope(implementationArguments, sub);
if(level.getLevel().equals(Block.ReturnLevel.BREAK)) break;
if(level.getLevel().isReturnFast()) return level;
}

View File

@ -7,16 +7,11 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike;
import com.dfsek.terra.addons.terrascript.parser.lang.*;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -37,15 +32,15 @@ public class IfKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
if(statement.apply(implementationArguments, variableMap)) return conditional.apply(implementationArguments, variableMap);
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Scope scope) {
if(statement.apply(implementationArguments, scope)) return conditional.apply(implementationArguments, scope);
else {
for(Pair<Returnable<Boolean>, Block> pair : elseIf) {
if(pair.getLeft().apply(implementationArguments, variableMap)) {
return pair.getRight().apply(implementationArguments, variableMap);
if(pair.getLeft().apply(implementationArguments, scope)) {
return pair.getRight().apply(implementationArguments, scope);
}
}
if(elseBlock != null) return elseBlock.apply(implementationArguments, variableMap);
if(elseBlock != null) return elseBlock.apply(implementationArguments, scope);
}
return new Block.ReturnInfo<>(Block.ReturnLevel.NONE, null);
}

View File

@ -7,13 +7,7 @@
package com.dfsek.terra.addons.terrascript.parser.lang.keywords.looplike;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Keyword;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.*;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -29,9 +23,9 @@ public class WhileKeyword implements Keyword<Block.ReturnInfo<?>> {
}
@Override
public Block.ReturnInfo<?> apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
while(statement.apply(implementationArguments, variableMap)) {
Block.ReturnInfo<?> level = conditional.apply(implementationArguments, variableMap);
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;
}

View File

@ -7,11 +7,9 @@
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
import java.util.Map;
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.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -29,8 +27,8 @@ public abstract class BinaryOperation<I, O> implements Returnable<O> {
public abstract O apply(I left, I right);
@Override
public O apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
return apply(left.apply(implementationArguments, variableMap), right.apply(implementationArguments, variableMap));
public O apply(ImplementationArguments implementationArguments, Scope scope) {
return apply(left.apply(implementationArguments, scope), right.apply(implementationArguments, scope));
}
@Override

View File

@ -7,11 +7,9 @@
package com.dfsek.terra.addons.terrascript.parser.lang.operations;
import java.util.Map;
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.variables.Variable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -27,8 +25,8 @@ public abstract class UnaryOperation<T> implements Returnable<T> {
public abstract T apply(T input);
@Override
public T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
return apply(input.apply(implementationArguments, variableMap));
public T apply(ImplementationArguments implementationArguments, Scope scope) {
return apply(input.apply(implementationArguments, scope));
}
@Override

View File

@ -7,20 +7,19 @@
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
import java.util.Map;
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.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
public class Assignment<T> implements Item<T> {
public class VariableAssignmentNode<T> implements Item<T> {
private final Returnable<T> value;
private final Position position;
private final String identifier;
public Assignment(Returnable<T> value, String identifier, Position position) {
public VariableAssignmentNode(Returnable<T> value, String identifier, Position position) {
this.value = value;
this.identifier = identifier;
this.position = position;
@ -28,9 +27,9 @@ public class Assignment<T> implements Item<T> {
@SuppressWarnings("unchecked")
@Override
public synchronized T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
T val = value.apply(implementationArguments, variableMap);
((Variable<T>) variableMap.get(identifier)).setValue(val);
public synchronized T apply(ImplementationArguments implementationArguments, Scope scope) {
T val = value.apply(implementationArguments, scope);
((Variable<T>) scope.get(identifier)).setValue(val);
return val;
}

View File

@ -7,21 +7,20 @@
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
import java.util.Map;
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.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
public class Declaration<T> implements Item<T> {
public class VariableDeclarationNode<T> implements Item<T> {
private final Position position;
private final String identifier;
private final Returnable<T> value;
private final Returnable.ReturnType type;
public Declaration(Position position, String identifier, Returnable<T> value, Returnable.ReturnType type) {
public VariableDeclarationNode(Position position, String identifier, Returnable<T> value, Returnable.ReturnType type) {
switch(type) {
case STRING:
case BOOLEAN:
@ -37,13 +36,14 @@ public class Declaration<T> implements Item<T> {
}
@Override
public T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
T result = value.apply(implementationArguments, variableMap);
switch(type) {
case NUMBER -> variableMap.put(identifier, new NumberVariable((Number) result, position));
case BOOLEAN -> variableMap.put(identifier, new BooleanVariable((Boolean) result, position));
case STRING -> variableMap.put(identifier, new StringVariable((String) result, position));
}
public T apply(ImplementationArguments implementationArguments, Scope scope) {
T result = value.apply(implementationArguments, scope);
scope.put(identifier, switch(type) {
case NUMBER -> new NumberVariable((Number) result, position);
case BOOLEAN -> new BooleanVariable((Boolean) result, position);
case STRING -> new StringVariable((String) result, position);
default -> throw new IllegalStateException("Unexpected value: " + type);
});
return result;
}

View File

@ -7,19 +7,18 @@
package com.dfsek.terra.addons.terrascript.parser.lang.variables;
import java.util.Map;
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 Getter implements Returnable<Object> {
public class VariableReferenceNode implements Returnable<Object> {
private final String identifier;
private final Position position;
private final ReturnType type;
public Getter(String identifier, Position position, ReturnType type) {
public VariableReferenceNode(String identifier, Position position, ReturnType type) {
this.identifier = identifier;
this.position = position;
this.type = type;
@ -31,8 +30,8 @@ public class Getter implements Returnable<Object> {
}
@Override
public synchronized Object apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
return variableMap.get(identifier).getValue();
public synchronized Object apply(ImplementationArguments implementationArguments, Scope scope) {
return scope.get(identifier).getValue();
}
@Override

View File

@ -8,14 +8,13 @@
package com.dfsek.terra.addons.terrascript.script.builders;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -37,9 +36,9 @@ public class BinaryNumberFunctionBuilder implements FunctionBuilder<Function<Num
@SuppressWarnings("unchecked")
@Override
public Number apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments, variableMap),
((Returnable<Number>) argumentList.get(1)).apply(implementationArguments, variableMap));
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));
}
@Override

View File

@ -8,14 +8,13 @@
package com.dfsek.terra.addons.terrascript.script.builders;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -38,8 +37,8 @@ public class UnaryBooleanFunctionBuilder implements FunctionBuilder<Function<Voi
@SuppressWarnings("unchecked")
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
function.accept(((Returnable<Boolean>) argumentList.get(0)).apply(implementationArguments, variableMap),
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
function.accept(((Returnable<Boolean>) argumentList.get(0)).apply(implementationArguments, scope),
(TerraImplementationArguments) implementationArguments);
return null;
}

View File

@ -8,13 +8,12 @@
package com.dfsek.terra.addons.terrascript.script.builders;
import java.util.List;
import java.util.Map;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -36,8 +35,8 @@ public class UnaryNumberFunctionBuilder implements FunctionBuilder<Function<Numb
@SuppressWarnings("unchecked")
@Override
public Number apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments, variableMap));
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
return function.apply(((Returnable<Number>) argumentList.get(0)).apply(implementationArguments, scope));
}
@Override

View File

@ -8,13 +8,12 @@
package com.dfsek.terra.addons.terrascript.script.builders;
import java.util.List;
import java.util.Map;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -36,8 +35,8 @@ public class UnaryStringFunctionBuilder implements FunctionBuilder<Function<Void
@SuppressWarnings("unchecked")
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
function.accept(((Returnable<String>) argumentList.get(0)).apply(implementationArguments, variableMap));
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
function.accept(((Returnable<String>) argumentList.get(0)).apply(implementationArguments, scope));
return null;
}

View File

@ -8,13 +8,12 @@
package com.dfsek.terra.addons.terrascript.script.builders;
import java.util.List;
import java.util.Map;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -37,7 +36,7 @@ public class ZeroArgFunctionBuilder<T> implements FunctionBuilder<Function<T>> {
}
@Override
public T apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public T apply(ImplementationArguments implementationArguments, Scope scope) {
return function.apply((TerraImplementationArguments) implementationArguments);
}

View File

@ -7,17 +7,14 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
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.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
@ -38,11 +35,11 @@ public class BiomeFunction implements Function<String> {
@Override
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public String apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()),
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()),
arguments.getRotation());
@ -52,7 +49,7 @@ public class BiomeFunction implements Function<String> {
.toVector3()
.mutable()
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
y.apply(implementationArguments, scope).intValue(),
FastMath.roundToInt(xz.getZ()))).immutable(), arguments.getWorld().getSeed()).getID();
}

View File

@ -7,6 +7,7 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.api.block.state.properties.base.Properties;
import net.jafama.FastMath;
@ -52,10 +53,10 @@ public class BlockFunction implements Function<Void> {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
BlockState rot = getBlockState(implementationArguments, variableMap);
setBlock(implementationArguments, variableMap, arguments, rot);
BlockState rot = getBlockState(implementationArguments, scope);
setBlock(implementationArguments, scope, arguments, rot);
return null;
}
@ -69,19 +70,19 @@ public class BlockFunction implements Function<Void> {
return ReturnType.VOID;
}
void setBlock(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap,
void setBlock(ImplementationArguments implementationArguments, Scope scope,
TerraImplementationArguments arguments, BlockState rot) {
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
rot = RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
try {
Vector3.Mutable set = Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).doubleValue(),
y.apply(implementationArguments, scope).doubleValue(),
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());
BlockState current = arguments.getWorld().getBlockState(set);
if(overwrite.apply(implementationArguments, variableMap) || current.isAir()) {
if(overwrite.apply(implementationArguments, scope) || current.isAir()) {
if(arguments.isWaterlog() && current.has(Properties.WATERLOGGED) && current.getBlockType().isWater()) {
current.set(Properties.WATERLOGGED, true);
}
@ -92,8 +93,8 @@ public class BlockFunction implements Function<Void> {
}
}
protected BlockState getBlockState(ImplementationArguments arguments, Map<String, Variable<?>> variableMap) {
return data.computeIfAbsent(blockData.apply(arguments, variableMap), platform.getWorldHandle()::createBlockState);
protected BlockState getBlockState(ImplementationArguments arguments, Scope scope) {
return data.computeIfAbsent(blockData.apply(arguments, scope), platform.getWorldHandle()::createBlockState);
}
@ -107,7 +108,7 @@ public class BlockFunction implements Function<Void> {
}
@Override
protected BlockState getBlockState(ImplementationArguments arguments, Map<String, Variable<?>> variableMap) {
protected BlockState getBlockState(ImplementationArguments arguments, Scope scope) {
return state;
}
}

View File

@ -7,14 +7,12 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
@ -35,11 +33,11 @@ public class CheckBlockFunction implements Function<String> {
@Override
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public String apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()),
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()),
arguments.getRotation());
@ -48,7 +46,7 @@ public class CheckBlockFunction implements Function<String> {
.toVector3()
.mutable()
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap)
y.apply(implementationArguments, scope)
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
.getAsString();
if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties

View File

@ -7,14 +7,12 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.Platform;
@ -45,12 +43,12 @@ public class EntityFunction implements Function<Void> {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).mutable().add(arguments.getOrigin()).add(0.5, 0, 0.5).immutable(), data);
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, scope).doubleValue(), xz.getZ()).mutable().add(arguments.getOrigin()).add(0.5, 0, 0.5).immutable(), data);
platform.getEventManager().callEvent(new EntitySpawnEvent(entity.world().getPack(), entity));
return null;
}

View File

@ -7,14 +7,12 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
@ -34,13 +32,13 @@ public class GetMarkFunction implements Function<String> {
}
@Override
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public String apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable());
y.apply(implementationArguments, scope).doubleValue()), FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable());
return mark == null ? "" : mark;
}

View File

@ -7,6 +7,7 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.entity.Container;
import com.dfsek.terra.api.event.events.world.generation.LootPopulateEvent;
@ -18,13 +19,11 @@ import net.jafama.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Random;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.StructureScript;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -57,20 +56,20 @@ public class LootFunction implements Function<Void> {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()),
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()),
arguments.getRotation());
String id = data.apply(implementationArguments, variableMap);
String id = data.apply(implementationArguments, scope);
registry.get(RegistryKey.parse(id))
.ifPresentOrElse(table -> {
Vector3 apply = Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap)
y.apply(implementationArguments, scope)
.intValue(),
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable();

View File

@ -7,16 +7,14 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
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.constants.ConstantExpression;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.Platform;
@ -43,14 +41,14 @@ public class PullFunction implements Function<Void> {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
BlockState rot = RotationUtil.rotateBlockData(data, arguments.getRotation().inverse());
Vector3.Mutable mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
Vector3.Mutable mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, scope).intValue(),
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());
while(mutable.getY() > arguments.getWorld().getMinHeight()) {
if(!arguments.getWorld().getBlockState(mutable).isAir()) {

View File

@ -7,12 +7,10 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import java.util.Map;
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.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -33,9 +31,9 @@ public class RandomFunction implements Function<Integer> {
}
@Override
public Integer apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Integer apply(ImplementationArguments implementationArguments, Scope scope) {
return ((TerraImplementationArguments) implementationArguments).getRandom().nextInt(
numberReturnable.apply(implementationArguments, variableMap).intValue());
numberReturnable.apply(implementationArguments, scope).intValue());
}
@Override

View File

@ -7,11 +7,9 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import java.util.Map;
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.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -29,7 +27,7 @@ public class RecursionsFunction implements Function<Number> {
}
@Override
public Number apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
return ((TerraImplementationArguments) implementationArguments).getRecursions();
}

View File

@ -7,14 +7,12 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
@ -36,16 +34,16 @@ public class SetMarkFunction implements Function<Void> {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
FastMath.floorToInt(
y.apply(implementationArguments, variableMap).doubleValue()),
FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable(), mark.apply(implementationArguments, variableMap));
y.apply(implementationArguments, scope).doubleValue()),
FastMath.floorToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable(), mark.apply(implementationArguments, scope));
return null;
}

View File

@ -7,16 +7,14 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.api.block.entity.BlockEntity;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
@ -43,17 +41,17 @@ public class StateFunction implements Function<Void> {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
Vector3 origin = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, scope).intValue(),
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin()).immutable();
try {
BlockEntity state = arguments.getWorld().getBlockEntity(origin);
state.applyState(data.apply(implementationArguments, variableMap));
state.applyState(data.apply(implementationArguments, scope));
state.update(false);
} catch(Exception e) {
LOGGER.warn("Could not apply BlockState at {}", origin, e);

View File

@ -7,6 +7,7 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import com.dfsek.terra.addons.terrascript.script.StructureScript;
import net.jafama.FastMath;
@ -14,12 +15,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.Platform;
@ -57,20 +56,20 @@ public class StructureFunction implements Function<Boolean> {
}
@Override
public Boolean apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Boolean apply(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, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue()), arguments.getRotation());
String app = id.apply(implementationArguments, variableMap);
String app = id.apply(implementationArguments, scope);
return registry.getByID(app).map(script -> {
Rotation rotation1;
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments, variableMap);
String rotString = rotations.get(arguments.getRandom().nextInt(rotations.size())).apply(implementationArguments, scope);
try {
rotation1 = Rotation.valueOf(rotString);
} catch(IllegalArgumentException e) {
@ -82,7 +81,7 @@ public class StructureFunction implements Function<Boolean> {
return structureScript.generate(arguments.getOrigin(),
arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
y.apply(implementationArguments, scope).intValue(),
FastMath.roundToInt(xz.getZ())),
arguments.getRandom(),
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
@ -90,7 +89,7 @@ public class StructureFunction implements Function<Boolean> {
return script.generate(arguments.getOrigin(),
arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
y.apply(implementationArguments, scope).intValue(),
FastMath.roundToInt(xz.getZ())),
arguments.getRandom(),
arguments.getRotation().rotate(rotation1));

View File

@ -8,6 +8,7 @@
package structure;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
@ -15,7 +16,6 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.addons.terrascript.parser.Parser;
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
@ -24,7 +24,6 @@ import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
@ -60,9 +59,9 @@ public class ParserTest {
long t = System.nanoTime() - l;
System.out.println("Took " + (double) t / 1000000);
block.apply(null, new HashMap<>());
block.apply(null, new Scope());
block.apply(null, new HashMap<>());
block.apply(null, new Scope());
}
private static class Test1 implements Function<Void> {
@ -77,9 +76,9 @@ public class ParserTest {
}
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
System.out.println("string: " + a.apply(implementationArguments, variableMap) + ", double: " +
b.apply(implementationArguments, variableMap));
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
System.out.println("string: " + a.apply(implementationArguments, scope) + ", double: " +
b.apply(implementationArguments, scope));
return null;
}

View File

@ -7,16 +7,14 @@
package com.dfsek.terra.addon.terrascript.check;
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
import net.jafama.FastMath;
import java.util.Map;
import com.dfsek.terra.addons.chunkgenerator.generation.NoiseChunkGenerator3D;
import com.dfsek.terra.addons.chunkgenerator.generation.math.samplers.SamplerProvider;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.util.RotationUtil;
@ -39,19 +37,19 @@ public class CheckFunction implements Function<String> {
@Override
public String apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public String apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
Vector2 xz = Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue());
Vector2 xz = Vector2.of(x.apply(implementationArguments, scope).doubleValue(),
z.apply(implementationArguments, scope).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
Vector3 location = arguments.getOrigin().toVector3Mutable().add(
Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, scope).doubleValue(),
FastMath.roundToInt(xz.getZ()))).immutable();
return apply(location, arguments.getWorld());

View File

@ -2,13 +2,12 @@ package com.dfsek.terra.addons.terrascript.sampler;
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.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.noise.NoiseSampler;
import java.util.Map;
import java.util.function.Supplier;
@ -38,17 +37,17 @@ public class SamplerFunction implements Function<Number> {
}
@Override
public Number apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
public Number apply(ImplementationArguments implementationArguments, Scope scope) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
double x = this.x.apply(implementationArguments, variableMap).doubleValue();
double x = this.x.apply(implementationArguments, scope).doubleValue();
double z = this.z.apply(implementationArguments, variableMap).doubleValue();
double z = this.z.apply(implementationArguments, scope).doubleValue();
NoiseSampler sampler = samplerFunction.apply(() -> function.apply(implementationArguments, variableMap));
NoiseSampler sampler = samplerFunction.apply(() -> function.apply(implementationArguments, scope));
if(twoD) {
return sampler.noise(arguments.getWorld().getSeed(), x, z);
} else {
double y = this.y.apply(implementationArguments, variableMap).doubleValue();
double y = this.y.apply(implementationArguments, scope).doubleValue();
return sampler.noise(arguments.getWorld().getSeed(), x, y, z);
}
}