Exemple #1
0
        public object Execute(FiMClass reportClass, bool global = false)
        {
            if (reportClass.GetVariable(this.Name) != null)
            {
                throw new FiMException("Variable " + this.Name + " already exists");
            }

            KirinValue value = new KirinValue(this.RawValue, reportClass, this.ExpectedType);

            if (value.Value == null)
            {
                value.Value = FiMHelper.GetDefaultValue(this.ExpectedType);
            }
            else
            {
                if (value.Type != this.ExpectedType)
                {
                    throw new FiMException("Expected " + this.ExpectedType.AsNamedString() +
                                           ", got " + value.Type.AsNamedString());
                }
            }
            value.Constant = Constant;

            FiMVariable var = new FiMVariable(this.Name, value);

            reportClass.Variables.Push(var, global);
            return(null);
        }
Exemple #2
0
        public KirinValue Load()
        {
            if (this.Raw == null)
            {
                if (FiMHelper.IsTypeArray(this.Type))
                {
                    this._Value = FiMHelper.GetDefaultValue(this.Type);
                }
                else
                {
                    if (this.ForcedType == null)
                    {
                        throw new FiMException("Value is null");
                    }
                    this._Value = FiMHelper.GetDefaultValue((KirinVariableType)this.ForcedType);
                }
            }
            else
            {
                string raw = this.Raw;

                var eType = FiMHelper.DeclarationType.Determine(" " + raw, out string eKeyword, false);
                if (eType != KirinVariableType.UNKNOWN)
                {
                    raw = raw.Substring(eKeyword.Length);
                    this.ForceType(eType);
                }

                if (this.ForcedType != null)
                {
                    eType = (KirinVariableType)this.ForcedType;
                }

                object value;
                if (KirinLiteral.TryParse(raw, out object lResult))
                {
                    value = lResult;
                }
                else
                {
                    value = KirinValue.Evaluate(Class, raw, out var returnedType, ForcedType);
                    this.ForceType(returnedType);
                }

                if (eType != KirinVariableType.UNKNOWN && FiMHelper.AsVariableType(value) != eType)
                {
                    throw new FiMException("Expected " + eType.AsNamedString() + ", got " + FiMHelper.AsVariableType(value));
                }
                this._Value = value;
            }
            return(this);
        }
Exemple #3
0
        public override object Execute(FiMClass reportClass)
        {
            if (reportClass.GetVariable(this.VariableName) != null)
            {
                throw new FiMException("Variable " + this.VariableName + " already exists");
            }

            var varFrom = new KirinValue(this.RawFrom, reportClass);
            var varTo   = new KirinValue(this.RawTo, reportClass);

            if (varFrom.Type != KirinVariableType.NUMBER || varTo.Type != KirinVariableType.NUMBER)
            {
                throw new FiMException("Expected type number on for-to loops");
            }

            var valFrom = Convert.ToDouble(varFrom.Value);
            var valTo   = Convert.ToDouble(varTo.Value);

            double interval;

            if (this.RawInterval != string.Empty)
            {
                var varInterval = new KirinValue(this.RawInterval, reportClass);
                if (varInterval.Type != KirinVariableType.NUMBER)
                {
                    throw new FiMException("Expected tpye number of for-to interval");
                }
                interval = Convert.ToDouble(varInterval.Value);
            }
            else
            {
                interval = valTo - valFrom > 0 ? 1.0d : -1.0d;
            }

            reportClass.Variables.Push(new FiMVariable(this.VariableName, valFrom));
            while (interval > 0 ? valFrom <= valTo : valFrom >= valTo)
            {
                reportClass.Variables.Get(this.VariableName).Value = valFrom;

                var value = this.Statement.Execute(reportClass);
                if (value != null)
                {
                    reportClass.Variables.Pop(false);
                    return(value);
                }

                valFrom += interval;
            }

            reportClass.Variables.Pop(false);
            return(null);
        }
Exemple #4
0
        public override object Execute(FiMClass reportClass)
        {
            var value = new KirinValue(this.RawParameters, reportClass);

            if (this.ExpectedType != KirinVariableType.UNKNOWN)
            {
                if (value.Type != this.ExpectedType)
                {
                    throw new FiMException("Expected " + this.ExpectedType.AsNamedString() +
                                           ", got " + value.Type.AsNamedString());
                }
            }
            return(value.Value);
        }
