mercredi 28 octobre 2020

What is program received signal sigsegv segmentation fault? [duplicate]

I'm working on a project which involves creating a very simple compiler. I currently have 2 versions of my code. Neither work properly, but the messier one gets a lot further than the better organized one. Both will be included as well as the file I'm using for testing.

I've tried using the debug function in dev-c++ but don't at all understand it. All I know is it returns "program received signal sigsegv segmentation fault"

Any help would be greatly appreciated y'all.

This code snippet is the entire first version, it's excessively hard to read, and the only reason I included it was because it was useful for comparison to my second version.

#include<iostream>
#include<array>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<typeinfo>
#include<ctype.h>
using namespace std;

vector <char> spacer(string &input) {
    vector <char> var;
    for(int i = 0; i < input.size(); i++) {
        var.push_back(input[i]);
        var.push_back(' ');
    }
    return var;
}



string ifHandler(vector<string> &com, array <string, 100> &SML, int &InstPtr, int (&gotoPlace)[100] 
[2], int &gotoCtr, string dataTable[300][3], int &datCtr) {
    //string con=to_string(com[2]);
    //stringstream(com[1])>>con;
    vector<char> condition=spacer(com[2]);
    int caser;
    int loc;
    for (int i = 0; i < condition.size(); i++) {
        if (condition[i] == '!') {
            loc=i;
            caser = 1;  
            break;
        }
        else if(condition[i] == '=') {
            loc=i;
            caser = 2;
            break;
        }
        else if(condition[i] == '<') {
            loc=i;
            if(condition[i + 2] == '=') {
                caser = 5;
            }
            else {
                caser = 3;
            }
            break;
        }
        else if(condition[i] == '>') {
            loc=i;
            if (condition[i + 2] == '=') {
                caser = 6;
            }
            else {
                caser = 4;
            }
            break;
        }
     }
     switch(caser) {
        case 1:
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+4]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+4]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                        SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            break;
        case 2:
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+4]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            break;
        case 3:
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            break;
        case 4:
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            break;
        case 5:
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+4]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="41"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="410"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="41";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            break;
        case 6:
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc+4]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="20"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="200"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            for(int it=0;it<300;it++){
                if(dataTable[it][0][0]==condition[loc-2]){
                    if(dataTable[it][2].size()>1){
                        SML[InstPtr]="31"+dataTable[it][2];
                    }else{
                        SML[InstPtr]="310"+dataTable[it][2];
                    }
                    InstPtr++;
                    break;
                }
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="41"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="410"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="41";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            if(com[4]<com[0]){
                for(int it=0;it<300;it++){
                    if (dataTable[it][2]==com[4]){
                        if(dataTable[it][2].size()>1){
                            SML[InstPtr]="42"+dataTable[it][2];
                        }else{
                            SML[InstPtr]="420"+dataTable[it][2];
                        }
                        InstPtr++;
                    }
                }
            }else{
                SML[InstPtr]="42";
                stringstream(com[4])>>gotoPlace[gotoCtr][0];
                gotoPlace[gotoCtr][1]=InstPtr;
                gotoCtr++;
                InstPtr++;
            }
            break;
    }
    cout<<"Not struggling with if"<<endl;
}



string compressor(string &input) { // we can pass a substring of the string to the method if we dont        want the whole thing to be compressed
    for (int i = 0; i < input.size(); i++) {
        if (input[i] == ' ') {
            input.erase(i,1); // erases the space at index i
        }
    }
    return input;
}



int gotoFix(array<string,100> &SML, string (&datTab)[300][3], int (&got)[100][2]){
    for(int i=0; i<sizeof(got);i++){
        for(int it=0; it<sizeof(datTab);it++){
            if(datTab[it][0]==""){
                break;
            }
            if(datTab[it][0]==to_string(got[i][0])){
                if(datTab[it][2].size()<2){
                    SML[got[i][1]]+="0"+datTab[it][2];
                }else{
                    SML[got[i][1]]+=datTab[it][2];
                }
            }
        }
    }
}



