Пример #1
0
        public bool ExecuteExpecting <T>(IScriptContext context, out T result)
            where T : struct
        {
            result = default(T);
            if (type == SymbolType.Void)
            {
                Debug.LogError($"Executable.ExecuteExpecting : cannot expect {typeof(T)}. " +
                               "Use Execute() or ExecuteExpectingArray() instead.");
                return(false);
            }
            ISymbol lastResult = Execute(context);

            if (lastResult == null || lastResult.Type() != type)
            {
                Debug.LogError("Executable.ExecuteExpecting : execution error.");
                return(false);
            }
            if (!TypeCompatibility <T>(lastResult.Type(), lastResult.ArrayType()))
            {
                Debug.LogError($"Executable.ExecuteExpecting : result type {typeof(T)} " +
                               $"imcompatible with executable type {type}.");
                return(false);
            }
            Symbol <T> lastResultTyped = lastResult as Symbol <T>;

            Assert.IsNotNull(lastResultTyped);
            result = lastResultTyped.Value;
            return(true);
        }
Пример #2
0
    public bool SetGlobalVariable(string variableName, ISymbol value)
    {
        GlobalVariable globalVariable = scriptGlobalVariables.Find(gv => gv.Name == variableName);

        if (globalVariable == null)
        {
            Debug.LogError($"WorldController.GetGlobalVariable(\"{variableName}\") : unkown global variable.");
            return(false);
        }
        if (value.Type() != globalVariable.Type)
        {
            Debug.LogError($"WorldController.GetGlobalVariable(\"{variableName}\") : type mismatch " +
                           $"({value.Type()} instead of {globalVariable.Type}).");
            return(false);
        }
        if (value.Type() == SymbolType.Array &&
            value.ArrayType() != globalVariable.ArrayType)
        {
            Debug.LogError($"WorldController.GetGlobalVariable(\"{variableName}\") : array type mismatch " +
                           $"({value.ArrayType()} instead of {globalVariable.ArrayType}).");
            return(false);
        }

        bool assigned = true;

        switch (variableName)
        {
        case "Company.Money":
            playerCompany.SetMoney(((FloatSymbol)value).Value);
            break;

        case "Company.NeverBailedOut":
            playerCompany.NeverBailedOut = ((BooleanSymbol)value).Value;
            break;

        default:
            assigned = false;
            break;
        }
        if (assigned)
        {
            return(true);
        }
        Debug.LogError($"WorldController.GetGlobalVariable(\"{variableName}\") : illegal assignment.");
        return(false);
    }
Пример #3
0
    private void AssertScriptResult(ISymbol expected, string script)
    {
        Executable executable = Executable.FromScript(script, parserContext);
        ISymbol    result     = executable.Execute(scriptContext);

        Assert.AreEqual(expected.Type(), result.Type());
        Assert.AreEqual(expected.ArrayType(), result.ArrayType());
        Assert.AreEqual(expected.ValueString(), result.ValueString());
        scriptContext.LocalVariables().Clear();
    }
Пример #4
0
        public bool ExecuteExpectingArray <T>(IScriptContext context, out T result)
            where T : class
        {
            result = default(T);
            if (type != SymbolType.Array)
            {
                Debug.LogError($"Executable.ExecuteExpectingArray : cannot expect {typeof(T)}. " +
                               "Use Execute() or ExecuteExpecting() instead.");
                return(false);
            }
            ISymbol lastResult = Execute(context);

            if (lastResult == null || lastResult.Type() != type)
            {
                Debug.LogError("Executable.ExecuteExpectingArray : execution error.");
                return(false);
            }
            if (!TypeCompatibility <T>(lastResult.Type(), lastResult.ArrayType()))
            {
                Debug.LogError($"Executable.ExecuteExpecting : result type {typeof(T)} " +
                               $"imcompatible with executable type {type}.");
                return(false);
            }
            switch (lastResult.ArrayType())
            {
            case SymbolType.Void:
                result = ((ArraySymbol <Void>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value).ToArray() as T;
                break;

            case SymbolType.Boolean:
                result = ((ArraySymbol <bool>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value).ToArray() as T;
                break;

            case SymbolType.Integer:
                result = ((ArraySymbol <int>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value).ToArray() as T;
                break;

            case SymbolType.Float:
                result = ((ArraySymbol <float>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value).ToArray() as T;
                break;

            case SymbolType.Id:
                result = ((ArraySymbol <Id>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value.Identifier).ToArray() as T;
                break;

            case SymbolType.String:
                result = ((ArraySymbol <string>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value).ToArray() as T;
                break;

            case SymbolType.Date:
                result = ((ArraySymbol <DateTime>)lastResult).Value.Elements.Select(
                    e => e.Evaluate(context).Value).ToArray() as T;
                break;

            default: throw new ArgumentOutOfRangeException();
            }
            return(true);
        }