Exemple #5
0
        public override object Execute(FiMClass reportClass)
        {
            var lVariable = reportClass.GetVariable(this.LeftOp);

            if (lVariable == null)
            {
                throw new FiMException("Variable " + this.LeftOp + " does not exist");
            }

            var kValue = new KirinValue(this.RightOp, reportClass);

            lVariable.Value = kValue.Value;
            return(null);
        }
Exemple #6
0
        public override object Execute(FiMClass reportClass)
        {
            if (reportClass.Variables.Has(this.VariableName))
            {
                throw new FiMException("Variable " + this.VariableName + " already exists");
            }

            var varArray = new KirinValue(this.RawValue, reportClass);

            if (!FiMHelper.IsTypeArray(varArray.Type) && varArray.Type != KirinVariableType.STRING)
            {
                throw new FiMException("Expected type array on for-in loops");
            }

            if (varArray.Type == KirinVariableType.STRING)
            {
                string str = Convert.ToString(varArray.Value);
                reportClass.Variables.Push(new FiMVariable(this.VariableName, str[0]));
                foreach (char c in str)
                {
                    reportClass.Variables.Get(this.VariableName).Value = c;
                    var value = this.Statement.Execute(reportClass);
                    if (value != null)
                    {
                        reportClass.Variables.Pop(false);
                        return(value);
                    }
                }
            }
            else
            {
                var list       = varArray.Value as System.Collections.IDictionary;
                var sortedKeys = list.Keys.Cast <int>().ToArray().OrderBy(k => k).ToArray();

                reportClass.Variables.Push(new FiMVariable(this.VariableName, list[sortedKeys[0]]));
                foreach (int k in sortedKeys)
                {
                    reportClass.Variables.Get(this.VariableName).Value = list[k];
                    var value = this.Statement.Execute(reportClass);
                    if (value != null)
                    {
                        reportClass.Variables.Pop(false);
                        return(value);
                    }
                }
            }

            reportClass.Variables.Pop(false);
            return(null);
        }
Exemple #7
0
        public override object Execute(FiMClass reportClass)
        {
            if (!this.Complete)
            {
                throw new FiMException("Executing an incomplete switch statement");
            }

            var variable = reportClass.GetVariable(this.RawVariable);

            if (variable == null)
            {
                throw new FiMException("Varible " + this.RawVariable + " does not exist");
            }

            if (FiMHelper.IsTypeArray(variable))
            {
                throw new FiMException("Cannot use array on a switch");
            }

            Dictionary <object, string> CasesLookup = new Dictionary <object, string>();

            foreach (var key in Cases.Keys)
            {
                if (!KirinSwitchCase.IsValidPlace(key, reportClass, out object value))
                {
                    throw new FiMException("Invalid case " + key);
                }

                if (CasesLookup.Keys.Any(k => KirinValue.IsEqual(k, value)))
                {
                    throw new FiMException("Duplicate case value " + key);
                }

                CasesLookup.Add(value, key);
            }

            KirinStatement s = DefaultCase;

            if (CasesLookup.Keys.Any(k => KirinValue.IsEqual(k, variable.Value)))
            {
                s = Cases[CasesLookup.First(l => KirinValue.IsEqual(l.Key, variable.Value)).Value];
            }

            if (s != null)
            {
                return(s.Execute(reportClass));
            }
            return(null);
        }
Exemple #8
0
            public override dynamic Eval(FiMClass reportClass)
            {
                var value = new KirinValue(this.RawValue, reportClass);

                if (value.Type == KirinVariableType.STRING)
                {
                    return(Convert.ToString(value.Value));
                }

                if (value.Type == KirinVariableType.CHAR)
                {
                    return(Convert.ToString(value.Value));
                }

                if (value.Type == KirinVariableType.NUMBER)
                {
                    return(Convert.ToDouble(value.Value));
                }

                throw new FiMException("Cannot do arithmetic on a non-number value");
            }
Exemple #9
0
 public static bool IsEqual(KirinValue x, KirinValue y) => KirinValue.IsEqual(x.Value, y.Value);
