Esempio n. 1
0
 public void SetInvokeProperties(string[] args, List <Token> vars, List <Token> oldargs)
 {
     invokeProperties = args;
     //if (LocalVariables == null)
     //trying emptying local vars each invoke, it might break things tho
     LocalVariables = new TokenStack();
     vars.RemoveAll(r => r.Name == "");
     oldargs.RemoveAll(r => r.Name == "");
     LocalVariables.AddRange(vars);
     LocalVariables.AddRange(oldargs);//add the parameters from the calling function to this functions local var stack
 }
Esempio n. 2
0
        public virtual void TryParse(TFunction caller)
        {
            ReturnBubble = null;
            ReturnFlag   = false;
            if (caller != null)
            {
                BlindExecute = caller.BlindExecute;
                Tracer       = caller.Tracer;
                Caller       = caller;
            }
            var findFor = Extensions.FirstOrDefault(f => f.Name == "For") as ExtensionFor;

            if (findFor != null)
            {
                //if for extension exists, reroutes this tryparse method to the loop version without the for check
                ForExtension(caller, findFor);
                return;
            }
            //combine expected args and given args and add them to variabel pool
            if (caller != null && caller.Arguments != null && ExpectedArgs != null && ExpectedArgs.Length > 0)
            {
                ProvidedArgs = new TokenStack();
                var args = caller.ReturnArgsArray();
                if (ExpectedArgs.Length > 0)
                {
                    for (var i = 0; i < ExpectedArgs.Length; i++)
                    {
                        var exp = ExpectedArgs[i].Replace("var ", "").Replace(" ", "");
                        if (args.ElementAtOrDefault(i) == null)
                        {
                            ProvidedArgs.Add(new Token(exp, "null", caller.Line));
                        }
                        else
                        {
                            ProvidedArgs.Add(new Token(exp, args[i], caller.Line));
                        }
                    }
                }
            }
            var guts  = Value.Split('{')[1].Split('}');
            var lines = guts[0].Split(';');

            foreach (var l in lines)
            {
                new Line(l, this);
            }
            //clear local var stack after use
            LocalVariables = new TokenStack();
        }
Esempio n. 3
0
 public TokenParser(List <IBaseFunction> functionList)
 {
     CancellationTokenSource = new CancellationTokenSource();
     FunctionStack.AddRange(functionList);
     AnonymousTokens = new TokenStack();
     GlobalVariables = new TokenStack();
     GlobalVariables.AddRange(new List <Token>()
     {
         new Token("DateTime", () => { return(DateTime.Now.ToString()); }, "{0}", locked: true),
         new Token("Date", () => { return(DateTime.Now.ToShortDateString()); }, "{0}", locked: true),
         new Token("Time", () => { return(DateTime.Now.ToShortTimeString()); }, "{0}", locked: true),
         new TArray("GetVersion", StrVersion(), "{0}", locked: true),
         new Token("null", "null", "{0}", locked: true),
         new Token("True", "True", "{0}", locked: true),
         new Token("true", "true", "{0}", locked: true),
         new Token("False", "False", "{0}", locked: true),
         new Token("false", "false", "{0}", locked: true)
     });
     StartParse();
 }
Esempio n. 4
0
        //this overload is when the function is called with the for extension
        public virtual void TryParse(TFunction caller, bool forFlag)
        {
            ReturnBubble = null;
            ReturnFlag   = false;
            if (caller != null)
            {
                BlindExecute = caller.BlindExecute;
                Tracer       = caller.Tracer;
                Caller       = caller;
            }
            //combine expected args and given args and add them to variabel pool
            if (caller != null && caller.Arguments != null && ExpectedArgs != null && ExpectedArgs.Length > 0)
            {
                ProvidedArgs = new TokenStack();
                var args = caller.ReturnArgsArray();
                if (ExpectedArgs.Length > 0)
                {
                    for (var i = 0; i < ExpectedArgs.Length; i++)
                    {
                        var exp = ExpectedArgs[i].Replace("var ", "").Replace(" ", "");
                        if (args.ElementAtOrDefault(i) == null)
                        {
                            ProvidedArgs.Add(new Token(exp, "null", caller.Line));
                        }
                        else
                        {
                            ProvidedArgs.Add(new Token(exp, args[i], caller.Line));
                        }
                    }
                }
            }
            var guts  = Value.Split('{')[1].Split('}');
            var lines = guts[0].Split(';');

            foreach (var l in lines)
            {
                new Line(l, this);
            }
            //clear local var stack after use
            LocalVariables = new TokenStack();
        }
Esempio n. 5
0
 public AnonymousFunction()
 {
     ProvidedArgs   = new TokenStack();
     LocalVariables = new TokenStack();
     UID            = GetUID();
 }
