当前位置: 首页>>代码示例>>C#>>正文


C# Interpreter.GetInput方法代码示例

本文整理汇总了C#中Interpreter.GetInput方法的典型用法代码示例。如果您正苦于以下问题:C# Interpreter.GetInput方法的具体用法?C# Interpreter.GetInput怎么用?C# Interpreter.GetInput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Interpreter的用法示例。


在下文中一共展示了Interpreter.GetInput方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TBlock

        /// <summary>
        /// The constructor for TBlock.
        /// </summary>
        /// <param name="interpreter">The interpreter that the constructor is being called from.</param>
        /// <param name="exception">An exception that will be written to if there was an error.</param>
        /// <param name="elseAllowed">
        /// Whether the 'else' keyword is allowed to be used in the top level of the block. Usually only used in when
        /// creating a block in IF statements.
        /// </param>
        public TBlock(Interpreter interpreter, out TException exception, bool elseAllowed)
        {
            statements = new List<string>();
            elseLocation = -1;
            this.elseAllowed = elseAllowed;
            exception = null;
            currentLine = -1;

            int blockLevel = 0;
            while (true) // Keep adding statements until the appropriate terminator (i.e. 'end') is found
            {
                string statement = interpreter.GetInput().Trim();

                // Split the statement into symbols so we can analyse it to find out where the block begin/ends are
                bool isComment;
                Interpreter.Group symbolGroup = interpreter.SplitIntoSymbols(statement, out exception, out isComment);
                if (exception != null)
                {
                    statements.Clear();
                    break;
                }
                if (isComment) continue;

                // A bit of Linq to easily count the number of a particular keyword in a group
                string keywordName = "";
                var keywordQuery =
                    from object item in symbolGroup
                    where (item is string) && ((string)item == keywordName)
                    select item;

                // If there's a begin, increment the block level. If there are too many then return an error
                keywordName = "begin";
                int beginCount = keywordQuery.Count();
                if (beginCount > 1)
                {
                    exception = new TException(interpreter, "Having more than one begin on a line is forbidden");
                    break;
                }
                else if (beginCount == 1) ++blockLevel;

                // If there's an end, decrement the block level
                keywordName = "end";
                int endCount = keywordQuery.Count();
                if (endCount > 1)
                {
                    exception = new TException(interpreter, "Having more than one end on a line is forbidden");
                    break;
                }
                else if (endCount == 1)
                {
                    if (blockLevel == 0) break;
                    else --blockLevel;
                }

                // Increment the block level if there is an IF statement or WHILE loop where a block of code is to
                // follow, i.e. there is no statement after the comma after the condition
                if (symbolGroup.IndexOf("if") >= 0)
                {
                    string commaStr = symbolGroup[symbolGroup.Count - 1] as string;
                    if ((commaStr != null) && (commaStr == ",")) ++blockLevel;
                }

                if (symbolGroup.IndexOf("while") >= 0)
                {
                    string commaStr = symbolGroup[symbolGroup.Count - 1] as string;
                    if ((commaStr != null) && (commaStr == ",")) ++blockLevel;
                }

                // Increment if there is a multi-line function declaration (let <ident><group> =<nothing>)
                int equalsIndex;
                if ((equalsIndex = symbolGroup.IndexOf("=")) == symbolGroup.Count - 1)
                {
                    if ((equalsIndex > 1) && (symbolGroup[equalsIndex - 1] is Interpreter.Group))
                    {
                        if (symbolGroup.LastIndexOf("let", equalsIndex - 2) >= 0) ++blockLevel;
                    }
                }

                bool breakNow = false; // For a double break
                int elseIndex = -1;
                while ((elseIndex = symbolGroup.IndexOf("else", elseIndex + 1)) >= 0)
                {
                    if (elseIndex == 0) // If the else is at the beginning of the line
                    {
                        if (blockLevel == 0)
                        {
                            // If a top level 'else' has not already been used and the use of 'else' is allowed
                            if ((elseLocation < 0) && elseAllowed)
                            {
                                elseLocation = statements.Count;
                                if (elseIndex < symbolGroup.Count - 1) // If there is a statement after the 'else'
//.........这里部分代码省略.........
开发者ID:supermaximo93,项目名称:Toast-Prototype-Interpreter,代码行数:101,代码来源:TBlock.cs


注:本文中的Interpreter.GetInput方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。