Search in sources :

Example 6 with CString

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

the class CompilerObject method compile0.

void compile0() throws ConfigCompileException {
    Token t = consume();
    if (t.type == TType.NEWLINE) {
        return;
    }
    if (t.type == TType.CONST_START) {
        StringBuilder constName = new StringBuilder();
        while ((t = consume()).type != TType.RCURLY_BRACKET) {
            if (t.type != TType.BARE_STRING && t.type != TType.CONCAT) {
                throw new ConfigCompileException("Constant names may only contain names and dots.", t.getTarget());
            }
            constName.append(t.val());
        }
        Construct constant = env.getConstant(constName.toString());
        if (constant == null) {
            throw new ConfigCompileException("Expected the constant ${" + constName.toString() + "} to be provided in the compilation options, but it wasn't.", t.getTarget());
        }
        t = new Token(TType.STRING, constant.val(), constant.getTarget());
    }
    if (t.type == TType.BARE_STRING && peek().type == TType.FUNC_START) {
        consume();
        CFunction f = new CFunction(t.val(), t.getTarget());
        functionLines.add(peek().getTarget());
        pushNode(f);
        return;
    }
    if (t.type == TType.FUNC_END || t.type == TType.COMMA) {
        if (autoConcatCounter > 0) {
            autoConcatCounter--;
            popNode(t.getTarget());
        }
    }
    if (t.type == TType.COMMA) {
        return;
    }
    if (t.type == TType.FUNC_END) {
        // We're done with this child, so push it up
        popNode(t.getTarget());
        functionLines.pop();
        return;
    }
    if (t.type == TType.LSQUARE_BRACKET) {
        CFunction f = new CFunction("__cbracket__", Target.UNKNOWN);
        pushNode(f);
        bracketCounter++;
        bracketLines.push(t.getTarget());
        return;
    }
    if (t.type == TType.RSQUARE_BRACKET) {
        if (bracketCounter == 0) {
            throw new ConfigCompileException("Unexpected right bracket. (Did you have too many right square brackets (]) in your code?)", t.getTarget());
        }
        bracketCounter--;
        bracketLines.pop();
        popNode(t.getTarget());
        return;
    }
    if (t.type == TType.LCURLY_BRACKET) {
        CFunction f = new CFunction("__cbrace__", Target.UNKNOWN);
        pushNode(f);
        braceCounter++;
        braceLines.push(t.getTarget());
        return;
    }
    if (t.type == TType.RCURLY_BRACKET) {
        if (braceCounter == 0) {
            throw new ConfigCompileException("Unexpected right brace. (Did you have too many right braces (}) in your code?)", t.getTarget());
        }
        braceCounter--;
        braceLines.pop();
        popNode(t.getTarget());
        return;
    }
    // If the next token ISN'T a ) , } ] we need to autoconcat this
    if (peek().type != TType.FUNC_END && peek().type != TType.COMMA && peek().type != TType.RCURLY_BRACKET && peek().type != TType.RSQUARE_BRACKET) {
        // ... unless we're already in an autoconcat
        if (!(pointer.getData() instanceof CFunction && ((CFunction) pointer.getData()).val().equals("__autoconcat__"))) {
            CFunction f = new CFunction("__autoconcat__", Target.UNKNOWN);
            pushNode(f);
            autoConcatCounter++;
        }
    }
    if (t.type == TType.BARE_STRING && peek().type == TType.LABEL) {
        consume();
        pointer.addChild(new ParseTree(new CLabel(new CString(t.val(), t.getTarget())), stream.getFileOptions()));
        return;
    }
    if (t.type.isIdentifier()) {
        // If it's an atomic, put it in a construct and parse tree, then add it
        pointer.addChild(new ParseTree(resolveIdentifier(t), stream.getFileOptions()));
        return;
    }
    if (t.type.isSymbol()) {
        pointer.addChild(new ParseTree(new CSymbol(t.val(), t.type, t.getTarget()), stream.getFileOptions()));
        return;
    }
// Now we have to check ahead for commas and other division parameters.
}
Also used : CLabel(com.laytonsmith.core.constructs.CLabel) CSymbol(com.laytonsmith.core.constructs.CSymbol) Construct(com.laytonsmith.core.constructs.Construct) CFunction(com.laytonsmith.core.constructs.CFunction) Token(com.laytonsmith.core.constructs.Token) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree) CString(com.laytonsmith.core.constructs.CString)

Example 7 with CString

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

the class BoundEvent method trigger.

