Created
February 15, 2016 05:25
-
-
Save mashiox/2842c780aa81c349c002 to your computer and use it in GitHub Desktop.
Tokenize a SIC/XE asm file string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstdlib> | |
#include <vector> | |
using namespace std; | |
struct line{ | |
private: | |
string label, | |
opcode, | |
operand, | |
comment; | |
public: | |
line(){ | |
label = opcode = operand = comment = ""; | |
} | |
string getlabel(){ | |
return label; | |
} | |
string getopcode(){ | |
return opcode; | |
} | |
string getoperand(){ | |
return operand; | |
} | |
string getcomment(){ | |
return comment; | |
} | |
void setlabel(string line){ | |
label = line; | |
} | |
void setopcode(string opc){ | |
opcode = opc; | |
} | |
void setoperand(string oper){ | |
operand = oper; | |
} | |
void setcomment(string com){ | |
comment = com; | |
} | |
}; | |
bool is_comment(string s){ | |
return ( s.length() > 0 && s[0] == '.'); | |
} | |
void print(std::string::size_type n, std::string const &s){ | |
if (n == std::string::npos) { | |
std::cout << "not found\n"; | |
} else { | |
std::cout << "found: " << s.substr(n) << '\n'; | |
} | |
} | |
int main(void){ | |
string line = " lds #3 . dammit all"; | |
struct line line_item; | |
string delim = "\t "; | |
string::size_type n; | |
vector<string> string_tokens; | |
if ( line.size() > 0 ){ | |
/** | |
* Finds comment string with the comment operator | |
*/ | |
n = line.find('.'); | |
if ( n != string::npos ){ | |
line_item.setcomment(line.substr(n)); | |
line = line.substr(0, n); | |
} | |
/** | |
* Naieve tokenization. | |
* we still don't know anything about | |
* the validity of these tokens | |
*/ | |
int last = line.find_first_not_of(delim, 0); | |
int first = line.find_first_of(delim, last); | |
string token = ""; | |
while ( first != -1 || last != -1 ){ | |
token = line.substr(last, first-last); | |
string_tokens.push_back(token); | |
last = line.find_first_not_of(delim, first); | |
first = line.find_first_of(delim, last); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@missmelni
@AzizShad