vector<string> parse(string &line){   //Takes the line and separates all of it's elements
    string parser="";                 //A placeholder string used to store each word into a vector
    vector<string> parsed;            //The Return vector
    char space = ' ';                
    for(int i=0; i<line.size();i++){   //If the character is not a space it is added into parser
        if (line[i]!=space){
            parser+=line[i];
        }else {                        //If the character is a space, parser is added into the vector, and is then reset
            parsed.push_back(parser);
            parser="";
        }
        if(i==(line.size()-1)){        //When the last character is reached, parser needs to be added into the vector, or it would be left out entirely
            parsed.push_back(parser);
        }
    }
    return parsed;
}



int registry(string command,int &DataCtr,int &InstPtr,int &symCtr){     //Used to determine what command is being worked with
    int literal;
    if(command==""){      //Absences are just here for blank lines
        return 5;
    }else if(command=="rem"){   //If command is rem, we stop the loop because content after rem is useless to the assembler
        return 5;
    }else if(command=="input"){   //If command is input, sets up the assembler command
        symCtr++;
        return 0;
    } else if(command=="print"){   //If command is print, sets up the assembler command
        return 1;
    }else if(command=="end"){   //If primary command is end, completely stops processing data
        return 2;
    }else if(command=="goto"){
        return 6;
    }
    else if(command=="if"){
        
        return 7;
    }else{
        if(isdigit(command[0])){   //Checks for a literal
            return 3;
        }else{   //if no literal is found, just establishes a variable
            return 4;
        }
    }
}



int maneuver(string (&arr)[300][3], vector<string> &com, int &line,int &DatCtr,int &InstPtr,array<string,100> &SML,int (&got)[100][2], int &gotoCtr){   //This creates the data table
    if (com[0]==""){
        return 1;
    }
    cout<<com[1]<<endl;
    line++;
    arr[line][0]=com[0];   //Processes line numbers
    arr[line][1]="L";
    arr[line][2]=to_string(InstPtr);
    for(int i=1;i<com.size();i++){     //This passes each part of the vector the function was passed to the registry for processing
        int retStat=registry(com[i],DatCtr,InstPtr,line);
        switch(retStat){
            case 0: 
                cout<<"input"<<endl;
                SML[InstPtr]="10"+to_string(DatCtr);
                InstPtr++;
                break;
            case 1:
                for(i=0;i<300;i++){
                    if(com[2]==arr[i][0]){
                        SML[InstPtr]="11"+arr[i][2];
                        i=300;
                    }
                }
                InstPtr++;
                break;
            case 2:
                cout<<"end"<<endl;
                SML[InstPtr]="4300";
                InstPtr++;
                i=com.size();
                break;
            case 3:
                cout<<"literal"<<endl;
                arr[line][0]=com[i];
                arr[line][1]="N";
                arr[line][2]=to_string(DatCtr);
                DatCtr--;
                break;
            case 4:
                cout<<"variable"<<endl;
                arr[line][0]=com[i];
                arr[line][1]="V";
                arr[line][2]=to_string(DatCtr);
                DatCtr--;
                break;
            case 5:
                cout<<"empty"<<endl;
                return 0;
            case 6:
                cout<<"goto"<<endl;
                if(com[0]<com[2]){
                    SML[InstPtr]="40";
                    stringstream(com[2])>>got[gotoCtr][0];
                    got[gotoCtr][1]=InstPtr;
                    gotoCtr++;
                }else{
                    for(i=0;i<300;i++){
                        if(com[2]==arr[i][0]){
                            if(arr[i][2].size()==1){
                                SML[InstPtr]="400"+arr[i][2];
                                i=300;
                            }else{
                                SML[InstPtr]="40"+arr[i][2];
                                i=300;
                            }
                        }
                    }   
                }
                InstPtr++;
                i=com.size();
                break;
            case 7:
                cout<<"if"<<endl;
                ifHandler(com, SML, InstPtr, got, gotoCtr, arr, DatCtr);
                cout<<"Made it through the if handler"<<endl;
                i=com.size();
                break;
        }
    }   
}



