Skip to content

Instantly share code, notes, and snippets.

@kmarenov
Last active October 18, 2019 09:51
Show Gist options
  • Select an option

  • Save kmarenov/b25ef76834774da24a0f35e72fa3e305 to your computer and use it in GitHub Desktop.

Select an option

Save kmarenov/b25ef76834774da24a0f35e72fa3e305 to your computer and use it in GitHub Desktop.
prefix to infix
function convert(tokens) {
const token = tokens.splice(0, 1)[0];
if (token === 'NOT') {
return `(${token} ${convert(tokens)})`;
}
if (['AND', 'OR'].includes(token)) {
return `(${convert(tokens)} ${token} ${convert(tokens)})`;
}
return token;
}
const source = "AND OR A B OR C NOT D";
const tokens = source.split(' ');
console.log(convert(tokens));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment