|
| 1 | +# BUG Language Examples |
| 2 | + |
| 3 | +This directory contains example programs demonstrating various BUG language |
| 4 | +features, organized by complexity. |
| 5 | + |
| 6 | +## Directory Structure |
| 7 | + |
| 8 | +``` |
| 9 | +examples/ |
| 10 | + basic/ # Simple single-concept examples |
| 11 | + intermediate/ # Multiple concepts combined |
| 12 | + advanced/ # Complex real-world patterns |
| 13 | + optimizations/ # Compiler optimization demos |
| 14 | + wip/ # Work-in-progress (not yet compiling) |
| 15 | +``` |
| 16 | + |
| 17 | +## Running Tests |
| 18 | + |
| 19 | +All examples are automatically tested by the test suite: |
| 20 | + |
| 21 | +```bash |
| 22 | +yarn test examples |
| 23 | +``` |
| 24 | + |
| 25 | +## Test Annotations |
| 26 | + |
| 27 | +Examples can include special comments to control test behavior: |
| 28 | + |
| 29 | +```bug |
| 30 | +// @wip - Skip test (work in progress) |
| 31 | +// @skip Reason - Skip test with reason |
| 32 | +// @expect-parse-error - Expected to fail parsing |
| 33 | +// @expect-typecheck-error - Expected to fail typechecking |
| 34 | +// @expect-ir-error - Expected to fail IR generation |
| 35 | +// @expect-bytecode-error - Expected to fail bytecode generation |
| 36 | +``` |
| 37 | + |
| 38 | +## Examples by Category |
| 39 | + |
| 40 | +### Basic |
| 41 | + |
| 42 | +Simple examples demonstrating single language features: |
| 43 | + |
| 44 | +- `minimal.bug` - Simplest possible BUG contract |
| 45 | +- `conditionals.bug` - If/else statements |
| 46 | +- `functions.bug` - Function definitions and calls |
| 47 | +- `variables.bug` - Variable types and declarations |
| 48 | +- `array-length.bug` - Array length property |
| 49 | +- `constructor-init.bug` - Constructor initialization |
| 50 | + |
| 51 | +### Intermediate |
| 52 | + |
| 53 | +Examples combining multiple language features: |
| 54 | + |
| 55 | +- `arrays.bug` - Array operations with loops |
| 56 | +- `mappings.bug` - Mapping access patterns |
| 57 | +- `scopes.bug` - Variable scoping and shadowing |
| 58 | +- `slices.bug` - Byte slice operations |
| 59 | +- `calldata.bug` - Calldata access via msg.data |
| 60 | +- `owner-counter.bug` - Owner checks with counters |
| 61 | +- `storage-arrays.bug` - Dynamic arrays in storage |
| 62 | +- `memory-arrays.bug` - Memory array allocation |
| 63 | +- `internal-functions.bug` - Internal function calls |
| 64 | + |
| 65 | +### Advanced |
| 66 | + |
| 67 | +Complex examples demonstrating real-world patterns: |
| 68 | + |
| 69 | +- `nested-mappings.bug` - Mapping of mappings |
| 70 | +- `nested-arrays.bug` - Multi-dimensional arrays |
| 71 | +- `nested-structs.bug` - Nested struct storage |
| 72 | +- `voting-system.bug` - Realistic voting contract |
| 73 | +- `token-registry.bug` - Token with function selectors |
| 74 | + |
| 75 | +### Optimizations |
| 76 | + |
| 77 | +Examples showcasing compiler optimizations: |
| 78 | + |
| 79 | +- `cse.bug` - Common subexpression elimination |
| 80 | +- `cse-simple.bug` - Simple CSE example |
| 81 | +- `constant-folding.bug` - Compile-time constant evaluation |
| 82 | + |
| 83 | +### Work in Progress |
| 84 | + |
| 85 | +Features not yet fully implemented: |
| 86 | + |
| 87 | +- `transient-storage.bug` - TSTORE/TLOAD opcodes (no syntax) |
| 88 | +- `returndata.bug` - Return data access |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Storage Access Patterns |
| 93 | + |
| 94 | +The BUG language has specific rules about how storage variables can be |
| 95 | +accessed and modified. |
| 96 | + |
| 97 | +### Key Concept: Storage References vs Local Copies |
| 98 | + |
| 99 | +In BUG, when you read a complex type (struct, array, or mapping) from storage |
| 100 | +into a local variable, you get a **copy** of the data, not a reference. This |
| 101 | +means: |
| 102 | + |
| 103 | +- ✅ **Reading** from local copies works fine |
| 104 | +- ❌ **Writing** to local copies does NOT update storage |
| 105 | + |
| 106 | +### Correct Patterns |
| 107 | + |
| 108 | +```bug |
| 109 | +// Direct storage access - changes are persisted |
| 110 | +accounts[user].balance = 1000; |
| 111 | +votes[proposalId][0].amount = 100; |
| 112 | +allowances[owner][spender] = 500; |
| 113 | +
|
| 114 | +// Reading into locals is fine |
| 115 | +let currentBalance = accounts[user].balance; |
| 116 | +let voteCount = votes[proposalId][0].amount; |
| 117 | +``` |
| 118 | + |
| 119 | +### Incorrect Patterns |
| 120 | + |
| 121 | +```bug |
| 122 | +// ❌ WRONG: Changes to local copies don't persist |
| 123 | +let userAccount = accounts[user]; |
| 124 | +userAccount.balance = 1000; // This doesn't update storage! |
| 125 | +
|
| 126 | +// ❌ WRONG: Same issue with array elements |
| 127 | +let firstVote = votes[proposalId][0]; |
| 128 | +firstVote.amount = 200; // This doesn't update storage! |
| 129 | +``` |
| 130 | + |
| 131 | +### Workaround |
| 132 | + |
| 133 | +If you need to perform multiple operations on a storage struct, access each |
| 134 | +field directly: |
| 135 | + |
| 136 | +```bug |
| 137 | +// Instead of: |
| 138 | +let account = accounts[user]; |
| 139 | +account.balance = account.balance + 100; |
| 140 | +account.isActive = true; |
| 141 | +
|
| 142 | +// Do this: |
| 143 | +accounts[user].balance = accounts[user].balance + 100; |
| 144 | +accounts[user].isActive = true; |
| 145 | +``` |
0 commit comments