Beispiel #1
0
        public void Interpret(string code, bool printStack)
        {
#if DEBUG
            var sw = Stopwatch.StartNew();
#endif
            SetupInterpreter(code);
#if DEBUG
            Utils.PrintLabels(labels);
#endif
            try
            {
                while (true)
                {
                    // Break on EOF
                    if (!Enumerator.MoveNext())
                    {
                        break;
                    }

                    switch (Enumerator.Current)
                    {
                    case '&':
                        ReadGlobal();
                        continue;

                    case '"':
                        ReadString();
                        continue;

                    case '@':
                        ReadLabel();
                        continue;

                    case '/':
                        SkipComment();
                        continue;

                    case '.':
                    case char c when char.IsDigit(c):
                        ReadNumber();

                        continue;

                    case char c when char.IsWhiteSpace(c):
                        continue;

                    default:
                        ReadOperation()?.Operate(this);
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex);
                Console.ResetColor();
                UserStack.Clear();
            }

#if DEBUG
            sw.Stop();
            Utils.PrintGlobals(globals);
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("Execution time: {0}ms", sw.ElapsedMilliseconds);
            Console.ResetColor();
#endif
            if (printStack)
            {
                Utils.PrintStack(UserStack);
                labels.Clear();
                CallStack.Clear();
            }
        }
 public void Clear()
 {
     UserStack.Clear();
     BootStack.Clear();
 }