Exemple #1
0
        public bool Execute(string startFunction)
        {
            try
            {
                SourceCode.Function f = src.GetFunction(startFunction);
                if (f == null)
                {
                    InterpreterErrors.DisplayError(3);
                    return(false);
                }

                Execute(f, new List <Variable>(), f.startLine);

                return(true);
            }
            catch (System.Exception e)
            {
                Debug.Exception(e);
                return(false);
            }
        }
Exemple #2
0
        void ExecuteFunctions(ref string line, int lineNumber)
        {
            int level = 0;

            string name       = "";
            int    startPoint = -1;

            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == '(')
                {
                    startPoint = i;
                    if (level == 0)
                    {
                        for (int j = i - 1; j >= 0; j--)
                        {
                            if (line[j] == ' ' || line[j] == '\n' || line[j] == '\t')
                            {
                                break;
                            }
                            else
                            {
                                name = name.Insert(0, line.Substring(j, 1));
                            }
                        }

                        //Debug.Log("Function name: \"" + name + "\"");
                    }
                    level++;
                }

                if (line[i] == ')')
                {
                    level--;
                    if (level == 0)
                    {
                        List <Variable> vars = new List <Variable>();

                        string arg = "";
                        for (int j = startPoint + 1; j < i; j++)
                        {
                            if (line[j] == ',')
                            {
                                vars.Add(GetVariable(arg, lineNumber));
                                arg = "";
                                continue;
                            }

                            arg += line[j];
                        }
                        if (arg.Length != 0)
                        {
                            vars.Add(GetVariable(arg, lineNumber));
                        }



                        Variable result = Functions.Execute(name, vars.ToArray());
                        if (result.IsError())
                        {
                            Debug.LogError("Error executing: " + result.value);
                            return;
                        }

                        name = "";
                    }
                }
            }

            if (level != 0)
            {
                InterpreterErrors.DisplayError(0, lineNumber);
                return;
            }
        }
Exemple #3
0
            List <Function> FindFunctions(string src)
            {
                List <Function> f = new List <Function>();



                string[] keys = { "fun", "def", "function" };

                for (int i = 0; i < keys.Length; i++)
                {
                    int it = 0;
                    while (src.Contains(keys[i]))
                    {
                        /*if (it > 10000) // Failsafe
                         * {
                         *  Debug.LogError("Failsafe");
                         *  break;
                         * }*/

                        //it++;

                        int level   = 0;
                        int brLevel = 0;

                        int pos = src.IndexOf(keys[i]);

                        string name    = "";
                        string args    = "";
                        string content = "";

                        int state = 0;

                        int startPoint = -1;
                        for (int j = 0; j < src.Length; j++)
                        {
                            if (state == 0) // Name
                            {
                                if (src[j] == '(')
                                {
                                    startPoint = GetLine(j);

                                    state = 1;
                                    level = 1;

                                    j++;
                                    while (j == '\n')
                                    {
                                        j++;
                                    }
                                    j--;
                                    continue;
                                }

                                name += src[j];
                            }
                            else if (state == 1) // Args
                            {
                                if (level == 0)
                                {
                                    //Debug.LogError("Error in line " + GetLine(j) + " - expected closing bracket");
                                    InterpreterErrors.DisplayError(1, GetLine(j));
                                    return(null);
                                }

                                if (src[j] == '(')
                                {
                                    level++;

                                    args += src[j];
                                    continue;
                                }
                                if (src[j] == ')')
                                {
                                    level--;

                                    if (level == 0)
                                    {
                                        state = 2;
                                        continue;
                                    }
                                }

                                if (src[j] == '{' || src[j] == '}')
                                {
                                    InterpreterErrors.DisplayError(2, GetLine(j));
                                    return(null);
                                }

                                args += src[j];
                            }
                            else // Content
                            {
                                if (src[j] == '{')
                                {
                                    brLevel++;

                                    if (brLevel > 1)
                                    {
                                        InterpreterErrors.DisplayError(2, GetLine(j));
                                        return(null);
                                    }

                                    if (brLevel == 1) // We don't want { in our src
                                    {
                                        j++;
                                        while (j == '\n')
                                        {
                                            j++;
                                        }
                                        j--;

                                        continue;
                                    }
                                }

                                if (src[j] == '}')
                                {
                                    brLevel--;

                                    if (brLevel == 0)
                                    {
                                        src = src.Remove(0, j);

                                        break;
                                    }
                                }

                                content += src[j];
                            }
                        }

                        f.Add(new Function(name.Substring(keys[i].Length + 1), content, startPoint + 1));
                    }
                }

                src = og;

                return(f);
            }