mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2026-01-15 08:22:37 +01:00
remove bin, dec, hex, other properties from Operand
This commit is contained in:
@@ -11,7 +11,7 @@ export default class BinaryStringView extends React.Component {
|
||||
}
|
||||
|
||||
if(this.props.onFlipBit) {
|
||||
this.props.onFlipBit(index);
|
||||
this.props.onFlipBit({ index: index, binaryString: this.props.binaryString, $event: e });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ export default class BinaryStringView extends React.Component {
|
||||
const allowFlipBits = this.props.allowFlipBits || false;
|
||||
const css = allowFlipBits ? ' flipable' : ''
|
||||
const classNames = { '0': `zero${css}`, '1' : `one ${css}` };
|
||||
|
||||
return bitChars.map((c, i) => <span className={classNames[c]} key={i} onClick={e => this.onBitClick(i, e)}>{c}</span>);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React from 'react';
|
||||
import * as expression from '../../expression';
|
||||
import { Operand, ListOfNumbersExpression, SingleOperandExpression, MultipleOperandsExpression } from '../../expression';
|
||||
import formatter from '../../formatter';
|
||||
import BinaryStringView from './BinaryStringView';
|
||||
import BitwiseExpressionViewModel from './models/BitwiseExpressionViewModel';
|
||||
@@ -23,17 +23,17 @@ export default class BitwiseOperationEpxressionView extends React.Component {
|
||||
const expr = this.props.expression;
|
||||
var model = null;
|
||||
|
||||
if(expr instanceof expression.ListOfNumbersExpression) {
|
||||
if(expr instanceof ListOfNumbersExpression) {
|
||||
model = BitwiseExpressionViewModel.buildListOfNumbers(expr, {
|
||||
emphasizeBytes: this.props.emphasizeBytes,
|
||||
allowFlipBits: true });
|
||||
}
|
||||
|
||||
if(expr instanceof expression.SingleOperandExpression) {
|
||||
if(expr instanceof SingleOperandExpression) {
|
||||
model = BitwiseExpressionViewModel.buildNot(expr, { emphasizeBytes: this.props.emphasizeBytes });
|
||||
}
|
||||
|
||||
if(expr instanceof expression.MultipleOperandsExpression) {
|
||||
if(expr instanceof MultipleOperandsExpression) {
|
||||
model = BitwiseExpressionViewModel.buildMultiple(expr, { emphasizeBytes: this.props.emphasizeBytes });
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class ExpressionRow extends React.Component {
|
||||
this.state = { operand: null };
|
||||
}
|
||||
render() {
|
||||
const { sign, label, bin, other, css, maxNumberOfBits, emphasizeBytes, allowFlipBits, operand } = this.props;
|
||||
const { sign, css, maxNumberOfBits, emphasizeBytes, allowFlipBits, operand } = this.props;
|
||||
|
||||
return <tr className={css}>
|
||||
<td className="sign">{sign}</td>
|
||||
@@ -56,28 +56,31 @@ class ExpressionRow extends React.Component {
|
||||
<td className="bin">
|
||||
<BinaryStringView
|
||||
emphasizeBytes={emphasizeBytes}
|
||||
binaryString={formatter.padLeft(operand.bin, maxNumberOfBits, '0')}
|
||||
binaryString={formatter.padLeft(operand.toBinaryString(), maxNumberOfBits, '0')}
|
||||
allowFlipBits={allowFlipBits}
|
||||
onFlipBit={idx => this.flipBit(idx)}/>
|
||||
</td>
|
||||
<td className="other">{operand.other}</td>
|
||||
</tr>;
|
||||
<td className="other">{this.getOther(operand)}</td>
|
||||
</tr>;;
|
||||
}
|
||||
|
||||
getLabel(op) {
|
||||
return op.kind == 'bin' ? op.dec : op.toString();
|
||||
return op.toString(op.kind == 'bin' ? 'dec' : op.kind);
|
||||
}
|
||||
|
||||
flipBit(index) {
|
||||
getOther(op) {
|
||||
return op.toString(op.getOtherKind());
|
||||
}
|
||||
|
||||
var op = this.props.operand;
|
||||
const binaryString = formatter.padLeft(op.bin, this.props.maxNumberOfBits, '0');
|
||||
flipBit(args) {
|
||||
|
||||
const op = this.props.operand;
|
||||
const { index, binaryString } = args;
|
||||
|
||||
var arr = binaryString.split('');
|
||||
// TODO: this code looks ugly
|
||||
arr[index] = arr[index] == '0' ? '1' : '0';
|
||||
var bin = arr.join('');
|
||||
|
||||
console.log('new bin: '+ bin);
|
||||
op.setValue(parseInt(bin, 2));
|
||||
|
||||
this.setState({ operand: op });
|
||||
|
||||
@@ -51,21 +51,16 @@ export default class BitwiseExpressionViewModel {
|
||||
this.maxNumberOfBits = Math.max(operand.getLengthInBits(), this.maxNumberOfBits);
|
||||
this.items.push({
|
||||
sign:'',
|
||||
label: this.getLabel(operand),
|
||||
bin: operand.bin,
|
||||
other: operand.other,
|
||||
css: '',
|
||||
operand: operand});
|
||||
operand: operand
|
||||
});
|
||||
};
|
||||
|
||||
addExpression(expression) {
|
||||
this.maxNumberOfBits = Math.max(expression.operand1.getLengthInBits(), this.maxNumberOfBits);
|
||||
this.items.push({
|
||||
sign: expression.sign,
|
||||
label: this.getLabel(expression.operand1),
|
||||
bin: expression.operand1.bin,
|
||||
other: expression.operand1.other,
|
||||
css: '',
|
||||
label: this.getLabel(expression.operand1),
|
||||
operand: expression.operand1
|
||||
});
|
||||
};
|
||||
@@ -74,22 +69,18 @@ export default class BitwiseExpressionViewModel {
|
||||
this.maxNumberOfBits = Math.max(resultOperand.getLengthInBits(), this.maxNumberOfBits);
|
||||
this.items.push({
|
||||
sign: expression.sign + expression.operand1.input,
|
||||
label: this.getLabel(resultOperand),
|
||||
bin: resultOperand.bin,
|
||||
other: resultOperand.other,
|
||||
css: 'expression-result',
|
||||
operand: resultOperand});
|
||||
operand: resultOperand
|
||||
});
|
||||
};
|
||||
|
||||
addExpressionResult(operand) {
|
||||
this.maxNumberOfBits = Math.max(operand.getLengthInBits(), this.maxNumberOfBits);
|
||||
this.items.push({
|
||||
sign:'=',
|
||||
label: this.getLabel(operand),
|
||||
bin: operand.bin,
|
||||
other: operand.other,
|
||||
css: 'expression-result',
|
||||
operand: operand});
|
||||
operand: operand
|
||||
});
|
||||
};
|
||||
|
||||
getLabel (op) {
|
||||
|
||||
@@ -4,17 +4,9 @@ import ExpressionError from './ExpressionError';
|
||||
// Represents numeric value
|
||||
export default class Operand {
|
||||
constructor(cfg) {
|
||||
|
||||
this.input = cfg.input;
|
||||
this.value = cfg.value;
|
||||
this.kind = cfg.kind;
|
||||
|
||||
this.hex = Operand.toHexString(this.value.toString(16));
|
||||
this.dec = this.value.toString(10);
|
||||
// >>> 0 makes negative numbers like -1 to be displayed as '11111111111111111111111111111111' in binary instead of -1
|
||||
this.bin = this.value < 0 ? (this.value >>> 0).toString(2) : this.value.toString(2);
|
||||
this.other = this.kind == 'hex' ? this.dec : this.hex;
|
||||
|
||||
this.lengthInBits = Operand.getBitLength(this.value);
|
||||
}
|
||||
|
||||
@@ -35,17 +27,29 @@ export default class Operand {
|
||||
}
|
||||
};
|
||||
|
||||
toString() {
|
||||
return this.input;
|
||||
toString(kind) {
|
||||
return Operand.toKindString(this.value, kind || this.kind);
|
||||
}
|
||||
|
||||
toOtherKindString() {
|
||||
return this.toString(this.getOtherKind());
|
||||
}
|
||||
|
||||
toDecimalString() {
|
||||
return this.toString('dec');
|
||||
}
|
||||
|
||||
toHexString() {
|
||||
return this.toString('hex');
|
||||
}
|
||||
|
||||
toBinaryString() {
|
||||
return this.toString('bin');
|
||||
}
|
||||
|
||||
setValue(value) {
|
||||
this.value = value;
|
||||
this.bin = Operand.toKindString(this.value, 'bin');
|
||||
this.dec = Operand.toKindString(this.value, 'dec');
|
||||
this.hex = Operand.toKindString(this.value, 'hex');
|
||||
this.other = Operand.toKindString(this.value, this.getOtherKind());
|
||||
this.input = Operand.toKindString(this.value, this.kind);
|
||||
this.input = this.toString();
|
||||
}
|
||||
|
||||
static getBitLength(num) {
|
||||
|
||||
@@ -94,9 +94,9 @@ describe('negative operands', function () {
|
||||
var op = parser.parseOperand('-0xa');
|
||||
it('shoold have correct values', function() {
|
||||
expect(op.value).toBe(-10);
|
||||
expect(op.hex).toBe('-0xa');
|
||||
expect(op.bin).toBe('11111111111111111111111111110110');
|
||||
expect(op.dec).toBe('-10');
|
||||
expect(op.toHexString()).toBe('-0xa');
|
||||
expect(op.toBinaryString()).toBe('11111111111111111111111111110110');
|
||||
expect(op.toDecimalString()).toBe('-10');
|
||||
expect(op.kind).toBe('hex');
|
||||
})
|
||||
});
|
||||
@@ -129,15 +129,15 @@ function rundOperandsTest(hexOperand, decOperand) {
|
||||
it('should have all kinds', function () {
|
||||
|
||||
expect(hexOperand.kind).toBe('hex');
|
||||
expect(hexOperand.dec).toBe('16');
|
||||
expect(hexOperand.bin).toBe('10000');
|
||||
expect(hexOperand.hex).toBe('0x10');
|
||||
expect(hexOperand.other).toBe('16');
|
||||
expect(hexOperand.toDecimalString()).toBe('16');
|
||||
expect(hexOperand.toBinaryString()).toBe('10000');
|
||||
expect(hexOperand.toHexString()).toBe('0x10');
|
||||
expect(hexOperand.toOtherKindString()).toBe('16');
|
||||
|
||||
expect(decOperand.kind).toBe('dec');
|
||||
expect(decOperand.dec).toBe('10');
|
||||
expect(decOperand.bin).toBe('1010');
|
||||
expect(decOperand.hex).toBe('0xa');
|
||||
expect(decOperand.other).toBe('0xa');
|
||||
expect(decOperand.toDecimalString()).toBe('10');
|
||||
expect(decOperand.toBinaryString()).toBe('1010');
|
||||
expect(decOperand.toHexString()).toBe('0xa');
|
||||
expect(decOperand.toOtherKindString()).toBe('0xa');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user