/**
 * When the event actually occurs, this should be run, after translating the original event object (of whatever type
 * it may be) into a standard map, which contains the event object data. It is converted into a CArray here, and
 * then the script is executed with the driver's execute function.
 *
 * @param activeEvent
 */
public void trigger(ActiveEvent activeEvent) throws EventException {
    try {
        // GenericTree<Construct> root = new GenericTree<Construct>();
        // root.setRoot(tree);
        Environment env = originalEnv.clone();
        CArray ca = CArray.GetAssociativeArray(Target.UNKNOWN);
        for (Map.Entry<String, Construct> entry : activeEvent.parsedEvent.entrySet()) {
            ca.set(new CString(entry.getKey(), Target.UNKNOWN), entry.getValue(), Target.UNKNOWN);
        }
        env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(CArray.TYPE, eventObjName, ca, Target.UNKNOWN));
        env.getEnv(GlobalEnv.class).SetEvent(activeEvent);
        activeEvent.addHistory("Triggering bound event: " + this);
        try {
            ProfilePoint p = env.getEnv(GlobalEnv.class).GetProfiler().start("Executing event handler for " + this.getEventName() + " defined at " + this.getTarget(), LogLevel.ERROR);
            try {
                this.execute(env, activeEvent);
            } finally {
                p.stop();
            }
        } catch (ConfigRuntimeException e) {
            // We don't know how to handle this, but we need to set the env,
            // then pass it up the chain
            e.setEnv(env);
            throw e;
        }
    } catch (CloneNotSupportedException ex) {
        Logger.getLogger(BoundEvent.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) Environment(com.laytonsmith.core.environments.Environment) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with CString

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

the class ObjectGenerator method enchants.

public CArray enchants(Map<MCEnchantment, Integer> map, Target t) {
    CArray ret = new CArray(t);
    for (Map.Entry<MCEnchantment, Integer> entry : map.entrySet()) {
        CArray eObj = CArray.GetAssociativeArray(t);
        eObj.set("etype", new CString(entry.getKey().getName(), t), t);
        eObj.set("elevel", new CInt(entry.getValue(), t), t);
        ret.push(eObj, t);
    }
    return ret;
}
Also used : MCEnchantment(com.laytonsmith.abstraction.MCEnchantment) CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) Map(java.util.Map) HashMap(java.util.HashMap) CString(com.laytonsmith.core.constructs.CString)

Example 9 with CString

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

the class ObjectGenerator method recipe.

public Construct recipe(MCRecipe r, Target t) {
    if (r == null) {
        return CNull.NULL;
    }
    CArray ret = CArray.GetAssociativeArray(t);
    ret.set("type", new CString(r.getRecipeType().name(), t), t);
    ret.set("result", item(r.getResult(), t), t);
    if (r instanceof MCFurnaceRecipe) {
        ret.set("input", item(((MCFurnaceRecipe) r).getInput(), t), t);
    } else if (r instanceof MCShapelessRecipe) {
        MCShapelessRecipe shapeless = (MCShapelessRecipe) r;
        CArray il = new CArray(t);
        for (MCItemStack i : shapeless.getIngredients()) {
            il.push(item(i, t), t);
        }
        ret.set("ingredients", il, t);
        if (shapeless.getKey() != null) {
            ret.set("key", shapeless.getKey(), t);
        }
    } else if (r instanceof MCShapedRecipe) {
        MCShapedRecipe shaped = (MCShapedRecipe) r;
        CArray shape = new CArray(t);
        for (String line : shaped.getShape()) {
            shape.push(new CString(line, t), t);
        }
        CArray imap = CArray.GetAssociativeArray(t);
        for (Map.Entry<Character, MCItemStack> entry : shaped.getIngredientMap().entrySet()) {
            imap.set(entry.getKey().toString(), item(entry.getValue(), t), t);
        }
        ret.set("shape", shape, t);
        ret.set("ingredients", imap, t);
        if (shaped.getKey() != null) {
            ret.set("key", shaped.getKey(), t);
        }
    }
    return ret;
}
Also used : MCShapedRecipe(com.laytonsmith.abstraction.MCShapedRecipe) MCItemStack(com.laytonsmith.abstraction.MCItemStack) MCShapelessRecipe(com.laytonsmith.abstraction.MCShapelessRecipe) CArray(com.laytonsmith.core.constructs.CArray) MCFurnaceRecipe(com.laytonsmith.abstraction.MCFurnaceRecipe) CString(com.laytonsmith.core.constructs.CString) Map(java.util.Map) HashMap(java.util.HashMap) CString(com.laytonsmith.core.constructs.CString)

