public override void GenerateIntermediateCode() { //Нельзя добавить петлю IntermediateCodeList.push(new CmpNode(first.MainVariable, second.MainVariable)); string label = "comp_" + (++Counters.comparsions).ToString(); IntermediateCodeList.push(new GoToLabel(label, "je")); //граф будет неориентированный, поэтому добавляем в обе стороны IntermediateCodeList.push(new SetGraphCell(Res.MainVariable, new GraphCell(graph.MainVariable, first.MainVariable, second.MainVariable))); IntermediateCodeList.push(new SetGraphCell(Res.MainVariable, new GraphCell(graph.MainVariable, second.MainVariable, first.MainVariable))); IntermediateCodeList.push(new PutLabel(label)); }
public override void GenerateIntermediateCode() { Variable arr = new Variable(); arr.AlternativeName = "array_" + (++Counters.arrays).ToString(); arr.Type = "array"; arr.Value = graph.MainVariable.Value; //Создали массив для вычислений IntermediateCodeList.addVar(arr); IntermediateCodeList.push(new NumComponentsCall(graph.MainVariable, arr, outVariable.MainVariable)); GeneratingAssembleCode.WasDFSUsed = true; GeneratingAssembleCode.NumComponentsUsed = true; }
public override void GenerateIntermediateCode() { Variable temp = new Variable(); temp.Type = "int"; temp.AlternativeName = "temp_" + (++Counters.temps); IntermediateCodeList.addVar(temp); Variable tr = new Variable(); tr.IsConst = true; tr.AlternativeName = "const_" + (++Counters.consts).ToString(); tr.Value = "true"; tr.Type = "bool"; IntermediateCodeList.addVar(tr); Variable fl = new Variable(); fl.IsConst = true; fl.AlternativeName = "const_" + (++Counters.consts).ToString(); fl.Value = "false"; fl.Type = "bool"; IntermediateCodeList.addVar(fl); Variable Edges = new Variable(); Edges.IsConst = true; Edges.AlternativeName = "const_" + (++Counters.consts).ToString(); Edges.Value = ((int.Parse(graph.MainVariable.Value) * (int.Parse(graph.MainVariable.Value) - 1)) / 2).ToString(); Edges.Type = "int"; IntermediateCodeList.addVar(Edges); //Считаем рёбра IntermediateCodeList.push(new CountEdgesInterNode(graph.MainVariable, temp)); //српавниваем IntermediateCodeList.push(new CmpNode(temp, Edges)); string label = "not_full_" + (++Counters.comparsions).ToString(); //Если не совпадает - плохо IntermediateCodeList.push(new GoToLabel(label, "jne")); //Если совпадает, то всё хорошо IntermediateCodeList.push(new AssignmentInterNode(tr, outVariable.MainVariable)); IntermediateCodeList.push(new GoToLabel(label + "_exit", "jmp")); IntermediateCodeList.push(new PutLabel(label)); IntermediateCodeList.push(new AssignmentInterNode(fl, outVariable.MainVariable)); IntermediateCodeList.push(new PutLabel(label + "_exit")); GeneratingAssembleCode.WasCountEdgesUsed = true; }
public override void GenerateIntermediateCode() { //номер условного оператора, нужен для формирования меток int num_if = ++Counters.ifs; Condition.GenerateIntermediateCode(); //то, что получилось, сравниваем с нулём if (Condition.MainVariable.Type == "int") { IntermediateCodeList.push(new CmpNode(Condition.MainVariable, new ConstantNode("int", "0", 0).MainVariable)); } else if (Condition.MainVariable.Type == "bool") { IntermediateCodeList.push(new CmpNode(Condition.MainVariable, new ConstantNode("bool", "false", 0).MainVariable)); } else { Console.WriteLine("Что-то не так с типами"); } if (ElseBranch == null) { //если else-ветки нет, формируем метку на выход, если условие не выполнено //если равно нулю, то условие ложно и уходим IntermediateCodeList.push(new GoToLabel("exit_if_" + num_if.ToString(), "je")); } else { //если вторая ветка есть, формируем метку на неё IntermediateCodeList.push(new GoToLabel("else_" + num_if.ToString(), "je")); } //генерим код для if-ветки, она будет в любом случае IfBranch.GenerateIntermediateCode(); //если есть вторая ветка, надо сгенерить код для неё if (ElseBranch != null) { //из if-ветки отправляем на выход IntermediateCodeList.push(new GoToLabel("exit_if_" + num_if.ToString(), "jmp")); //ставим метку else IntermediateCodeList.push(new PutLabel("else_" + num_if.ToString())); //Генерируем код else-ветки ElseBranch.GenerateIntermediateCode(); } //ставим метку на выход IntermediateCodeList.push(new PutLabel("exit_if_" + num_if.ToString())); }
public string ConstantValue; //значение вне зависимости от типа будет храниться в строковом виде public ConstantNode(string Type, string ConstValue, int numLine) { TypeOfNode = NodeType.Constant; MainVariable = new Variable();//создаём новую временную переменную MainVariable.IsDeclared = true; MainVariable.WasUsed = true; LineNumber = numLine; Value = ConstValue; MainVariable.Type = Type; MainVariable.WasIdentified = true; //Тут сразу же всё поместим MainVariable.AlternativeName = "const_" + (++Counters.consts); MainVariable.IsConst = true; MainVariable.Value = Value; //константы все отдельно будут (пофиг на оптимизацию) IntermediateCodeList.addVar(MainVariable); //Это никуда в таблицу переменных не заносится, просто само себе }
public override void GenerateIntermediateCode() { IntermediateCodeList.push(new CountEdgesInterNode(graph.MainVariable, outVariable.MainVariable)); GeneratingAssembleCode.WasCountEdgesUsed = true; }
public override void GenerateIntermediateCode() { IntermediateCodeList.push(new CopyGraphsInterNode(outGraph.MainVariable, inGraph.MainVariable)); IntermediateCodeList.push(new FloydCall(outGraph.MainVariable)); GeneratingAssembleCode.WasFloydUsed = true; }
public override void GenerateIntermediateCode() { IntermediateCodeList.push(new CopyGraphsInterNode(outGraph.MainVariable, inGraph.MainVariable)); }
public override void GenerateIntermediateCode() { IntermediateCodeList.push(new GetGraphCell(Res.MainVariable, new GraphCell(graph.MainVariable, first.MainVariable, second.MainVariable))); }
public override void GenerateIntermediateCode() { MainVariable.AlternativeName = "temp_" + (++Counters.temps).ToString(); //присваиваем временной переменной имя IntermediateCodeList.addVar(MainVariable); //временную переменную - в список переменных FirstOperand.GenerateIntermediateCode(); //вот тут самое веселье if (IsUnary) { //из унарных получается только унарный минус и отрицание switch (Value) { case ("!"): { IntermediateCodeList.push(new NegativeInterNode(FirstOperand.MainVariable, MainVariable)); break; } case ("-"): { IntermediateCodeList.push(new UnaryMinusInterNode(FirstOperand.MainVariable, MainVariable)); break; } } } else { SecondOperand.GenerateIntermediateCode(); switch (Value) { case ("+"): { IntermediateCodeList.push(new AddInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("-"): { IntermediateCodeList.push(new SubInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("*"): { IntermediateCodeList.push(new MulInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("/"): { IntermediateCodeList.push(new DivInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("%"): { IntermediateCodeList.push(new ModInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("&&"): { IntermediateCodeList.push(new AndInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("||"): { IntermediateCodeList.push(new OrInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("&"): { IntermediateCodeList.push(new BitAndInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("|"): { IntermediateCodeList.push(new BitOrInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } case ("^"): { IntermediateCodeList.push(new BitXorInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable)); break; } //с операторами сравнения посложнее case ("=="): { //сравниваем два операнда IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable)); //если не равно, то переходим дальше IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jne")); //если равно, то идём дальше IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable)); //присвоили и уходим IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp")); //если не равно, идём сюда IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString())); //присваиваем false IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable)); //и на выход IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString())); break; } //с другими операторами сравнения аналогично, только условие другое case ("!="): { //сравниваем два операнда IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable)); //если равно, то переходим дальше IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "je")); //иначе, если всё хорошо, то присваиваем true IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable)); //присвоили и уходим IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp")); //если не равно, идём сюда IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString())); //присваиваем false IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable)); //и на выход IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString())); break; } case (">="): { //сравниваем два операнда IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable)); //если меньше, то переходим дальше IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jl")); //иначе, если всё хорошо, то присваиваем true IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable)); //присвоили и уходим IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp")); //если не равно, идём сюда IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString())); //присваиваем false IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable)); //и на выход IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString())); break; } case ("<="): { //сравниваем два операнда IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable)); //если больше, то переходим дальше IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jg")); //иначе, если всё хорошо, то присваиваем true IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable)); //присвоили и уходим IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp")); //если не равно, идём сюда IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString())); //присваиваем false IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable)); //и на выход IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString())); break; } case ("<"): { //сравниваем два операнда IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable)); //если больше, то переходим дальше IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jge")); //иначе, если всё хорошо, то присваиваем true IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable)); //присвоили и уходим IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp")); //если не равно, идём сюда IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString())); //присваиваем false IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable)); //и на выход IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString())); break; } case (">"): { //сравниваем два операнда IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable)); //если больше, то переходим дальше IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jle")); //иначе, если всё хорошо, то присваиваем true IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable)); //присвоили и уходим IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp")); //если не равно, идём сюда IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString())); //присваиваем false IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable)); //и на выход IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString())); break; } } } }
public override void GenerateIntermediateCode() { //тут может быть выражение WriteVariable.GenerateIntermediateCode(); IntermediateCodeList.push(new WriteVarInterNode(MainVariable)); }
public override void GenerateIntermediateCode() { IntermediateCodeList.push(new ReadVarInterNode(MainVariable)); }
public override void GenerateIntermediateCode() { if (RightPart != null) { RightPart.GenerateIntermediateCode(); } switch (AssignmentOperation) { case ("="): { IntermediateCodeList.push(new AssignmentInterNode(RightPart.MainVariable, MainVariable)); break; } case ("+="): { IntermediateCodeList.push(new AddInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("-="): { IntermediateCodeList.push(new SubInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("*="): { IntermediateCodeList.push(new MulInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("/="): { IntermediateCodeList.push(new DivInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("%="): { IntermediateCodeList.push(new ModInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("&&="): { IntermediateCodeList.push(new AndInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("||="): { IntermediateCodeList.push(new OrInterNode(AssignedVariable.MainVariable, RightPart.MainVariable, AssignedVariable.MainVariable)); break; } case ("++"): { IntermediateCodeList.push(new IncrementInterNode(AssignedVariable.MainVariable)); break; } case ("--"): { IntermediateCodeList.push(new DecrementInterNode(AssignedVariable.MainVariable)); break; } } }
public override void GenerateIntermediateCode() { int num_cycle = ++Counters.cycles; if (BeginningActivity != null) { foreach (var oper in BeginningActivity) { oper.GenerateIntermediateCode(); } } //ставим метку - сюда будем возвращаться после каждой итерации IntermediateCodeList.push(new PutLabel("again_cycle_" + num_cycle.ToString())); if (IsPredCondition) { ContinueCondition.GenerateIntermediateCode(); //то, что получилось, сравниваем с нулём if (ContinueCondition.MainVariable.Type == "int") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("int", "0", 0).MainVariable)); } else if (ContinueCondition.MainVariable.Type == "bool") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("bool", "false", 0).MainVariable)); } else { Console.WriteLine("Что-то с типами в цикле"); } //если ложь, то на выход IntermediateCodeList.push(new GoToLabel("exit_cycle_" + num_cycle.ToString(), "je")); } //генерим код для тела цикла foreach (var oper in ChildrenOperators) { oper.GenerateIntermediateCode(); } //если есть последующее действие - генерим код для него if (IterationActivity != null) { IterationActivity.GenerateIntermediateCode(); } //если цикл с постусловием, то генерить здесть if (!IsPredCondition) { ContinueCondition.GenerateIntermediateCode(); //то, что получилось, сравниваем с нулём if (ContinueCondition.MainVariable.Type == "int") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("int", "0", 0).MainVariable)); } else if (ContinueCondition.MainVariable.Type == "bool") { IntermediateCodeList.push(new CmpNode(ContinueCondition.MainVariable, new ConstantNode("bool", "false", 0).MainVariable)); } else { Console.WriteLine("Что-то с типами в цикле"); } //если ложь, то на выход IntermediateCodeList.push(new GoToLabel("exit_cycle_" + num_cycle.ToString(), "je")); } //переход на следующую итерацию IntermediateCodeList.push(new GoToLabel("again_cycle_" + num_cycle.ToString(), "jmp")); //последняя метка - на выход IntermediateCodeList.push(new PutLabel("exit_cycle_" + num_cycle.ToString())); }
public override void GenerateIntermediateCode() { Variable temp = new Variable(); temp.Type = "int"; temp.AlternativeName = "temp_" + (++Counters.temps); IntermediateCodeList.addVar(temp); Variable tr = new Variable(); tr.IsConst = true; tr.AlternativeName = "const_" + (++Counters.consts).ToString(); tr.Value = "true"; tr.Type = "bool"; IntermediateCodeList.addVar(tr); Variable fl = new Variable(); fl.IsConst = true; fl.AlternativeName = "const_" + (++Counters.consts).ToString(); fl.Value = "false"; fl.Type = "bool"; IntermediateCodeList.addVar(fl); Variable Edges = new Variable(); Edges.IsConst = true; Edges.AlternativeName = "const_" + (++Counters.consts).ToString(); Edges.Value = (int.Parse(graph.MainVariable.Value) - 1).ToString(); Edges.Type = "int"; IntermediateCodeList.addVar(Edges); Variable temp_arr = new Variable(); temp_arr.Value = graph.MainVariable.Value; temp_arr.Type = "array"; temp_arr.AlternativeName = "array_" + (++Counters.arrays).ToString(); IntermediateCodeList.addVar(temp_arr); Variable comps = new Variable(); comps.IsConst = true; comps.AlternativeName = "const_" + (++Counters.consts).ToString(); comps.Value = "1"; comps.Type = "int"; IntermediateCodeList.addVar(comps); //Считаем рёбра IntermediateCodeList.push(new CountEdgesInterNode(graph.MainVariable, temp)); //српавниваем IntermediateCodeList.push(new CmpNode(temp, Edges)); string label = "not_tree_" + (++Counters.comparsions).ToString(); //Если не совпадает - плохо IntermediateCodeList.push(new GoToLabel(label, "jne")); //Если пока совпадает, проверим на связность IntermediateCodeList.push(new NumComponentsCall(graph.MainVariable, temp_arr, temp)); IntermediateCodeList.push(new CmpNode(temp, comps)); //Если не совпадает - плохо IntermediateCodeList.push(new GoToLabel(label, "jne")); IntermediateCodeList.push(new AssignmentInterNode(tr, outVariable.MainVariable)); IntermediateCodeList.push(new GoToLabel(label + "_exit", "jmp")); IntermediateCodeList.push(new PutLabel(label)); IntermediateCodeList.push(new AssignmentInterNode(fl, outVariable.MainVariable)); IntermediateCodeList.push(new PutLabel(label + "_exit")); GeneratingAssembleCode.WasCountEdgesUsed = true; GeneratingAssembleCode.NumComponentsUsed = true; GeneratingAssembleCode.WasDFSUsed = true; }
public override void GenerateIntermediateCode() { IntermediateCodeList.addVar(MainVariable); IntermediateCodeList.push(new CreateGraphInterNode(MainVariable)); }
public override void GenerateIntermediateCode() { //добавили переменную MainVariable.IsConst = false;//не константа IntermediateCodeList.addVar(MainVariable); }