-
Notifications
You must be signed in to change notification settings - Fork 72
Reserved Word Operators
To understand this part of the tutorial you should read first the Defining New Operations and the Defining Reserved Words pages.
This page will describe how to create an operator using alphabetical characters that will be processed by the parser engine.
This process is not different from what you would do with normal operators, first you should declare a function describing the operation, e.g.:
packToken my_AND(const packToken& left, const packToken& right, evaluationData* data) {
return left.asDouble() + right.asDouble();
}Then add the operator to the operator's precedence map and to the operations map:
struct Startup_Op {
Startup_Op() {
OppMap_t& opp = calculator::Default().opPrecedence;
// Note the operator name here uses characters,
// but does not need to be the same name you want to use on the code:
opp.add("my op", 2);
// Add the operation function to the default opMap:
opMap_t& opMap = calculator::Default().opMap;
// Here you should use the same name used on the OppMap:
opMap.add({NUM, "my op", NUM}, &my_sum);
}
} Startup_Op;Note: Operator names starting with capital letters "L" and "R" are reserved for internal usage of the engine. So avoid using these as starting letters for the names of your operators and prefere lowercased names.
After that you must define a parser for your reserved word operator, and add it to the parser map:
// This parser is very simple and only requires one line:
void my_op_parser(const char* expr, const char** rest, rpnBuilder* data) {
// Use here the same name as before
data->handle_op("my op");
}
struct Startup_Parser {
Startup_Parser() {
parserMap_t& parser = calculator::Default().parserMap;
// Here you must use the actual word you expect to use
// on the code instead an Ad Hoc name as before:
parser.add("and", &my_op_parser);
}
} Startup_Parser;And that should do the trick. Now when the code is being parsed it will read the word "and" and then add the operator "my op" to the expression, then when executing the Operation Matching Loop it will access the definitions we wrote on Startup_Op.
Note: To keep it simpler you might want to join both startup functions into a single one. There is no need for 2 of them.