int main(){                  
    int currentDataCtr=99;   //Position for storing data
    int currentInstPtr=0;    //Position for storing commands
    int currentSymCtr=0;     //Position in the symbol table
    int gotoPlace[100][2]={0};
    int gotoCtr=0;
    array<string,100> SML;
    string baseCommand;
    string name;
    cout<<"Please enter the name of the file you would like compiled, \nnote it must be in the same folder as this program. (Example: test.txt)"<<endl;
    getline(cin,name);
    string ofile;
    cout<<"\nPlease enter the name of the file you would like the symbol table entered into, \nnote it will be in the same folder as the program. (Example: out.txt)"<<endl;
    getline(cin,ofile);
    string mfile;
    cout<<"\n Please enter the name of hte file you would like the machine code stored in, \nnote it will be in the same folder as the program. (Example: SMLCode.txt)"<<endl;
    getline(cin,mfile);
    ifstream baseFile(name);   //opens the file
    string dataTable[300][3];  //creates the data table (which is arbitrarily sized)
    dataTable[0][0]="Symbol";  //These 3 just create titles because titles are nice
    dataTable[0][1]="Type";
    dataTable[0][2]="Location";
    ofstream machFile(mfile);  //creates the outfile for the machine code
    while(getline(baseFile,baseCommand)){   //Pulls the lines from the infile and performs all methods on it
        vector<string> command=parse(baseCommand);
        cout<<command[1]<<endl;
        int condition=maneuver(dataTable,command,currentSymCtr,currentDataCtr,currentInstPtr,SML,gotoPlace,gotoCtr);
        if (currentDataCtr<=currentInstPtr){  //Checks to make sure data and commands dont ovelap
            cout<<"This code is too long, and is running into issues of overlapping in memory."<<endl;
            exit(EXIT_FAILURE);
        }
        if(condition==1){  //Handles end
            break;
        }
    }
    baseFile.close();
    ofstream outFile(ofile);// all of this just prints the data table into a file
    outFile<<dataTable[0][0]<<" "<<dataTable[0][1]<<" "<<dataTable[0][2]<<endl;
    for (int i=1;i<=currentSymCtr;i++){
        for(int it=0;it<3;it++){
            outFile<<endl;
        }
    }
    gotoFix(SML,dataTable,gotoPlace);
    for (int i=0;i<currentInstPtr;i++){
        machFile<<SML[i]<<endl;
    }
    machFile.close();
    outFile.close();
    return 0;
}

Included below is the second revision, which is the one I would prefer to continue using, it however does not get nearly as far as the first does. It is however much shorter, and easier to read so.

#include<iostream>
#include<array>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<typeinfo>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;




//Parse lines to create commands
vector<string> parse(string &line){   //Takes the line and separates all of it's elements
    string parser="";                 //A placeholder string used to store each word into a vector
    vector<string> parsed;            //The Return vector
    char space = ' ';                
    for(int i=0; i<line.size();i++){   //If the character is not a space it is added into parser
        if (line[i]!=space){
            parser+=line[i];
        }else {                        //If the character is a space, parser is added into the vector, and is then reset
            parsed.push_back(parser);
            parser="";
        }
        if(i==(line.size()-1)){        //When the last character is reached, parser needs to be added into the vector, or it would be left out entirely
            parsed.push_back(parser);
        }
    }
    return parsed;
}





//Command to parse conditions (and maybe math one day)
vector <char> spacer(string &input) {
    vector <char> var;
    for(int i = 0; i < input.size(); i++) {
        var.push_back(input[i]);
        var.push_back(' ');
    }
    return var;
}




