-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to assemble constants (#57)
* Create compiler folder * Add option to assemble constants when compiling * Add py-algorand-sdk as a dependency * Update docs and changelog * Fix typo * Document constants.py
- Loading branch information
1 parent
815892d
commit cfb4506
Showing
16 changed files
with
935 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
|
||
[mypy-pytest.*] | ||
ignore_missing_imports = True | ||
|
||
[mypy-algosdk.*] | ||
ignore_missing_imports = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .compiler import MAX_TEAL_VERSION, MIN_TEAL_VERSION, DEFAULT_TEAL_VERSION, CompileOptions, compileTeal | ||
|
||
__all__ = [ | ||
"MAX_TEAL_VERSION", | ||
"MIN_TEAL_VERSION", | ||
"DEFAULT_TEAL_VERSION", | ||
"CompileOptions", | ||
"compileTeal", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import pytest | ||
|
||
from .. import * | ||
|
||
def test_compile_single(): | ||
expr = Int(1) | ||
|
||
expected = """ | ||
#pragma version 2 | ||
int 1 | ||
""".strip() | ||
actual_application = compileTeal(expr, Mode.Application) | ||
actual_signature = compileTeal(expr, Mode.Signature) | ||
|
||
assert actual_application == actual_signature | ||
assert actual_application == expected | ||
|
||
def test_compile_sequence(): | ||
expr = Seq([Pop(Int(1)), Pop(Int(2)), Int(3) + Int(4)]) | ||
|
||
expected = """ | ||
#pragma version 2 | ||
int 1 | ||
pop | ||
int 2 | ||
pop | ||
int 3 | ||
int 4 | ||
+ | ||
""".strip() | ||
actual_application = compileTeal(expr, Mode.Application) | ||
actual_signature = compileTeal(expr, Mode.Signature) | ||
|
||
assert actual_application == actual_signature | ||
assert actual_application == expected | ||
|
||
def test_compile_branch(): | ||
expr = If(Int(1), Bytes("true"), Bytes("false")) | ||
|
||
expected = """ | ||
#pragma version 2 | ||
int 1 | ||
bnz l2 | ||
byte "false" | ||
b l3 | ||
l2: | ||
byte "true" | ||
l3: | ||
""".strip() | ||
actual_application = compileTeal(expr, Mode.Application) | ||
actual_signature = compileTeal(expr, Mode.Signature) | ||
|
||
assert actual_application == actual_signature | ||
assert actual_application == expected | ||
|
||
def test_compile_mode(): | ||
expr = App.globalGet(Bytes("key")) | ||
|
||
expected = """ | ||
#pragma version 2 | ||
byte "key" | ||
app_global_get | ||
""".strip() | ||
actual_application = compileTeal(expr, Mode.Application) | ||
|
||
assert actual_application == expected | ||
|
||
with pytest.raises(TealInputError): | ||
compileTeal(expr, Mode.Signature) | ||
|
||
def test_compile_version_invalid(): | ||
expr = Int(1) | ||
|
||
with pytest.raises(TealInputError): | ||
compileTeal(expr, Mode.Signature, version=1) # too small | ||
|
||
with pytest.raises(TealInputError): | ||
compileTeal(expr, Mode.Signature, version=4) # too large | ||
|
||
with pytest.raises(TealInputError): | ||
compileTeal(expr, Mode.Signature, version=2.0) # decimal | ||
|
||
def test_compile_version_2(): | ||
expr = Int(1) | ||
|
||
expected = """ | ||
#pragma version 2 | ||
int 1 | ||
""".strip() | ||
actual = compileTeal(expr, Mode.Signature, version=2) | ||
assert actual == expected | ||
|
||
def test_compile_version_default(): | ||
expr = Int(1) | ||
|
||
actual_default = compileTeal(expr, Mode.Signature) | ||
actual_version_2 = compileTeal(expr, Mode.Signature, version=2) | ||
assert actual_default == actual_version_2 | ||
|
||
def test_compile_version_3(): | ||
expr = Int(1) | ||
|
||
expected = """ | ||
#pragma version 3 | ||
int 1 | ||
""".strip() | ||
actual = compileTeal(expr, Mode.Signature, version=3) | ||
assert actual == expected | ||
|
||
def test_slot_load_before_store(): | ||
|
||
program = AssetHolding.balance(Int(0), Int(0)).value() | ||
with pytest.raises(TealInternalError): | ||
compileTeal(program, Mode.Application, version=2) | ||
|
||
program = AssetHolding.balance(Int(0), Int(0)).hasValue() | ||
with pytest.raises(TealInternalError): | ||
compileTeal(program, Mode.Application, version=2) | ||
|
||
program = App.globalGetEx(Int(0), Bytes("key")).value() | ||
with pytest.raises(TealInternalError): | ||
compileTeal(program, Mode.Application, version=2) | ||
|
||
program = App.globalGetEx(Int(0), Bytes("key")).hasValue() | ||
with pytest.raises(TealInternalError): | ||
compileTeal(program, Mode.Application, version=2) | ||
|
||
program = ScratchVar().load() | ||
with pytest.raises(TealInternalError): | ||
compileTeal(program, Mode.Application, version=2) | ||
|
||
def test_assembleConstants(): | ||
program = Itob(Int(1) + Int(1) + Int(2)) == Concat(Bytes("test"), Bytes("test"), Bytes("test2")) | ||
|
||
expectedNoAssemble = """ | ||
#pragma version 3 | ||
int 1 | ||
int 1 | ||
+ | ||
int 2 | ||
+ | ||
itob | ||
byte "test" | ||
byte "test" | ||
concat | ||
byte "test2" | ||
concat | ||
== | ||
""".strip() | ||
actualNoAssemble = compileTeal(program, Mode.Application, version=3, assembleConstants=False) | ||
assert expectedNoAssemble == actualNoAssemble | ||
|
||
expectedAssemble = """ | ||
#pragma version 3 | ||
intcblock 1 | ||
bytecblock 0x74657374 | ||
intc_0 // 1 | ||
intc_0 // 1 | ||
+ | ||
pushint 2 // 2 | ||
+ | ||
itob | ||
bytec_0 // "test" | ||
bytec_0 // "test" | ||
concat | ||
pushbytes 0x7465737432 // "test2" | ||
concat | ||
== | ||
""".strip() | ||
actualAssemble = compileTeal(program, Mode.Application, version=3, assembleConstants=True) | ||
assert expectedAssemble == actualAssemble | ||
|
||
with pytest.raises(TealInternalError): | ||
compileTeal(program, Mode.Application, version=2, assembleConstants=True) |
Oops, something went wrong.