inline const bool Rule::DFA_Edge::compare_char(char c)const { #ifdef _DEBUG if (!isChar()) throw error<const char*>("error calling function compare_char!", __FILE__, __LINE__); #endif if (isNot()) return c != value.data.Char.value1; else return c == value.data.Char.value1; }
inline const bool Rule::DFA_Edge::compare_fromto(char c)const { #ifdef _DEBUG if (!isFromTo()) throw error<const char*>("error calling function compare_fromto!", __FILE__, __LINE__); #endif if (isNot()) return c < value.data.Char.value1 || c > value.data.Char.value2; else return c >= value.data.Char.value1 && c <= value.data.Char.value2; }
void tokenize(const LangOptions &LangOpts, const SourceManager &SM, unsigned BufferID, unsigned Offset, unsigned EndOffset, CommentRetentionMode RetainComments, TriviaRetentionMode TriviaRetention, bool TokenizeInterpolatedString, ArrayRef<Token> SplitTokens, DF &&DestFunc) { assert((TriviaRetention != TriviaRetentionMode::WithTrivia || !TokenizeInterpolatedString) && "string interpolation with trivia is not implemented yet"); if (Offset == 0 && EndOffset == 0) EndOffset = SM.getRangeForBuffer(BufferID).getByteLength(); Lexer L(LangOpts, SM, BufferID, /*Diags=*/nullptr, /*InSILMode=*/false, RetainComments, TriviaRetention, Offset, EndOffset); auto TokComp = [&](const Token &A, const Token &B) { return SM.isBeforeInBuffer(A.getLoc(), B.getLoc()); }; std::set<Token, decltype(TokComp)> ResetTokens(TokComp); for (auto C = SplitTokens.begin(), E = SplitTokens.end(); C != E; ++C) { ResetTokens.insert(*C); } Token Tok; syntax::Trivia LeadingTrivia, TrailingTrivia; do { L.lex(Tok, LeadingTrivia, TrailingTrivia); // If the token has the same location as a reset location, // reset the token stream auto F = ResetTokens.find(Tok); if (F != ResetTokens.end()) { assert(F->isNot(tok::string_literal)); DestFunc(*F, syntax::Trivia(), syntax::Trivia()); auto NewState = L.getStateForBeginningOfTokenLoc( F->getLoc().getAdvancedLoc(F->getLength())); L.restoreState(NewState); continue; } if (Tok.is(tok::string_literal) && TokenizeInterpolatedString) { std::vector<Token> StrTokens; getStringPartTokens(Tok, LangOpts, SM, BufferID, StrTokens); for (auto &StrTok : StrTokens) { DestFunc(StrTok, syntax::Trivia(), syntax::Trivia()); } } else { DestFunc(Tok, LeadingTrivia, TrailingTrivia); } } while (Tok.getKind() != tok::eof); }
const bool Rule::DFA_Edge::compare_string(const char* first, const char* last, size_t& n)const { #ifdef _DEBUG if (!isString()) throw error<const char*>("error calling function compare_string!", __FILE__, __LINE__); #endif if (!isNot()) { if ((size_t)(last - first) < value.data.String.size) return false; n = value.data.String.size; return strncmp(first, value.data.String.value, n) == 0; } return false; }
bool TableFieldBinaryTreeEvalNode::testEvaluator(const QHash<QString, bool>& vals) const { //qDebug(qPrintable(QString("Type(%1) Value(%2)").arg(nodeType()).arg(nodeValue()))); if (m_node == nullptr) { qDebug("NULL Node in the evaluator"); } else if (isValue()) { //qDebug(qPrintable(QString("Returning %1").arg(vals.value(nodeValue())))); return vals.value(nodeValue()); } else if (m_children.size() > 0) { bool rc = m_children.at(0)->testEvaluator(vals); if (isAnd()) { for (int i=1; rc && i<m_children.size(); ++i) { rc = m_children.at(i)->testEvaluator(vals); } } else if (isOr()) { for (int i=1; !rc && i<m_children.size(); ++i) { rc = m_children.at(i)->testEvaluator(vals); } } else if (isNot()) { return !rc; } else { return false; qDebug("Wrong number of arguments in the evaluator."); } return rc; } else { qDebug("No children for the evaluator to evaluate"); } return false; }
//Takes an expression in a string and evalutates in, boolean or arithmentic double evaluate_expression(string& input) { SyntaxChecker check; //Here's our checker object double rhs, lhs, result; //Some doubles we will need syntax_status oper; //this is for passing into the function process stack<double> operands; //Operand stack stack<syntax_status> operators; //Operator stack list<exprToken> expression; //Here's the list we need to pass into syntax_check //Here we pass in the expression to see if it passes the test if (check.syntax_check(input, expression) != 0) //this also populates the list of tokens { return numeric_limits<double>::quiet_NaN(); //if the input is invalid, return NaN } if (expression.size() == 0) { return numeric_limits<double>::quiet_NaN(); } //Iterate through the list of tokens for (list<exprToken>::iterator itr = expression.begin(); itr != expression.end(); ++itr) { //If it's a number push it on the operand stack if (itr->isANumber) { operands.push(itr->number); //If there is a negative or not on top of the stack, process it now while (!operators.empty() && isUnary(operators.top())) { if (isNot(operators.top())) result = !operands.top(); else if (isNegative(operators.top())) result = -operands.top(); operands.pop(); operators.pop(); operands.push(result); } } //If it's an operator else if (isOperator(itr->token)) { //if there are none, push it onto the stack if (operators.empty()) { operators.push(itr->token); } //if it's precedence is lower or equal to what's on top, process the last one else if (precedence(itr->token) <= precedence(operators.top())) { rhs = operands.top(); operands.pop(); lhs = operands.top(); operands.pop(); oper = operators.top(); operators.pop(); result = process(lhs, rhs, oper); operands.push(result); operators.push(itr->token); } //if it's of higher precedence, push it on to be processed later else if (precedence(itr->token) > precedence(operators.top())) { operators.push(itr->token); } } //it's an opening parenthesis, put it in operator stack else if (isOpen(itr->token)) { operators.push(itr->token); } //It's a closing parenthesis, process until the last opening parenthesis else if (isClose(itr->token)) { while (!isOpen(operators.top())) { rhs = operands.top(); operands.pop(); lhs = operands.top(); operands.pop(); oper = operators.top(); operators.pop(); operands.push(process(lhs, rhs, oper)); } operators.pop(); //dump the last opening parenthesis, we're done with it //Now if there is a ! or - on top of the operator stack we need to evaluate it //for the expression that was inside the parenthesis while (!operators.empty() && isUnary(operators.top())) { if (isNot(operators.top())) result = !operands.top(); else if (isNegative(operators.top())) result = -operands.top(); operands.pop(); operators.pop(); operands.push(result); } } //If we have a - or ! we need to put it in the operand stack regardless of any precedence else if (isUnary(itr->token)) { operators.push(itr->token); } } //After we finish going through the list, we need to evaluate any remaining operators while (!operators.empty()) { rhs = operands.top(); operands.pop(); lhs = operands.top(); operands.pop(); oper = operators.top(); operators.pop(); operands.push(process(lhs, rhs, oper)); } //The top of the operand stack is the solution return operands.top(); }
//This covers both isNegative and isNot bool isUnary(syntax_status token) { return isNot(token) || isNegative(token); }
// // Sort a term if it commutes. // The following simplifications implemented // (should be refactored to separate methods?): // - `and': // - drop constant true terms // - convert an empty `and' to the constant term `true' // - convert an `and' containing `false' to a replicate `false' // - `or': // - drop constant false terms // - convert an empty `or' to a replicate `false' term // - convert an `or' containing `true' to a replicate `true' // // void Logic::simplify(SymRef& s, vec<PTRef>& args) { // First sort it #ifdef SORT_BOOLEANS if (sym_store[s].commutes()) #else if (sym_store[s].commutes() && !isAnd(s) && !isOr(s)) #endif sort(args, LessThan_PTRef()); if (!isBooleanOperator(s) && !isEquality(s)) return; int dropped_args = 0; bool replace = false; if (s == getSym_and()) { int i, j; PTRef p = PTRef_Undef; for (i = j = 0; i < args.size(); i++) { if (args[i] == getTerm_false()) { args.clear(); s = getSym_false(); #ifdef SIMPLIFY_DEBUG cerr << "and -> false" << endl; #endif return; } else if (args[i] != getTerm_true() && args[i] != p) { args[j++] = p = args[i]; } else { #ifdef SIMPLIFY_DEBUG cerr << "and -> drop" << endl; #endif } } dropped_args = i-j; if (dropped_args == args.size()) { s = getSym_true(); args.clear(); #ifdef SIMPLIFY_DEBUG cerr << "and -> true" << endl; #endif return; } else if (dropped_args == args.size() - 1) replace = true; else if (dropped_args > 0) args.shrink(dropped_args); } if (s == getSym_or()) { int i, j; PTRef p = PTRef_Undef; for (i = j = 0; i < args.size(); i++) { if (args[i] == getTerm_true()) { args.clear(); s = getSym_true(); #ifdef SIMPLIFY_DEBUG cerr << "or -> true" << endl; #endif return; } else if (args[i] != getTerm_false() && args[i] != p) { args[j++] = p = args[i]; } else { #ifdef SIMPLIFY_DEBUG cerr << "or -> drop" << endl; #endif } } dropped_args = i-j; if (dropped_args == args.size()) { s = getSym_false(); args.clear(); #ifdef SIMPLIFY_DEBUG cerr << "or -> false" << endl; #endif return; } else if (dropped_args == args.size() - 1) replace = true; else if (dropped_args > 0) args.shrink(dropped_args); } if (isEquality(s)) { assert(args.size() == 2); if (isBooleanOperator(s) && (args[0] == getTerm_true())) { Pterm& t = getPterm(args[1]); s = t.symb(); args.clear(); for (int i = 0; i < t.size(); i++) args.push(t[i]); #ifdef SIMPLIFY_DEBUG cerr << "eq -> second" << endl; #endif return; } else if (isBooleanOperator(s) && (args[0] == getTerm_false())) { PTRef old = args[1]; PTRef tr = mkNot(args[1]); Pterm& t = getPterm(tr); s = t.symb(); args.clear(); args.push(old); #ifdef SIMPLIFY_DEBUG cerr << "eq -> not second" << endl; #endif return; } else if (isBooleanOperator(s) && (args[1] == getTerm_true())) { args.clear(); Pterm& t = getPterm(args[0]); s = t.symb(); args.clear(); for (int i = 0; i < t.size(); i++) args.push(t[i]); #ifdef SIMPLIFY_DEBUG cerr << "eq -> first" << endl; #endif return; } else if (isBooleanOperator(s) && (args[1] == getTerm_false())) { PTRef old = args[0]; PTRef tr = mkNot(args[0]); Pterm& t = getPterm(tr); s = t.symb(); args.clear(); args.push(old); #ifdef SIMPLIFY_DEBUG cerr << "eq -> not first"<< endl; #endif return; } else if (args[0] == args[1]) { args.clear(); s = getSym_true(); #ifdef SIMPLIFY_DEBUG cerr << "eq -> true" << endl; #endif return; } else if (isBooleanOperator(s) && (args[0] == mkNot(args[1]))) { args.clear(); s = getSym_false(); #ifdef SIMPLIFY_DEBUG cerr << "eq -> false" << endl; #endif return; } } if (isNot(s)) { if (isTrue(args[0])) { args.clear(); s = getSym_false(); #ifdef SIMPLIFY_DEBUG cerr << "not -> false" << endl; #endif return; } if (isFalse(args[0])) { args.clear(); s = getSym_true(); #ifdef SIMPLIFY_DEBUG cerr << "not -> true" << endl; #endif return; } } // Others, to be implemented: // - distinct // - implies // - xor // - ite if (replace) { // Return whatever is the sole argument Pterm& t = getPterm(args[0]); s = t.symb(); args.clear(); for (int i = 0; i < t.size(); i++) args.push(t[i]); #ifdef SIMPLIFY_DEBUG cerr << " replace" << endl; #endif } }