示例#1
0
        /// <summary>
        /// Конструктор. Считывает исходники с файла
        /// </summary>
        public ConsoleProgram(string[] args)
        {
            #region  азбор аргyментов командной строки

            switch (args.Length)
            {
            case 1:
                if (args[0].ToUpper() == "-HELP")
                {
                    Console.WriteLine(ConsoleProgram.getUserGuide());
                }
                else
                {
                    throw new ConsoleException("Некорректное использование аргументов командной строки");
                }
                break;

            case 2:
                if (args[0].ToUpper() == "-INPUT_FILE")
                {
                    this.input_file = args[1];
                }
                else if (args[0].ToUpper() == "-OUTPUT_FILE")
                {
                    this.output_file = args[1];
                }
                else
                {
                    throw new ConsoleException("Некорректное использование аргументов командной строки");
                }
                break;

            case 4:
                if (args[0].ToUpper() == "-INPUT_FILE")
                {
                    this.input_file = args[1];
                    if (args[2].ToUpper() == "-OUTPUT_FILE")
                    {
                        this.output_file = args[3];
                    }
                    else
                    {
                        throw new ConsoleException("Недопустимый ключ! Должен быть -OUTPUT_FILE");
                    }
                }
                else if (args[0].ToUpper() == "-OUTPUT_FILE")
                {
                    this.output_file = args[1];
                    if (args[2].ToUpper() == "-INPUT_FILE")
                    {
                        this.input_file = args[3];
                    }
                    else
                    {
                        throw new ConsoleException("Недопустимый ключ! Должен быть -INPUT_FILE");
                    }
                }
                else
                {
                    throw new ConsoleException("Некорректное использование аргументов командной строки");
                }
                break;

            default:
                throw new ConsoleException("Неверное количество аргументов");
            }

            #endregion
            string[] temp = null;
            try
            {
                temp = System.IO.File.ReadAllLines(this.input_file);
            }
            catch (Exception e)
            {
                throw new ConsoleException("Не удалось загрузить данные с файла. Возможно путь к файлу указан неверно");
            }
            this.sourceCode    = new SourceCode(temp);
            this.sourceStrings = new List <string>(temp);
        }
示例#2
0
 /// <summary>
 /// Обновляет результаты предыдущего прохода
 /// </summary>
 private void refresh(string[] temp)
 {
     this.sourceCode    = new SourceCode(temp);
     this.sourceStrings = new List <string>(temp);
 }
