Skip to content

Commit

Permalink
TypeHandlersTuple handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed Jul 15, 2024
1 parent 4978123 commit 195c76f
Show file tree
Hide file tree
Showing 25 changed files with 202 additions and 108 deletions.
Binary file modified TIVarsLib.wasm
Binary file not shown.
7 changes: 3 additions & 4 deletions cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

using namespace std;
using namespace tivars;
using TH_Tokenized::LANG_EN;
using TH_Tokenized::LANG_FR;
using namespace tivars::TypeHandlers;

enum FileType
{
Expand Down Expand Up @@ -213,10 +212,10 @@ int main(int argc, char** argv)
string langStr = result["lang"].as<string>();
if (langStr == "en")
{
contentOptions["lang"] = LANG_EN;
contentOptions["lang"] = TH_Tokenized::LANG_EN;
} else if (langStr == "fr")
{
contentOptions["lang"] = LANG_FR;
contentOptions["lang"] = TH_Tokenized::LANG_FR;
} else
{
cout << langStr << " is not a valid language code" << endl;
Expand Down
4 changes: 2 additions & 2 deletions src/TIVarFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ namespace tivars
void TIVarFile::setContentFromString(const std::string& str, const options_t& options, uint16_t entryIdx)
{
auto& entry = this->entries[entryIdx];
entry.data = (entry._type.getHandlers().first)(str, options, this);
entry.data = std::get<0>(entry._type.getHandlers())(str, options, this);
this->refreshMetadataFields();
}
void TIVarFile::setContentFromString(const std::string& str, const options_t& options)
Expand Down Expand Up @@ -386,7 +386,7 @@ namespace tivars
std::string TIVarFile::getReadableContent(const options_t& options, uint16_t entryIdx)
{
const auto& entry = this->entries[entryIdx];
return (entry._type.getHandlers().second)(entry.data, options, this);
return std::get<1>(entry._type.getHandlers())(entry.data, options, this);
}
std::string TIVarFile::getReadableContent(const options_t& options)
{
Expand Down
2 changes: 1 addition & 1 deletion src/TIVarType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace tivars
class_<tivars::TIVarType>("TIVarType")
.constructor<>()
.constructor<const char*>()
.constructor<int, const std::string&, const std::vector<std::string>&, const tivars::handler_pair_t&>()
.constructor<int, const std::string&, const std::vector<std::string>&, const tivars::TypeHandlers::TypeHandlersTuple&>()

.function("getId" , &tivars::TIVarType::getId)
.function("getName" , &tivars::TIVarType::getName)
Expand Down
7 changes: 4 additions & 3 deletions src/TIVarType.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace tivars
static TIVarType createFromID(uint8_t id) { return TIVarType{id}; }
static TIVarType createFromName(const std::string& name) { return TIVarType{name}; }

TIVarType(int id, const std::string& name, const std::vector<std::string>& exts, const TypeHandlers::handler_pair_t& handlers) : id(id), name(name), exts(exts), handlers(handlers)
TIVarType(int id, const std::string& name, const std::vector<std::string>& exts, const TypeHandlers::TypeHandlersTuple& handlers)
: id(id), name(name), exts(exts), handlers(handlers)
{}

~TIVarType() = default;
Expand All @@ -36,13 +37,13 @@ namespace tivars
int getId() const { return this->id; }
std::string getName() const { return this->name; }
const std::vector<std::string>& getExts() const { return this->exts; }
const TypeHandlers::handler_pair_t& getHandlers() const { return this->handlers; };
TypeHandlers::TypeHandlersTuple getHandlers() const { return this->handlers; };

private:
int id = -1;
std::string name = "Unknown";
std::vector<std::string> exts;
TypeHandlers::handler_pair_t handlers;
TypeHandlers::TypeHandlersTuple handlers;

};

Expand Down
58 changes: 31 additions & 27 deletions src/TIVarTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ namespace tivars

// Wrap the makeDataFromStr function by one that adds the type/subtype in the options
// Ideally, the handlers would parse the string and select the correct handler to dispatch...
#define GenericHandlerPair(which, type) make_pair([](const std::string& str, const options_t& options, const TIVarFile* _ctx) -> data_t { \
options_t options_withType = options; \
options_withType["_type"] = type; \
return (TypeHandlers::TH_Generic##which::makeDataFromString)(str, options_withType, _ctx); \
}, &TypeHandlers::TH_Generic##which::makeStringFromData)
#define GenericHandlerTuple(which, type) TypeHandlersTuple{ \
[](const std::string& str, const options_t& options, const TIVarFile* _ctx) -> data_t { \
options_t options_withType = options; \
options_withType["_type"] = type; \
return (TypeHandlers::TH_Generic##which::makeDataFromString)(str, options_withType, _ctx); \
}, \
&(TypeHandlers::TH_Generic##which::makeStringFromData), \
&(TypeHandlers::TH_Generic##which::getMinVersionFromData), \
}

void TIVarTypes::insertType(const std::string& name, int id, const std::vector<std::string>& exts, const handler_pair_t& handlers)
void TIVarTypes::insertType(const std::string& name, int id, const std::vector<std::string>& exts, const TypeHandlersTuple& handlers)
{
const TIVarType varType(id, name, exts, handlers);
types[name] = varType;
Expand All @@ -67,44 +71,44 @@ namespace tivars
const std::string _;

/* Standard types */
insertType("Real", 0x00, {"82n", "83n", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerPair(Real, 0x00) );
insertType("RealList", 0x01, {"82l", "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerPair(List, 0x00) );
insertType("Matrix", 0x02, {"82m", "83m", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm"}, make_handler_pair(TH_Matrix) );
insertType("Equation", 0x03, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, make_handler_pair(TH_Tokenized) );
insertType("String", 0x04, {"82s", "83s", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs"}, make_handler_pair(TH_Tokenized) );
insertType("Program", 0x05, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, make_handler_pair(TH_Tokenized) );
insertType("ProtectedProgram", 0x06, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, make_handler_pair(TH_Tokenized) );
insertType("Real", 0x00, {"82n", "83n", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerTuple(Real, 0x00) );
insertType("RealList", 0x01, {"82l", "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerTuple(List, 0x00) );
insertType("Matrix", 0x02, {"82m", "83m", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm"}, SpecificHandlerTuple(TH_Matrix) );
insertType("Equation", 0x03, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, SpecificHandlerTuple(TH_Tokenized) );
insertType("String", 0x04, {"82s", "83s", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs"}, SpecificHandlerTuple(TH_Tokenized) );
insertType("Program", 0x05, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, SpecificHandlerTuple(TH_Tokenized) );
insertType("ProtectedProgram", 0x06, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, SpecificHandlerTuple(TH_Tokenized) );
insertType("Picture", 0x07, {"82i", "83i", "8xi", "8xi", "8xi", "8ci", "8ci", "8ci", "8ci"});
insertType("GraphDataBase", 0x08, {"82d", "83d", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd"}, make_handler_pair(TH_GDB) );
insertType("GraphDataBase", 0x08, {"82d", "83d", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd"}, SpecificHandlerTuple(TH_GDB) );
// insertType("Unknown", 0x09, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
// insertType("UnknownEqu", 0x0A, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
insertType("SmartEquation", 0x0B, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, make_handler_pair(TH_Tokenized) ); // aka "New Equation"
insertType("Complex", 0x0C, { _ , "83c", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc"}, GenericHandlerPair(Complex, 0x0C) );
insertType("ComplexList", 0x0D, { _ , "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerPair(List, 0x0C) );
insertType("SmartEquation", 0x0B, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, SpecificHandlerTuple(TH_Tokenized) ); // aka "New Equation"
insertType("Complex", 0x0C, { _ , "83c", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x0C) );
insertType("ComplexList", 0x0D, { _ , "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerTuple(List, 0x0C) );
// insertType("Undef", 0x0E, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
insertType("WindowSettings", 0x0F, {"82w", "83w", "8xw", "8xw", "8xw", "8xw", "8xw", "8xw", "8xw"});
insertType("RecallWindow", 0x10, {"82z", "83z", "8xz", "8xz", "8xz", "8xz", "8xz", "8xz", "8xz"});
insertType("TableRange", 0x11, {"82t", "83t", "8xt", "8xt", "8xt", "8xt", "8xt", "8xt", "8xt"});
insertType("ScreenImage", 0x12, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
insertType("Backup", 0x13, {"82b", "83b", "8xb", _ , "8xb", "8cb", _ , _ , _ });
insertType("App", 0x14, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
insertType("AppVar", 0x15, { _ , _ , "8xv", "8xv", "8xv", "8xv", "8xv", "8xv", "8xv"}, GenericHandlerPair(AppVar, 0x15) );
insertType("PythonAppVar", 0x15, { _ , _ , _ , _ , _ , _ , "8xv", "8xv", "8xv"}, make_handler_pair(STH_PythonAppVar) );
insertType("AppVar", 0x15, { _ , _ , "8xv", "8xv", "8xv", "8xv", "8xv", "8xv", "8xv"}, GenericHandlerTuple(AppVar, 0x15) );
insertType("PythonAppVar", 0x15, { _ , _ , _ , _ , _ , _ , "8xv", "8xv", "8xv"}, SpecificHandlerTuple(STH_PythonAppVar) );
insertType("TemporaryItem", 0x16, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
insertType("GroupObject", 0x17, {"82g", "83g", "8xg", "8xg", "8xg", "8xg", "8cg", "8cg", "8cg"});
insertType("RealFraction", 0x18, { _ , _ , _ , _ , "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerPair(Real, 0x18) );
insertType("RealFraction", 0x18, { _ , _ , _ , _ , "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerTuple(Real, 0x18) );
insertType("MixedFraction", 0x19, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
insertType("Image", 0x1A, { _ , _ , _ , _ , _ , _ , "8ca", "8ca", "8ca"});

/* Exact values (TI-83 Premium CE [Edition Python] and TI-82 Advanced Edition Python) */
/* See https://docs.google.com/document/d/1P_OUbnZMZFg8zuOPJHAx34EnwxcQZ8HER9hPeOQ_dtI and especially this lib's implementation */
insertType("ExactComplexFrac", 0x1B, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1B) );
insertType("ExactRealRadical", 0x1C, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerPair(Real, 0x1C) );
insertType("ExactComplexRadical", 0x1D, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1D) );
insertType("ExactComplexPi", 0x1E, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1E) );
insertType("ExactComplexPiFrac", 0x1F, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1F) );
insertType("ExactRealPi", 0x20, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerPair(Real, 0x20) );
insertType("ExactRealPiFrac", 0x21, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerPair(Real, 0x21) );
insertType("ExactComplexFrac", 0x1B, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1B) );
insertType("ExactRealRadical", 0x1C, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerTuple(Real, 0x1C) );
insertType("ExactComplexRadical", 0x1D, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1D) );
insertType("ExactComplexPi", 0x1E, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1E) );
insertType("ExactComplexPiFrac", 0x1F, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1F) );
insertType("ExactRealPi", 0x20, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerTuple(Real, 0x20) );
insertType("ExactRealPiFrac", 0x21, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerTuple(Real, 0x21) );

/* System/Flash-related things */
// 0x22 - IDList (68k calcs)
Expand Down
2 changes: 1 addition & 1 deletion src/TIVarTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace tivars
static bool isValidID(uint8_t id);

private:
static void insertType(const std::string& name, int id, const std::vector<std::string>& exts, const TypeHandlers::handler_pair_t& handlers = make_handler_pair(TypeHandlers::DummyHandler));
static void insertType(const std::string& name, int id, const std::vector<std::string>& exts, const TypeHandlers::TypeHandlersTuple& handlers = { &TypeHandlers::DummyHandler::makeDataFromString, &TypeHandlers::DummyHandler::makeStringFromData, &TypeHandlers::DummyHandler::getMinVersionFromData });

};
}
Expand Down
6 changes: 6 additions & 0 deletions src/TypeHandlers/STH_DataAppVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ namespace tivars::TypeHandlers

return str;
}

uint8_t STH_DataAppVar::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
5 changes: 5 additions & 0 deletions src/TypeHandlers/STH_ExactFraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ namespace tivars::TypeHandlers
return dec2frac(stod(STH_FP::makeStringFromData(data, options)));
}

uint8_t STH_ExactFraction::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
5 changes: 5 additions & 0 deletions src/TypeHandlers/STH_ExactFractionPi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ namespace tivars::TypeHandlers
return dec2frac(stod(STH_FP::makeStringFromData(data, options)), "π");
}

uint8_t STH_ExactFractionPi::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
5 changes: 5 additions & 0 deletions src/TypeHandlers/STH_ExactPi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ namespace tivars::TypeHandlers
return multiple(stoi(STH_FP::makeStringFromData(data, options)), "π");
}

uint8_t STH_ExactPi::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
5 changes: 5 additions & 0 deletions src/TypeHandlers/STH_ExactRadical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ namespace tivars::TypeHandlers
return str;
}

uint8_t STH_ExactRadical::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
8 changes: 7 additions & 1 deletion src/TypeHandlers/STH_FP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,11 @@ namespace tivars::TypeHandlers
}
*++i = '\0';
return std::string(result);
};
}

uint8_t STH_FP::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
6 changes: 6 additions & 0 deletions src/TypeHandlers/STH_PythonAppVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ namespace tivars::TypeHandlers

return std::string(data.begin() + 6 + scriptOffset, data.end());
}

uint8_t STH_PythonAppVar::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
12 changes: 12 additions & 0 deletions src/TypeHandlers/TH_GDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ namespace tivars::TypeHandlers
const bool compactJSON = has_option(options, "compact") && options.at("compact") == 1;
return json(gdb).dump(compactJSON ? -1 : 4);
}

uint8_t TH_GDB::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}

#else
Expand All @@ -567,6 +573,12 @@ namespace tivars::TypeHandlers
{
throw std::runtime_error("GDB support is not compiled in this tivars_lib_cpp version");
}

uint8_t TH_GDB::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}

#endif /* GDB_SUPPORT */
6 changes: 6 additions & 0 deletions src/TypeHandlers/TH_GenericAppVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ namespace tivars::TypeHandlers

return STH_DataAppVar::makeStringFromData(data, options);
}

uint8_t TH_GenericAppVar::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
23 changes: 14 additions & 9 deletions src/TypeHandlers/TH_GenericComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

namespace tivars::TypeHandlers
{
static const std::unordered_map<uint8_t, handler_pair_t> type2handlers = {
{ 0x0C, make_handler_pair(STH_FP) },
{ 0x1B, make_handler_pair(STH_ExactFraction) },
{ 0x1D, make_handler_pair(STH_ExactRadical) },
{ 0x1E, make_handler_pair(STH_ExactPi) },
{ 0x1F, make_handler_pair(STH_ExactFractionPi) },
static const std::unordered_map<uint8_t, TypeHandlersTuple> type2handlers = {
{ 0x0C, SpecificHandlerTuple(STH_FP) },
{ 0x1B, SpecificHandlerTuple(STH_ExactFraction) },
{ 0x1D, SpecificHandlerTuple(STH_ExactRadical) },
{ 0x1E, SpecificHandlerTuple(STH_ExactPi) },
{ 0x1F, SpecificHandlerTuple(STH_ExactFractionPi) },
};
static const std::unordered_map<uint8_t, const char*> type2patterns = {
{ 0x0C, STH_FP::validPattern },
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace tivars::TypeHandlers
{
throw std::runtime_error("Unknown/Invalid type for this TH_GenericComplex: " + std::to_string(type));
}
const auto& handler = handlerIter->second.first;
const auto& handler = std::get<0>(handlerIter->second);

std::string newStr = str;
newStr = std::regex_replace(newStr, std::regex(" "), "");
Expand Down Expand Up @@ -119,8 +119,8 @@ namespace tivars::TypeHandlers
throw std::runtime_error("Unknown/Invalid type for this TH_GenericComplex: " + std::to_string(typeI));
}

const auto& handlerR = handlerRIter->second.second;
const auto& handlerI = handlerIIter->second.second;
const auto& handlerR = std::get<1>(handlerRIter->second);
const auto& handlerI = std::get<1>(handlerIIter->second);

const data_t::const_iterator mid = data.cbegin() + bytesPerMember;
const std::string coeffR = handlerR(data_t(data.cbegin(), mid), options, _ctx);
Expand All @@ -145,4 +145,9 @@ namespace tivars::TypeHandlers
return str;
}

uint8_t TH_GenericComplex::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
6 changes: 6 additions & 0 deletions src/TypeHandlers/TH_GenericList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ namespace tivars::TypeHandlers

return str;
}

uint8_t TH_GenericList::getMinVersionFromData(const data_t& data)
{
(void)data;
return 0;
}
}
Loading

0 comments on commit 195c76f

Please sign in to comment.