示例#1
0
        /// <summary>
        /// Iterate over the list of transforemd tokens generated from the token list generated
        /// by the tokenizer and generate a list of
        /// </summary>
        public bool PreInterpret()
        {
            Dictionary <string, int> definedLabels = new Dictionary <string, int>()
            {
            };

            for (int i = 0; i < this.InputTokens.Count; i++)
            {
                TransformedToken currentToken = this.InputTokens[i];
                if (
                    currentToken.Type == TransformedTokenType.LabelDefinition ||
                    currentToken.Type == TransformedTokenType.StacklessLabelDefinition
                    )
                {
                    if (!definedLabels.ContainsKey(currentToken.Data[0]))
                    {
                        definedLabels.Add(currentToken.Data[0], i);
                    }
                }
            }

            this.Environment.DefinedLabels = definedLabels;
            return(true);
        }
示例#2
0
        /// <summary>
        /// Iterate over the list of transformed tokens generated from the token list generated
        /// by the tokenizer and produce a result.
        /// </summary>
        public void Interpret()
        {
            Stopwatch interpreterStopwatch = new Stopwatch();

            interpreterStopwatch.Start();

            while (this.Environment.CurrentTokenIndex < this.InputTokens.Count)
            {
                TransformedToken currentToken = this.InputTokens[this.Environment.CurrentTokenIndex];
                switch (currentToken.Type)
                {
                case TransformedTokenType.CallFunction:
                    if (this.Functions.ContainsKey(currentToken.Data[0]))
                    {
                        bool success = this.Functions[currentToken.Data[0]].Execute(this, this.Environment);
                        if (!success)
                        {
                            goto exitInterpreter;
                        }
                    }
                    else
                    {
                        Errors.UnknownFunction.Report(
                            currentToken.Position.File,
                            currentToken.Position.Line,
                            currentToken.Position.Column,
                            currentToken.Data[0]
                            );
                        goto exitInterpreter;
                    }
                    this.Environment.CurrentTokenIndex++;
                    break;

                case TransformedTokenType.LabelDefinition:
                    this.Environment.CurrentTokenIndex++;
                    break;

                case TransformedTokenType.LabelUsage:
                    this.Environment.LabelStack.Push(currentToken);
                    this.Environment.CurrentTokenIndex++;
                    break;

                case TransformedTokenType.StacklessLabelDefinition:
                    this.Environment.CurrentTokenIndex++;
                    break;

                case TransformedTokenType.StacklessLabelUsage:
                    this.Environment.LabelStack.Push(currentToken);
                    this.Environment.CurrentTokenIndex++;
                    break;

                case TransformedTokenType.Name:
                    this.Environment.NameStack.Push(currentToken);
                    this.Environment.CurrentTokenIndex++;
                    break;

                default:
                    this.Environment.ValueStack.Push(currentToken);
                    this.Environment.CurrentTokenIndex++;
                    break;
                }
            }

exitInterpreter:
            if (this.GlobalOptions.Contains("ENABLE_STAGE_TIMING_OUTPUT"))
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(
                    "[TIMING::Interpreter] Stage took {0}s / {1}ms / {2}t.",
                    interpreterStopwatch.Elapsed,
                    interpreterStopwatch.ElapsedMilliseconds,
                    interpreterStopwatch.ElapsedTicks
                    );
                Console.ResetColor();
            }

            if (this.GlobalOptions.Contains("ENABLE_STAGE_EXIT_ERROR"))
            {
                Errors.ExitInterpreter.Report("", 0, 0);
            }
        }