Skip to content

Commit

Permalink
fix: biguint construction from uint64 and conversion to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Oct 10, 2024
1 parent e0a6fae commit 1312e9d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 49 deletions.
15 changes: 9 additions & 6 deletions src/awst_build/eb/biguint-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { nodeFactory } from '../../awst/node-factory'
import type { Expression } from '../../awst/nodes'
import { BigUIntBinaryOperator, BigUIntPostfixUnaryOperator, IntegerConstant, NumericComparison } from '../../awst/nodes'
import type { SourceLocation } from '../../awst/source-location'
import { bytesWType } from '../../awst/wtypes'
import { NotSupported } from '../../errors'
import { logger } from '../../logger'
import { tryConvertEnum } from '../../util'
Expand Down Expand Up @@ -65,13 +66,15 @@ export class BigUintFunctionBuilder extends FunctionBuilder {
if (expr instanceof IntegerConstant) {
biguint = nodeFactory.bigUIntConstant({
...expr,
sourceLocation,
})
} else {
biguint = nodeFactory.reinterpretCast({
expr: initialValue.toBytes(sourceLocation),
sourceLocation,
wtype: biguintPType.wtype,
})
}
biguint = nodeFactory.reinterpretCast({
expr: initialValue.toBytes(sourceLocation),
sourceLocation,
wtype: biguintPType.wtype,
})
} else {
return initialValue
}
Expand Down Expand Up @@ -204,6 +207,6 @@ export class BigUintExpressionBuilder extends InstanceExpressionBuilder<Instance
}

