1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-24 01:00:25 +00:00

argf now parses the given argument

This commit is contained in:
Zeno Rogue 2018-09-10 19:28:12 +02:00
parent dbdb1ba6dc
commit 7da2b7e9a1
4 changed files with 70 additions and 63 deletions

View File

@ -69,7 +69,7 @@ namespace arg {
const char* argcs() { return args().c_str(); }
int argi() { return atoi(argcs()); }
unsigned arghex() { return strtoll(argcs(), NULL, 16); }
ld argf() { return atof(argcs()); }
ld argf() { return parseld(args()); }
bool argis(const string& s) { return args() == s; }
void init(int argc, char **argv) { for(int i=0; i<argc; i++) argument.push_back(argv[i]); shift(); }

View File

@ -599,68 +599,6 @@ namespace dialog {
reaction_t extra_options;
struct exp_parser {
string s;
int at;
exp_parser() { at = 0; }
bool ok() { return at == isize(s); }
char next() { if(at == isize(s) || at == -1) return 0; else return s[at]; }
bool eat(const char *c) {
int orig_at = at;
while(*c && *c == next()) at++, c++;
if(*c == 0) return true;
else at = orig_at;
return false;
}
ld parse(int prio = 0) {
ld res;
if(eat("sin(")) res = sin(parsepar());
else if(eat("cos(")) res = cos(parsepar());
else if(eat("sinh(")) res = sinh(parsepar());
else if(eat("cosh(")) res = cosh(parsepar());
else if(eat("asin(")) res = asin(parsepar());
else if(eat("acos(")) res = acos(parsepar());
else if(eat("asinh(")) res = asinh(parsepar());
else if(eat("acosh(")) res = acosh(parsepar());
else if(eat("exp(")) res = exp(parsepar());
else if(eat("log(")) res = log(parsepar());
else if(next() == '(') at++, res = parsepar();
else {
string number;
while(true) {
char c = next();
if((c >= '0' && c <= '9') || among(c, 'e', 'p', '.'))
number += c, at++;
else break;
}
if(number == "e") res = exp(1);
else if(number == "p") res = M_PI;
else if(number == "") at = -1;
else if(number[0] >= 'a' && number[0] <= 'z') at = -1;
else { std::stringstream ss; res = 0; ss << number; ss >> res; }
}
while(true) {
if(next() == '+' && prio == 0) at++, res = res + parse(1);
else if(next() == '-' && prio == 0) at++, res = res - parse(1);
else if(next() == '*' && prio <= 1) at++, res = res * parse(2);
else if(next() == '/' && prio <= 1) at++, res = res / parse(2);
else if(next() == '^') at++, res = pow(res, parse(3));
else break;
}
return res;
}
ld parsepar() {
ld res = parse();
if(next() != ')') { at = -1; return res; }
at++;
return res;
}
};
void affect(char kind) {
if(ne.intval) {

View File

@ -4100,5 +4100,6 @@ namespace anims {
extern int animation_lcm;
extern ld animation_factor;
ld parseld(const string& s);
}

View File

@ -145,4 +145,72 @@ struct indenter {
void doindent() { for(int i=0; i<current_indentation; i++) printf(" "); }
struct exp_parser {
string s;
int at;
exp_parser() { at = 0; }
bool ok() { return at == isize(s); }
char next() { if(at == isize(s) || at == -1) return 0; else return s[at]; }
bool eat(const char *c) {
int orig_at = at;
while(*c && *c == next()) at++, c++;
if(*c == 0) return true;
else at = orig_at;
return false;
}
ld parse(int prio = 0) {
ld res;
if(eat("sin(")) res = sin(parsepar());
else if(eat("cos(")) res = cos(parsepar());
else if(eat("sinh(")) res = sinh(parsepar());
else if(eat("cosh(")) res = cosh(parsepar());
else if(eat("asin(")) res = asin(parsepar());
else if(eat("acos(")) res = acos(parsepar());
else if(eat("asinh(")) res = asinh(parsepar());
else if(eat("acosh(")) res = acosh(parsepar());
else if(eat("exp(")) res = exp(parsepar());
else if(eat("log(")) res = log(parsepar());
else if(next() == '(') at++, res = parsepar();
else {
string number;
while(true) {
char c = next();
if((c >= '0' && c <= '9') || among(c, 'e', 'p', '.'))
number += c, at++;
else break;
}
if(number == "e") res = exp(1);
else if(number == "p" || number == "pi") res = M_PI;
else if(number == "") at = -1;
else if(number[0] >= 'a' && number[0] <= 'z') at = -1;
else { std::stringstream ss; res = 0; ss << number; ss >> res; }
}
while(true) {
if(next() == '+' && prio == 0) at++, res = res + parse(1);
else if(next() == '-' && prio == 0) at++, res = res - parse(1);
else if(next() == '*' && prio <= 1) at++, res = res * parse(2);
else if(next() == '/' && prio <= 1) at++, res = res / parse(2);
else if(next() == '^') at++, res = pow(res, parse(3));
else break;
}
return res;
}
ld parsepar() {
ld res = parse();
if(next() != ')') { at = -1; return res; }
at++;
return res;
}
};
ld parseld(const string& s) {
exp_parser ep;
ep.s = s;
return ep.parse();
}
}