Disable flip-bit for now

This commit is contained in:
Borys_Levytskyi
2016-11-27 10:48:34 +02:00
parent a981448846
commit b31ad81396
7 changed files with 48 additions and 24 deletions

View File

@@ -59,7 +59,6 @@ var cmd = {
} }
function invokeHandler (input, handler) { function invokeHandler (input, handler) {
console.log('[invokeHandler]: ' + input);
var cmdResult = handler.handle({ input: input}); var cmdResult = handler.handle({ input: input});
if(cmdResult != null) { if(cmdResult != null) {
console.log(cmdResult); console.log(cmdResult);

View File

@@ -13,7 +13,6 @@ export default {
canHandle: (input) => expression.parser.canParse(input), canHandle: (input) => expression.parser.canParse(input),
handle: function(c) { handle: function(c) {
var expr = expression.parser.parse(c.input); var expr = expression.parser.parse(c.input);
console.log(expr);
appState.addCommandResult(new ExpressionResult(c.input, expr)); appState.addCommandResult(new ExpressionResult(c.input, expr));
} }
}) })

View File

@@ -12,6 +12,8 @@ export default class AppRoot extends React.Component {
this.setState(this.props.appState); this.setState(this.props.appState);
} }
render() { render() {
console.log('[AppRoot] render():this.state.commandResults', this.state.commandResults)
var results = this.state.commandResults.map((r, i) => <DisplayResultView key={i} content={r} input={r.input} inputHash={r.inputHash} />); var results = this.state.commandResults.map((r, i) => <DisplayResultView key={i} content={r} input={r.input} inputHash={r.inputHash} />);
return <div> return <div>
<div className="header"> <div className="header">

View File

@@ -5,8 +5,12 @@ import * as expression from '../../expression';
export default class ExpressionResultView extends React.Component { export default class ExpressionResultView extends React.Component {
render() { render() {
var expr = this.props.result.expression; var expr = this.props.result.expression;
if(expr instanceof expression.ListOfNumbersExpression) { if(expr instanceof expression.ListOfNumbersExpression) {
return <ListOfNumbersExpressionView expression={expr} /> return <div>
<b>Expression: {expr.expressionString}</b>
<ListOfNumbersExpressionView expression={expr} />
</div>
} }
return <b>Expression: {expr.expressionString}</b>; return <b>Expression: {expr.expressionString}</b>;

View File

@@ -4,35 +4,47 @@ import formatter from '../../formatter';
export default class ListOfNumersExpressionView extends React.Component { export default class ListOfNumersExpressionView extends React.Component {
render() { render() {
const expr = this.props.expression; const expr = this.props.expression;
const numberViews = expr.numbers.map((n, i) => <OperandView key={i} operand={n} maxBitsLegnth={expr.maxBitsLegnth} />) const numberRows = expr.numbers.map((n, i) => <OperandView key={i} operand={n} maxBitsLegnth={expr.maxBitsLegnth} />);
return <table className="expression" cellspacing="0"> console.log('Numbers: ', expr.numbers);
{numberViews} return <div>
</table> <div>
!{expr.toString()}!
</div>
<table className="expression">
<tbody>
{numberRows}
</tbody>
</table>
</div>
} }
} }
class OperandView extends React.Component { class OperandView extends React.Component {
componentWillMount() { constructor() {
this.setState(this.props.operand); super();
} this.state = { operand: null };
}
render() { render() {
const op = this.state; const op = this.props.operand;
const binaryString = formatter.padLeft(op.bin, this.props.maxBitsLegnth, '0'); const binaryString = formatter.padLeft(op.bin, this.props.maxBitsLegnth, '0');
return <tr data-kind={op.kind}> return <tr data-kind={op.kind}>
<td className="label">{op.input}</td> <td className="label">{op.input}</td>
<td className="bin"><ClickableBinary binaryString={binaryString} onFlipBit={i => this.flipBit(i)} /></td> <td className="bin"><ClickableBinary binaryString={binaryString} onFlipBit={e => this.flipBit(e)} /></td>
<td className="other">{op.other}</td> <td className="other">{op.other}</td>
</tr> </tr>;
}; };
flipBit(index) { flipBit(index) {
var op = this.props.operand; var op = this.props.operand;
const binaryString = formatter.padLeft(op.bin, this.props.maxBitsLegnth, '0'); const binaryString = formatter.padLeft(op.bin, this.props.maxBitsLegnth, '0');
var arr = binaryString.split(''); var arr = binaryString.split('');
// TODO: this code looks ugly
arr[index] = arr[index] == '0' ? '1' : '0'; arr[index] = arr[index] == '0' ? '1' : '0';
op.update(arr.join('')); var bin = arr.join('');
this.setState(op); op.setValue(parseInt(bin, 2));
this.setState({ operand: op });
} }
} }

View File

@@ -134,7 +134,7 @@ export class Operand {
getOtherKind(kind) { getOtherKind(kind) {
switch(kind) { switch(kind || this.kind) {
case 'dec': return 'hex'; case 'dec': return 'hex';
case 'hex': return 'dec'; case 'hex': return 'dec';
default : throw new Error(kind + " kind doesn't have opposite kind") default : throw new Error(kind + " kind doesn't have opposite kind")
@@ -145,11 +145,15 @@ export class Operand {
return this.input; return this.input;
} }
update(binary) { setValue(value) {
this.value = parseInt(2); console.log('Before ' + value, this);
this.bin = binary; this.value = value;
this.bin = Operand.toKindString(this.value, 'bin');
this.dec = Operand.toKindString(this.value, 'dec'); this.dec = Operand.toKindString(this.value, 'dec');
this.hex = Operand.toKindString(this.value, 'hex'); this.hex = Operand.toKindString(this.value, 'hex');
this.other = Operand.toKindString(this.value, this.getOtherKind());
this.input = Operand.toKindString(this.value, this.kind);
console.log('After ' + value, this);
} }
static getBitLength(num) { static getBitLength(num) {
@@ -241,6 +245,10 @@ export class ListOfNumbersExpression {
this.numbers = numbers; this.numbers = numbers;
this.maxBitsLegnth = _.chain(numbers).map(n => n.lengthInBits).reduce((n , c) => n >= c ? n : c, 0).value(); this.maxBitsLegnth = _.chain(numbers).map(n => n.lengthInBits).reduce((n , c) => n >= c ? n : c, 0).value();
} }
toString() {
return this.numbers.map(n => n.value.toString()).join(' ');
}
} }
export class Expression { export class Expression {

View File

@@ -8,9 +8,9 @@ import AppRoot from './components/AppRoot';
commands.initialize(cmd); commands.initialize(cmd);
cmd.execute('1'); // cmd.execute('1');
cmd.execute('2'); // cmd.execute('2');
cmd.execute('3'); // cmd.execute('3');
var root = <AppRoot appState={appState} />; var root = <AppRoot appState={appState} />;
ReactDOM.render(root, document.getElementById('root')); ReactDOM.render(root, document.getElementById('root'));