mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-18 20:53:50 -04:00
generalize +, -, *
This commit is contained in:
parent
c5a3916b44
commit
9a29c92ab6
@ -686,6 +686,37 @@ tokenize_numeric_pair(const string &str, double &a, double &b) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: PPScope::tokenize_ints
|
||||||
|
// Access: Public
|
||||||
|
// Description: This function is used by the arithmetic functions +,
|
||||||
|
// -, etc. It separates the string into parameters
|
||||||
|
// based on the comma, interprets each parameter as an
|
||||||
|
// integer, and fills up the indicated vector.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
bool PPScope::
|
||||||
|
tokenize_ints(const string &str, vector<int> &tokens) {
|
||||||
|
vector<string> words;
|
||||||
|
tokenize_params(str, words, true);
|
||||||
|
|
||||||
|
vector<string>::const_iterator wi;
|
||||||
|
for (wi = words.begin(); wi != words.end(); ++wi) {
|
||||||
|
const char *param = (*wi).c_str();
|
||||||
|
char *n;
|
||||||
|
int result = strtol(param, &n, 0);
|
||||||
|
if (*n != '\0') {
|
||||||
|
// strtol failed--not an integer.
|
||||||
|
cerr << "Warning: " << param << " is not an integer.\n";
|
||||||
|
if (n == param) {
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens.push_back(result);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
// Function: PPScope::scan_to_whitespace
|
// Function: PPScope::scan_to_whitespace
|
||||||
// Access: Public
|
// Access: Public
|
||||||
@ -2574,14 +2605,18 @@ expand_gen(const string ¶ms) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
string PPScope::
|
string PPScope::
|
||||||
expand_plus(const string ¶ms) {
|
expand_plus(const string ¶ms) {
|
||||||
double a, b;
|
vector<int> tokens;
|
||||||
if (!tokenize_numeric_pair(params, a, b)) {
|
if (!tokenize_ints(params, tokens)) {
|
||||||
return string();
|
return string();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai = (int)a;
|
int result = 0;
|
||||||
int bi = (int)b;
|
vector<int>::const_iterator ti;
|
||||||
return format_int(ai + bi);
|
for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
|
||||||
|
result += (*ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
return format_int(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
@ -2592,41 +2627,25 @@ expand_plus(const string ¶ms) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
string PPScope::
|
string PPScope::
|
||||||
expand_minus(const string ¶ms) {
|
expand_minus(const string ¶ms) {
|
||||||
// We cannot use tokenize_numeric_pair(), because we also support
|
vector<int> tokens;
|
||||||
// unary minus.
|
if (!tokenize_ints(params, tokens)) {
|
||||||
|
|
||||||
vector<string> words;
|
|
||||||
tokenize_params(params, words, true);
|
|
||||||
double results[2];
|
|
||||||
int num_words = words.size();
|
|
||||||
|
|
||||||
if (num_words == 1) {
|
|
||||||
// Simulate unary minus by setting the first operand to 0.
|
|
||||||
results[0] = 0.0;
|
|
||||||
|
|
||||||
} else if (num_words != 2) {
|
|
||||||
cerr << num_words << " parameters supplied when two were expected:\n"
|
|
||||||
<< params << "\n";
|
|
||||||
return string();
|
return string();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < num_words; i++) {
|
int result = 0;
|
||||||
const char *param = words[i].c_str();
|
|
||||||
char *n;
|
if (tokens.size() == 1) {
|
||||||
int j = 2 - num_words + i;
|
// A special case: unary minus.
|
||||||
results[j] = strtod(param, &n);
|
result = -tokens[0];
|
||||||
if (*n != '\0') {
|
|
||||||
// strtod failed--not a numeric representation.
|
} else if (tokens.size() > 1) {
|
||||||
cerr << "Warning: " << words[i] << " is not a number.\n";
|
result = tokens[0];
|
||||||
if (n == param) {
|
for (int i = 1; i < (int)tokens.size(); i++) {
|
||||||
results[j] = 0.0;
|
result -= tokens[i];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai = (int)results[0];
|
return format_int(result);
|
||||||
int bi = (int)results[1];
|
|
||||||
return format_int(ai - bi);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
@ -2637,14 +2656,18 @@ expand_minus(const string ¶ms) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
string PPScope::
|
string PPScope::
|
||||||
expand_times(const string ¶ms) {
|
expand_times(const string ¶ms) {
|
||||||
double a, b;
|
vector<int> tokens;
|
||||||
if (!tokenize_numeric_pair(params, a, b)) {
|
if (!tokenize_ints(params, tokens)) {
|
||||||
return string();
|
return string();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai = (int)a;
|
int result = 1;
|
||||||
int bi = (int)b;
|
vector<int>::const_iterator ti;
|
||||||
return format_int(ai * bi);
|
for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
|
||||||
|
result *= (*ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
return format_int(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
@ -2655,14 +2678,18 @@ expand_times(const string ¶ms) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
string PPScope::
|
string PPScope::
|
||||||
expand_divide(const string ¶ms) {
|
expand_divide(const string ¶ms) {
|
||||||
double a, b;
|
vector<int> tokens;
|
||||||
if (!tokenize_numeric_pair(params, a, b)) {
|
if (!tokenize_ints(params, tokens)) {
|
||||||
return string();
|
return string();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai = (int)a;
|
if (tokens.size() != 2) {
|
||||||
int bi = (int)b;
|
cerr << tokens.size() << " parameters supplied when two were expected:\n"
|
||||||
return format_int(ai / bi);
|
<< params << "\n";
|
||||||
|
return string();
|
||||||
|
}
|
||||||
|
|
||||||
|
return format_int(tokens[0] / tokens[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
@ -2673,14 +2700,18 @@ expand_divide(const string ¶ms) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
string PPScope::
|
string PPScope::
|
||||||
expand_modulo(const string ¶ms) {
|
expand_modulo(const string ¶ms) {
|
||||||
double a, b;
|
vector<int> tokens;
|
||||||
if (!tokenize_numeric_pair(params, a, b)) {
|
if (!tokenize_ints(params, tokens)) {
|
||||||
return string();
|
return string();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai = (int)a;
|
if (tokens.size() != 2) {
|
||||||
int bi = (int)b;
|
cerr << tokens.size() << " parameters supplied when two were expected:\n"
|
||||||
return format_int(ai % bi);
|
<< params << "\n";
|
||||||
|
return string();
|
||||||
|
}
|
||||||
|
|
||||||
|
return format_int(tokens[0] % tokens[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
|
@ -61,6 +61,7 @@ public:
|
|||||||
void tokenize_params(const string &str, vector<string> &tokens,
|
void tokenize_params(const string &str, vector<string> &tokens,
|
||||||
bool expand);
|
bool expand);
|
||||||
bool tokenize_numeric_pair(const string &str, double &a, double &b);
|
bool tokenize_numeric_pair(const string &str, double &a, double &b);
|
||||||
|
bool tokenize_ints(const string &str, vector<int> &tokens);
|
||||||
size_t scan_to_whitespace(const string &str, size_t start = 0);
|
size_t scan_to_whitespace(const string &str, size_t start = 0);
|
||||||
static string format_int(int num);
|
static string format_int(int num);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user