Skip to content

Commit

Permalink
fix bugs for array calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
qihaoyu committed May 20, 2021
1 parent 410c1c8 commit a03cf68
Show file tree
Hide file tree
Showing 25 changed files with 187 additions and 371 deletions.
26 changes: 13 additions & 13 deletions IntermediateCode.cpp → InterCode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "IntermediateCode.h"
#include "InterCode.h"

NewLabeler::NewLabeler() {
index = 1;
Expand All @@ -9,7 +9,7 @@ string NewLabeler::newLabel () {
return string("Label") + to_string(index++);
}

void IntermediateCode::divideBlocks(vector<pair<int, string> > funcEnter) {
void InterCode::divideBlocks(vector<pair<int, string> > funcEnter) {
//对每一个函数块
for (vector<pair<int, string> >::iterator iter = funcEnter.begin(); iter != funcEnter.end(); iter++) {
vector<Block>blocks;
Expand Down Expand Up @@ -126,7 +126,7 @@ void IntermediateCode::divideBlocks(vector<pair<int, string> > funcEnter) {
}
}

void IntermediateCode::output(ostream& out) {
void InterCode::output(ostream& out) {
int i = 0;
for (vector<Quaternary>::iterator iter = code.begin(); iter != code.end(); iter++, i++) {
out << setw(4) << i;
Expand All @@ -138,7 +138,7 @@ void IntermediateCode::output(ostream& out) {
}
}

void IntermediateCode::outputBlocks(ostream& out) {
void InterCode::outputBlocks(ostream& out) {
for (map<string, vector<Block> >::iterator iter = funcBlocks.begin(); iter != funcBlocks.end(); iter++) {
out << "[" << iter->first << "]" << endl;
for (vector<Block>::iterator bIter = iter->second.begin(); bIter != iter->second.end(); bIter++) {
Expand All @@ -153,25 +153,25 @@ void IntermediateCode::outputBlocks(ostream& out) {
}
}

void IntermediateCode::emit(Quaternary q) {
void InterCode::emit(Quaternary q) {
code.push_back(q);
}

void IntermediateCode::emit(string op, string src1, string src2, string des) {
void InterCode::emit(string op, string src1, string src2, string des) {
emit(Quaternary{ op,src1,src2,des });
}

void IntermediateCode::back_patch(list<int>nextList, int quad) {
void InterCode::back_patch(list<int>nextList, int quad) {
for (list<int>::iterator iter = nextList.begin(); iter != nextList.end(); iter++) {
code[*iter].des = to_string(quad);
}
}

void IntermediateCode::output() {
void InterCode::output() {
output(cout);
}

void IntermediateCode::output(const char* fileName) {
void InterCode::output(const char* fileName) {
ofstream fout;
fout.open(fileName);
if (!fout.is_open()) {
Expand All @@ -183,11 +183,11 @@ void IntermediateCode::output(const char* fileName) {
fout.close();
}

void IntermediateCode::outputBlocks() {
void InterCode::outputBlocks() {
outputBlocks(cout);
}

void IntermediateCode::outputBlocks(const char* fileName) {
void InterCode::outputBlocks(const char* fileName) {
ofstream fout;
fout.open(fileName);
if (!fout.is_open()) {
Expand All @@ -199,10 +199,10 @@ void IntermediateCode::outputBlocks(const char* fileName) {
fout.close();
}

int IntermediateCode::nextQuad() {
int InterCode::nextQuad() {
return code.size();
}

map<string, vector<Block> >* IntermediateCode::getFuncBlock() {
map<string, vector<Block> >* InterCode::getFuncBlock() {
return &funcBlocks;
}
2 changes: 1 addition & 1 deletion IntermediateCode.h → InterCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NewLabeler {
string newLabel();
};

class IntermediateCode {
class InterCode {
private:
vector<Quaternary> code;
map<string, vector<Block> >funcBlocks;
Expand Down
Binary file added InterCode.o
Binary file not shown.
Binary file removed IntermediateCode.o
Binary file not shown.
201 changes: 0 additions & 201 deletions LICENSE

This file was deleted.

24 changes: 12 additions & 12 deletions LexicalAnalyser.cpp → Lexical.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "LexicalAnalyser.h"
#include "Lexical.h"

string token_to_string(Token t) {
const char* LexicalTypeStr[] = {
Expand Down Expand Up @@ -41,18 +41,18 @@ string token_to_string(Token t) {
return res;
}

LexicalAnalyser::LexicalAnalyser(const char* path) {
Lexical::Lexical(const char* path) {
lineCount = 0;
openFile(path);
}

LexicalAnalyser::~LexicalAnalyser() {
Lexical::~Lexical() {
if (src.is_open()) {
src.close();
}
}

void LexicalAnalyser::openFile(const char* path) {
void Lexical::openFile(const char* path) {
src.open(path, ios::in);
if (!src.is_open()) {
cerr << "file " << path << " open error" << endl;
Expand All @@ -61,7 +61,7 @@ void LexicalAnalyser::openFile(const char* path) {
}

//获取下一个字符,忽略空白符
char LexicalAnalyser::getNextChar() {
char Lexical::getNextChar() {
char c;
while (src >> c) {
if (c == ' '||c=='\t') {
Expand All @@ -79,7 +79,7 @@ char LexicalAnalyser::getNextChar() {
return c;
}

Token LexicalAnalyser::getNextToken() {
Token Lexical::getNextToken() {
char c = getNextChar();
switch (c) {
case '\n':
Expand Down Expand Up @@ -244,8 +244,8 @@ Token LexicalAnalyser::getNextToken() {
return Token(ERROR, "UNKOWN ERROR");
}

// LexicalAnalyser::result存放解析后的Token列表
void LexicalAnalyser::analyse() {
// Lexical::result存放解析后的Token列表
void Lexical::analyse() {
while (1) {
Token t = getNextToken();
result.push_back(t);
Expand All @@ -258,7 +258,7 @@ void LexicalAnalyser::analyse() {
}
}

void LexicalAnalyser::outputToStream(ostream&out) {
void Lexical::outputToStream(ostream&out) {
if (result.back().first == ERROR) {
out << token_to_string(result.back())<<endl;
}
Expand All @@ -270,11 +270,11 @@ void LexicalAnalyser::outputToStream(ostream&out) {
}
}

void LexicalAnalyser::outputToScreen() {
void Lexical::outputToScreen() {
outputToStream(cout);
}

void LexicalAnalyser::outputToFile(const char *fileName) {
void Lexical::outputToFile(const char *fileName) {
ofstream fout;
fout.open(fileName, ios::out);
if (!fout.is_open()) {
Expand All @@ -285,6 +285,6 @@ void LexicalAnalyser::outputToFile(const char *fileName) {
fout.close();
}

list<Token>&LexicalAnalyser::getResult() {
list<Token>&Lexical::getResult() {
return result;
}
Loading

0 comments on commit a03cf68

Please sign in to comment.