Skip to content

Commit

Permalink
Merge pull request #9 from dedoussis/refactor-treeifier
Browse files Browse the repository at this point in the history
refactor(utils.ts): Treefier extraced from Node.ts to utils.ts
  • Loading branch information
dedoussis committed Apr 13, 2020
2 parents f1e397c + 3c99f24 commit cd25e73
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ compiled
.rpt2_cache
docs
.antlr
.npmrc
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "algebrain",
"version": "0.0.7",
"version": "0.0.9",
"description": "Combuter Algebra System focusing on symbolic transformations",
"keywords": [
"algebra",
Expand Down
13 changes: 7 additions & 6 deletions src/Command.ts
Expand Up @@ -2,6 +2,7 @@ import { List, Map } from 'immutable';
import Executable, { Namespace, Output } from './Executable';
import Node from './Node';
import simplification from './transformations/simplification';
import { treeify } from './utils';

export enum CommandName {
Transform = 'transform',
Expand Down Expand Up @@ -48,7 +49,7 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
stdOut: transformed.toString(),
};
},
description: 'Transform current expression using the active transformation',
description: 'Transform the current expression',
},
],
[
Expand All @@ -70,7 +71,7 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
stdOut: evaluated.toString(),
};
},
description: 'Evaluate current expression',
description: 'Evaluate the current expression',
},
],
[
Expand Down Expand Up @@ -100,7 +101,7 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
};
},
description:
'List rules of given transformation - Requires transformation name as a string parameter',
'List the rules of a given transformation - Requires transformation name as a string parameter',
},
],
[
Expand Down Expand Up @@ -136,10 +137,10 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
}
return {
namespace: namespace,
stdOut: namespace.expression.treeify('', '\xa0'),
stdOut: treeify(namespace.expression),
};
},
description: 'Tree representation of expression',
description: 'Tree representation of the expression',
},
],
[
Expand All @@ -151,7 +152,7 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
stdOut: `[ ${namespace.transformations.keySeq().join(', ')} ]`,
};
},
description: 'List all defined transformations',
description: 'List all the defined transformations',
},
],
]);
Expand Down
18 changes: 0 additions & 18 deletions src/Node.ts
Expand Up @@ -25,10 +25,6 @@ export default abstract class Node implements Executable {
return this;
}

treeify(childPrefix: string, space: string): string {
return this.toString();
}

transform(
defaultTransformation: Transformation,
transformationMap: TransformationMap = Map()
Expand Down Expand Up @@ -151,20 +147,6 @@ export class Operator extends Node {
return infix(this.value, stringifiedChildren);
}

treeify(childPrefix: string, space: string): string {
return this.children.reduce(
(treeified: string, child: Node, index: number, children: List<Node>) => {
const [branch, prefixExtention]: [string, string] =
index === children.size - 1 ? ['└─', space] : ['β”œβ”€', 'β”‚'];
return `${treeified}\n${childPrefix}${branch} ${child.treeify(
`${childPrefix}${prefixExtention}${space}${space}`,
space
)}`;
},
this.value
);
}

transform(
defaultTransformation: Transformation,
transformationMap: TransformationMap = Map()
Expand Down
4 changes: 3 additions & 1 deletion src/Transformation.ts
@@ -1,6 +1,7 @@
import { List } from 'immutable';
import Executable, { Namespace, Output } from './Executable';
import Rule from './Rule';
import { indent, newLine } from './utils';

export default class Transformation implements Executable {
constructor(readonly name: string, readonly rules: List<Rule> = List()) {}
Expand All @@ -10,7 +11,8 @@ export default class Transformation implements Executable {
}

toString(): string {
return `[\n\xa0\xa0${this.rules.join(`,\n\xa0\xa0`)}\n]`;
const newLineIndent = newLine + indent;
return '[' + newLineIndent + this.rules.join(',' + newLineIndent) + newLine + ']';
}

equals(other: any): boolean {
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Expand Up @@ -19,3 +19,25 @@ export const and = generateOperator(OperatorSymbol.And);
export const or = generateOperator(OperatorSymbol.Or);
export const flag = generateOperator(OperatorSymbol.Flag);
export const equals = generateOperator(OperatorSymbol.Equals);

export const space = ' ';
export const newLine = '\n';
export const indent = space.repeat(2);

export function treeify(node: Node, childPrefix = ''): string {
if (!(node instanceof Operator)) {
return node.toString();
}

return node.children.reduce(
(treeified: string, child: Node, index: number, children: List<Node>) => {
const [branch, prefixExtention] =
index === children.size - 1 ? ['└─', space] : ['β”œβ”€', 'β”‚'];
const newPrefix = childPrefix + prefixExtention + indent;
const treefiedChild = treeify(child, newPrefix);
const prefixedTreefiedChild = childPrefix + branch + space + treefiedChild;
return treeified + newLine + prefixedTreefiedChild;
},
node.value
);
}
19 changes: 0 additions & 19 deletions test/node.test.ts
Expand Up @@ -311,23 +311,4 @@ describe('Operator', () => {
);
expect(one.equals(another)).toBeFalsy();
});

it('it treeifies', () => {
const expr: Operator = div(
plus(minus(new Rewritable('u'), new Symbol('x')), new Num(5)),
pow(new Symbol('g'), new Num(5))
);
const treefied: string[] = [
'/',
'β”œβ”€ +',
'β”‚ β”œβ”€ -',
'β”‚ β”‚ β”œβ”€ $u',
'β”‚ β”‚ └─ x',
'β”‚ └─ 5',
'└─ ^',
' β”œβ”€ g',
' └─ 5',
];
expect(expr.treeify('', ' ')).toEqual(treefied.join('\n'));
});
});
35 changes: 33 additions & 2 deletions test/utils.test.ts
@@ -1,6 +1,16 @@
import { List } from 'immutable';
import { generateOperator, OperatorGenerator, plus, minus, mul, div, pow } from '../src/utils';
import { Operator, OperatorSymbol, Num } from '../src/Node';
import {
generateOperator,
OperatorGenerator,
plus,
minus,
mul,
div,
pow,
treeify,
newLine,
} from '../src/utils';
import { Operator, OperatorSymbol, Num, Rewritable, Symbol } from '../src/Node';

describe('generateOperator', () => {
it('generates any given operator symbol', () => {
Expand Down Expand Up @@ -33,3 +43,24 @@ describe('generateOperator', () => {
expect(new Operator(OperatorSymbol.Pow).equals(operator)).toBeTruthy;
});
});

describe('treeifier', () => {
it('it treeifies', () => {
const expr = div(
plus(minus(new Rewritable('u'), new Symbol('x')), new Num(5)),
pow(new Symbol('g'), new Num(5))
);
const expected = [
'/',
'β”œβ”€ +',
'β”‚ β”œβ”€ -',
'β”‚ β”‚ β”œβ”€ $u',
'β”‚ β”‚ └─ x',
'β”‚ └─ 5',
'└─ ^',
' β”œβ”€ g',
' └─ 5',
].join(newLine);
expect(treeify(expr)).toEqual(expected);
});
});

0 comments on commit cd25e73

Please sign in to comment.