Load structure scripts into registry

This commit is contained in:
dfsek
2020-12-23 02:35:07 -07:00
parent e9dc7428b8
commit 405a96034c
13 changed files with 117 additions and 52 deletions
@@ -50,6 +50,7 @@ import java.util.Map;
public class Parser {
private final String data;
private final Map<String, FunctionBuilder<? extends Function<?>>> functions = new HashMap<>();
private String id;
public Parser(String data) {
this.data = data;
@@ -60,6 +61,10 @@ public class Parser {
return this;
}
public String getID() {
return id;
}
/**
* Parse input
*
@@ -75,6 +80,14 @@ public class Parser {
} catch(TokenizerException e) {
throw new ParseException("Failed to tokenize input", e);
}
// Parse ID
ParserUtil.checkType(tokens.remove(0), Token.Type.ID); // First token must be ID
Token idToken = tokens.get(0);
ParserUtil.checkType(tokens.remove(0), Token.Type.STRING); // Second token must be string literal containing ID
ParserUtil.checkType(tokens.remove(0), Token.Type.STATEMENT_END);
this.id = idToken.getContent();
// Check for dangling brackets
int blockLevel = 0;
for(Token t : tokens) {
@@ -1,4 +1,50 @@
package com.dfsek.terra.api.structures.script;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.world.Chunk;
import com.dfsek.terra.api.structures.parser.Parser;
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.script.builders.BlockFunctionBuilder;
import com.dfsek.terra.api.structures.script.builders.CheckFunctionBuilder;
import com.dfsek.terra.api.structures.structure.Rotation;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
public class StructureScript {
private final Block block;
private final String id;
public StructureScript(InputStream inputStream, TerraPlugin main) {
Parser parser;
try {
parser = new Parser(IOUtils.toString(inputStream));
} catch(IOException e) {
throw new RuntimeException(e);
}
parser.addFunction("block", new BlockFunctionBuilder(main))
.addFunction("check", new CheckFunctionBuilder(main));
try {
block = parser.parse();
} catch(ParseException e) {
throw new RuntimeException(e);
}
this.id = parser.getID();
}
public void execute(Location location, Rotation rotation) {
block.apply(location, rotation);
}
public void execute(Location location, Chunk chunk, Rotation rotation) {
block.apply(location, chunk, rotation);
}
public String getId() {
return id;
}
}
@@ -206,6 +206,10 @@ public class Token {
/**
* Break statement
*/
BREAK
BREAK,
/**
* ID declaration
*/
ID
}
}
@@ -141,6 +141,9 @@ public class Tokenizer {
if(tokenString.equals("break"))
return new Token(tokenString, Token.Type.BREAK, new Position(reader.getLine(), reader.getIndex()));
if(tokenString.equals("id"))
return new Token(tokenString, Token.Type.ID, new Position(reader.getLine(), reader.getIndex()));
return new Token(tokenString, Token.Type.IDENTIFIER, new Position(reader.getLine(), reader.getIndex()));
}