Skip to content

Commit

Permalink
refactor: use the same expression bulider for all bitwise op
Browse files Browse the repository at this point in the history
  • Loading branch information
boblat committed Nov 6, 2024
1 parent 796995f commit 898cd45
Showing 1 changed file with 9 additions and 44 deletions.
53 changes: 9 additions & 44 deletions src/awst_build/eb/bytes-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ export class BytesExpressionBuilder extends InstanceExpressionBuilder<InstanceTy
case 'bitwiseInvert':
return new BytesInvertBuilder(this._expr)
case 'bitwiseAnd':
return new BitwiseAndExpressionBuilder(this._expr)
return new BitwiseOpExpressionBuilder(this._expr, BytesBinaryOperator.bitAnd)
case 'bitwiseOr':
return new BitwiseOrExpressionBuilder(this._expr)
return new BitwiseOpExpressionBuilder(this._expr, BytesBinaryOperator.bitOr)
case 'bitwiseXor':
return new BitwiseXorExpressionBuilder(this._expr)
return new BitwiseOpExpressionBuilder(this._expr, BytesBinaryOperator.bitXor)
case 'toString':
return new ToStringBuilder(this._expr)
case 'concat':
Expand Down Expand Up @@ -279,46 +279,11 @@ export class BytesInvertBuilder extends ParameterlessFunctionBuilder {
}
}

export class BitwiseAndExpressionBuilder extends FunctionBuilder {
constructor(private expr: awst.Expression) {
super(expr.sourceLocation)
}

call(args: ReadonlyArray<NodeBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder {
const [other] = requireExpressionsOfType(args, [bytesPType], sourceLocation)
return new BytesExpressionBuilder(
nodeFactory.bytesBinaryOperation({
wtype: wtypes.bytesWType,
left: this.expr,
right: other,
op: BytesBinaryOperator.bitAnd,
sourceLocation,
}),
)
}
}

export class BitwiseOrExpressionBuilder extends FunctionBuilder {
constructor(private expr: awst.Expression) {
super(expr.sourceLocation)
}

call(args: ReadonlyArray<NodeBuilder>, typeArgs: ReadonlyArray<PType>, sourceLocation: SourceLocation): NodeBuilder {
const [other] = requireExpressionsOfType(args, [bytesPType], sourceLocation)
return new BytesExpressionBuilder(
nodeFactory.bytesBinaryOperation({
wtype: wtypes.bytesWType,
left: this.expr,
right: other,
op: BytesBinaryOperator.bitOr,
sourceLocation,
}),
)
}
}

export class BitwiseXorExpressionBuilder extends FunctionBuilder {
constructor(private expr: awst.Expression) {
export class BitwiseOpExpressionBuilder extends FunctionBuilder {
constructor(
private expr: awst.Expression,
private op: BytesBinaryOperator,
) {
super(expr.sourceLocation)
}

Expand All @@ -329,7 +294,7 @@ export class BitwiseXorExpressionBuilder extends FunctionBuilder {
wtype: wtypes.bytesWType,
left: this.expr,
right: other,
op: BytesBinaryOperator.bitXor,
op: this.op,
sourceLocation,
}),
)
Expand Down

0 comments on commit 898cd45

Please sign in to comment.