Exemplo n.º 1
0
        //this is the constructor used when function is an override
        public AnonymousFunction(string value, List <IBaseFunction> predefined) : this()
        {
            Override = true;
            Name     = value.Split('.')[1].Split('(')[0];

            var b = predefined.FirstOrDefault(f => f.Name == Name);

            if (b == null)
            {
                Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.CompilerException, $"Unexpected error. Function failed to override: {Name}.", value));
            }
            if (b.Sealed == true)
            {
                Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.CompilerException,
                                                                      $"Invalid Operation. Cannot override Sealed function: {Name}.", value));
            }
            Base = b;
            //get top level anonymous functions before everything else
            var anonRegex        = new Regex(Compiler.ScopeRegex(@"=>"), RegexOptions.IgnorePatternWhitespace);
            var anonRegexMatches = anonRegex.Matches(value);

            foreach (var a in anonRegexMatches)
            {
                var func = new AnonymousFunction(a.ToString(), true, _base);
                FunctionStack.Add(func);
                value = value.Replace(a.ToString(), $"\"{func.Name}\"");
            }
            //
            Value        = value;
            ExpectedArgs = value.Split('(')[1].Split(')')[0].Split(',');
            ParseDirectives(value);
        }
Exemplo n.º 2
0
        //standard constructor
        public AnonymousFunction(string value) : this()
        {
            Name = value.Split('.')[1].Split('(')[0];
            var b = Compiler.PredefinedList.FirstOrDefault(f => f.Name == Name);

            if (b != null)
            {
                if (b.Sealed == true)
                {
                    Compiler.ExceptionListener.Throw(new ExceptionHandler(ExceptionType.CompilerException,
                                                                          $"Invalid Operation. Cannot create a new instance of a Sealed function: {Name}.", value));
                }
            }
            //get top level anonymous functions before everything else
            var anonRegex        = new Regex(Compiler.ScopeRegex(@"=>"), RegexOptions.IgnorePatternWhitespace);
            var anonRegexMatches = anonRegex.Matches(value);

            foreach (var a in anonRegexMatches)
            {
                var func = new AnonymousFunction(a.ToString(), true, _base);
                func.Base = Base;
                FunctionStack.Add(func);
                value = value.Replace(a.ToString(), $"\"{func.Name}\"");
            }
            //
            Value        = value;
            ExpectedArgs = value.Split('(')[1].Split(')')[0].Split(',');
            ParseDirectives(value);
        }
Exemplo n.º 3
0
        private List <IBaseFunction> GetScopes(string file, List <IBaseFunction> predefined)
        {
            var temp = new List <IBaseFunction>();
            //remove all comments
            var commRegex = new Regex(@"#([^\n]+)$", RegexOptions.Multiline);

            file = commRegex.Replace(file, "");

            //add functions first
            var scopeRegex = new Regex(ScopeRegex(@"\bfunction\.\b"), RegexOptions.IgnorePatternWhitespace);
            var scopes     = scopeRegex.Matches(file);

            foreach (var s in scopes)
            {
                temp.Add(new AnonymousFunction(s.ToString()));
            }
            //add inherits second
            var _inheritStack = new List <IBaseFunction>();
            var inheritRegex  = new Regex(ScopeRegex(@"\boverride\.\b"), RegexOptions.IgnorePatternWhitespace);
            var inherits      = inheritRegex.Matches(file);

            foreach (var i in inherits)
            {
                var obj = new AnonymousFunction(i.ToString(), predefined);
                _inheritStack.Add(obj);
            }
            //add async
            var _asyncStack = new List <IBaseFunction>();
            var asyncRegex  = new Regex(ScopeRegex(@"\basync\.\b"), RegexOptions.IgnorePatternWhitespace);
            var asyncs      = asyncRegex.Matches(file);

            foreach (var i in asyncs)
            {
                var obj = new AnonymousFunction(i.ToString());
                obj.Async = true;
                _asyncStack.Add(obj);
            }
            //add imports
            var imports        = new List <IBaseFunction>();
            var splitEndImport = file.Split(new string[] { "@end" }, StringSplitOptions.None);

            if (splitEndImport.Length > 1)
            {
                var splitImport = splitEndImport[0].Split(new string[] { "@import " }, StringSplitOptions.None);
                imports.AddRange(GetImports(splitImport, predefined));
            }
            temp.AddRange(_inheritStack);
            temp.AddRange(_asyncStack);
            temp.AddRange(imports);
            return(temp);
        }
