Skip to content

Commit

Permalink
四元式已测完 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
IceCapriccio committed Dec 9, 2018
1 parent e7d74de commit 5b2d8ad
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 120 deletions.
237 changes: 137 additions & 100 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
add_executable(Compiler main.cpp LexAnalyse.cpp LexAnalyse.h SyntaxAnalyse.cpp SyntaxAnalyse.h SemanticAnalyse.cpp SemanticAnalyse.h IntermediateCodeGenerate.h IntermediateCodeGenerate.cpp)
52 changes: 37 additions & 15 deletions IntermediateCodeGenerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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] = ;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
}

Expand Down
1 change: 1 addition & 0 deletions SyntaxAnalyse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions cmake-build-debug/CMakeFiles/Compiler.dir/CXX.includecache
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down
Binary file modified cmake-build-debug/CMakeFiles/Compiler.dir/SyntaxAnalyse.cpp.obj
Binary file not shown.
Binary file modified cmake-build-debug/CMakeFiles/Compiler.dir/objects.a
Binary file not shown.
Binary file modified cmake-build-debug/Compiler.exe
Binary file not shown.
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ int main() {
// freopen("CON", "w", stdout);
CodeSequence codeSequence = visitor.Visit(root);
codeSequence.show();
// new line
return 0;
}

0 comments on commit 5b2d8ad

Please sign in to comment.