Пример #1
0
        private IObject ParsString(Dictionary<string, IObject> scope, string str)
        {
            if (str == null || str == "" || str == " ")
                return new IObject();

            if (SimpleMethodCall.IsMatch(str))
            {
                string methodName = SimpleMethodCall.Match(str).Groups[1].Value;
                if (scope.ContainsKey(methodName))
                {
                    string methodArgsStr = SimpleMethodCall.Match(str).Groups[2].Value;
                    string[] inParams = Tools.SplitArgs(methodArgsStr);
                    List<IObject> inIObjectParams = new List<IObject>();

                    for (int i = 0; i < inParams.Length; ++i)
                    {
                        IObject obj = ParsString(scope, inParams[i]);
                        if (!(obj.IType == IObjectType.I_Error || obj.IType == IObjectType.I_Null))
                            inIObjectParams.Add(obj);
                    }

                    if (scope.ContainsKey(methodName))
                    {
                        IObject methodObj = GetIObjectFromScope(scope, methodName);
                        IObject ret = methodObj.MethodOperator(inIObjectParams.ToArray());
                        return ret;
                    }
                }
            }
            else if(SimpleString.IsMatch(str))
            {
                IObject ret = new I_String(SimpleString.Match(str).Groups[1].Value);
                return ret;
            }
            else if (SimpleVariable.IsMatch(str))
            {
                string variableName = SimpleVariable.Match(str).Groups[1].Value;

                if (scope.ContainsKey(variableName))
                {
                    IObject ret = GetIObjectFromScope(scope, variableName);
                    return ret;
                }

                if (userdefinedIObjects.ContainsKey(variableName))
                {
                    IObject ret = userdefinedIObjects[variableName];
                    return ret;
                }
            }
            else if (SimpleInteger.IsMatch(str))
            {
                string integerValueStr = SimpleInteger.Match(str).Groups[1].Value;
                return new I_Int(integerValueStr);
            }
            else if (SimpleFloat.IsMatch(str))
            {
                string floatValueStr = SimpleFloat.Match(str).Groups[1].Value;
                floatValueStr = floatValueStr.Replace('.', ',');

                if (floatValueStr.Length < 10)
                {
                    IObject ret = new I_Float(Double.Parse(floatValueStr));
                    return ret;
                }
                else
                {
                    try
                    {
                        IObject ret = new I_Float(double.Parse(floatValueStr));
                        return ret;
                    }
                    catch
                    {
                        IObject ret = new I_Error("Float to big. Must be in range (10^k where |k| < 308).");
                        return ret;
                    }
                }

            }
            else if (SimpleDeclaration.IsMatch(str))
            {
                string type = SimpleDeclaration.Match(str).Groups[1].Value;
                string name = SimpleDeclaration.Match(str).Groups[2].Value;
                string operand = SimpleDeclaration.Match(str).Groups[3].Value;
                string value = SimpleDeclaration.Match(str).Groups[4].Value;

                if (!types.ContainsKey(type))
                    return new I_Error("Type not recognized.");

                IObject_Constructor ioc = types[type];

                if (value == "")
                {
                    if (operand == "")
                    {
                        IObject newInstanse = ioc.GetNewInstans();
                        IObject ret = AddOrUpdateUserDefinedIObject(name, newInstanse);
                        return ret;
                    }
                    else
                    {
                        variableBracket.Name = name;
                        variableBracket.IType = ioc.GetObjectType();

                        return new IObject();
                    }
                }
                else
                {
                    IObject getValue = ParsString(scope, value);
                    IObject[] objInParams = { getValue };
                    IObject newValue = ioc.GetNewInstans(objInParams);

                    if (newValue.IType == IObjectType.I_Error)
                    {
                        IObject ret = newValue;
                        return ret;
                    }
                    else
                    {
                        IObject ret = AddOrUpdateUserDefinedIObject(name, newValue);
                        return ret;
                    }
                }
            }
            else if (SimpleEqual.IsMatch(str))
            {
                string variableName = SimpleEqual.Match(str).Groups[1].Value;
                string variableValueStr = SimpleEqual.Match(str).Groups[2].Value;

                if (userdefinedIObjects.ContainsKey(variableName))
                {
                    // The variable exists, need to do a new version of it.
                    IObject compiledValue = ParsString(scope, variableValueStr);
                    IObject previousVersionValue = GetUserDefinedIObject(variableName);
                    IObject newVersionCopy;

                    switch (compiledValue.IType)
                    {
                        case IObjectType.I_Null:
                            newVersionCopy = new IObject();
                            break;
                        case IObjectType.I_Error:
                            newVersionCopy = new I_Error(((I_Error)compiledValue));
                            break;
                        default:
                            newVersionCopy = iTypes.GetConstructor(compiledValue.IType).GetNewInstans(compiledValue);
                            break;
                    }

                    AddOrUpdateUserDefinedIObject(variableName, newVersionCopy);
                    newVersionCopy.EqualOperator(compiledValue);
                    return newVersionCopy;
                }
                else
                {
                    IObject compiledValue = ParsString(scope, variableValueStr);
                    AddOrUpdateUserDefinedIObject(variableName, compiledValue);
                    return compiledValue;
                }
            }
            else if (SimpleEqualBracket.IsMatch(str))
            {
                string variableName = SimpleEqualBracket.Match(str).Groups[1].Value;
                variableBracket.Name = variableName;

                return new IObject();
            }
            else if (SimpleDotSeparator.IsMatch(str))
            {
                string memberName = SimpleDotSeparator.Match(str).Groups[1].Value;
                string rest = SimpleDotSeparator.Match(str).Groups[2].Value;

                Dictionary<string, IObject> newScope = ParsString(scope, memberName).GetMembers();

                if (newScope.Count == 0)
                {
                    newScope = ParsString(preDefinedIObjects, memberName).GetMembers();
                }

                if (newScope.Count == 0 && types.ContainsKey(memberName))
                {
                    newScope = types[memberName].GetMembers();
                }

                IObject ret = ParsString(newScope, rest);
                return ret;
            }

            if (operandTree.ContainOperands(str))
            {
                return operandTree.ParseOperands(str, scope);
            }

            if (SimpleContainsParentheses.IsMatch(str))
            {
                IObject ret = ParsParentheses(scope, str);
                if (ret.IType != IObjectType.I_Null)
                {
                    return ret;
                }
            }

            IObject finalReturn = new I_Error("Command not understood (" + str + ")");
            return finalReturn;
        }
Пример #2
0
 public I_Number(I_Float v)
 {
     value = v;
     iType = IObjectType.I_Number;
 }