Search in sources :

Example 6 with Parser

use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.

the class TriggerTest method testNestedIf.

@Test
public void testNestedIf() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "x = 4.0;" + "IF x < 0.0;" + "    #TEST \"no\";" + "ELSEIF x > 0.0;" + "    #TEST \"pass\";" + "ELSE;" + "    #TEST \"no\";" + "ENDIF;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    executorMap.put("TEST", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
            Assert.assertEquals("pass", args[0]);
            return null;
        }
    });
    Map<String, Placeholder> placeholderMap = new HashMap<>();
    Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.startWithContext(null);
}
Also used : Placeholder(io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 7 with Parser

use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.

the class TriggerTest method testStringAppend.

@Test
public void testStringAppend() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "arr = array(4)\n" + "arr[0] = \"beh\"+player.in.health\n" + "arr[1] = player.in.health+\"beh\"\n" + "arr[2] = \"beh\"+1+1\n" + "arr[3] = \"beh\"+(1+1)\n" + "#MESSAGE arr\n";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    executorMap.put("MESSAGE", new Executor() {

        @Override
        public Integer execute(boolean sync, Object context, Object... args) {
            Object[] arr = (Object[]) args[0];
            Assert.assertEquals("beh0.82", arr[0]);
            Assert.assertEquals("0.82beh", arr[1]);
            Assert.assertEquals("beh11", arr[2]);
            Assert.assertEquals("beh2", arr[3]);
            return null;
        }
    });
    TheTest reference = new TheTest();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.getVars().put("player", reference);
    interpreter.getVars().put("text", "hello");
    interpreter.startWithContext(null);
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 8 with Parser

use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.

the class TriggerTest method testEnumParse.

@Test
public void testEnumParse() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "result = parseEnum(\"io.github.wysohn.triggerreactor.TriggerTest$TestEnum\", value);";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<String, Object>() {

        {
            put("value", "IMTEST");
        }
    }, new CommonFunctions(null));
    interpreter.startWithContext(null);
    Assert.assertEquals(TestEnum.IMTEST, interpreter.getVars().get("result"));
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 9 with Parser

use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.

the class TriggerTest method testReference.

@Test
public void testReference() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "X = 5\n" + "str = \"abc\"\n" + "WHILE 1 > 0\n" + "    str = str + X\n" + "    IF player.in.health > 2 && player.in.health > 0\n" + "        #MESSAGE 3*4\n" + "    ELSE\n" + "        #MESSAGE str\n" + "    ENDIF\n" + "    #MESSAGE text\n" + "    player.getTest().in.health = player.getTest().in.getHealth() + 1.2\n" + "    #MESSAGE player.in.hasPermission(\"t\")\n" + "    X = X - 1\n" + "    IF X < 0\n" + "        #STOP\n" + "    ENDIF\n" + "ENDWHILE";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    executorMap.put("MESSAGE", new Executor() {

        @Override
        public Integer execute(boolean sync, Object context, Object... args) {
            return null;
        }
    });
    TheTest reference = new TheTest();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
    interpreter.getVars().put("player", reference);
    interpreter.getVars().put("text", "hello");
    interpreter.startWithContext(null);
    Assert.assertEquals(12.43, reference.getTest().in.getHealth(), 0.001);
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 10 with Parser

use of io.github.wysohn.triggerreactor.core.script.parser.Parser in project TriggerReactor by wysohn.

the class ParserTest method testPlaceholder.

@Test
public void testPlaceholder() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "x = 10;" + "#MESSAGE $placeholdertest:0:x:5:true;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Queue<Node> queue = new LinkedList<Node>();
    serializeNode(queue, root);
    assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
    assertEquals(new Node(new Token(Type.ID, "x")), queue.poll());
    assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
    assertEquals(new Node(new Token(Type.INTEGER, "10")), queue.poll());
    assertEquals(new Node(new Token(Type.OPERATOR, "=")), queue.poll());
    assertEquals(new Node(new Token(Type.INTEGER, "0")), queue.poll());
    assertEquals(new Node(new Token(Type.THIS, "<This>")), queue.poll());
    assertEquals(new Node(new Token(Type.ID, "x")), queue.poll());
    assertEquals(new Node(new Token(Type.OPERATOR, ".")), queue.poll());
    assertEquals(new Node(new Token(Type.INTEGER, "5")), queue.poll());
    assertEquals(new Node(new Token(Type.BOOLEAN, "true")), queue.poll());
    assertEquals(new Node(new Token(Type.PLACEHOLDER, "placeholdertest")), queue.poll());
    assertEquals(new Node(new Token(Type.EXECUTOR, "MESSAGE")), queue.poll());
    assertEquals(new Node(new Token(Type.ROOT, "<ROOT>")), queue.poll());
    assertEquals(0, queue.size());
}
Also used : Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Token(io.github.wysohn.triggerreactor.core.script.Token) LinkedList(java.util.LinkedList) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Test(org.junit.Test)

Aggregations

Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)27 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)27 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)27 Charset (java.nio.charset.Charset)27 Test (org.junit.Test)27 CommonFunctions (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions)21 Executor (io.github.wysohn.triggerreactor.core.script.interpreter.Executor)21 Interpreter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter)21 HashMap (java.util.HashMap)21 Placeholder (io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder)7 Token (io.github.wysohn.triggerreactor.core.script.Token)6 LinkedList (java.util.LinkedList)6 Player (org.bukkit.entity.Player)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Location (org.bukkit.Location)1 ItemStack (org.bukkit.inventory.ItemStack)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1