Rename NumericOperand to ScalarOperand

This commit is contained in:
BorysLevytskyi
2023-05-05 16:20:39 +02:00
parent 883dbe437c
commit 9819bd5c4f
14 changed files with 155 additions and 87 deletions

View File

@@ -1,5 +1,5 @@
import calc from './calc';
import { BitwiseOperationExpression, NumericOperand, ExpressionOperand } from '../expression/expression';
import { BitwiseOperationExpression, ScalarOperand, ExpressionOperand } from '../expression/expression';
import exp from 'constants';
describe("calc", () => {
@@ -18,9 +18,9 @@ describe("calc", () => {
var result = calc.calcExpression(new BitwiseOperationExpression(
"1|2&3",
[
new NumericOperand(1),
new ExpressionOperand("|2", new NumericOperand(2), "|"),
new ExpressionOperand("&3", new NumericOperand(3), "&"),
new ScalarOperand(1),
new ExpressionOperand("|2", new ScalarOperand(2), "|"),
new ExpressionOperand("&3", new ScalarOperand(3), "&"),
]
));

View File

@@ -1,8 +1,8 @@
import NumericOperand from "./NumericOperand";
import ScalarOperand from "./ScalarOperand";
import ExpressionOperand from './ExpressionOperand';
it('can apply ~ operand', () => {
var op = new NumericOperand(10, 'dec');
var op = new ScalarOperand(10, 'dec');
var expr = new ExpressionOperand("~10", op, "~");
var result = expr.evaluate();
expect(result.value).toBe(-11);
@@ -10,8 +10,8 @@ it('can apply ~ operand', () => {
});
it('can apply & operand', () => {
var op1 = new NumericOperand(3, 'dec');
var op2 = new NumericOperand(4, 'dec');
var op1 = new ScalarOperand(3, 'dec');
var op2 = new ScalarOperand(4, 'dec');
var expr = new ExpressionOperand("|3", op1, "|");
var result = expr.evaluate(op2);
expect(result.value).toBe(7);

View File

@@ -1,4 +1,4 @@
import NumericOperand from './NumericOperand';
import ScalarOperand from './ScalarOperand';
import { ExpressionInputItem } from './expression-interfaces';
export default class ExpressionOperand implements ExpressionInputItem {
@@ -18,7 +18,7 @@ export default class ExpressionOperand implements ExpressionInputItem {
this.isNotExpression = this.sign === '~';
}
evaluate(operand?: NumericOperand) : NumericOperand {
evaluate(operand?: ScalarOperand) : ScalarOperand {
if (operand instanceof ExpressionOperand) {
throw new Error('value shouldnt be expression');
}
@@ -35,10 +35,10 @@ export default class ExpressionOperand implements ExpressionInputItem {
str = operand.value + this.sign + evaluatedOperand.value;
}
return NumericOperand.create(eval(str), evaluatedOperand.base);
return ScalarOperand.create(eval(str), evaluatedOperand.base);
}
getUnderlyingOperand() : NumericOperand {
getUnderlyingOperand() : ScalarOperand {
return this.operand.getUnderlyingOperand();
}

View File

@@ -0,0 +1,68 @@
import { NumericLiteral } from "typescript";
import ExpressionOperand from "./ExpressionOperand";
import { ScalarOperand } from "./expression";
import { ExpressionInputItem } from "./expression-interfaces";
const NUMBER_REGEX = /./;
class ExpressionParser
{
input: string;
pos: number;
constructor(input: string) {
this.input = input;
this.pos = 0;
}
tryParse () : ExpressionInputItem[] | null {
return null;
}
parse() : ExpressionInputItem[] {
const s = this.input;
const p = this.pos;
const items : ExpressionInputItem[] = [];
while(p < s.length) {
}
return items;
}
parseSingle(): ExpressionInputItem {
const s = this.input;
const p = this.pos;
const ch = s[p];
if(ch == '~')
{
this.move(1);
const inner = this.parseSingle()
return new ExpressionOperand("~" + inner, inner, "~");
}
const ss = s.substring(p);
const scalar = ScalarOperand.tryParse(ss);
if(scalar == null)
throw new Error(`Expected scalar value at pos ${p}: ${ss}`);
return {} as ExpressionInputItem;
}
move(step : number) {
this.pos += step;
}
}
it("parses simple expression", () => {
const input = "1+1";
})

View File

@@ -1,7 +1,7 @@
import NumericOperand from "./NumericOperand";
import ScalarOperand from "./ScalarOperand";
import ListOfNumbersExpression from "./ListOfNumbersExpression";
it('calculates max bits length', () => {
var expr = new ListOfNumbersExpression("10 0xabef 0b01010", [NumericOperand.parse("10"), NumericOperand.parse("0xabef"), NumericOperand.parse("0b01010")])
var expr = new ListOfNumbersExpression("10 0xabef 0b01010", [ScalarOperand.parse("10"), ScalarOperand.parse("0xabef"), ScalarOperand.parse("0b01010")])
expect(expr.maxBitsLength).toBe(16);
});

View File

@@ -1,12 +1,12 @@
import NumericOperand from "./NumericOperand";
import ScalarOperand from "./ScalarOperand";
import { ExpressionInput, ExpressionInputItem } from "./expression-interfaces";
export default class ListOfNumbersExpression implements ExpressionInput {
numbers: NumericOperand[];
numbers: ScalarOperand[];
expressionString: string;
maxBitsLength: number;
constructor(expressionString: string, numbers: NumericOperand[]) {
constructor(expressionString: string, numbers: ScalarOperand[]) {
this.expressionString = expressionString;
this.numbers = numbers;
this.maxBitsLength = numbers.map(n => n.lengthInBits).reduce((n , c) => n >= c ? n : c, 0);

View File

@@ -1,26 +0,0 @@
import NumericOperand, { INT_MAX_VALUE } from './NumericOperand';
it('parsed from string', () => {
var op = NumericOperand.parse('123');
expect(op.base).toBe('dec');
expect(op.value).toBe(123);
});
it('can get other kind', () => {
var op = new NumericOperand(10, 'dec');
expect(op.getOtherBase('hex')).toBe('dec');
expect(op.getOtherBase('bin')).toBe('hex');
});
it('negtive value binary string', () => {
expect(NumericOperand.toBaseString(-1, 'bin')).toBe('11111111111111111111111111111111');
});
it('64 bit operand binary string', () => {
expect(NumericOperand.toBaseString(68719476735, 'bin')).toBe('111111111111111111111111111111111111');
});
it('throws on negative 64 bit numbers', () => {
var bigN = -(INT_MAX_VALUE+1);
expect(() => new NumericOperand(bigN)).toThrowError("BitwiseCmd currently doesn't support 64 bit negative numbers such as " + bigN);
})

View File

@@ -0,0 +1,26 @@
import ScalarOperand, { INT_MAX_VALUE } from './ScalarOperand';
it('parsed from string', () => {
var op = ScalarOperand.parse('123');
expect(op.base).toBe('dec');
expect(op.value).toBe(123);
});
it('can get other kind', () => {
var op = new ScalarOperand(10, 'dec');
expect(op.getOtherBase('hex')).toBe('dec');
expect(op.getOtherBase('bin')).toBe('hex');
});
it('negtive value binary string', () => {
expect(ScalarOperand.toBaseString(-1, 'bin')).toBe('11111111111111111111111111111111');
});
it('64 bit operand binary string', () => {
expect(ScalarOperand.toBaseString(68719476735, 'bin')).toBe('111111111111111111111111111111111111');
});
it('throws on negative 64 bit numbers', () => {
var bigN = -(INT_MAX_VALUE+1);
expect(() => new ScalarOperand(bigN)).toThrowError("BitwiseCmd currently doesn't support 64 bit negative numbers such as " + bigN);
})

View File

@@ -7,8 +7,8 @@ const INT_MAX_VALUE = 2147483647;
export {INT_MAX_VALUE};
// Represents numeric value
export default class NumericOperand implements ExpressionInputItem {
// Represents scalar numeric value
export default class ScalarOperand implements ExpressionInputItem {
id: number;
value: number;
base: NumberBase;
@@ -19,10 +19,10 @@ export default class NumericOperand implements ExpressionInputItem {
this.id = globalId++;
this.value = value;
this.base = base || "dec";
this.lengthInBits = NumericOperand.getBitLength(this.value);
this.lengthInBits = ScalarOperand.getBitLength(this.value);
this.isExpression = false;
if(value < 0 && !NumericOperand.is32Bit(value))
if(value < 0 && !ScalarOperand.is32Bit(value))
throw new Error("BitwiseCmd currently doesn't support 64 bit negative numbers such as " + value);
}
@@ -44,7 +44,7 @@ export default class NumericOperand implements ExpressionInputItem {
};
toString(base?: NumberBase) : string {
return NumericOperand.toBaseString(this.value, base || this.base);
return ScalarOperand.toBaseString(this.value, base || this.base);
}
toOtherKindString() : string {
@@ -65,14 +65,14 @@ export default class NumericOperand implements ExpressionInputItem {
setValue(value : number) {
this.value = value;
this.lengthInBits = NumericOperand.getBitLength(value);
this.lengthInBits = ScalarOperand.getBitLength(value);
}
evaluate() : NumericOperand {
evaluate() : ScalarOperand {
return this;
}
getUnderlyingOperand() : NumericOperand {
getUnderlyingOperand() : ScalarOperand {
return this
}
@@ -89,12 +89,12 @@ export default class NumericOperand implements ExpressionInputItem {
};
static create(value : number, base? : NumberBase) {
return new NumericOperand(value, base || "dec");
return new ScalarOperand(value, base || "dec");
};
static parse(input: string) : NumericOperand {
static parse(input: string) : ScalarOperand {
var parsed = NumericOperand.tryParse(input);
var parsed = ScalarOperand.tryParse(input);
if(parsed == null) {
throw new Error(input + " is not a valid number");
@@ -103,7 +103,7 @@ export default class NumericOperand implements ExpressionInputItem {
return parsed;
}
static tryParse(input: string) : NumericOperand | null {
static tryParse(input: string) : ScalarOperand | null {
var parsed = numberParser.parse(input);
@@ -111,7 +111,7 @@ export default class NumericOperand implements ExpressionInputItem {
return null;
}
return new NumericOperand(parsed.value, parsed.base);
return new ScalarOperand(parsed.value, parsed.base);
}
static toBaseString(value : number, base : NumberBase) : string {
@@ -122,7 +122,7 @@ export default class NumericOperand implements ExpressionInputItem {
case 'bin':
// >>> 0 is used to get an actual bit representation of the negative numbers
// https://stackoverflow.com/questions/5747123/strange-javascript-operator-expr-0
const is32Bit = NumericOperand.is32Bit(value);
const is32Bit = ScalarOperand.is32Bit(value);
return (is32Bit && value < 0 ? (value >>> 0) : value).toString(2);
case 'dec':
return value.toString(10);

View File

@@ -1,4 +1,4 @@
import { NumericOperand, ListOfNumbersExpression, BitwiseOperationExpression, ExpressionOperand } from '../expression';
import { ScalarOperand, ListOfNumbersExpression, BitwiseOperationExpression, ExpressionOperand } from '../expression';
import { ExpressionInputItem, ExpressionInput } from '../expression-interfaces';
type Config = {
@@ -41,11 +41,11 @@ export default class BitwiseExpressionViewModel {
i = 0, len = expr.expressionItems.length,
ex, m = new BitwiseExpressionViewModel(config);
var prev : NumericOperand | null = null;
var prev : ScalarOperand | null = null;
for (;i<len;i++) {
ex = expr.expressionItems[i];
if(ex instanceof NumericOperand) {
if(ex instanceof ScalarOperand) {
m.addOperandRow(ex);
prev = ex;
continue;
@@ -61,11 +61,11 @@ export default class BitwiseExpressionViewModel {
prev = notResult;
}
else if(eo.isShiftExpression){
prev = eo.evaluate(prev as NumericOperand);
prev = eo.evaluate(prev as ScalarOperand);
m.addShiftExpressionResultRow(eo, prev);
} else {
prev = eo.evaluate(prev as NumericOperand);
prev = eo.evaluate(prev as ScalarOperand);
m.addExpressionOperandRow(eo);
m.addExpressionResultRow(prev);
}
@@ -84,7 +84,7 @@ export default class BitwiseExpressionViewModel {
return m;
};
addOperandRow(operand: NumericOperand) {
addOperandRow(operand: ScalarOperand) {
this.maxNumberOfBits = Math.max(operand.getLengthInBits(), this.maxNumberOfBits);
this.items.push({
sign:'',
@@ -108,7 +108,7 @@ export default class BitwiseExpressionViewModel {
});
};
addShiftExpressionResultRow(expression : ExpressionOperand, resultOperand : NumericOperand) {
addShiftExpressionResultRow(expression : ExpressionOperand, resultOperand : ScalarOperand) {
this.maxNumberOfBits = Math.max(resultOperand.getLengthInBits(), this.maxNumberOfBits);
this.items.push({
sign: expression.sign + expression.operand.toString(),
@@ -119,7 +119,7 @@ export default class BitwiseExpressionViewModel {
});
};
addExpressionResultRow(operand : NumericOperand) {
addExpressionResultRow(operand : ScalarOperand) {
this.maxNumberOfBits = Math.max(operand.getLengthInBits(), this.maxNumberOfBits);
this.items.push({
sign:'=',
@@ -130,7 +130,7 @@ export default class BitwiseExpressionViewModel {
});
};
getLabel (op: NumericOperand) : string {
getLabel (op: ScalarOperand) : string {
if(op.base == 'bin') {
return op.toString("dec");

View File

@@ -3,7 +3,7 @@ import formatter from '../../core/formatter';
import BinaryStringView, { FlipBitEventArg } from '../../core/components/BinaryString';
import BitwiseExpressionViewModel from './BitwiseExpressionModel';
import { ExpressionInput, ExpressionInputItem } from '../expression-interfaces';
import { ExpressionOperand, NumericOperand } from '../expression';
import { ExpressionOperand, ScalarOperand } from '../expression';
type BitwiseOperationExpressionViewProps = {
expression: ExpressionInput;
@@ -114,7 +114,7 @@ class ExpressionRow extends React.Component<ExpressionRowProps> {
return this.props.expressionItem.evaluate().toOtherKindString();
}
getLabelString (op: NumericOperand) : string {
getLabelString (op: ScalarOperand) : string {
return op.toString(op.base == 'bin' ? 'dec' : op.base);
}

View File

@@ -1,4 +1,4 @@
import { NumericOperand } from "./expression";
import { ScalarOperand } from "./expression";
export interface ExpressionInput
{
@@ -8,8 +8,8 @@ export interface ExpressionInput
export interface ExpressionInputItem
{
isExpression: boolean;
getUnderlyingOperand: () => NumericOperand;
evaluate(operand? : NumericOperand): NumericOperand;
getUnderlyingOperand: () => ScalarOperand;
evaluate(operand? : ScalarOperand): ScalarOperand;
}
export type NumberBase = 'dec' | 'hex' | 'bin';

View File

@@ -1,5 +1,5 @@
import { OperationCanceledException } from "typescript";
import { parser, ListOfNumbersExpression, BitwiseOperationExpression, NumericOperand, ExpressionOperand } from "./expression";
import { parser, ListOfNumbersExpression, BitwiseOperationExpression, ScalarOperand, ExpressionOperand } from "./expression";
describe("expression parser", () => {
@@ -37,16 +37,16 @@ describe("expression parser", () => {
const first = result.expressionItems[0];
const second = result.expressionItems[1];
expect(first).toBeInstanceOf(NumericOperand);
expect(first).toBeInstanceOf(ScalarOperand);
expect((first as NumericOperand).value).toBe(1);
expect((first as ScalarOperand).value).toBe(1);
expect(second).toBeInstanceOf(ExpressionOperand);
var secondOp = second as ExpressionOperand;
expect(secondOp.sign).toBe("^");
expect(secondOp.operand).toBeInstanceOf(NumericOperand);
var childOp = secondOp.operand as NumericOperand;
expect(secondOp.operand).toBeInstanceOf(ScalarOperand);
var childOp = secondOp.operand as ScalarOperand;
expect(childOp.value).toBe(2);
});

View File

@@ -1,10 +1,10 @@
import NumericOperand from './NumericOperand';
import ScalarOperand from './ScalarOperand';
import ExpressionOperand from './ExpressionOperand'
import ListOfNumbersExpression from './ListOfNumbersExpression';
import BitwiseOperationExpression from './BitwiseOperationExpression';
import { ExpressionInput, ExpressionInputItem, NumberBase } from './expression-interfaces';
export { default as NumericOperand } from './NumericOperand';
export { default as ScalarOperand } from './ScalarOperand';
export { default as ExpressionOperand } from './ExpressionOperand';
export { default as ListOfNumbersExpression } from './ListOfNumbersExpression';
export { default as BitwiseOperationExpression } from './BitwiseOperationExpression';
@@ -46,12 +46,12 @@ class ExpressionParser {
return null;
};
parseOperand (input : string) : NumericOperand {
return NumericOperand.parse(input);
parseOperand (input : string) : ScalarOperand {
return ScalarOperand.parse(input);
};
createOperand (number : number, base : NumberBase) : NumericOperand {
return NumericOperand.create(number, base);
createOperand (number : number, base : NumberBase) : ScalarOperand {
return ScalarOperand.create(number, base);
};
addFactory (factory: IExpressionParserFactory) {
@@ -69,7 +69,7 @@ class ListOfNumbersExpressionFactory implements IExpressionParserFactory
return input.split(' ')
.filter(p => p.length > 0)
.map(p => NumericOperand.tryParse(p))
.map(p => ScalarOperand.tryParse(p))
.filter(n => n == null)
.length == 0;
};
@@ -78,7 +78,7 @@ class ListOfNumbersExpressionFactory implements IExpressionParserFactory
const numbers = input.split(' ')
.filter(p => p.length > 0)
.map(m => NumericOperand.parse(m));
.map(m => ScalarOperand.parse(m));
return new ListOfNumbersExpression(input, numbers);
}
@@ -116,16 +116,16 @@ class BitwiseOperationExpressionFactory implements IExpressionParserFactory {
var parsed = null;
if(num.indexOf('~') == 0) {
parsed = new ExpressionOperand(num, NumericOperand.parse(num.substring(1)), '~');
parsed = new ExpressionOperand(num, ScalarOperand.parse(num.substring(1)), '~');
}
else {
parsed = NumericOperand.parse(num);
parsed = ScalarOperand.parse(num);
}
if(sign == null) {
return parsed as ExpressionOperand;
} else {
return new ExpressionOperand(input, parsed as NumericOperand, sign);
return new ExpressionOperand(input, parsed as ScalarOperand, sign);
}
};