Ejemplo n.º 1
0
        public Interpreter(IScope globalScope, IInterpreterListener listener)
        {
            if (globalScope == null)
            {
                throw new ArgumentNullException(nameof(globalScope), "Global scope can't be null");
            }
            GlobalScope = globalScope;

            if (listener == null)
            {
                throw new ArgumentNullException(nameof(listener), "Listener can't be null");
            }
            Listener = listener;

            _parser = new KermitParser(null, globalScope)
            {
                TreeAdaptor = new KermitAdaptor()
            };
            //_parser.TraceDestination = Console.Error;

            _currentSpace = Globals;

            AddInternalNativeFunctions();
            AddNativeType("List", typeof(List <object>));
            AddNativeType(typeof(string));
            AddNativeType("Dictionary", typeof(Dictionary <object, object>));
        }
Ejemplo n.º 2
0
        public static void Loop2()
        {
            bool         exit   = false;
            string       input  = "";
            KermitParser parser = new KermitParser(null);

            //parser.TreeAdaptor = new KermitAdaptor();
            while (!exit)
            {
                if (input == string.Empty)
                {
                    Console.Write("> ");
                }
                else
                {
                    Console.Write(".. ");
                }
                input += Console.ReadLine() + "\n";
                ANTLRStringStream  stream = new ANTLRStringStream(input);
                KermitLexer        lexer  = new KermitLexer(stream);
                TokenRewriteStream tokens = new TokenRewriteStream(lexer);
                parser.TokenStream = tokens;
                try
                {
                    AstParserRuleReturnScope <KermitAST, CommonToken> result = parser.program();
                    var tree             = (CommonTree)result.Tree;
                    DotTreeGenerator gen = new DotTreeGenerator();
                    Console.WriteLine("{0}", gen.ToDot(tree));
                    input = "";

                    //Console.WriteLine(globalScope.ToString());
                }
                catch (PartialStatement)
                {
                }
                catch (ParserException e)
                {
                }
            }
        }