Skip to content

Commit 05587ea

Browse files
authored
Merge pull request #7 from oralang/complexity_analysis
Complexity analysis
2 parents ec75f8d + 6736378 commit 05587ea

File tree

11 files changed

+1814
-9
lines changed

11 files changed

+1814
-9
lines changed

README.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,55 @@ See [website/docs/specifications/mlir.md](website/docs/specifications/mlir.md) f
231231
## CLI Usage
232232

233233
```bash
234-
# Lex (tokenize) a file
235-
./zig-out/bin/ora lex contract.ora
234+
# Compile to EVM bytecode (default)
235+
./zig-out/bin/ora contract.ora
236236

237-
# Parse and show AST
238-
./zig-out/bin/ora parse contract.ora
237+
# Compilation stages
238+
./zig-out/bin/ora --emit-tokens contract.ora # Stop after lexing
239+
./zig-out/bin/ora --emit-ast contract.ora # Stop after parsing
240+
./zig-out/bin/ora --emit-mlir contract.ora # Stop after MLIR generation
241+
./zig-out/bin/ora --emit-yul contract.ora # Stop after Yul lowering
239242

240-
# Generate AST JSON
241-
./zig-out/bin/ora -o build ast contract.ora
243+
# Optimization levels
244+
./zig-out/bin/ora -O0 contract.ora # No optimization
245+
./zig-out/bin/ora -O1 contract.ora # Basic optimizations
246+
./zig-out/bin/ora -O2 contract.ora # Aggressive optimizations
242247

243-
# Compile (when Yul backend is complete)
244-
./zig-out/bin/ora compile contract.ora
248+
# Code analysis
249+
./zig-out/bin/ora --analyze-complexity contract.ora # Analyze function complexity
250+
251+
# Output control
252+
./zig-out/bin/ora -o build/ contract.ora # Output to directory
253+
./zig-out/bin/ora --save-all contract.ora # Save all intermediate stages
254+
255+
# Examples
256+
./zig-out/bin/ora ora-example/smoke.ora # Compile example
257+
./zig-out/bin/ora --analyze-complexity ora-example/complexity_example.ora # Analyze complexity
258+
259+
# Showcase: Analyze a realistic DeFi contract (400+ lines)
260+
./zig-out/bin/ora --analyze-complexity ora-example/defi_lending_pool.ora
261+
```
262+
263+
Run `./zig-out/bin/ora --help` for complete options.
264+
265+
### 🎯 Complexity Analysis Showcase
266+
267+
Want to see the power of Ora's analysis tools? Try analyzing our realistic DeFi lending pool contract:
268+
269+
```bash
270+
./zig-out/bin/ora --analyze-complexity ora-example/defi_lending_pool.ora
245271
```
246272

247-
See [website/docs/specifications/api.md](website/docs/specifications/api.md) for complete CLI reference.
273+
This 400+ line contract demonstrates:
274+
- **15 functions** with varying complexity levels
275+
- **73% simple functions** - optimal for performance
276+
- **26% moderate functions** - well-structured business logic
277+
- Real-world DeFi patterns: lending, borrowing, liquidations, interest calculations
278+
279+
The analysis helps you:
280+
- ✓ Identify functions suitable for `inline` optimization
281+
- ○ Ensure moderate complexity stays maintainable
282+
- ✗ Catch overly complex functions before they become technical debt
248283

249284
## Roadmap to ASUKA Release
250285

ora-example/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ The following examples use syntax not yet fully implemented:
4848
- `storage/storage_test_3.ora` - Complex storage operations
4949
- `max_features.ora` - Comprehensive feature showcase (various advanced features)
5050

51+
## 🔍 Analysis & Development Tools
52+
53+
### Complexity Analysis Examples
54+
55+
- `complexity_example.ora` - Simple example demonstrating different function complexity levels
56+
```bash
57+
./zig-out/bin/ora --analyze-complexity ora-example/complexity_example.ora
58+
```
59+
60+
- `defi_lending_pool.ora` - **Some what of a realistic DeFi lending pool contract** (700+ lines)
61+
- Complete lending/borrowing implementation with enums and switch statements
62+
- Interest rate calculations
63+
- Health factor monitoring
64+
- Liquidation mechanics
65+
- 21 functions demonstrating full complexity spectrum:
66+
* ✓ 76% Simple functions (perfect for inline)
67+
* ○ 19% Moderate functions (well-structured)
68+
* ✗ 4% Complex functions (needs refactoring)
69+
```bash
70+
./zig-out/bin/ora --analyze-complexity ora-example/defi_lending_pool.ora
71+
```
72+
73+
**Complexity Distribution:**
74+
- ✓ 11 Simple functions (73%) - Optimal for performance
75+
- ○ 4 Moderate functions (26%) - Well-structured business logic
76+
- Perfect example of maintainable smart contract design!
77+
5178
## Testing Examples
5279

5380
To validate all examples:

ora-example/complexity_example.ora

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
contract ComplexityTest {
2+
storage var balance: u256;
3+
4+
// Simple function
5+
pub fn simple_add(a: u256, b: u256) -> u256
6+
requires(a < u128_MAX)
7+
requires(b < u128_MAX)
8+
{
9+
return a + b;
10+
}
11+
12+
// Moderate function
13+
pub fn moderate_transfer(recipient: address, amount: u256) -> bool
14+
requires(balance >= amount)
15+
requires(recipient != std.constants.ZERO_ADDRESS)
16+
{
17+
if (amount == 0) {
18+
return false;
19+
}
20+
21+
balance -= amount;
22+
log Transfer(std.transaction.sender, recipient, amount);
23+
return true;
24+
}
25+
26+
// Complex function with nested blocks
27+
inline fn complex_process(data: u256) -> u256 {
28+
var result: u256 = 0;
29+
30+
if (data > 100) {
31+
if (data > 1000) {
32+
result = data * 2;
33+
} else {
34+
result = data + 100;
35+
}
36+
} else {
37+
if (data > 10) {
38+
result = data / 2;
39+
} else {
40+
result = data + 1;
41+
}
42+
}
43+
44+
var i: u256 = 0;
45+
while (i < 10) {
46+
if (result > 50) {
47+
result -= 1;
48+
} else {
49+
result += 1;
50+
}
51+
i += 1;
52+
}
53+
54+
return result;
55+
}
56+
}
57+

0 commit comments

Comments
 (0)