Example #1
0
        private static void WriteParser(Dictionary <string, string> syntaxDict, Dictionary <string, string> defDict, Dictionary <string, int> expDict, Dictionary <string, string> tempDict)
        {
            var syntaxList = new List <SyntaxInfo>();

            foreach (var syntaxObj in syntaxDict)
            {
                var syntaxInfo = new SyntaxInfo();
                syntaxInfo.name = syntaxObj.Key;
                syntaxInfo.Parse(syntaxObj.Value, defDict, expDict);
                syntaxList.Add(syntaxInfo);
            }
            foreach (var syntaxInfo in syntaxList)
            {
                var fileStr  = syntaxInfo.Generate(tempDict);
                var filePath = string.Format("{0}/{1}Parser.cs", syntaxRoot, syntaxInfo.name);
                File.WriteAllText(filePath, fileStr);
            }
        }
Example #2
0
        private static void WriteParser(string[] syntaxStrList, Dictionary <string, string> tempDict)
        {
            var syntaxList = new List <SyntaxInfo>();
            var syntaxDict = new Dictionary <string, SyntaxInfo>();
            var exprDict   = new Dictionary <string, SyntaxInfo>();
            var statDict   = new Dictionary <string, SyntaxInfo>();

            foreach (var syntaxStr in syntaxStrList.Where(t => !string.IsNullOrEmpty(t)))
            {
                var syntaxInfo = new SyntaxInfo();
                syntaxInfo.index = syntaxList.Count;
                syntaxInfo.Parse(syntaxStr.Trim());
                syntaxList.Add(syntaxInfo);
                syntaxDict.Add(syntaxInfo.name, syntaxInfo);
                if (syntaxInfo.type == SyntaxInfo.Type.Expr)
                {
                    exprDict.Add(syntaxInfo.name, syntaxInfo);
                }
                else if (syntaxInfo.type == SyntaxInfo.Type.Stat)
                {
                    statDict.Add(syntaxInfo.name, syntaxInfo);
                }
            }

            // stat syntax
            var statSyntax = new SyntaxInfo();

            statSyntax.index = syntaxList.Count;
            statSyntax.Parse(string.Format("stat := {0}", string.Join(" | ", statDict.Values.Select(t => t.name))));
            syntaxList.Add(statSyntax);

            // expr syntax
            var exprSyntax = new SyntaxInfo();

            exprSyntax.index = syntaxList.Count;
            exprSyntax.Parse(string.Format("expr := {0}", string.Join(" | ", exprDict.Values.Select(t => t.name))) + " | name | number | string | boolean | nil");
            syntaxList.Add(exprSyntax);

            // unary expr
            var unaryOp = syntaxDict["unary_op"];

            exprDict.Remove("unary_expr");
            foreach (var op in unaryOp.items)
            {
                var syntaxInfo = new SyntaxInfo();
                syntaxInfo.index = syntaxList.Count;
                syntaxInfo.Parse(string.Format("unary_expr_{0} := {0} expr", op));
                syntaxList.Add(syntaxInfo);
                exprDict.Add(syntaxInfo.name, syntaxInfo);
            }

            // binary expr
            var binaryOp = syntaxDict["binary_op"];

            exprDict.Remove("binary_expr");
            foreach (var op in binaryOp.items)
            {
                var syntaxInfo = new SyntaxInfo();
                syntaxInfo.index = syntaxList.Count;
                syntaxInfo.Parse(string.Format("binary_expr_{0} := expr {0} expr", op));
                syntaxList.Add(syntaxInfo);
                exprDict.Add(syntaxInfo.name, syntaxInfo);
            }

            var context = new GenerateContext {
                sb = new StringBuilder(), tempDict = tempDict
            };

            foreach (var syntaxInfo in syntaxList)
            {
                syntaxInfo.Generate(context);
            }
        }