Exemple #1
0
        public static Any[] ParseParameters(string s_parameters)
        {
            if (s_parameters == "()")
            {
                return(new Any[0]);                // no parameters
            }
            List <Any> parameters = new List <Any>();

            s_parameters = s_parameters.Substring(1, s_parameters.Length - 2);            // remove parentheses

            s_parameters = Utils.ReplaceBetween(s_parameters, ',', '(', ')', (char)0x1a); // prevent splitting of commas in nested parentheses

            string[] splitParams = Regex.Split(s_parameters, ",");                        // split by each comma/space (each item is a parameter)

            for (int i = 0; i < splitParams.Length; i++)
            {
                splitParams[i] = splitParams[i].Replace(((char)0x1a).ToString(), ",");

                if (splitParams[i][0] == ',')
                {
                    splitParams[i] = splitParams[i].Substring(1, splitParams[i].Length - 1);
                }
            }             // splitting has been done so we can revert placeholders back

            for (int i = 0; i < splitParams.Length; i++)
            {
                string parameter = splitParams[i];

                while (parameter.Length > 0 && parameter[0] == ' ')
                {
                    parameter = parameter.Remove(0, 1);                     // remove beginning spaces
                }
                while (parameter.Length > 0 && parameter[parameter.Length - 1] == ' ')
                {
                    parameter = parameter.Remove(parameter.Length - 1, 1);                     // remove trailing spaces
                }
                string cleanedUp;
                if ((parameter[0] == '\"' || parameter[0] == '\'') && (parameter[parameter.Length - 1] == '\"' || parameter[parameter.Length - 1] == '\''))
                {
                    cleanedUp = parameter.Substring(1, parameter.Length - 2);                     // remove quotes/apostrophes
                }
                else
                {
                    cleanedUp = parameter;
                }

                Token token = null;

                if (parameter.OccurrencesOf("\"") <= 2)
                {
                    token = Lexer.ParseToken(parameter, false);                     // a flaw in the parsing function for strings would take a string chain if it starts and ends with a string as 1 string (this is a janky workaround)
                }
                Type vtype = null;

                if (token == null)
                {
                    // unknown identifier, could be an equation waiting to be solved
                    Line tl = new Line(Lexer.ParseLine(parameter));
                    parameters.Add(tl.Execute());
                    continue;
                }
                else if (token.type == TokenType.VariableIdentifier)
                {
                    Variable _var = Interpreter.Vars[token.identifier];
                    parameters.Add(new Any(_var.Type, _var.Value));
                    continue;
                }
                else if (token.type == TokenType.FunctionCall)
                {
                    while (token.identifier[0] < 'A' || token.identifier[0] > 'z')
                    {
                        token.identifier = token.identifier.Remove(0, 1);                         // i don't remember why this is here tbh
                    }
                    BaseFunction func = Interpreter.Functions[token.identifier];
                    if (func.ReturnType == Types.Void)
                    {
                        throw new BaseException("Cannot pass void function as a parameter");
                    }
                    parameters.Add(new Any(func.ReturnType, func.Invoke(token.identifier).Value));
                    continue;
                }
                else if (token.type == TokenType.FunctionChain)
                {
                    parameters.Add(FunctionChain.Evaluate(token.identifier));
                    continue;
                }
                else if (token.type == TokenType.StringLiteral || token.type == TokenType.CharacterLiteral)
                {
                    switch (token.type)
                    {
                    case TokenType.StringLiteral:
                        vtype = Types.String;
                        break;

                    case TokenType.CharacterLiteral:
                        vtype = Types.Character;
                        break;
                    }

                    if (cleanedUp.Length > 2)
                    {
                        cleanedUp = cleanedUp.FromBase64().Replace("\\\"", "\"").Replace("\\'", "\'");                         // convert encoded base64 text given it's not an empty literal & do literal conversions (\" -> ") and (\' -> ')
                    }
                }
                else if (token.type == TokenType.Integer64Literal || token.type == TokenType.IntegerLiteral || token.type == TokenType.DecimalLiteral)
                {
                    if (Interpreter.Definitions.ContainsKey(cleanedUp))
                    {
                        cleanedUp = Interpreter.Definitions[cleanedUp].To;
                    }

                    // get the appropriate type for the literal
                    switch (token.type)
                    {
                    case TokenType.DecimalLiteral:
                        vtype = Types.Decimal;
                        break;

                    case TokenType.IntegerLiteral:
                        vtype = Types.Integer;
                        if (Utils.IsHexadecimal(cleanedUp))
                        {
                            cleanedUp = int.Parse(cleanedUp.Substring(2), NumberStyles.HexNumber).ToString();
                        }
                        break;

                    case TokenType.Integer64Literal:
                        vtype = Types.Integer64;
                        if (Utils.IsHexadecimal(cleanedUp))
                        {
                            cleanedUp = long.Parse(cleanedUp.Substring(2), NumberStyles.HexNumber).ToString();
                        }
                        break;
                    }
                }
                else if (token.type == TokenType.BooleanLiteral)
                {
                    vtype = Types.Boolean;
                }

                if (vtype == null)
                {
                    throw new BaseException("Unknown type passed as parameter: " + token.type);
                }

                parameters.Add(new Any(vtype, Convert.ChangeType(cleanedUp, vtype.CSharpType)));
            }

            return(parameters.ToArray());
        }