toBytes(sourceLocation: SourceLocation): awst.Expression {
return intrinsicFactory.itob({ value: this.resolve(), sourceLocation })
return nodeFactory.reinterpretCast({ expr: this.resolve(), sourceLocation, wtype: bytesWType })
}
}
16 changes: 8 additions & 8 deletions tests/approvals/out/boolean-conversions.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@
"file": "tests/approvals/boolean-conversions.algo.ts",
"line": 9,
"end_line": 9,
"column": 18,
"end_column": 19
"column": 10,
"end_column": 20
},
"wtype": {
"_type": "WType",
Expand Down Expand Up @@ -512,8 +512,8 @@
"file": "tests/approvals/boolean-conversions.algo.ts",
"line": 10,
"end_line": 10,
"column": 17,
"end_column": 18
"column": 9,
"end_column": 19
},
"wtype": {
"_type": "WType",
Expand Down Expand Up @@ -1509,8 +1509,8 @@
"file": "tests/approvals/boolean-conversions.algo.ts",
"line": 24,
"end_line": 24,
"column": 25,
"end_column": 26
"column": 17,
"end_column": 27
},
"wtype": {
"_type": "WType",
Expand Down Expand Up @@ -1602,8 +1602,8 @@
"file": "tests/approvals/boolean-conversions.algo.ts",
"line": 24,
"end_line": 24,
"column": 49,
"end_column": 50
"column": 41,
"end_column": 51
},
"wtype": {
"_type": "WType",
Expand Down
4 changes: 2 additions & 2 deletions tests/approvals/out/byte-expressions.awst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ subroutine test(a: uint64, b: biguint, c: string): void
assert(len(0x) == 0)
assert(itob(a) == itob(a))
assert(0x01 == 0x0000000000000001)
assert(0x0100 == 0x0100)
assert(reinterpret_cast<biguint>(itob(b)) == b)
assert(reinterpret_cast<bytes>(256) == 0x0100)
assert(reinterpret_cast<biguint>(reinterpret_cast<bytes>(b)) == b)
assert(reinterpret_cast<string>(reinterpret_cast<bytes>(c)) == c)
assert("123" == "123")
assert(0x01020304 == 0x01020304)
Expand Down
66 changes: 39 additions & 27 deletions tests/approvals/out/byte-expressions.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@
"scalar_type": 2
},
"lhs": {
"_type": "BytesConstant",
"_type": "ReinterpretCast",
"source_location": {
"file": "tests/approvals/byte-expressions.algo.ts",
"line": 8,
Expand All @@ -469,8 +469,25 @@
"ephemeral": false,
"scalar_type": 1
},
"value": "0RR",
"encoding": "base16"
"expr": {
"_type": "IntegerConstant",
"source_location": {
"file": "tests/approvals/byte-expressions.algo.ts",
"line": 8,
"end_line": 8,
"column": 15,
"end_column": 27
},
"wtype": {
"_type": "WType",
"name": "biguint",
"immutable": true,
"ephemeral": false,
"scalar_type": 1
},
"value": "256",
"teal_alias": null
}
},
"operator": "==",
"rhs": {
Expand Down Expand Up @@ -558,7 +575,7 @@
"scalar_type": 1
},
"expr": {
"_type": "IntrinsicCall",
"_type": "ReinterpretCast",
"source_location": {
"file": "tests/approvals/byte-expressions.algo.ts",
"line": 9,
Expand All @@ -573,29 +590,24 @@
"ephemeral": false,
"scalar_type": 1
},
"op_code": "itob",
"immediates": [],
"stack_args": [
{
"_type": "VarExpression",
"source_location": {
"file": "tests/approvals/byte-expressions.algo.ts",
"line": 9,
"end_line": 9,
"column": 23,
"end_column": 24
},
"wtype": {
"_type": "WType",
"name": "biguint",
"immutable": true,
"ephemeral": false,
"scalar_type": 1
},
"name": "b"
}
],
"comment": null
"expr": {
"_type": "VarExpression",
"source_location": {
"file": "tests/approvals/byte-expressions.algo.ts",
"line": 9,
"end_line": 9,
"column": 23,
"end_column": 24
},
"wtype": {
"_type": "WType",
"name": "biguint",
"immutable": true,
"ephemeral": false,
"scalar_type": 1
},
"name": "b"
}
}
},
"operator": "==",
Expand Down
2 changes: 1 addition & 1 deletion tests/approvals/out/itxn.awst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract ItxnDemoContract extends @algorandfoundation/algorand-typescript/base-c
assert(asset2_txn.ConfigAssetName == "AST2")
assert(checked_maybe(asset_params_get<AssetName>(asset1_txn.CreatedAssetID), comment=asset exists) == "AST1")
assert(checked_maybe(asset_params_get<AssetName>(asset2_txn.CreatedAssetID), comment=asset exists) == "AST2")
var appCreateParams: inner_transaction_fields_appl = create_inner_transaction(Fee=0, ApprovalProgram=0x000800, ClearStateProgram=0x000800)
var appCreateParams: inner_transaction_fields_appl = create_inner_transaction(Fee=0, ApprovalProgram=0x098101, ClearStateProgram=0x098101)
update_inner_transaction(assetParams, ConfigAssetName="AST3")
var <tuple>[appCreateTxn, asset3_txn]: tuple[inner_transaction_appl, inner_transaction_acfg] = submit_txn(appCreateParams, assetParams)
assert(Boolean(appCreateTxn.ApplicationID))
Expand Down
4 changes: 2 additions & 2 deletions tests/approvals/out/itxn.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,7 @@
"ephemeral": false,
"scalar_type": 1
},
"value": "00;m",
"value": "34s9",
"encoding": "base16"
},
"ClearStateProgram": {
Expand All @@ -1917,7 +1917,7 @@
"ephemeral": false,
"scalar_type": 1
},
"value": "00;m",
"value": "34s9",
"encoding": "base16"
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/approvals/out/switch-statements.awst
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ subroutine test_bytes(x: bytes): bytes
case "hmmm":
#switch₁ᶜ₀:
goto #switch₁ᶜ₁
case 0x0f:
case 0xff:
#switch₁ᶜ₁:
goto #switch₁ᶜ₂
case b64<ZHNmc2Rmc2Q=>:
#switch₁ᶜ₂:
goto #switch₁ᶜ₃
case b32<OASGMZ3OMATGOZDGMASGM>:
case b32<ONSGMZ3OMJTGOZDGMRSGM>:
#switch₁ᶜ₃:
return x
}
Expand Down
2 changes: 1 addition & 1 deletion tests/approvals/out/switch-statements.awst.json
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@
"ephemeral": false,
"scalar_type": 1
},
"value": "4*",
"value": "{{",
"encoding": "base16"
},
{
Expand Down

0 comments on commit 1312e9d

Please sign in to comment.