Example 10 with CString

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

the class ObjectGenerator method item.

/**
 * Gets an MCItemStack from a given item "object". Supports both the old and new formats currently
 *
 * @param i
 * @param t
 * @return An abstract item stack
 */
public MCItemStack item(Construct i, Target t) {
    if (i instanceof CNull) {
        return EmptyItem();
    }
    if (!(i instanceof CArray)) {
        throw new CREFormatException("Expected an array!", t);
    }
    CArray item = (CArray) i;
    MCMaterial mat;
    int data = 0;
    int qty = 1;
    if (item.containsKey("qty")) {
        qty = Static.getInt32(item.get("qty", t), t);
        if (qty <= 0) {
            return EmptyItem();
        }
    }
    if (item.containsKey("name")) {
        mat = StaticLayer.GetConvertor().GetMaterial(item.get("name", t).val());
    } else if (item.containsKey("type")) {
        Construct type = item.get("type", t);
        if (type instanceof CString) {
            int seperatorIndex = type.val().indexOf(':');
            if (seperatorIndex != -1) {
                try {
                    data = Integer.parseInt(type.val().substring(seperatorIndex + 1));
                } catch (NumberFormatException e) {
                    throw new CRERangeException("The item data \"" + type.val().substring(seperatorIndex + 1) + "\" is not a valid integer.", t);
                }
                type = new CString(type.val().substring(0, seperatorIndex), t);
            }
        }
        mat = StaticLayer.GetConvertor().getMaterial(Static.getInt32(type, t));
    } else {
        throw new CREFormatException("Could not find item name!", t);
    }
    if (mat == null) {
        throw new CRENotFoundException("A material could not be found based on the given name.", t);
    }
    if (mat.getType() == 0) {
        return EmptyItem();
    }
    if (item.containsKey("data")) {
        data = Static.getInt32(item.get("data", t), t);
    }
    MCItemMeta meta = null;
    if (item.containsKey("meta")) {
        meta = itemMeta(item.get("meta", t), mat, t);
    }
    // Create itemstack
    MCItemStack ret = StaticLayer.GetItemStack(mat, data, qty);
    if (meta != null) {
        ret.setItemMeta(meta);
    }
    // Fallback to enchants in item array if not in meta
    if (item.containsKey("enchants")) {
        try {
            Map<MCEnchantment, Integer> enchants = enchants((CArray) item.get("enchants", t), t);
            for (Map.Entry<MCEnchantment, Integer> entry : enchants.entrySet()) {
                ret.addUnsafeEnchantment(entry.getKey(), entry.getValue());
            }
        } catch (ClassCastException ex) {
            throw new CREFormatException("Enchants must be an array of enchantment arrays.", t);
        }
    }
    return ret;
}
Also used : CRENotFoundException(com.laytonsmith.core.exceptions.CRE.CRENotFoundException) CArray(com.laytonsmith.core.constructs.CArray) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta) CString(com.laytonsmith.core.constructs.CString) MCMaterial(com.laytonsmith.abstraction.blocks.MCMaterial) MCEnchantment(com.laytonsmith.abstraction.MCEnchantment) MCItemStack(com.laytonsmith.abstraction.MCItemStack) Construct(com.laytonsmith.core.constructs.Construct) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) Map(java.util.Map) HashMap(java.util.HashMap) CRERangeException(com.laytonsmith.core.exceptions.CRE.CRERangeException) CNull(com.laytonsmith.core.constructs.CNull)

Aggregations

CString (com.laytonsmith.core.constructs.CString)34 CArray (com.laytonsmith.core.constructs.CArray)23 Construct (com.laytonsmith.core.constructs.Construct)19 CInt (com.laytonsmith.core.constructs.CInt)12 HashMap (java.util.HashMap)10 IVariable (com.laytonsmith.core.constructs.IVariable)9 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)9 Map (java.util.Map)8 CDouble (com.laytonsmith.core.constructs.CDouble)7 CNull (com.laytonsmith.core.constructs.CNull)7 Variable (com.laytonsmith.core.constructs.Variable)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 CFunction (com.laytonsmith.core.constructs.CFunction)5 Target (com.laytonsmith.core.constructs.Target)5 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)5 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 ArrayList (java.util.ArrayList)5 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4 ParseTree (com.laytonsmith.core.ParseTree)4