Files
musrfit/src/tests/spirit/spirit_fcn_test.cpp
Andreas Suter 5d3981d8b2
All checks were successful
Build and Deploy Documentation / build-and-deploy (push) Successful in 19s
test spirit parser moved from classic to X3.
2025-12-27 12:32:01 +01:00

108 lines
3.1 KiB
C++

#include <cstring>
#include <iostream>
#include <string>
#include "PFunctionHandler.h"
//-----------------------------------------------------
void syntax()
{
std::cout << std::endl << "spirit_fcn_test [--file <filename> [--debug]] | [--help]";
std::cout << std::endl << " without arguments: interactive mode";
std::cout << std::endl << " --file <filename>: function block etc. from file";
std::cout << std::endl << " --help: this help";
std::cout << std::endl << " --debug: will print the ast tree only (no evaluation)";
std::cout << std::endl << std::endl;
}
//-----------------------------------------------------
void handle_input(std::vector<std::string> &lines)
{
std::cout << std::endl << "will handle input ...";
std::cout << std::endl << "you should provide a PAR, a MAP, and a FUNCTION block";
std::cout << std::endl << " Map block:";
std::cout << std::endl << " MAP <map1> <map2> ... <mapM>";
std::cout << std::endl << " Parameter block:";
std::cout << std::endl << " PAR <par1> <par2> ... <parN>";
std::cout << std::endl << " Function Block:";
std::cout << std::endl << " FUNCTION";
std::cout << std::endl << " fun1 = <function1>";
std::cout << std::endl << " fun2 = <function2>";
std::cout << std::endl << " ...";
std::cout << std::endl << " funX = <functionX>";
std::cout << std::endl << " END";
std::cout << std::endl << "to get out of the input handle type '.q'";
std::cout << std::endl;
bool done = false;
char str[128];
do {
std::cout << ">> ";
std::cin.getline(str, sizeof(str));
if (!strcmp(str, ".q"))
done = true;
else
lines.push_back(str);
} while (!done);
}
//-----------------------------------------------------
int main(int argc, char *argv[])
{
bool inputFile = false;
bool debug = false;
if (argc > 4) {
syntax();
return 0;
} else if (argc == 2) {
syntax();
return 0;
} else if (argc >= 3) {
if (strcmp(argv[1], "--file")) {
syntax();
return 0;
} else {
inputFile = true;
}
if (argc == 4) {
if (strcmp(argv[3], "--debug")) {
syntax();
return 0;
} else {
debug = true;
}
}
}
std::unique_ptr<PFunctionHandler> fcnHandler;
if (inputFile) {
fcnHandler = std::make_unique<PFunctionHandler>(argv[2], debug);
} else {
std::vector<std::string> lines;
handle_input(lines);
fcnHandler = std::make_unique<PFunctionHandler>(lines);
}
if (fcnHandler == 0) {
std::cout << std::endl << "Couldn't invoke function handler, sorry ..." << std::endl;
return 0;
}
bool go_on = fcnHandler->IsValid();
if (go_on) {
std::cout << std::endl << "will do the parsing ...";
if (fcnHandler->DoParse()) {
if (!debug) {
std::cout << std::endl << "will do the evaluation ...";
for (unsigned int i=0; i<fcnHandler->GetNoOfFuncs(); i++)
std::cout << std::endl << "FUN" << fcnHandler->GetFuncNo(i) << " = " << fcnHandler->Eval(fcnHandler->GetFuncNo(i));
}
}
}
return 1;
}