Skip to content

Instantly share code, notes, and snippets.

@mashiox
Created February 15, 2016 05:25
Show Gist options
  • Save mashiox/2842c780aa81c349c002 to your computer and use it in GitHub Desktop.
Save mashiox/2842c780aa81c349c002 to your computer and use it in GitHub Desktop.
Tokenize a SIC/XE asm file string
#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;
}
@mashiox
Copy link
Author

mashiox commented Feb 15, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment