Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My question is to see if anyone can translate this C++ program into a Python pro

ID: 642539 • Letter: M

Question

My question is to see if anyone can translate this C++ program into a Python program. I have already completed it in C++ and python. Thank you.

#include<fstream>
#include<iostream>
#include<string>
#define CALL_STACK_MAX 200
using namespace std;

enum cmdType {C_ARITHMETIC, C_PUSH, C_POP, C_LABEL, C_GOTO, C_IF, C_FUNCTION, C_RETURN, C_CALL};

class Parser{
fstream VM;
int lineNumber;
string currentCommand;
public:
Parser(char* filename){
VM.open(filename,ios::in);
if(VM.fail()){
cout << filename << " cannot be found Please check whether you have entered the correct pathname ";
getchar(); exit(0);
}
VM.unsetf(ios::skipws);
lineNumber=0;
}
~Parser(){VM.close();}
bool hasMoreCommands(){
if(VM.fail()) return false;
return true;
}
int advance();
cmdType commandType();
void argument(string&,int&);
};

class CodeWriter{
fstream Asm;
string fileName, funcName[CALL_STACK_MAX];
int trueCount,callCount,curFunction;
public:
CodeWriter(char* filename){
fileName=filename;
string file=fileName;
file.erase(file.size()-2,2);
file+="asm";
Asm.open(file.c_str(),ios::out);
fileName.erase(0,fileName.find_last_of('/')+1);
trueCount=0;
curFunction=0;
callCount=0;
}
~CodeWriter(){
Asm << "(INFINITE_LOOP) @INFINITE_LOOP 0;JEQ ";
Asm.close();
}
void writeInit();
void writeArithmetic(string);
void writePushPop(cmdType,string,int);
void writeLabel(string);
void writeGoto(string);
void writeIf(string);
void writeCall(string,int);
void writeReturn();
void writeFunction(string,int);
};

int Parser::advance(){
char ch;
string funcCommand;
currentCommand.erase(0,currentCommand.size());
while(hasMoreCommands()){
VM >> ch;
if(ch==' '){
if(currentCommand.size()!=0){lineNumber++; break;}
else continue;
}
if(ch!=' '&&ch!=' '&&ch!=' '&&ch!=' '){currentCommand+=ch; funcCommand+=ch;}
if(ch==' '||ch==' '){funcCommand+=' ';}
if(currentCommand[currentCommand.size()-1]=='/'&&currentCommand[currentCommand.size()-2]=='/'){
currentCommand.erase(currentCommand.size()-2, 2);
funcCommand.erase(funcCommand.size()-2,2);
VM >> ch;
while(ch!=' ') VM >> ch;
if(currentCommand.size()!=0){
lineNumber++; break;}
}
}
if(!hasMoreCommands()) return -1;
if(commandType()==C_FUNCTION||commandType()==C_CALL) currentCommand=funcCommand;
return 0;
}

cmdType Parser::commandType(){
if(currentCommand=="add"||currentCommand=="sub"||currentCommand=="neg"||
currentCommand=="and"||currentCommand=="not"||currentCommand=="eq"||
currentCommand=="gt"||currentCommand=="lt"||currentCommand=="or")
return C_ARITHMETIC;
if(currentCommand.find("push")==0) return C_PUSH;
if(currentCommand.find("pop")==0) return C_POP;
if(currentCommand.find("label")==0) return C_LABEL;
if(currentCommand.find("goto")==0) return C_GOTO;
if(currentCommand.find("if-goto")==0) return C_IF;
if(currentCommand.find("function")==0) return C_FUNCTION;
if(currentCommand.find("call")==0) return C_CALL;
if(currentCommand=="return") return C_RETURN;
cout << lineNumber << ": Invalid command ";
getchar();
exit(0);
}

void Parser::argument(string& arg1, int& arg2){
string arg=currentCommand;
if(commandType()==C_ARITHMETIC)arg1=arg;
else if(commandType()==C_PUSH||commandType()==C_POP){
if(commandType()==C_PUSH) arg.erase(0,4);
else arg.erase(0,3);
if(arg.find("argument")==0) arg1="argument";
else if(arg.find("local")==0) arg1="local";
else if(arg.find("static")==0) arg1="static";
else if(arg.find("this")==0) arg1="this";
else if(arg.find("that")==0) arg1="that";
else if(arg.find("pointer")==0) arg1="pointer";
else if(arg.find("temp")==0) arg1="temp";
else if(arg.find("constant")==0) arg1="constant";
else { cout << lineNumber << ": Segment name missing in a Push/Pop instruction ";
getchar();
exit(0); }
arg.erase(0,arg1.size());
if(arg.size()==0) {
cout << lineNumber << ": Segment index missing in a Push/Pop instruction ";
getchar();
exit(0);
}
arg2=0;
for(int i=0;i<arg.size();i++)
arg2=(arg2*10+arg[i]-'0');
}
else if(commandType()==C_LABEL){ arg.erase(0,5); arg1=arg; }
else if(commandType()==C_GOTO){ arg.erase(0,4); arg1=arg; }
else if(commandType()==C_IF){ arg.erase(0,7); arg1=arg; }
else if(commandType()==C_FUNCTION||commandType()==C_CALL){
if(commandType()==C_FUNCTION) arg.erase(0,9);
else arg.erase(0,5);
arg1=arg;
arg.erase(0,arg.find(" ")+1);
arg1.erase(arg1.find(" "),arg.size()+1);
if(arg.size()==0) {
cout << lineNumber << ": Number of arguments not specified in a Function/Call instruction ";
getchar();
exit(0);
}
arg2=0;
for(int i=0;i<arg.size();i++)
arg2=(arg2*10+arg[i]-'0');
}
}

