예제 #1
0
        /// <summary>
        /// Add a native type to the interpreter
        /// </summary>
        /// <param name="name">Name for the type</param>
        /// <param name="type">Type to be added</param>
        public void AddNativeType(string name, Type type)
        {
            GlobalScope.Define(new NativeSymbol(name, type));
            CommitableScope g = GlobalScope as CommitableScope;

            g?.CommitScope();
        }
예제 #2
0
        /// <summary>
        /// Add a native function to the interpreter
        /// </summary>
        /// <param name="name">Name of the function</param>
        /// <param name="function">Function object to be called</param>
        public void AddNativeFunction(string name, NativeFunction function)
        {
            function.Name = name;
            NativeFunctionSymbol symbol = new NativeFunctionSymbol(name, GlobalScope, function);

            GlobalScope.Define(symbol);
            CommitableScope g = GlobalScope as CommitableScope;

            g?.CommitScope();
        }
예제 #3
0
        /// <summary>
        /// Execute the input stream
        /// </summary>
        /// <param name="input">Input stream to be executed</param>
        public void InterpretAntlrStream(ANTLRStringStream input)
        {
            KermitLexer        lexer  = new KermitLexer(input);
            TokenRewriteStream tokens = new TokenRewriteStream(lexer);

            _parser.TokenStream = tokens;

            AstParserRuleReturnScope <KermitAST, CommonToken> ret;

            try
            {
                ret = _parser.program();
                CommitableScope g = GlobalScope as CommitableScope;
                g?.CommitScope();
            }
            catch (Exception e) when(e is ParserException || e is PartialStatement)
            {
                CommitableScope g = GlobalScope as CommitableScope;

                g?.RevertScope();
                throw;
            }

            if (_parser.NumberOfSyntaxErrors == 0)
            {
                _root = ret.Tree;

                try
                {
                    Block(_root);
                    CommitableScope g = GlobalScope as CommitableScope;
                    g?.CommitScope();
                }
                catch (InterpreterException e)
                {
                    if (e.CallStack != null)
                    {
                        Listener.Error("CallStack:");
                        foreach (string call in e.CallStack)
                        {
                            Listener.Error(" - " + call);
                        }
                    }
                    Listener.Error(e);
                    Stack.Clear(); // Clear the function stack after an error
                }
            }
            else // We shouldn't reach this condition never
            {
                //throw new InterpreterException($"{_parser.NumberOfSyntaxErrors} syntax errors");
                Listener.Error($"{_parser.NumberOfSyntaxErrors} syntax errors");
            }
        }