Esempio n. 6
0
        private string EvaluateVar(string value)
        {
            //get the var scope
            TokenStack varList = null;

            if (value.Contains("$var%"))
            {
                varList = TokenParser.GlobalVariables;
            }
            else if (value.Contains("var%"))
            {
                varList = _reference.LocalVariables;
            }
            if (varList == null)
            {
                Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                      $"[244]Unexpected error occured.", Value));
                return(null);
            }
            //assign based on operator

            var strip = value.Replace("$", "").Replace("var%", "");

            string[] assign = default(string[]);
            if (strip.Contains("++"))
            {
                assign = strip.Split(new string[] { "++" }, StringSplitOptions.None);
            }
            else if (strip.Contains("--"))
            {
                assign = strip.Split(new string[] { "--" }, StringSplitOptions.None);
            }
            else if (strip.Contains("+="))
            {
                assign = strip.Split(new string[] { "+=" }, StringSplitOptions.None);
            }
            else if (strip.Contains("-="))
            {
                assign = strip.Split(new string[] { "-=" }, StringSplitOptions.None);
            }
            else if (strip.Contains("="))
            {
                assign = strip.Split(new string[] { "=" }, StringSplitOptions.None);
            }

            //get the left hand
            var leftHand = assign[0].Replace(" ", "");
            var varRef   = varList.First(leftHand);

            if (varRef != null && varRef.Locked)
            {
                Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                      $"[282]Cannot re-assign a sealed variable!", Value));
                return(null);
            }
            //one sided assignment
            if (strip.Contains("++") || strip.Contains("--"))
            {
                if (varRef == null)
                {
                    Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                          $"[269]Cannot find the left hand variable.", Value));
                    return(null);
                }
                double numOut = 0;
                double.TryParse(varRef.ToString(), out numOut);
                if (strip.Contains("++"))
                {
                    numOut++;
                }
                else
                {
                    numOut--;
                }
                varRef.SetValue(numOut.ToString());
                return("");
            }
            Token token     = null;
            var   rightHand = assign[1].Replace(" ", "");
            //if (rightHand.Contains('+'))
            //{
            var    parts  = rightHand.Split('+');
            string output = "";

            foreach (var p in parts)
            {
                var x = p;
                if (x.Contains("->"))
                {
                    var fext        = ParseExtensions(x);
                    var fcheckSplit = x.Split(new string[] { "->" }, StringSplitOptions.None);
                    var fcheck      = FunctionStack.First(fcheckSplit[0]);
                    if (fcheck != null)
                    {
                        x = ParseFunctions(x, fext);
                    }
                }
                if (x.Contains("<-"))
                {
                    x = EvaluateVarExtensions(x);
                }
                if (x == null || x == "" || x == " ")
                {
                    Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                          $"[688]Right hand must be a value.", Value));
                    return(null);
                }
                var prentoken = GetTokens(new string[] { x });
                if (prentoken == null)
                {
                    return(null);
                }
                var ntoken = prentoken.ElementAtOrDefault(0);
                if (ntoken == null)
                {
                    Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                          $"[692]Right hand must be a value.", Value));
                    return(null);
                }
                output += ntoken.ToString();
            }
            token     = new Token("concatination", output, Value);
            rightHand = output;
            //}
            if (token == null)
            {
                Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                      $"[699]Right hand must be a value.", Value));
                return(null);
            }
            if (strip.Contains("+=") || strip.Contains("-="))
            {
                if (varRef == null)
                {
                    Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                          $"[291]Cannot find the left hand variable.", Value));
                    return(null);
                }
                //check if number and apply the change
                double leftNumOut  = 0;
                double rightNumOut = 0;

                //check if token is a number too
                var nofailRight = double.TryParse(token.ToString(), out rightNumOut);
                var nofailLeft  = double.TryParse(varRef.ToString(), out leftNumOut);
                if (nofailLeft && nofailRight)
                {
                    if (strip.Contains("+="))
                    {
                        leftNumOut += rightNumOut;
                    }
                    else
                    {
                        leftNumOut -= rightNumOut;
                    }
                    varRef.SetValue(leftNumOut.ToString());
                }
                else//one or both arent numbers, which means concatenation intead of incrementation.
                {
                    var str = varRef.ToString();
                    if (strip.Contains("+="))
                    {
                        str += token.ToString();
                    }
                    else
                    {
                        Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.SyntaxException,
                                                                              "[314]Cannot apply the operand -= with type string.", Value));
                        return(null);
                    }
                    varRef.SetValue(str);
                }
                return("");
            }
            if (strip.Contains("="))
            {
                if (varRef != null)
                {
                    varRef.SetValue(token.ToString());
                }
                else
                {
                    varList.Add(new Token(leftHand, token.ToString(), Value));
                }
                return("");
            }
            Compiler.ExceptionListener.Throw("[330]Unknown error with assignment.", ExceptionType.SyntaxException, Value);
            return(null);
        }