Search in sources :

Example 21 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class TriggerTest method testUnaryMinus.

@Test
public void testUnaryMinus() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "x = 4.0;" + "#TEST1 -1+-5;" + "#TEST2 -2.0--5;" + "#TEST3 -$test3-5;" + "#TEST4 -x-5;";
    Lexer lexer = new Lexer(text, charset);
    Parser parser = new Parser(lexer);
    Node root = parser.parse();
    Map<String, Executor> executorMap = new HashMap<>();
    executorMap.put("TEST1", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
            Assert.assertEquals(-6, args[0]);
            return null;
        }
    });
    executorMap.put("TEST2", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
            Assert.assertEquals(3.0, args[0]);
            return null;
        }
    });
    executorMap.put("TEST3", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
            Assert.assertEquals(-8, args[0]);
            return null;
        }
    });
    executorMap.put("TEST4", new Executor() {

        @Override
        protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
            Assert.assertEquals(-9.0, args[0]);
            return null;
        }
    });
    Map<String, Placeholder> placeholderMap = new HashMap<>();
    placeholderMap.put("test3", new Placeholder() {

        @Override
        public Object parse(Object context, Object... args) throws Exception {
            return 3;
        }
    });
    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 22 with Executor

use of io.github.wysohn.triggerreactor.core.script.interpreter.Executor in project TriggerReactor by wysohn.

the class TriggerTest method testArray.

@Test
public void testArray() throws Exception {
    Charset charset = Charset.forName("UTF-8");
    String text = "" + "args[0] = \"arg1\"\n" + "args[1] = \"arg2\"\n" + "#MESSAGE args[0]+\", \"+args[1*-1*-1+1-1--1-1]\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) {
            Assert.assertEquals("arg1, arg2", args[0]);
            return null;
        }
    });
    Map<String, Object> map = new HashMap<String, Object>();
    Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), new CommonFunctions(null));
    String[] args = new String[] { "item1", "item2" };
    interpreter.getVars().put("args", args);
    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)

Aggregations

Executor (io.github.wysohn.triggerreactor.core.script.interpreter.Executor)22 HashMap (java.util.HashMap)22 CommonFunctions (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions)21 Interpreter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter)21 Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)21 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)21 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)21 Charset (java.nio.charset.Charset)21 Test (org.junit.Test)21 Placeholder (io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder)7 Player (org.bukkit.entity.Player)5 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ScriptException (javax.script.ScriptException)1 Location (org.bukkit.Location)1 ItemStack (org.bukkit.inventory.ItemStack)1