file: line.cpp
#include "line.h"
String line::resolvedstring(){
String temp;
int i;
cout << "The resolved string is: ";
for (i=1; i < (int)tokens.size(); i++)
temp += tokens[i] + ' ';
cout << temp << '\n';
return temp;
};
bool line::equ() {
int i;
bool EQU=false;
cout << fcompare(tokens[1],"DEFINE") << " " << fcompare(tokens[1],"define") << '\n';
if (tokens.size() == 3)
if (!(num(tokens[0],i) && num(tokens[1],i)))
if ((fcompare(tokens[1],"DEFINE")==0) || (fcompare(tokens[2],"EQU")==0))
EQU = true;
cout << EQU <<'\n';
return EQU;
}
bool line::next(String &token){
int loc;
int value;
bool unresolved=true;
bool search=true;
if (compile){
// When called on a compilable line, it will return the next
// argument that it cannot resolve. When called on a pseudoop,
// uncompilable line, it will act accordingly!
for (loc=(int)arguments.size()+1; search && loc<=(int)tokens.size(); loc++){
if (num(tokens[loc],value))
arguments.push_back(value);
else{
search = false;
cout << loc << " " << tokens[loc] << '\n';
}
}
// The -1 below is necessary because the structure of the loop is
// such that loc will be greater by one than the location, due to
// the fact vectors number at one rather than zero.
if (!search)
token = tokens[loc-1];
else
unresolved=false;
}
else{
// So then, the case is that we have a pseudoop! ORG will return
// the second argument, a label will return the first argument,
// and both EQU's and DEFINES return the third argument, while
// EQU's return the first and DEFINES return the second.
cout << "Checking for type of psuedoop!" << '\n';
cout << arguments.size() <<'\n';
if (org())
token = tokens[2];
if (label())
token = tokens[1];
if (equ()){
cout << "Could be an EQU" <<'\n';
if (arguments.size() == 1)
token = tokens[2];
else
if (arguments.size() == 0){
cout << "First access" <<'\n';
arguments.push_back(0);
if (fcompare(tokens[0], "DEFINE") == 0)
token = tokens[1];
else{
token = tokens[0];
cout << tokens[1]<<'\n';
}
}
else
cout << "Error in number of calls!" << '\n';
}
}
return unresolved;
};
void line::fillarg(String &rep, String &with){
int i;
int value;
// Fillarg should only be called on compilable lines!
i = arguments.size() +1;
if (tokens[i] == rep){
tokens[i] = with;
if (num(with, value))
arguments.push_back(value);
// cout << value << " " << (int)arguments.size() << '\n';
}
else
cout << "Error in algorithm!" << '\n';
};
void line::rip(){
String store[10];
Regex c="[ \t,:#]+";
int i,a;
a = split(raw,store,10,c);
for (i = 0; i < a; i++){
cout << store[i] << '\n';
tokens.push_back(store[i]);
}
};
line::line(String &instr, int &number, bool compilable=true){
// save the instruction
raw = instr;
lin = number;
// compilable is anything not a comment, newline, or #define X.
// This includes #define X Y, instr A, B, instr A, ORG D, N EQU L,
// F:, etc.
if (compilable)
rip();
if (psuedoop()){
compile = false;
cout << fcompare(tokens[1], "DEFINE")<<" "<<fcompare(tokens[2],"EQU")<<'\n';
}
else
// Eventually the 0 will be replaced by a object that takes a
// vector and returns a compiled byte; in this case, we just want
// the instruction!
arguments.push_back(0);
};
bool line::num(String &token, int &value){
bool Number=false;
int i;
int k;
int length;
if (isdigit(token[0])){
// Check for hex
if (upcase(token.lastchar()) == 'H'){
cout << "Token is hexadecimal!" << '\n';
k = 16;
length = (int)token.length() - 1;
}
// Check for bin
if (upcase(token.lastchar()) == 'B'){
cout << "Token is binary!" << '\n';
k = 2;
length = (int)token.length() - 1;
}
// Check for octal
if (upcase(token.lastchar()) == 'O'){
cout << "Token is octal!" << '\n';
k = 8;
length = (int)token.length() - 1;
}
// Check for decimal!
if ((upcase(token.lastchar()) == 'D') || (isdigit(token.lastchar()))){
cout << "Token is decimal!" << '\n';
k = 10;
length = (int)token.length();
if (!isdigit(token.lastchar()))
length = length - 1;
}
}
else k = 0;
// do a hex conversion!
if (k == 16){
value = token[0] - '0';
for (i=1; (i < length) && isxdigit(token[i]); i++){
value *=k;
if (isdigit(token[i]))
value += token[i] - '0';
else{
char d = (toupper(token[i]));
value += d - 'A' + 10;
}
}
}
// easy to do a non-hex, since we don't have to worry about values
// greater than 9!
else{
if (k > 0){
value = token[0] - '0';
for (i=1; (i < length) && isdigit(token[i]); i++){
value *= k;
value += token[i] - '0';
}
}
else
cout << "Could not be converted!" << '\n';
}
if (i == length)
Number = true;
return Number;
};
ostream& operator<<(ostream &o, line &a){
int i;
cout << a.lin << " " << a.raw << '\n';
//#ifdef DEBUG
for (i=1; i <= (int)a.tokens.size(); i++)
cout << a.tokens[i] << "_";
//#endif
cout << '\n';
return o;
};
C++ to HTML Conversion by ctoohtml