void CodeWriter::writeInit(){
Asm << "@256 D=A @SP M=D )";
writeCall("Sys.init",0);
}

void CodeWriter::writeArithmetic(string command){
Asm << "@SP A=M-1 ";
if(command=="neg") Asm << "M=-M ";
else if(command=="not") Asm << "M=!M ";
else{
Asm << "D=M @SP AM=M-1 M=0 A=A-1 ";
if(command=="add") Asm << "M=M+D ";
else if(command=="sub") Asm << "M=M-D ";
else if(command=="and") Asm << "M=M&D ";
else if(command=="or") Asm << "M=M|D ";
else{
Asm << "D=M-D @TRUE" << trueCount << " D;J";
if(command=="eq") Asm << "EQ";
else if(command=="lt") Asm << "LT";
else if(command=="gt") Asm << "GT";
Asm << " @SP A=M-1 M=0 @FALSE" << trueCount << " 0;JEQ (TRUE" << trueCount
<< ") @SP A=M-1 M=-1 " << "(FALSE" << trueCount << ") ";
trueCount++;
}
}
}

void CodeWriter::writePushPop(cmdType command, string segment, int index){
if(segment=="static"&&index>240){
cout << "Index of a static variable > 240 "; getchar(); exit(0); }
string strIndex;
strIndex+=(index+'0');
if(command==C_POP&&segment=="constant"){
cout << "Popping into a constant is invalid ";
getchar();
exit(0);
}
if(segment=="local") Asm << "@LCL D=M";
else if(segment=="argument") Asm << "@ARG D=M";
else if(segment=="this") Asm << "@THIS D=M";
else if(segment=="that") Asm << "@THAT D=M";
else if(segment=="temp") Asm << "@5 D=A";
else if(segment=="pointer") Asm << "@3 D=A";
else if(segment=="static") Asm << "@" << fileName << "." << index << " D=A";
else if(segment=="constant"){
if(index==0||index==1) Asm << " D=" << index;
else Asm << "@" << index << " D=A";
}
if(segment!="static"&&segment!="constant"&&index!=0)
Asm << " @" << index << " D=D+A";
if(command==C_PUSH){
if(segment=="constant") Asm << " @SP AM=M+1 A=A-1 M=D ";
else Asm << " A=D D=M @SP AM=M+1 A=A-1 M=D ";
}
else Asm << " @R13 M=D @SP AM=M-1 D=M M=0 @R13 A=M M=D ";
}

void CodeWriter::writeLabel(string label){
Asm << "(" << funcName[curFunction-1] << "." << label << ") ";
}

void CodeWriter::writeGoto(string label){
Asm << "@" << funcName[curFunction-1] << "." << label << " 0;JMP ";
}

void CodeWriter::writeIf(string label){
Asm << "@SP A=M-1 D=M @" << funcName[curFunction-1] << "." << label
<< " D=D+1;JEQ ";
}

void CodeWriter::writeCall(string functionName,int numArgs){
callCount++;
string pushString=" D=A @SP AM=M+1 A=A-1 M=D ";
Asm << "@RETURN_ADDRESS." << callCount << pushString
<< "@LCL" << pushString
<< "@ARG" << pushString
<< "@THIS" << pushString
<< "@THAT" << pushString
<< "@SP D=A @" << numArgs << " D=D-A @5 D=D-A @ARG M=D "
<< "@SP D=A @LCL M=D ";
writeGoto(functionName);
Asm << "(" << "RETURN_ADDRESS." << callCount << ") ";
funcName[curFunction++]=functionName;
}

void CodeWriter::writeReturn(){
Asm << "@LCL D=A @R13 M=D "
<< "@5 AD=D-A D=M @R14 M=D "
<< "@SP M=M-1 A=M D=M M=0 @ARG M=D "
<< "@ARG D=A+1 @SP M=D "
<< "@R13 D=M-1 @THAT M=D "
<< "@THIS DM=D-1 "
<< "@ARG DM=D-1 "
<< "@LCL DM=D-1 "
<< "@R14 A=M 0;JMP ";
curFunction--;
}

void CodeWriter::writeFunction(string functionName, int numLocals){
Asm << "@" << numLocals << " D=A "
<< "(" << funcName[curFunction-1] << "." << functionName << ") "
<< "@" << funcName[curFunction-1] << "." << functionName << " "
<< "@SP AM=M+1 A=A-1 M=0 "
<< "D=D-1;JEQ";
}

void translate(char* filename){
Parser p(filename);
CodeWriter c(filename);
while(p.hasMoreCommands()){
string command;
int index=-1;
if(p.advance()==-1) return;
if(p.commandType()!=C_RETURN)
p.argument(command, index);
if(p.commandType()==C_ARITHMETIC){
if(index==-1) c.writeArithmetic(command);
else{
cout << "Invalid Arithmetic Command "; getchar(); exit(0);
}
}
else if(p.commandType()==C_PUSH||p.commandType()==C_POP)
c.writePushPop(p.commandType(),command,index);
}
}

int main(int argc, char** argv){
if(argc==2){
translate(argv[1]);
cout << argv[1] << " successfully translated ";}
else
cout << "Correct Syntax is: ./VMtranslator.out <filename> ";
getchar();
return 0;
}

Explanation / Answer

Hi, I can do a high quality guaranteed A+ solution for you. Trust me once and I will help you forever. Just follow the instruction here: http://programmingtutor3.tk