Search in sources :

Example 21 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class RandomTests method testClone.

@Test
public void testClone() throws CloneNotSupportedException {
    CArray c1 = C.Array(C.Void(), C.Void()).clone();
    CBoolean c2 = C.Boolean(true).clone();
    CDouble c4 = C.Double(1).clone();
    CFunction c5 = new CFunction("", Target.UNKNOWN).clone();
    CInt c6 = C.Int(1).clone();
    CNull c7 = C.Null().clone();
    CString c8 = C.String("").clone();
    Construct c9 = C.Void().clone();
    Command c10 = new Command("/c", Target.UNKNOWN).clone();
    IVariable c12 = new IVariable(Auto.TYPE, "@name", C.Null(), Target.UNKNOWN).clone();
    Variable c13 = new Variable("$name", "", false, false, Target.UNKNOWN);
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) Command(com.laytonsmith.core.constructs.Command) CBoolean(com.laytonsmith.core.constructs.CBoolean) IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CFunction(com.laytonsmith.core.constructs.CFunction) Construct(com.laytonsmith.core.constructs.Construct) CNull(com.laytonsmith.core.constructs.CNull) CString(com.laytonsmith.core.constructs.CString) Test(org.junit.Test)

Example 22 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class RandomTests method testJSONDecodeString.

@Test
public void testJSONDecodeString() throws MarshalException {
    CArray ca = new CArray(Target.UNKNOWN);
    ca.push(C.Int(1), Target.UNKNOWN);
    ca.push(C.Double(2.2), Target.UNKNOWN);
    ca.push(C.String("string"), Target.UNKNOWN);
    ca.push(C.String("\"Quote\""), Target.UNKNOWN);
    ca.push(C.Boolean(true), Target.UNKNOWN);
    ca.push(C.Boolean(false), Target.UNKNOWN);
    ca.push(C.Null(), Target.UNKNOWN);
    ca.push(C.Void(), Target.UNKNOWN);
    ca.push(new Command("/Command", Target.UNKNOWN), Target.UNKNOWN);
    ca.push(new CArray(Target.UNKNOWN, new CInt(1, Target.UNKNOWN)), Target.UNKNOWN);
    StaticTest.assertCEquals(ca, Construct.json_decode("[1, 2.2, \"string\", \"\\\"Quote\\\"\", true, false, null, \"\", \"\\/Command\", [1]]", Target.UNKNOWN));
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) Command(com.laytonsmith.core.constructs.Command) CArray(com.laytonsmith.core.constructs.CArray) Test(org.junit.Test)

Example 23 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class ArrayHandlingTest method setUp.

@Before
public void setUp() {
    fakePlayer = StaticTest.GetOnlinePlayer();
    commonArray = new CArray(Target.UNKNOWN, new CInt(1, Target.UNKNOWN), new CInt(2, Target.UNKNOWN), new CInt(3, Target.UNKNOWN));
    env.getEnv(CommandHelperEnvironment.class).SetPlayer(fakePlayer);
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Before(org.junit.Before)

Example 24 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class BukkitMCCommand method handleTabComplete.

// I may be able to move these to c.l.c.f.Commands.java
@Override
public List<String> handleTabComplete(MCCommandSender sender, String alias, String[] args) {
    if (Commands.onTabComplete.containsKey(cmd.getName().toLowerCase())) {
        Target t = Target.UNKNOWN;
        CArray cargs = new CArray(t);
        for (String arg : args) {
            cargs.push(new CString(arg, t), t);
        }
        CClosure closure = Commands.onTabComplete.get(cmd.getName().toLowerCase());
        try {
            closure.execute(new CString(alias, t), new CString(sender.getName(), t), cargs, // reserved for an obgen style command array
            new CArray(t));
        } catch (FunctionReturnException e) {
            Construct fret = e.getReturn();
            if (fret instanceof CArray) {
                List<String> ret = new ArrayList<>();
                if (((CArray) fret).inAssociativeMode()) {
                    for (Construct key : ((CArray) fret).keySet()) {
                        ret.add(((CArray) fret).get(key, Target.UNKNOWN).val());
                    }
                } else {
                    for (Construct value : ((CArray) fret).asList()) {
                        ret.add(value.val());
                    }
                }
                return ret;
            }
        } catch (ConfigRuntimeException cre) {
            ConfigRuntimeException.HandleUncaughtException(cre, closure.getEnv());
            return new ArrayList<>();
        }
    }
    BukkitMCCommandTabCompleteEvent event = new BukkitMCCommandTabCompleteEvent(sender, cmd, alias, args);
    EventUtils.TriggerListener(Driver.TAB_COMPLETE, "tab_complete_command", event);
    return event.getCompletions();
}
Also used : BukkitMCCommandTabCompleteEvent(com.laytonsmith.abstraction.bukkit.events.BukkitMiscEvents.BukkitMCCommandTabCompleteEvent) Target(com.laytonsmith.core.constructs.Target) CClosure(com.laytonsmith.core.constructs.CClosure) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) ArrayList(java.util.ArrayList) List(java.util.List) CString(com.laytonsmith.core.constructs.CString) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString)

