This commit is contained in:
BorysLevytskyi
2023-05-05 14:47:00 +02:00
parent f274aadace
commit 883dbe437c
4 changed files with 40 additions and 14 deletions

View File

@@ -28,6 +28,7 @@
"start": "react-scripts start",
"build": "react-scripts build & npx http-server ./build -p 3030",
"release": "react-scripts build & gh-pages -d build --message Release",
"deploy": "gh-pages -d build --message Release",
"test": "react-scripts test",
"eject": "react-scripts eject",
"serve-build": "npx http-server ./build -p 3030"

View File

@@ -94,10 +94,21 @@ export default class NumericOperand implements ExpressionInputItem {
static parse(input: string) : NumericOperand {
var parsed = NumericOperand.tryParse(input);
if(parsed == null) {
throw new Error(input + " is not a valid number");
}
return parsed;
}
static tryParse(input: string) : NumericOperand | null {
var parsed = numberParser.parse(input);
if(!parsed) {
throw new Error(input + " is not a valid number");
return null;
}
return new NumericOperand(parsed.value, parsed.base);

View File

@@ -8,12 +8,28 @@ describe("expression parser", () => {
expect(result).toBeInstanceOf(ListOfNumbersExpression);
});
it("doesn't list of numbers in case of bad numbers", () => {
expect(parser.parse("1 2 z")).toBeNull();
//expect(parser.parse("-")).toBeNull();
expect(parser.parse("")).toBeNull();
});
it("pares different operations expressions", () => {
expect(parser.parse("~1")).toBeInstanceOf(BitwiseOperationExpression);
expect(parser.parse("1^2")).toBeInstanceOf(BitwiseOperationExpression);
expect(parser.parse("1|2")).toBeInstanceOf(BitwiseOperationExpression);
});
it("parses big binary bitwise expression", () => {
const input = "0b00010010001101000101011001111000 | 0b10101010101010101010101000000000";
const actual = parser.parse(input);
expect(actual).toBeInstanceOf(BitwiseOperationExpression);
const expr = actual as BitwiseOperationExpression;
expect(expr.expressionItems[0].getUnderlyingOperand().value).toBe(305419896);
expect(expr.expressionItems[1].getUnderlyingOperand().value).toBe(2863311360);
})
it("pares multiple operand expression", () => {
const result = parser.parse("1^2") as BitwiseOperationExpression;
expect(result.expressionItems.length).toBe(2);

View File

@@ -61,26 +61,24 @@ class ExpressionParser {
class ListOfNumbersExpressionFactory implements IExpressionParserFactory
{
regex: RegExp;
constructor() {
this.regex = /^(-?(?:\d+|0x[\d,a-f]+|0b[0-1])\s?)+$/;
}
canCreate (input: string): boolean {
return this.regex.test(input);
if(input.length == 0) return false;
return input.split(' ')
.filter(p => p.length > 0)
.map(p => NumericOperand.tryParse(p))
.filter(n => n == null)
.length == 0;
};
create (input : string) : ExpressionInput {
var matches = this.regex.exec(input) as RegExpExecArray;
var numbers = [] as NumericOperand[];
var input = matches.input;
input.split(' ').forEach((n: string) => {
if(n.trim().length > 0) {
numbers.push(NumericOperand.parse(n.trim()));
}
});
const numbers = input.split(' ')
.filter(p => p.length > 0)
.map(m => NumericOperand.parse(m));
return new ListOfNumbersExpression(input, numbers);
}