示例#3
0
        public List <SourceEntity> invoke(string[] prms)
        {
            // Подставим значения параметров
            List <SourceEntity> newBody = CheckMacros.InvokeMacroParams(this, prms);

            SourceCode   sc      = new SourceCode(newBody);
            SourceEntity current = null;

            // проверки
            CheckMacros.CheckMacroLabels(this);

            //заменяем метки на "крутые" уникальные метки
            this.local = new Dictionary <string, int?>();
            foreach (SourceEntity se in sc.entities)
            {
                current = se.Clone();
                if (!String.IsNullOrEmpty(current.label))
                {
                    if (!Utils.isLabel(current.label))
                    {
                        throw new SPException("Некорректное задание метки в макросе: " + se.sourceString);
                    }
                    Utils.checkNames(current.label);
                    if (this.local.Keys.Contains(current.label))
                    {
                        throw new SPException("Повторное задание метки в макросе: " + se.sourceString);
                    }
                    this.local.Add(current.label, null);
                }
            }

            // исполнять ли команду дальше
            Stack <bool> runStack = new Stack <bool>();

            runStack.Push(true);
            // стек комманд, появлявшихся ранее
            Stack <Command> commandStack = new Stack <Command>();
            // стек строк, куда надо вернуться при while
            Stack <int> whileStack = new Stack <int>();
            //
            bool elseFlag;

            for (int i = 0; i < sc.entities.Count; i++)
            {
                this.counter++;
                if (this.counter == 1000000)
                {
                    throw new SPException("Обнаружен бесконечный цикл");
                }

                current = newBody[i].Clone();
                CheckMacros.checkInner(current, commandStack);

                #region IF
                //if (current.operation == "IF")
                //{
                //    CheckMacros.checkIF(this);
                //    commandStack.Push(Command.if_);
                //    runStack.Push(runStack.Peek() && Utils.compare(current.operands[0]));
                //    continue;
                //}
                //if (current.operation == "ELSE")
                //{
                //    CheckMacros.checkIF(this);
                //    commandStack.Pop();
                //    commandStack.Push(Command.else_);
                //    elseFlag = runStack.Pop();
                //    runStack.Push(runStack.Peek() && !elseFlag);
                //    continue;
                //}
                //if (current.operation == "ENDIF")
                //{
                //    CheckMacros.checkIF(this);
                //    commandStack.Pop();
                //    runStack.Pop();
                //    continue;
                //}
                #endregion

                if (current.operation == "WHILE")
                {
                    CheckMacros.checkWhileEndw(this);
                    commandStack.Push(Command.while_);
                    runStack.Push(runStack.Peek() && Utils.compare(current.operands[0]));
                    whileStack.Push(i);
                    continue;
                }
                if (current.operation == "ENDW")
                {
                    CheckMacros.checkWhileEndw(this);
                    commandStack.Pop();
                    int newI = whileStack.Pop() - 1;
                    if (runStack.Pop())
                    {
                        i = newI;
                    }
                    continue;
                }

                #region AIF AGO

                //if (current.operation == "AIF" && Utils.compare(current.operands[0]) || current.operation == "AGO")
                //{
                //    CheckMacros.checkAIF(this);
                //    if (runStack.Peek())
                //    {
                //        string label = current.operation == "AIF" ? current.operands[1] : current.operands[0];
                //        // находим метку, чтобы туда прыгнуть
                //        bool ready = false;
                //        Stack<bool> agoStack = new Stack<bool>();
                //        // вверх
                //        for (int j = i; j >= 0; j--)
                //        {
                //            if (this.body[j].operation == "IF" || this.body[j].operation == "WHILE")
                //            {
                //                if (agoStack.Count > 0)
                //                {
                //                    agoStack.Pop();
                //                }
                //            }
                //            if (this.body[j].operation == "ELSE")
                //            {
                //                if (agoStack.Count > 0)
                //                {
                //                    agoStack.Pop();
                //                }
                //                agoStack.Push(false);
                //            }
                //            if (this.body[j].operation == "ENDIF" || this.body[j].operation == "ENDW")
                //            {
                //                agoStack.Push(false);
                //            }
                //            if (this.body[j].label == label && (agoStack.Count == 0 || agoStack.Peek()))
                //            {
                //                i = j - 1;
                //                ready = true;
                //                break;
                //            }
                //        }

                //        // вниз
                //        if (!ready)
                //        {
                //            for (int j = i; j < this.body.Count; j++)
                //            {
                //                if (this.body[j].operation == "IF" || this.body[j].operation == "WHILE")
                //                {
                //                    agoStack.Push(false);
                //                }
                //                if (this.body[j].operation == "ELSE")
                //                {
                //                    if (agoStack.Count > 0)
                //                    {
                //                        agoStack.Pop();
                //                    }
                //                    agoStack.Push(false);
                //                }
                //                if (this.body[j].operation == "ENDIF" || this.body[j].operation == "ENDW")
                //                {
                //                    if (agoStack.Count > 0)
                //                    {
                //                        agoStack.Pop();
                //                    }
                //                }
                //                if (this.body[j].label == label && (agoStack.Count == 0 || agoStack.Peek()))
                //                {
                //                    i = j - 1;
                //                    ready = true;
                //                    break;
                //                }
                //            }
                //        }
                //        if (!ready)
                //        {
                //            throw new SPException("Метка " + label + " при директиве " + current.operation + " находится вне зоны видимости или не описана");
                //        }
                //    }
                //    continue;
                //}

                #endregion

                if (runStack.Peek())
                {
                    if (!String.IsNullOrEmpty(current.label))
                    {
                        current.label = current.label + "_" + this.name + "_" + this.count;
                    }
                    for (int j = 0; j < current.operands.Count; j++)
                    {
                        if (this.local.Keys.Contains(current.operands[j]))
                        {
                            current.operands[j] = current.operands[j] + "_" + this.name + "_" + this.count;
                        }
                    }

                    sc.firstRunStep(current, this);
                }
            }
            this.count++;

            //foreach (SourceEntity se in sc.result)
            //{
            //se.label = null;
            //}
            return(sc.result);
        }