Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean calculator jan 17 1 #66

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
import {BooleanCalculator, Expression, Result} from "./index";

describe('boolean calculator', () => {
const testMessage = 'should know that the result for %s is %s';

describe('single values', () => {
const cases: [Expression, boolean][] = [
['TRUE', true],
['FALSE', false]
];
it.each(cases)(testMessage, (expression: Expression, expected: boolean) => {
expect(BooleanCalculator.run(expression)).toBe(expected);
})
})

describe('NOT operator', () => {
const cases: [Expression, boolean][] = [
['NOT FALSE', true],
['NOT TRUE', false]
];
it.each(cases)(testMessage, (expression: Expression, expected: boolean) => {
expect(BooleanCalculator.run(expression)).toBe(expected);
})
})

describe('AND operator', () => {
const cases: [Expression, boolean][] = [
['TRUE AND TRUE', true],
['TRUE AND FALSE', false]
];
it.each(cases)(testMessage, (expression: Expression, expected: boolean) => {
expect(BooleanCalculator.run(expression)).toBe(expected);
})
})

describe('OR operator', () => {
const cases: [Expression, boolean][] = [
['TRUE OR FALSE', true],
['FALSE OR FALSE', false]
];
it.each(cases)(testMessage, (expression: Expression, expected: boolean) => {
expect(BooleanCalculator.run(expression)).toBe(expected);
})
})

describe('combination of operations and precedence', () => {
const cases: [Expression, boolean][] = [
['TRUE OR TRUE OR TRUE AND FALSE', true],
['TRUE OR FALSE AND NOT FALSE', true],
['(TRUE OR TRUE OR TRUE) AND FALSE', false],
['NOT (TRUE AND TRUE)', false]
]
it.each(cases)(testMessage, (expression: Expression, expected: boolean) => {
expect(BooleanCalculator.run(expression)).toBe(expected);
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export type Expression = string;
export type Result = boolean;

export class BooleanCalculator {
static run(expression: Expression): Result {
const tokens: Expression[] = this.tokenize(expression);
const { value } = this.parseExpression(tokens);
return value;
}

private static tokenize(expression: Expression): Expression[] {
return expression
.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ')
.trim()
.split(/\s+/);
}

private static parseExpression(tokens: string[]): { value: boolean; rest: string[] } {
return this.parseOr(tokens);
}

private static parseOr(tokens: string[]): { value: boolean; rest: string[] } {
let { value: left, rest } = this.parseAnd(tokens)
while (rest[0] === 'OR') {
const partial = this.parseAnd(rest.slice(1))
left = left || partial.value
rest = partial.rest
}
return { value: left, rest }
}

private static parseAnd(tokens: string[]): { value: boolean; rest: string[] } {
let { value: left, rest } = this.parseNot(tokens)
while (rest[0] === 'AND') {
const partial = this.parseNot(rest.slice(1))
left = left && partial.value
rest = partial.rest
}
return { value: left, rest }
}

private static parseNot(tokens: string[]): { value: boolean; rest: string[] } {
if (tokens[0] === 'NOT') {
const partial = this.parseNot(tokens.slice(1))
return { value: !partial.value, rest: partial.rest }
}
return this.parsePrimary(tokens)
}

private static parsePrimary(tokens: string[]): { value: boolean; rest: string[] } {
if (tokens[0] === '(') {
const partial = this.parseExpression(tokens.slice(1))
return { value: partial.value, rest: partial.rest.slice(1) }
}
if (tokens[0] === 'TRUE') {
return { value: true, rest: tokens.slice(1) }
}
if (tokens[0] === 'FALSE') {
return { value: false, rest: tokens.slice(1) }
}
throw new Error(`Unexpected token: ${tokens[0]}`)
}
}