//Performs gotos
int gotos(int gotoNum, int type, int (&gotoArr)[100][2], int &gotoCtr, int lineNum, array<string,100> &SML, int &InstPtr, string (&dataTable)[300][3]){
    if(lineNum<gotoNum){
        SML[InstPtr]=to_string(type);
        gotoArr[gotoCtr][0]=gotoNum;
        gotoArr[gotoCtr][1]=InstPtr;
        gotoCtr++;
    }else{
        for(int i=0;i<300;i++){
            if(to_string(gotoNum)==dataTable[i][0]){
                if(dataTable[i][2].size()==1){
                    SML[InstPtr]=to_string(type)+"0"+dataTable[i][2];
                    i=300;
                }else{
                    SML[InstPtr]=to_string(type)+dataTable[i][2];
                    i=300;
                }
            }
        }   
    }
    InstPtr++;
}





//Performs conditional statements for the sml creater
int condi(array<string,100> &SML, int &InstPtr, string (&dataTable)[300][3], string item, string type){
    for(int it=0;it<300;it++){
        if(dataTable[it][0]==item){
            if(dataTable[it][2].size()>1){
                SML[InstPtr]=type+dataTable[it][2];
            }else{
                SML[InstPtr]=type+"0"+dataTable[it][2];
            }
            InstPtr++;
            break;
        }
    }       
}




//Perform the commands
int enact(string (&dataTable)[300][3],vector<string> &com,int &symCtr, int &dataCtr, int &InstPtr,array<string,100> &SML, int (&gotoPlace)[100][2], int &gotoCtr){
    if(com[0]==""){
        return 1;
    }
    dataTable[symCtr][0]=com[0];
    dataTable[symCtr][1]="L";
    dataTable[symCtr][2]=to_string(InstPtr);
    symCtr++;
    for(int i=0;i<com.size();i++){
        if(com[i]=="rem"){
            i=com.size();
        }else if(com[i]=="input"){
            SML[InstPtr]="10"+to_string(dataCtr);
            InstPtr++;
        }else if(com[i]=="print"){
            for(int it=0;it<300;it++){
                if(com[i+1]==dataTable[it][0]){
                    SML[InstPtr]="11"+dataTable[it][3];
                    InstPtr++;
                    i=300;
                }
            }
        }else if(com[i]=="end"){
            SML[InstPtr]="4300";
            InstPtr++;
        }else if(com[i]=="goto"){
            gotos(stoi(com[i+1]), 40, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
            i=300;
        }else if(com[i]=="if"){
            vector<char> command=spacer(com[i+1]);
            int caser;
            int loc;
            for (int it=0;it<command.size();it++){
                if(command[it]=='!'){
                    loc=it;
                    caser=1;
                    it=command.size();
                }else if(command[it]=='='){
                    loc=it;
                    caser=2;
                    it=command.size();
                }else if(command[it]=='<'){
                    loc=it;
                    if(command[it+2]=='='){
                        caser=5;
                    }else{
                        caser=3;
                    }
                    it=command.size();
                }else if(command[it]=='>'){
                    loc=it;
                    if(command[it+2]=='='){
                        caser=6;
                    }else{
                        caser=4;
                    }
                    it=command.size();
                }
            }
            switch(caser){
                case 1:
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc+4]),"31");
                    gotos(stoi(com[i+3]), 42, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    condi(SML,InstPtr,dataTable,string(1,command[loc+4]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"31");
                    gotos(stoi(com[i+3]), 42, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    break;
                case 2:
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc+4]),"31");
                    gotos(stoi(com[i+3]), 41, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    break;
                case 3:
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc+2]),"31");
                    gotos(stoi(com[i+3]), 42, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    break;
                case 4:
                    condi(SML,InstPtr,dataTable,string(1,command[loc+4]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"31");
                    gotos(stoi(com[i+3]), 42, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    break;
                case 5:
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc+2]),"31");
                    gotos(stoi(com[i+3]), 42, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    gotos(stoi(com[i+3]), 41, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    break;
                case 6:
                    condi(SML,InstPtr,dataTable,string(1,command[loc+4]),"20");
                    condi(SML,InstPtr,dataTable,string(1,command[loc-2]),"31");
                    gotos(stoi(com[i+3]), 42, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    gotos(stoi(com[i+3]), 41, gotoPlace, gotoCtr, stoi(com[0]), SML, InstPtr, dataTable);
                    break;
            }
            i=com.size();
        }else{
            if(isdigit(com[i][0])){
                dataTable[symCtr][0]=com[i];
                dataTable[symCtr][1]="N";
                dataTable[symCtr][2]=to_string(InstPtr);
                symCtr++;
            }else{
                dataTable[symCtr][0]=com[i];
                dataTable[symCtr][1]="V ";
                dataTable[symCtr][2]=to_string(InstPtr);
                symCtr++;
            }
        }   
    }
}





