// Builds a dictionary mapping labels to variable names void buildDict(Node program, programAux &aux, int labelLength) { Metadata m = program.metadata; // Token if (program.type == TOKEN) { if (isNumberLike(program)) { aux.step += 1 + toByteArr(program.val, m).size(); } else if (program.val[0] == '~') { aux.vars[program.val.substr(1)] = unsignedToDecimal(aux.step); } else if (program.val[0] == '$') { aux.step += labelLength + 1; } else aux.step += 1; } // A sub-program (ie. LLL) else if (program.val == "____CODE") { int step = aux.step; aux.step = 0; for (unsigned i = 0; i < program.args.size(); i++) { buildDict(program.args[i], aux, labelLength); } aux.step += step; } // Normal sub-block else { for (unsigned i = 0; i < program.args.size(); i++) { buildDict(program.args[i], aux, labelLength); } } }
// Compiled fragtree -> compiled fragtree without labels Node dereference(Node program) { int sz = treeSize(program) * 4; int labelLength = 1; while (sz >= 256) { labelLength += 1; sz /= 256; } programAux aux = buildDict(program, Aux(), labelLength); return substDict(program, aux, labelLength); }
// Compiled fragtree -> compiled fragtree without labels std::vector<Node> dereference(Node program) { int sz = treeSize(program) * 4; int labelLength = 1; while (sz >= 256) { labelLength += 1; sz /= 256; } programAux aux = Aux(); buildDict(program, aux, labelLength); std::vector<Node> o; substDict(program, aux, labelLength, o); return o; }
string replaceWords(vector<string>& dict, string sentence) { buildDict(dict); vector<string> words = split(sentence, ' '); string result; for (size_t i = 0; i < words.size(); ++i) { const string& word = words[i]; result += findPrefix(word); result += " "; } if (!result.empty()) { result.pop_back(); } return result; }
// Builds a dictionary mapping labels to variable names programAux buildDict(Node program, programAux aux, int labelLength) { Metadata m = program.metadata; // Token if (program.type == TOKEN) { if (isNumberLike(program)) { aux.step += 1 + toByteArr(program.val, m).size(); } else if (program.val[0] == '~') { aux.vars[program.val.substr(1)] = unsignedToDecimal(aux.step); } else if (program.val[0] == '$') { aux.step += labelLength + 1; } else aux.step += 1; } // A sub-program (ie. LLL) else if (program.val == "____CODE") { programAux auks = Aux(); for (unsigned i = 0; i < program.args.size(); i++) { auks = buildDict(program.args[i], auks, labelLength); } for (std::map<std::string,std::string>::iterator it=auks.vars.begin(); it != auks.vars.end(); it++) { aux.vars[(*it).first] = (*it).second; } aux.step += auks.step; } // Normal sub-block else { for (unsigned i = 0; i < program.args.size(); i++) { aux = buildDict(program.args[i], aux, labelLength); } } return aux; }