Example 25 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class Interpreter method execute.

/**
 * This executes an entire script. The cmdline_prompt_event is first triggered (if used) and if the event is
 * cancelled, nothing happens.
 *
 * @param script
 * @param args
 * @param fromFile
 * @throws ConfigCompileException
 * @throws IOException
 */
public void execute(String script, List<String> args, File fromFile) throws ConfigCompileException, IOException, ConfigCompileGroupException {
    CmdlineEvents.cmdline_prompt_input.CmdlinePromptInput input = new CmdlineEvents.cmdline_prompt_input.CmdlinePromptInput(script, inShellMode);
    EventUtils.TriggerListener(Driver.CMDLINE_PROMPT_INPUT, "cmdline_prompt_input", input);
    if (input.isCancelled()) {
        return;
    }
    ctrlCcount = 0;
    if ("exit".equals(script)) {
        if (inShellMode) {
            inShellMode = false;
            return;
        }
        pl(YELLOW + "Use exit() if you wish to exit.");
        return;
    }
    if ("help".equals(script)) {
        pl(getHelpMsg());
        return;
    }
    if (fromFile == null) {
        fromFile = new File("Interpreter");
    }
    boolean localShellMode = false;
    if (!inShellMode && script.startsWith("$$")) {
        localShellMode = true;
        script = script.substring(2);
    }
    if (inShellMode || localShellMode) {
        // Wrap this in shell_adv
        if (doBuiltin(script)) {
            return;
        }
        List<String> shellArgs = StringUtils.ArgParser(script);
        List<String> escapedArgs = new ArrayList<>();
        for (String arg : shellArgs) {
            escapedArgs.add(new CString(arg, Target.UNKNOWN).getQuote());
        }
        script = "shell_adv(" + "array(" + StringUtils.Join(escapedArgs, ",") + ")," + "array(" + "'stdout':closure(@l){sys_out(@l);}," + "'stderr':closure(@l){sys_err(@l);})" + ");";
    }
    isExecuting = true;
    ProfilePoint compile = env.getEnv(GlobalEnv.class).GetProfiler().start("Compilation", LogLevel.VERBOSE);
    final ParseTree tree;
    try {
        TokenStream stream = MethodScriptCompiler.lex(script, fromFile, true);
        tree = MethodScriptCompiler.compile(stream);
    } finally {
        compile.stop();
    }
    // Environment env = Environment.createEnvironment(this.env.getEnv(GlobalEnv.class));
    final List<Variable> vars = new ArrayList<>();
    if (args != null) {
        // Build the @arguments variable, the $ vars, and $ itself. Note that
        // we have special handling for $0, that is the script name, like bash.
        // However, it doesn't get added to either $ or @arguments, due to the
        // uncommon use of it.
        StringBuilder finalArgument = new StringBuilder();
        CArray arguments = new CArray(Target.UNKNOWN);
        {
            // Set the $0 argument
            Variable v = new Variable("$0", "", Target.UNKNOWN);
            v.setVal(fromFile.toString());
            v.setDefault(fromFile.toString());
            vars.add(v);
        }
        for (int i = 0; i < args.size(); i++) {
            String arg = args.get(i);
            if (i > 0) {
                finalArgument.append(" ");
            }
            Variable v = new Variable("$" + Integer.toString(i + 1), "", Target.UNKNOWN);
            v.setVal(new CString(arg, Target.UNKNOWN));
            v.setDefault(arg);
            vars.add(v);
            finalArgument.append(arg);
            arguments.push(new CString(arg, Target.UNKNOWN), Target.UNKNOWN);
        }
        Variable v = new Variable("$", "", false, true, Target.UNKNOWN);
        v.setVal(new CString(finalArgument.toString(), Target.UNKNOWN));
        v.setDefault(finalArgument.toString());
        vars.add(v);
        env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(CArray.TYPE, "@arguments", arguments, Target.UNKNOWN));
    }
    try {
        ProfilePoint p = this.env.getEnv(GlobalEnv.class).GetProfiler().start("Interpreter Script", LogLevel.ERROR);
        try {
            final MutableObject<Throwable> wasThrown = new MutableObject<>();
            scriptThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        MethodScriptCompiler.execute(tree, env, new MethodScriptComplete() {

                            @Override
                            public void done(String output) {
                                if (System.console() != null && !"".equals(output.trim())) {
                                    StreamUtils.GetSystemOut().println(output);
                                }
                            }
                        }, null, vars);
                    } catch (CancelCommandException e) {
                    // Nothing, though we could have been Ctrl+C cancelled, so we need to reset
                    // the interrupt flag. But we do that unconditionally below, in the finally,
                    // in the other thread.
                    } catch (ConfigRuntimeException e) {
                        ConfigRuntimeException.HandleUncaughtException(e, env);
                        // No need for the full stack trace
                        if (System.console() == null) {
                            System.exit(1);
                        }
                    } catch (NoClassDefFoundError e) {
                        StreamUtils.GetSystemErr().println(RED + Main.getNoClassDefFoundErrorMessage(e) + reset());
                        StreamUtils.GetSystemErr().println("Since you're running from standalone interpreter mode, this is not a fatal error, but one of the functions you just used required" + " an actual backing engine that isn't currently loaded. (It still might fail even if you load the engine though.) You simply won't be" + " able to use that function here.");
                        if (System.console() == null) {
                            System.exit(1);
                        }
                    } catch (InvalidEnvironmentException ex) {
                        StreamUtils.GetSystemErr().println(RED + ex.getMessage() + " " + ex.getData() + "() cannot be used in this context.");
                        if (System.console() == null) {
                            System.exit(1);
                        }
                    } catch (RuntimeException e) {
                        pl(RED + e.toString());
                        e.printStackTrace(StreamUtils.GetSystemErr());
                        if (System.console() == null) {
                            System.exit(1);
                        }
                    }
                }
            }, "MethodScript-Main");
            scriptThread.start();
            try {
                scriptThread.join();
            } catch (InterruptedException ex) {
            // 
            }
        } finally {
            p.stop();
        }
    } finally {
        env.getEnv(GlobalEnv.class).SetInterrupt(false);
        isExecuting = false;
    }
}
Also used : TokenStream(com.laytonsmith.core.compiler.TokenStream) InvalidEnvironmentException(com.laytonsmith.core.environments.InvalidEnvironmentException) IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) IVariable(com.laytonsmith.core.constructs.IVariable) ArrayList(java.util.ArrayList) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString) CmdlineEvents(com.laytonsmith.core.events.drivers.CmdlineEvents) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) MutableObject(com.laytonsmith.PureUtilities.Common.MutableObject) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) MethodScriptComplete(com.laytonsmith.core.MethodScriptComplete) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) File(java.io.File) ParseTree(com.laytonsmith.core.ParseTree)

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)50 CString (com.laytonsmith.core.constructs.CString)27 Construct (com.laytonsmith.core.constructs.Construct)23 CInt (com.laytonsmith.core.constructs.CInt)17 CNull (com.laytonsmith.core.constructs.CNull)12 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)12 HashMap (java.util.HashMap)9 CDouble (com.laytonsmith.core.constructs.CDouble)8 IVariable (com.laytonsmith.core.constructs.IVariable)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)4 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4