int main(){
    //This is all establishment variables
    int CurrentDataCtr=99;
    int currentInstPtr=0;
    int currentSymCtr=1;
    int gotoCtr=0;
    int gotoPlace[100][2]={0};
    array<string,100> SML;
    string baseCommand;
    string name;
    string ofile;
    string mfile;
    
    //This is getting the names of the files to open
    cout<<"Please enter the name of the file you would like compiled, \nnote it must be in the same folder as this program. (Example: test.txt)"<<endl;
    getline(cin,name);
    cout<<"\nPlease enter the name of the file you would like the symbol table entered into, \nnote it will be in the same folder as the program. (Example: out.txt)"<<endl;
    getline(cin,ofile);
    cout<<"\n Please enter the name of hte file you would like the machine code stored in, \nnote it will be in the same folder as the program. (Example: SMLCode.txt)"<<endl;
    getline(cin,mfile);
    
    //Lets open up the files
    ifstream baseFile(name);
    ofstream outFile(ofile);
    ofstream SMLFile(mfile);
    
    //Establish that Data Table
    string dataTable[300][3];
    dataTable[0][0]="Symbol";
    dataTable[0][1]="Type";
    dataTable[0][2]="Location";
    
    //Initial Processing - Grabbing lines and operating on them
    while(getline(baseFile,baseCommand)){
        vector<string> command=parse(baseCommand);
        int condition=enact(dataTable,command,currentSymCtr,CurrentDataCtr,currentInstPtr,SML,gotoPlace,gotoCtr);
        if (CurrentDataCtr<=currentInstPtr){  //Checks to make sure data and commands dont ovelap
            cout<<"This code is too long, and is running into issues of overlapping in memory."<<endl;
            exit(EXIT_FAILURE);
        }
        if(condition==1){  //Handles end
            break;
        }
    }
    
    baseFile.close();
    
    //Print the data table to the outfile
    outFile<<dataTable[0][0]<<" "<<dataTable[0][1]<<" "<<dataTable[0][2]<<endl;
    for (int i=1;i<=currentSymCtr;i++){
        for(int it=0;it<3;it++){
            outFile<<dataTable[i][0]<<"      "<<dataTable[i][1]<<"      "<<dataTable[i][2]<<"      "<<endl;
        }
    }
    outFile.close();
    
    //Fix any gotos that need remaking
    for(int i=0; i<sizeof(gotoPlace);i++){
        for(int it=0; it<sizeof(dataTable);it++){
            if(dataTable[it][0]==""){
                break;
            }
            if(dataTable[it][0]==to_string(gotoPlace[i][0])){
                if(dataTable[it][2].size()<2){
                    SML[gotoPlace[i][1]]+="0"+dataTable[it][2];
                }else{
                    SML[gotoPlace[i][1]]+=dataTable[it][2];
                }
            }
        }
    }
    
    //Finally print the SML file
    for (int i=0;i<currentInstPtr;i++){
        SMLFile<<SML[i]<<endl;
    }
    SMLFile.close();
    
    return 0;
}

I'm sorry if it seems like my indentation is inconsistent, I copied from dev and pasted into code snippet, which SO did not like and I had to go through and manually indent every line

Finally included is the test file which I currently have titled test.txt. This is the code that will be run through the compiler.

10 rem why must this code hurt me so
20 input x
30 input y
40 if (x!=y) goto 60
50 goto 80
60 print x
70 print y
80 end

So I have no clue what is wrong with it. Any help offered would be greatly appreciated. Thanks y'all

Aucun commentaire:

Enregistrer un commentaire