diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dae313c..7850fc4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,37 +11,16 @@ - - - - + - - - - + - - - - - - - - - - - - - - - - + + + + + + + + + + - + @@ -115,8 +103,8 @@ - - + + @@ -125,37 +113,52 @@ - + - - + + - + - - + + - + - - + + + + + - + - - + + + + + + + + + + + + + + @@ -218,18 +221,18 @@ @@ -241,8 +244,8 @@ - @@ -269,14 +272,15 @@ + - + - + @@ -334,7 +338,7 @@ - + 1543414459497 @@ -357,21 +361,29 @@ - - - + + - + - + @@ -456,15 +468,21 @@ - + + file://$PROJECT_DIR$/IntermediateCodeGenerate.cpp + 169 + file://$PROJECT_DIR$/SyntaxAnalyse.cpp - 209 - @@ -638,6 +656,9 @@ + + + @@ -649,20 +670,6 @@ - - - - - - - - - - - - - - @@ -677,13 +684,6 @@ - - - - - - - @@ -698,17 +698,17 @@ - + - - + + - + - - + + @@ -722,31 +722,48 @@ - + - - + + + + + - - + + + + + + + + + + + + + + + + - - + + - - + + @@ -755,8 +772,28 @@ - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/CMakeLists.txt b/CMakeLists.txt index bb34894..0908678 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project(Compiler) set(CMAKE_CXX_STANDARD 14) #add_executable(Compiler main.cpp LexAnalyse.cpp LexAnalyse.h SyntaxAnalyse.cpp SyntaxAnalyse.h SemanticAnalyse.cpp SemanticAnalyse.h) -add_executable(Compiler main.cpp LexAnalyse.cpp LexAnalyse.h SyntaxAnalyse.cpp SyntaxAnalyse.h SemanticAnalyse.cpp SemanticAnalyse.h IntermediateCodeGenerate.cpp IntermediateCodeGenerate.h) \ No newline at end of file +add_executable(Compiler main.cpp LexAnalyse.cpp LexAnalyse.h SyntaxAnalyse.cpp SyntaxAnalyse.h SemanticAnalyse.cpp SemanticAnalyse.h IntermediateCodeGenerate.h IntermediateCodeGenerate.cpp) \ No newline at end of file diff --git a/IntermediateCodeGenerate.cpp b/IntermediateCodeGenerate.cpp index 30470d3..ce8190a 100644 --- a/IntermediateCodeGenerate.cpp +++ b/IntermediateCodeGenerate.cpp @@ -130,11 +130,7 @@ CodeSequence Visitor::VisitIfSentence(AbstractSyntaxTreeNode *root, int IfSenten total++; code.push_back(ElseCode); } else { // ElseSentence -> null - supportTable.LabelAssign(Expression_false, total + 1); // 直接到 IfSentence.next : - codeTerm = CodeTerm(DefLabel, -1, -1, Expression_false); - code.push_back(codeTerm); // {expression.false :} - total++; - code.push_back(ElseCode); + supportTable.LabelAssign(Expression_false, total); } // IfSentence.code = Expression.code || "Expression.true :" || CompoundStatement.code || // "Expression.false: " || ElseSentence.code @@ -159,12 +155,23 @@ CodeSequence Visitor::VisitSentence(AbstractSyntaxTreeNode *root) { if (root->child[0]->info.value == "IfSentence") { // Sentence -> IfSentence IfCode = VisitIfSentence(root->child[0], next); code.push_back(IfCode); + supportTable.LabelAssign(next, total); + CodeTerm codeTerm(DefLabel, -1, -1, next); + code.push_back(codeTerm); + total++; } else if (root->child[0]->info.value == "WhileSentence") { // Sentence -> WhileSentence WhileCode = VisitWhileSentence(root->child[0], next); code.push_back(WhileCode); + supportTable.LabelAssign(next, total); + CodeTerm codeTerm(DefLabel, -1, -1, next); + code.push_back(codeTerm); + total++; } else if (root->child[0]->info.value == "ForSentence") { // Sentence -> ForSentence ForCode = VisitForSentence(root->child[0], next); code.push_back(ForCode); + CodeTerm codeTerm(DefLabel, -1, -1, next); + code.push_back(codeTerm); + total++; } else if (root->child[0]->info.value == "ScanfSentence") { // Sentence -> ScanfSentence ScanfCode = VisitScanfSentence(root->child[0]); code.push_back(ScanfCode); @@ -180,7 +187,6 @@ CodeSequence Visitor::VisitSentence(AbstractSyntaxTreeNode *root) { AssignCode = VisitAssignmentStatement(root->child[0], -1, -1); code.push_back(AssignCode); } - supportTable.LabelAssign(next, total); return code; } @@ -217,6 +223,12 @@ CodeSequence Visitor::VisitForSentence(AbstractSyntaxTreeNode *root, int ForSent // child[1] = ( Exp1Code = VisitExpression(root->child[2], -1, -1); code.push_back(Exp1Code); + if(Exp1Code.getOp() == Assignment && Exp1Code.getParam1() && Exp1Code.getParam2()) + { + CodeTerm codeTerm1(Exp1Code.getOp(),Exp1Code.getParam2(),-1,Exp1Code.getParam1()); + code.push_back(codeTerm1); + total++; + } int expression2_true = supportTable.NewLabel(); int expression2_false = ForSentence_next; // child[3] = ; @@ -234,6 +246,12 @@ CodeSequence Visitor::VisitForSentence(AbstractSyntaxTreeNode *root, int ForSent // child[8] = CompoundStatement Exp3Code = VisitExpression(root->child[6], -1, -1); code.push_back(Exp3Code); + if(Exp3Code.getOp() == Assignment && Exp3Code.getParam1() && Exp3Code.getParam2()) + { + CodeTerm codeTerm1(Exp3Code.getOp(),Exp3Code.getParam2(),-1,Exp3Code.getParam1()); + code.push_back(codeTerm1); + total++; + } return code; } @@ -299,12 +317,15 @@ CodeSequence Visitor::VisitExpression(AbstractSyntaxTreeNode *node,int Expressio //} if(code.getParam1() != -1 && code.getParam2() != -1 && code.getOp() != Number) { - int dest = supportTable.NewLabel(); - CodeTerm codeTerm(code.getOp(),code.getParam1(),code.getParam2(),dest); - code.setParam1(dest); - total++; - code.push_back(codeTerm); - code.setParam2(-1); + if(code.getOp() != Assignment) + { + int dest = supportTable.NewLabel(); + CodeTerm codeTerm(code.getOp(),code.getParam1(),code.getParam2(),dest); + code.setParam1(dest); + total++; + code.push_back(codeTerm); + code.setParam2(-1); + } } if(ExpressionS.getParam3() != -1 && ExpressionS.getOp1() != None) { @@ -315,7 +336,7 @@ CodeSequence Visitor::VisitExpression(AbstractSyntaxTreeNode *node,int Expressio total++; if(Expression_true != -1) { - CodeTerm codeGoto(JEqual,code.getParam2(),-2,Expression_true); + CodeTerm codeGoto(JEqual,dest,-2,Expression_true); total++; code.push_back(codeGoto); CodeTerm codeFalse(Goto,-1,-1,Expression_false); @@ -634,8 +655,9 @@ void CodeSequence::show() { cout << "SymbolTable:" << endl; SyntaxAnalyse::symbolTable.show(); cout << "TAC:" << endl; - for (auto term : code) { - term.show(); + for (int i = 0; i < code.size(); i++) { + printf("%2d ", i); + code[i].show(); } } diff --git a/SyntaxAnalyse.cpp b/SyntaxAnalyse.cpp index 3086502..5152b0c 100644 --- a/SyntaxAnalyse.cpp +++ b/SyntaxAnalyse.cpp @@ -249,6 +249,7 @@ AbstractSyntaxTreeNode* SyntaxAnalyse::ParseWhileSentence() { AbstractSyntaxTreeNode* SyntaxAnalyse::ParseForSentence() { auto root = new AbstractSyntaxTreeNode; + root->info.value = "ForSentence"; switch (lex[cur].type) { case ForKey: root->child[root->childNum++] = MatchToken(ForKey, false); diff --git a/cmake-build-debug/CMakeFiles/Compiler.dir/CXX.includecache b/cmake-build-debug/CMakeFiles/Compiler.dir/CXX.includecache index 825f935..9916c43 100644 --- a/cmake-build-debug/CMakeFiles/Compiler.dir/CXX.includecache +++ b/cmake-build-debug/CMakeFiles/Compiler.dir/CXX.includecache @@ -32,10 +32,6 @@ C:/Users/IceCapriccio/CLionProjects/Compiler/LexAnalyse.h vector - -C:/Users/IceCapriccio/CLionProjects/Compiler/SyntaxAnalyse.cpp -SyntaxAnalyse.h -C:/Users/IceCapriccio/CLionProjects/Compiler/SyntaxAnalyse.h - C:/Users/IceCapriccio/CLionProjects/Compiler/SyntaxAnalyse.h string - diff --git a/cmake-build-debug/CMakeFiles/Compiler.dir/SyntaxAnalyse.cpp.obj b/cmake-build-debug/CMakeFiles/Compiler.dir/SyntaxAnalyse.cpp.obj index b7a8dc7..7b9587c 100644 Binary files a/cmake-build-debug/CMakeFiles/Compiler.dir/SyntaxAnalyse.cpp.obj and b/cmake-build-debug/CMakeFiles/Compiler.dir/SyntaxAnalyse.cpp.obj differ diff --git a/cmake-build-debug/CMakeFiles/Compiler.dir/objects.a b/cmake-build-debug/CMakeFiles/Compiler.dir/objects.a index a6c583d..776e63b 100644 Binary files a/cmake-build-debug/CMakeFiles/Compiler.dir/objects.a and b/cmake-build-debug/CMakeFiles/Compiler.dir/objects.a differ diff --git a/cmake-build-debug/Compiler.exe b/cmake-build-debug/Compiler.exe index c0cfa53..f9b2989 100644 Binary files a/cmake-build-debug/Compiler.exe and b/cmake-build-debug/Compiler.exe differ diff --git a/main.cpp b/main.cpp index 96eb959..188570a 100644 --- a/main.cpp +++ b/main.cpp @@ -18,5 +18,6 @@ int main() { // freopen("CON", "w", stdout); CodeSequence codeSequence = visitor.Visit(root); codeSequence.show(); + // new line return 0; } \ No newline at end of file