Example #1
0
        public Compiler(string filename, string file, List <IBaseFunction> predefined)
        {
            LoopTracerStack = new List <LoopTracer>();
            FunctionStack.Clear();//clear this every run
            Files = new Dictionary <string, string>();
            Files.Add(filename, file);
            var onefile = ParseImports(file);

            _compileStack = ParseScopes(onefile, predefined);
            _compileStack.AddRange(predefined);
            _compileStack.AddRange(importFStack);
            ExtensionStack.AddRange(importEStack);
            StartScope(_compileStack);
        }
Example #2
0
        private List <EDefinition> ParseExtensions(string value)
        {
            List <EDefinition> temp = new List <EDefinition>();

            if (value.Contains("<-"))
            {
                string val        = value;
                var    firstSplit = val.Split(new string[] { "<-" }, StringSplitOptions.None);
                for (int i = 0; i < firstSplit.Length; i++)
                {
                    if (i == 0)
                    {
                        continue;
                    }
                    var first       = firstSplit[i];
                    var secondSplit = first.Split(new string[] { "->" }, StringSplitOptions.None);
                    if (secondSplit.Length != 2)
                    {
                        Compiler.ExceptionListener.Throw("[160]Extensions must provide arguments", ExceptionType.SyntaxException);
                        return(null);
                    }
                    var original = ExtensionStack.First(secondSplit[0]);
                    if (original == null)
                    {
                        Compiler.ExceptionListener.Throw($"[310]Cannot find extension [{secondSplit[0]}]");
                        return(null);
                    }
                    //Console.WriteLine(secondSplit[0] + " " + secondSplit[1]);
                    var clone = DeepCopy(original);
                    var param = GetTokens(new string[] { secondSplit[1].Replace("|", "") });
                    if (param.Count != 1)
                    {
                        Compiler.ExceptionListener.Throw("[166]Extensions must provide arguments", ExceptionType.SyntaxException);
                        return(null);
                    }
                    if (clone.Invoking)
                    {
                        var invokeFuncName = param[0].ToString();
                        if (invokeFuncName.Contains("AnonymousFunction"))
                        {
                            var functionToInvoke = FunctionStack.First(invokeFuncName.Replace("\"", ""));

                            if (functionToInvoke != null)
                            {
                                //Console.WriteLine($"n: {functionToInvoke.Name} exp: {string.Join(",",functionToInvoke.ExpectedArgs)}");
                                var           args    = GetTokens(functionToInvoke.ExpectedArgs, true, true);
                                List <string> argsarr = new List <string>();
                                foreach (var x in args)
                                {
                                    argsarr.Add(x.ToString());
                                }
                                clone.SetInvokeProperties(argsarr.ToArray());
                            }
                        }
                    }
                    clone.SetArguments(param[0].ToString());
                    temp.Add(clone);
                }
            }
            return(temp);
        }
Example #3
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);
        }