Python compiler for DNA
- Free software: LGPL license
Neptune
is a tool for compiling Python files to the .avm
format for usage in smart contracts on the DNA blockchain. It currently only supports a subset of the Python language.
- Compiles a subset of the Python language to the
.avm
format for use in the DNA blockchain - Works for Python 3.6+
- Compile a larger subset of the Python language
- Additional syntax checks
- Optimize instr stream
- Basic abstract syntax trees
- Ability to write numerous expressions per line. (See Example)
global
keyword, works the same as global in the standard python library. Allows functions to share variables. (See Example #1) (See Example #2)- Cascade bool operation. (See Example)
- Augmented assignment operators such as
+= *= /= -=
(See Example) - Chained comparisons (See Example)
- Chained assignment
- Maps nested within maps. (See Example)
if expression
statements. (See Example)for
,while
,break
andcontinue
. (See Example #1) (See Example #2)if x in y
statements. (See Example)loop else
statements. (See Example)range(len), range(start, stop), range(start, stop, step)
. (See Example)- Function default argument (See Example)
- Function variable argument (See Example)
- Pass Starred argument for function call (See Example)
- List comprehension. (See Example)
- Dict comprehension. (See Example)
- Conditional operation
- Assert. (See Example)
- String slicing (See Example)
- Added libdna.py to support various list functions such as list removal and element in
- Additional syntax checks
- More accurate debug messages
Installation requires Python version 3.6 or later.
Clone the repository and navigate into the project directory. Make a Python 3 virtual environment and activate it via:
python3 -m venv venv
source venv/bin/activate
Then, install the requirements:
pip install -r requirements.txt
Import the compiler into a file with:
from dna.compiler import Compiler
The compiler can be used in three different ways:
- Compiling a python file to a new file
# Compiles the python file and creates an avm in 'path/to/your/file.avm'.
compiler = Compiler.Compile('path/to/your/file.py')
- Compiling a python file to
.avm
# Compiles the python file and returns an avm.
avm = Compiler.Compile_File('path/to/your/file.py')
- Compiling a variable to
.avm
# Compiles a contract stored in memory and returns an avm.
avm = Compiler.Compile_Contract('def Main(operation, args): ...')
You can also print out the instr instream:
compiler.DumpAsm()
You can run the tests using the runall.bash
or runall-testing.bash
files located in dna_test
. You can run the compiler tests using the compile-avm-test.py
file in the root directory.
FuncName: indicate the opcode blongs to which function. Global#Code is for Code in Global.
Lineno: indicate the opcode blongs to which line number in source code.
Col : indicate the opcode blongs to which columns in source code.
offset: the address of Opcode. from 0 to len of avm.
Opcode: the Opcode.
JumpTarget: the target address(offset) of jump instruct.
TargetOff: the relative offset between target address and current jump instruction.
FuncName Lineno Col Offset OpCode JumpTarget TargetOff
Global#Code 1 0 0 PUSH2
Global#Code 1 0 1 NEWARRAY
Global#Code 1 0 2 TOALTSTACK
Global#Code 1 14 3 PUSHBYTES5
Global#Code 1 0 9 DUPFROMALTSTACK
Global#Code 1 0 10 PUSH0
Global#Code 1 0 11 PUSH2
Global#Code 1 0 12 ROLL
Global#Code 1 0 13 SETITEM
Global#Code 3 4 14 PUSH2
Global#Code 3 0 15 DUPFROMALTSTACK
Global#Code 3 0 16 PUSH1
Global#Code 3 0 17 PUSH2
Global#Code 3 0 18 ROLL
Global#Code 3 0 19 SETITEM
Global#Code 4 0 20 FROMALTSTACK
Main 4 0 21 PUSH3
Main 4 0 22 NEWARRAY
Main 4 0 23 TOALTSTACK
Main 4 0 24 DUPFROMALTSTACK
Main 4 0 25 PUSH0
Main 4 0 26 PUSH2
Main 4 0 27 ROLL
Main 4 0 28 SETITEM
Main 5 8 29 PUSH0
Main 5 4 30 DUPFROMALTSTACK
Main 5 4 31 PUSH1
Main 5 4 32 PUSH2
Main 5 4 33 ROLL
Main 5 4 34 SETITEM
Main 6 8 35 DUPFROMALTSTACK
Main 6 8 36 PUSH0
Main 6 8 37 PICKITEM
Main 6 8 38 CALL 49 11