Example #1
0
        public static (Instructions Instructions, CodeContexts Contexts) ParseFile(string file)
        {
            var lexer    = new FlyLexer(new AntlrFileStream(file));
            var parser   = new FlyParser(new CommonTokenStream(lexer));
            var listener = new Listener();

            listener.EnterProgram(parser.program());
            return(listener.Code.Instructions, listener.Code.Contexts);
        }
Example #2
0
            public void Run()
            {
                while (true)
                {
                    Console.Write(">>> ");
                    var input = ReadInput();
                    var lexer = new FlyLexer(new AntlrInputStream(input));
                    try
                    {
                        _prev = (Listener)listener.Clone();
                        var parser = new FlyParser(new CommonTokenStream(lexer))
                        {
                            ErrorHandler = new BailErrorStrategy()
                        };
                        listener.EnterProgram(parser.program());
                    }
                    catch (Exception e)
                    {
                        listener = _prev;
                        Console.WriteLine(e.Message.Split('\n').First());
                    }

                    if (vm == null)
                    {
                        vm = new VirtualMachine(listener.Code.Instructions, listener.Code.Contexts, In, Out);
                    }
                    else
                    {
                        vm.Instructions  = listener.Code.Instructions;
                        vm.DebugContexts = listener.Code.Contexts;
                    }

                    vm.constants = new StringConstants(listener.Code.Instructions.StringConstants.ToArray());
#if !DEBUG
                    try
                    {
#endif
                    var res = vm.EvalRun();
                    Console.WriteLine(res);
#if !DEBUG
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR: " + e.Message);
                }
#endif
                }
            }
Example #3
0
        public void TestStatements()
        {
            var cases = new[]
            {
                "test = 5;",
                "if true { print(0); } elif false { print(1); } else { print(2); }",
                "b = box (a) { return a; }",
                "a, b = 5, 4;",
            };

            foreach (var @case in cases)
            {
                var lexer = new FlyLexer(new AntlrInputStream(@case));

                var parser = new FlyParser(new CommonTokenStream(lexer));
                // parser.ErrorHandler= new BailErrorStrategy();
                var p = parser.program();
                Assert.True(p != null);

                // var listener = new Listener();
                // listener.EnterProgram(parser.program());
            }
        }