Parser

Converts a token stream from a scanner into EVM bytecode.

Members

Functions

parse
ubyte[] parse(ref Scanner scanner)

Parses the given token stream into a sequence of bytes representing an EVM bytecode program.

Examples

1 // Simple single opcode
2 auto scanner = new Scanner(cast(ubyte[])"ADD");
3 auto parser = new Parser();
4 auto bytecode = parser.parse(scanner);
5 assert(bytecode == [0x01]);
6 
7 // Multiple opcodes
8 scanner = new Scanner(cast(ubyte[])"PUSH1 0xfa\nPUSH1 0xab");
9 bytecode = parser.parse(scanner);
10 assert(bytecode == [Opcode.PUSH1, 0xfa, Opcode.PUSH1, 0xab]);
11 
12 // Mixed stack and push opcodes
13 scanner = new Scanner(cast(ubyte[])"PUSH1 0xfa\nDUP1\nMUL\nPUSH1 0x60\n");
14 bytecode = parser.parse(scanner);
15 assert(bytecode == [Opcode.PUSH1, 0xfa,
16                     Opcode.DUP1,
17                     Opcode.MUL,
18                     Opcode.PUSH1, 0x60]);
19 
20 // Properly ignores all sorts of whitespace
21 scanner = new Scanner(cast(ubyte[])"  \r\nPUSH1\t0xfa\r\nDUP1\n\r\t    ");
22 bytecode = parser.parse(scanner);
23 assert(bytecode == [Opcode.PUSH1, 0xfa, Opcode.DUP1]);

Meta