Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Added more boolean expressions. #30

Merged
merged 1 commit into from
Nov 12, 2017
Merged
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
5 changes: 5 additions & 0 deletions src/calder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export { default as AndExpression } from './expressions/boolean/and_expression';
export { default as OrExpression } from './expressions/boolean/or_expression';
export { default as EqualExpression } from './expressions/boolean/equal_expression';
export { default as NotEqualExpression } from './expressions/boolean/not_equal_expression';
export { default as XorExpression } from './expressions/boolean/xor_expression';
export { default as LessThanExpression } from './expressions/boolean/less_than_expression';
export { default as LessThanEqualExpression } from './expressions/boolean/less_than_equal_expression';
export { default as GreaterThanExpression } from './expressions/boolean/greater_than_expression';
export { default as GreaterThanEqualExpression } from './expressions/boolean/greater_than_equal_expression';
export { default as Statement } from './statement';
export { default as Assignment } from './expressions/assignment';
export { default as Function } from './function';
Expand Down
12 changes: 12 additions & 0 deletions src/expressions/boolean/greater_than_equal_expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BooleanExpression from './boolean_expression';
import Expression from '../expression';

export default class GreaterThanEqualExpression extends BooleanExpression {
constructor(lhs: Expression, rhs: Expression) {
super(lhs, rhs);
}

public source(): string {
return `(${this.lhs.source()} >= ${this.rhs.source()})`;
}
}
12 changes: 12 additions & 0 deletions src/expressions/boolean/greater_than_expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BooleanExpression from './boolean_expression';
import Expression from '../expression';

export default class GreaterThanExpression extends BooleanExpression {
constructor(lhs: Expression, rhs: Expression) {
super(lhs, rhs);
}

public source(): string {
return `(${this.lhs.source()} > ${this.rhs.source()})`;
}
}
12 changes: 12 additions & 0 deletions src/expressions/boolean/less_than_equal_expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BooleanExpression from './boolean_expression';
import Expression from '../expression';

export default class LessThanEqualExpression extends BooleanExpression {
constructor(lhs: Expression, rhs: Expression) {
super(lhs, rhs);
}

public source(): string {
return `(${this.lhs.source()} <= ${this.rhs.source()})`;
}
}
12 changes: 12 additions & 0 deletions src/expressions/boolean/less_than_expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BooleanExpression from './boolean_expression';
import Expression from '../expression';

export default class LessThanExpression extends BooleanExpression {
constructor(lhs: Expression, rhs: Expression) {
super(lhs, rhs);
}

public source(): string {
return `(${this.lhs.source()} < ${this.rhs.source()})`;
}
}
12 changes: 12 additions & 0 deletions src/expressions/boolean/xor_expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import BooleanExpression from './boolean_expression';
import Expression from '../expression';

export default class XorExpression extends BooleanExpression {
constructor(lhs: Expression, rhs: Expression) {
super(lhs, rhs);
}

public source(): string {
return `(${this.lhs.source()} ^^ ${this.rhs.source()})`;
}
}
85 changes: 52 additions & 33 deletions test/if.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,76 +78,95 @@ describe('If', () => {
expect(ifStmt.source()).to.equalIgnoreSpaces('if (a) { a=b; }');
});

describe('operators', () => {
describe('boolean expressions', () => {
it('and', () => {
const ifStmt = new cgl.If(
new cgl.AndExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([
new cgl.Statement(
new cgl.Assignment(new cgl.Reference(a), new cgl.Reference(b))
)
]),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a && b)) { a=b; }');
expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a && b)) {}');
});

it('or', () => {
const ifStmt = new cgl.If(
new cgl.OrExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([
new cgl.Statement(
new cgl.Assignment(new cgl.Reference(a), new cgl.Reference(b))
)
]),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a || b)) { a=b; }');
expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a || b)) {}');
});

it('equal', () => {
const ifStmt = new cgl.If(
new cgl.EqualExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([
new cgl.Statement(
new cgl.Assignment(new cgl.Reference(a), new cgl.Reference(b))
)
]),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a == b)) { a=b; }');
expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a == b)) {}');
});

it('xor', () => {
const ifStmt = new cgl.If(
new cgl.XorExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a ^^ b)) {}');
});

it('less than', () => {
const ifStmt = new cgl.If(
new cgl.LessThanExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a < b)) {}');
});

it('less than equal', () => {
const ifStmt = new cgl.If(
new cgl.LessThanEqualExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a <= b)) {}');
});

it('greater than', () => {
const ifStmt = new cgl.If(
new cgl.GreaterThanExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a > b)) {}');
});

it('not equal', () => {
it('greater than equal', () => {
const ifStmt = new cgl.If(
new cgl.NotEqualExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([
new cgl.Statement(
new cgl.Assignment(new cgl.Reference(a), new cgl.Reference(b))
)
]),
new cgl.GreaterThanEqualExpression(new cgl.Reference(a), new cgl.Reference(b)),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a != b)) { a=b; }');
expect(ifStmt.source()).to.equalIgnoreSpaces('if ((a >= b)) {}');
});

it('nested', () => {
const ifStmt = new cgl.If(
new cgl.AndExpression(new cgl.Reference(a), new cgl.OrExpression(new cgl.Reference(b), new cgl.Reference(c))),
new cgl.Block([
new cgl.Statement(
new cgl.Assignment(new cgl.Reference(a), new cgl.Reference(b))
)
]),
new cgl.Block([]),
new cgl.Block()
);

expect(ifStmt.source()).to.equalIgnoreSpaces('if ( ( a && (b || c) ) ) { a=b; }');
expect(ifStmt.source()).to.equalIgnoreSpaces('if ( ( a && (b || c) ) ) {}');
});
});
});
Expand Down