Exemplo n.º 4
0
        //this constructor is when function is anonomysly named
        public AnonymousFunction(string value, bool anon, IBaseFunction callerBase) : this()
        {
            Base        = callerBase;
            IsAnonymous = true;
            //get top level anonymous functions before everything else
            value = value.Substring(1);
            var anonRegex        = new Regex(Compiler.ScopeRegex(@"=>"), RegexOptions.IgnorePatternWhitespace);
            var anonRegexMatches = anonRegex.Matches(value);

            foreach (var a in anonRegexMatches)
            {
                var func = new AnonymousFunction(a.ToString(), true, _base);
                FunctionStack.Add(func);
                value = value.Replace(a.ToString(), $"\"{func.Name}\"");
            }
            Value        = value;
            Name         = "AnonymousFunction" + Compiler.AnonymousFunctionIndex;
            ExpectedArgs = value.Split('(')[1].Split(')')[0].Split(',');
            ParseDirectives(value);
            //Name = value.Split('.')[1].Split('(')[0];
        }
Exemplo n.º 5
0
        //the construction for custom extensions
        public AnonymousFunction(string value, CustomExtension parent) : this()
        {
            Name = value.Split('.')[1].Split('(')[0];

            //get top level anonymous functions before everything else
            var anonRegex        = new Regex(Compiler.ScopeRegex(@"=>"), RegexOptions.IgnorePatternWhitespace);
            var anonRegexMatches = anonRegex.Matches(value);

            foreach (var a in anonRegexMatches)
            {
                var func = new AnonymousFunction(a.ToString(), true, _base);
                func.Base = Base;
                FunctionStack.Add(func);
                value = value.Replace(a.ToString(), $"\"{func.Name}\"");
            }
            //
            Value        = value;
            ExpectedArgs = value.Split('(')[1].Split(')')[0].Split(',');
            var type = ExpectedArgs.ElementAtOrDefault(0);

            if (type != null && type == "this variable")
            {
                parent.SetProperties(Name, new string[] { }, false, false, true, new string[] { });
            }
            else if (type != null && type == "this function")
            {
                parent.SetProperties(Name, new string[] { }, false, false, false, new string[] { });
            }
            else
            {
                Compiler.ExceptionListener.Throw("[287]Custom extension must have input parameter of `this variable` or `this function`");
            }
            //replace this from the parameter, so it can be called by just `function` or `variable` at runtime
            //the `this` is only there for definition, to direct the extension parser to look for this extension
            //in the variable lot or not.
            ExpectedArgs[0] = ExpectedArgs[0].Replace("this ", "");
            ParseDirectives(value);
        }
Exemplo n.º 6
0
        private List <IBaseFunction> ParseScopes(string file, List <IBaseFunction> predefined)
        {
            var temp = new List <IBaseFunction>();
            //remove all comments
            var commRegex = new Regex(@"#([^\n]+)$", RegexOptions.Multiline);

            file = commRegex.Replace(file, "");

            //add functions first
            var _functionStack = new List <IBaseFunction>();
            var scopeRegex     = new Regex(ScopeRegex(@"\bfunction\.\b"), RegexOptions.IgnorePatternWhitespace);
            var scopes         = scopeRegex.Matches(file);

            foreach (var s in scopes)
            {
                _functionStack.Add(new AnonymousFunction(s.ToString()));
            }
            //add inherits second
            var _inheritStack     = new List <IBaseFunction>();
            var inheritRegex      = new Regex(ScopeRegex(@"\boverride\.\b"), RegexOptions.IgnorePatternWhitespace);
            var inherits          = inheritRegex.Matches(file);
            var tempfunctionstack = new List <IBaseFunction>();

            tempfunctionstack.AddRange(_functionStack);
            tempfunctionstack.AddRange(predefined);
            for (var i = inherits.Count - 1; i >= 0; i--)
            {
                var inherit = inherits[i];
                var obj     = new AnonymousFunction(inherit.ToString(), tempfunctionstack);
                _inheritStack.Add(obj);
                tempfunctionstack.Insert(0, obj);
            }
            //add custom extensions
            var _extStack = new List <EDefinition>();
            var extRegex  = new Regex(ScopeRegex(@"\bextension\.\b"), RegexOptions.IgnorePatternWhitespace);
            var exts      = extRegex.Matches(file);

            for (var i = exts.Count - 1; i >= 0; i--)
            {
                var ext  = exts[i];
                var cext = new CustomExtension();
                cext.FunctionReference = new AnonymousFunction(ext.ToString(), cext);
                _extStack.Add(cext);
            }
            ExtensionStack.AddRange(_extStack);
            //add async
            var _asyncStack = new List <IBaseFunction>();
            var asyncRegex  = new Regex(ScopeRegex(@"\basync\.\b"), RegexOptions.IgnorePatternWhitespace);
            var asyncs      = asyncRegex.Matches(file);

            foreach (var i in asyncs)
            {
                var obj = new AnonymousFunction(i.ToString());
                obj.Async = true;
                _asyncStack.Add(obj);
            }
            temp.AddRange(_inheritStack.Reverse <IBaseFunction>());
            temp.AddRange(_functionStack);
            temp.AddRange(_asyncStack);
            return(temp);
        }