Exemple #10
0
        /// <summary>
        /// Evaluates raw FiM++ string into a value
        /// </summary>
        public static object Evaluate(
            FiMClass reportClass,
            string evaluatable,
            out KirinVariableType returnedType,
            KirinVariableType?expectedType = null
            )
        {
            returnedType = KirinVariableType.UNKNOWN;

            // Nothing
            if (evaluatable == "nothing" && expectedType != null)
            {
                returnedType = (KirinVariableType)expectedType;
                return(FiMHelper.GetDefaultValue(returnedType));
            }

            // Calling an existing variable
            if (reportClass.GetVariable(evaluatable) != null)
            {
                var variable = reportClass.GetVariable(evaluatable);
                returnedType = variable.Type;
                return(variable.Value);
            }

            // Calling an existing method
            if (reportClass.GetParagraphLazy(evaluatable) != null)
            {
                KirinValue[] args  = null;
                string       pName = evaluatable;

                if (pName.Contains(KirinFunctionCall.FunctionParam))
                {
                    int pIndex = pName.IndexOf(KirinFunctionCall.FunctionParam);
                    pName = pName.Substring(0, pIndex);
                    args  = KirinFunctionCall.ParseCallArguments(
                        evaluatable.Substring(pName.Length + KirinFunctionCall.FunctionParam.Length), reportClass
                        ).ToArray();
                }

                var paragraph = reportClass.GetParagraph(pName);
                if (paragraph == null)
                {
                    throw new FiMException("Paragraph " + pName + " not found");
                }

                if (paragraph.ReturnType == KirinVariableType.UNKNOWN)
                {
                    throw new FiMException("Paragraph returns nothing");
                }
                returnedType = paragraph.ReturnType;
                return(paragraph.Execute(args));
            }

            // Array
            if (expectedType != null && FiMHelper.IsTypeArray((KirinVariableType)expectedType))
            {
                System.Collections.IDictionary dict = null;
                var args = KirinFunctionCall.ParseCallArguments(evaluatable, reportClass);

                if (!FiMHelper.IsTypeOfArray(args[0].Type, (KirinArrayType)expectedType))
                {
                    throw new FiMException("Invalid list value type");
                }
                if (!args.All(a => a.Type == args[0].Type))
                {
                    throw new FiMException("Unidentical list value type");
                }

                int i = 1;
                if (expectedType == KirinVariableType.STRING_ARRAY)
                {
                    dict = new Dictionary <int, string>();
                    args.ForEach(kv => dict.Add(i++, Convert.ToString(kv.Value)));
                }
                else if (expectedType == KirinVariableType.NUMBER_ARRAY)
                {
                    dict = new Dictionary <int, double>();
                    args.ForEach(kv => dict.Add(i++, Convert.ToDouble(kv.Value)));
                }
                else if (expectedType == KirinVariableType.BOOL_ARRAY)
                {
                    dict = new Dictionary <int, bool>();
                    args.ForEach(kv => dict.Add(i++, Convert.ToBoolean(kv.Value)));
                }

                returnedType = (KirinVariableType)expectedType;
                return(dict);
            }

            // Array index
            if (FiMHelper.ArrayIndex.IsArrayIndex(evaluatable, reportClass))
            {
                var match = FiMHelper.ArrayIndex.GetArrayIndex(evaluatable, reportClass);

                var varIndex = new KirinValue(match.RawIndex, reportClass);
                if (varIndex.Type != KirinVariableType.NUMBER)
                {
                    throw new FiMException("Invalid index value");
                }
                int index = Convert.ToInt32(varIndex.Value);

                string strVar = match.RawVariable;
                if (!reportClass.Variables.Has(strVar))
                {
                    throw new FiMException("Variable " + strVar + " does not exist");
                }
                var variable = reportClass.Variables.Get(strVar);
                if (!FiMHelper.IsTypeArray(variable.Type) && variable.Type != KirinVariableType.STRING)
                {
                    throw new FiMException("Cannot index a non-array variable");
                }

                if (variable.Type == KirinVariableType.STRING)
                {
                    returnedType = KirinVariableType.CHAR;
                    return(Convert.ToString(variable.Value)[index - 1]);
                }

                var dict = variable.Value as System.Collections.IDictionary;
                if (variable.Type == KirinVariableType.STRING_ARRAY)
                {
                    returnedType = KirinVariableType.STRING;
                    return(Convert.ToString(dict[index]));
                }
                if (variable.Type == KirinVariableType.BOOL_ARRAY)
                {
                    returnedType = KirinVariableType.BOOL;
                    return(Convert.ToBoolean(dict[index]));
                }
                if (variable.Type == KirinVariableType.NUMBER_ARRAY)
                {
                    returnedType = KirinVariableType.NUMBER;
                    return(Convert.ToDouble(dict[index]));
                }
            }

            // Arithmetic
            if (KirinArithmetic.IsArithmetic(evaluatable, out var arithmeticResult))
            {
                var arithmetic = new KirinArithmetic(arithmeticResult);
                var value      = arithmetic.GetValue(reportClass);
                returnedType = FiMHelper.AsVariableType(value.GetType());
                return(value);
            }

            // Conditional
            if (KirinConditional.IsConditional(evaluatable, out var conditionalResult))
            {
                var conditional = new KirinConditional(conditionalResult);
                returnedType = KirinVariableType.BOOL;
                return(conditional.GetValue(reportClass));
            }

            throw new FiMException("Cannot evaluate " + evaluatable);
        }
