예제 #1
0
        public IType[] InterpretLines(Global environment, string input)
        {
            if (input == null)
            {
                return new IType[] { }
            }
            ;
            try
            {
                AntlrInputStream inputStream = new AntlrInputStream(input);

                FAILangLexer      lexer             = new FAILangLexer(inputStream);
                CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
                FAILangParser     parser            = new FAILangParser(commonTokenStream);
                parser.ErrorHandler = new BailErrorStrategy();

                FAILangParser.CompileUnitContext expressionContext = parser.compileUnit();
                FAILangVisitor visitor = new FAILangVisitor(environment);

                return(visitor.VisitCompileUnit(expressionContext).Select(x => environment.Evaluate(x)).ToArray());
            }
            catch (Antlr4.Runtime.Misc.ParseCanceledException)
            {
                return(new IType[] { new Error("ParseError", "The input failed to parse.") });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            return(new IType[] { });
        }
    }
예제 #2
0
        static void Main(string[] args)
        {
            Global.LoadBuiltins(new NumberBuiltinProvider());
            Console.InputEncoding  = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;

            // Read-eval-print loop
            while (true)
            {
                try
                {
                    string input = Console.ReadLine();
                    while (input.EndsWith("  "))
                    {
                        input += Console.ReadLine();
                    }

                    AntlrInputStream inputStream = new AntlrInputStream(input);

                    FAILangLexer      lexer             = new FAILangLexer(inputStream);
                    CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
                    FAILangParser     parser            = new FAILangParser(commonTokenStream);
                    parser.ErrorHandler = new BailErrorStrategy();

                    FAILangParser.CallsContext expressionContext = parser.calls();
                    FAILangVisitor             visitor           = new FAILangVisitor();

                    var vals = visitor.VisitCalls(expressionContext).Select(x => Global.Evaluate(x));
                    foreach (var val in vals)
                    {
                        if (val != null)
                        {
                            Console.WriteLine(val);
                        }
                    }
                }
                catch (StackOverflowException)
                {
                    Console.WriteLine(new Error("StackOverflow", "The call stack has overflowed"));
                }
                catch (Antlr4.Runtime.Misc.ParseCanceledException)
                {
                    Console.WriteLine(new Error("ParseError", "The input failed to parse."));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
            }
        }