예제 #1
0
        public Program Parse()
        {
            var lexer  = new AntlrLexer(new ANTLRStringStream(_code));
            var tokens = new CommonTokenStream(lexer);

            var parser = new AntlrParser(tokens);

            parser.TreeAdaptor = new Adapter();
            var result = parser.program();

            lexer.Errors.ForEach(x => Errors.Add(x));
            parser.Errors.ForEach(x => Errors.Add(x));
            if (Errors.Any())
            {
                return(null);
            }

            var bridge = new BridgeVisitor(result.Tree);

            return(bridge.CreateAst());
        }
예제 #2
0
        static void Main(string[] args)
        {
            string code = @"

when [ToPractice] is 'cardiology of bob'

*[Insurance] is 'blue cross' +[Insurance] is 'blah' +[Insurance] is 'blah3'
*[DocumentCount] greater than 1
*[DocumentCount] less than 3

when [ToPractice] is 'test'
*[DocumentCount] greater than 10
            
";

            var lexer  = new DslLexer(new ANTLRStringStream(code));
            var tokens = new CommonTokenStream(lexer);

            var parser = new DslParser(tokens);

            parser.TreeAdaptor = new BridgeAdapter();
            var result = parser.program();

            //Generate .dot graph. View it at http://mdaines.github.io/viz.js/
            var graphGenerator = new DotTreeGenerator();
            var dotFile        = graphGenerator.ToDot(result.Tree);

            if (lexer.Errors.Count > 0)
            {
                foreach (var error in lexer.Errors)
                {
                    Console.WriteLine(error);
                }

                return;
            }

            if (parser.Errors.Count > 0)
            {
                foreach (var error in parser.Errors)
                {
                    Console.WriteLine(error);
                }

                return;
            }

            var bridge = new BridgeVisitor(result.Tree);
            var ast    = bridge.CreateAst();

            var codeGen  = new CodeGenVisitor(ast);
            var codeUnit = codeGen.Generate();

            //Code Gen;
            string codeString = Persist.ToCSharpSource(codeUnit);

            //Compile
            var persist  = new Persist(codeUnit);
            var assembly = persist.ToAssembly();

            if (persist.Errors.Count > 0)
            {
                foreach (var error in persist.Errors)
                {
                    Console.WriteLine(error);
                }

                return;
            }

            //Run
            var instance   = assembly.CreateInstance("Code");
            var method     = assembly.GetType("Code").GetMethod("Run");
            var methodArgs = new Args {
                ToPractice = "test", Insurance = "blue cross", DocumentCount = 5
            };
            var codeResult = (bool)method.Invoke(instance, new[] { methodArgs });

            Console.WriteLine(codeResult);
        }