Exemple #11
0
        public override object Execute(FiMClass reportClass)
        {
            var variable = reportClass.GetVariable(this.LeftOp);

            if (variable == null)
            {
                throw new FiMException("Variable " + this.LeftOp + " does not exist");
            }
            if (!FiMHelper.IsTypeArray(variable.Type) && variable.Type != KirinVariableType.STRING)
            {
                throw new FiMException("Variable " + this.LeftOp + " is not an array");
            }

            var kIndex = new KirinValue(this.RawIndex, reportClass);

            if (kIndex.Type != KirinVariableType.NUMBER)
            {
                throw new FiMException("Invalid index " + kIndex.Value);
            }
            var iValue = Convert.ToInt32(kIndex.Value);

            var value = new KirinValue(this.RightOp, reportClass);

            if (variable.Type == KirinVariableType.STRING)
            {
                if (value.Type != KirinVariableType.CHAR)
                {
                    throw new FiMException("Invalid array modify value");
                }

                var sb = new StringBuilder(variable.Value as string);
                sb[iValue]     = (char)variable.Value;
                variable.Value = sb.ToString();
            }
            else
            {
                if (!FiMHelper.IsTypeOfArray(value.Type, (KirinArrayType)variable.Type))
                {
                    throw new FiMException("Invalid array modify value");
                }

                dynamic dict;
                int     index = Convert.ToInt32(kIndex.Value);
                if (variable.Type == KirinVariableType.STRING_ARRAY)
                {
                    dict        = variable.Value as Dictionary <int, string>;
                    dict[index] = Convert.ToString(value.Value);
                }
                else if (variable.Type == KirinVariableType.NUMBER_ARRAY)
                {
                    dict        = variable.Value as Dictionary <int, double>;
                    dict[index] = Convert.ToDouble(value.Value);
                }
                else if (variable.Type == KirinVariableType.BOOL_ARRAY)
                {
                    dict        = variable.Value as Dictionary <int, bool>;
                    dict[index] = Convert.ToBoolean(value.Value);
                }
            }

            ;
            return(null);
        }
Exemple #12
0
        public override object Execute(FiMClass reportClass)
        {
            if (reportClass.GetVariable(this.RawVariable) != null)
            {
                var variable = reportClass.GetVariable(this.RawVariable);

                if (variable.Type != KirinVariableType.NUMBER)
                {
                    if (this.Increment)
                    {
                        throw new FiMException("Cannot apply unary increment on a non-number variable");
                    }
                    else
                    {
                        throw new FiMException("Cannot apply unary decrement on a non-number variable");
                    }
                }

                if (this.Increment)
                {
                    variable.Value = Convert.ToDouble(variable.Value) + 1.0d;
                }
                else
                {
                    variable.Value = Convert.ToDouble(variable.Value) - 1.0d;
                }
            }
            else if (FiMHelper.ArrayIndex.IsArrayIndex(this.RawVariable, reportClass))
            {
                var match    = FiMHelper.ArrayIndex.GetArrayIndex(this.RawVariable, reportClass);
                var variable = reportClass.GetVariable(match.RawVariable);
                if (variable.Type != KirinVariableType.NUMBER_ARRAY)
                {
                    if (this.Increment)
                    {
                        throw new FiMException("Cannot apply unary increment on a non-number array");
                    }
                    else
                    {
                        throw new FiMException("Cannot apply unary decrement on a non-number array");
                    }
                }

                var varIndex = new KirinValue(match.RawIndex, reportClass);
                if (varIndex.Type != KirinVariableType.NUMBER)
                {
                    throw new FiMException("Invalid index value");
                }
                int index = Convert.ToInt32(varIndex.Value);

                var dict = variable.Value as Dictionary <int, double>;
                if (!dict.ContainsKey(index))
                {
                    dict[index] = 0.0d;
                }

                if (this.Increment)
                {
                    dict[index] += 1.0d;
                }
                else
                {
                    dict[index] -= 1.0d;
                }
            }
            else
            {
                throw new FiMException("Variable " + this.RawVariable + " does not exist");
            }

            return(null);
        }
Exemple #13
0
            public override object Eval(FiMClass reportClass)
            {
                var value = new KirinValue(this.RawValue, reportClass);

                return(value.Value);
            }