0

I have string in the format (AND, true, false, (OR, true, false, false, (AND, false, true))) for example, Is there a way in JavaScript by which we can evaluate this string to a boolean value?

expression 1: (AND, true, false) => false

expression 2: (AND, true, (OR, false, true)) will evaluate to => (AND, true, true) => true

like this we can have an expression with nested conditions.

Any solution for solving this one will be a huge help. Thanks in advance

AdityaHM
  • 35
  • 4
  • 2
    Nothing native. You'll have to parse that apart yourself. If you're creating that string, consider using JSON.stringify() instead. That at least will save you trying to figure out how to take it back apart. – Charlie Bamford Aug 26 '20 at 17:39
  • Please show any attempt that you have made to solve this issue yourself. You are expected to have made an attempt that we can then help you debug. https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users Also review [ask] – Taplar Aug 26 '20 at 17:40

3 Answers3

0

One option is to use a regular expression to match either an (OR ... or (AND sequence which doesn't contain any nested parentheses, and replace it with the appropriate resolve value. Do this recursively until the string has only one item in it:

const parse = (input) => {
  while (input.includes(' ')) {
    input = input.replace(
      /\((AND|OR)([^()]+)\)/g,
      (_, type, restOfBlock) => {
        // Words will become an array of the words present here, eg ['true', 'false']
        const words = restOfBlock.match(/\w+/g);
        if (type === 'OR') return words.includes('true');
        return !words.includes('false');
      }
    );
  }
  return input === 'true';
};

console.log(parse(`(AND, true, false, (OR, true, false, false, (AND, false, true)))`)); // expression 0
console.log(parse(`(AND, true, false)`)); // expression 1
console.log(parse(`(AND, true, (OR, false, true))`)); // expression 2
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

If you're trying to write code that will evaluate any (well-formed) expression like the examples you've given, then you're looking to write a parser for your language.

Stepping back for a moment: your examples seem to be examples of a language for expressing logical expressions. It looks like a "custom" lanuage, so it's not too likely you'll find a ready-made solution, but it's not too hard to create your own.

I would suggest looking into PEG.js. This package is a parser generator: you give it a file describing your language and it will generate Javascript code to parse it.

Kryten
  • 15,230
  • 6
  • 45
  • 68
0

You could replace some parts to get an evaluable string.

function evaluate(string) {
    const
        AND = (...args) => args.every(Boolean),
        OR = (...args) => args.some(Boolean);
    return eval(string.replace(/\(([^,]+),\s*/g, '$1('));
}


console.log(evaluate('(AND, true, true)'));

console.log(evaluate('(AND, true, false, (OR, true, false, false, (AND, false, true)))'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392