|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Ephapax Syntax Guide and Examples |
| 3 | +// |
| 4 | +// This file demonstrates the correct Ephapax syntax. |
| 5 | +// Note: Ephapax uses ML-style syntax, not C-style syntax. |
| 6 | + |
| 7 | +// ============================================================================ |
| 8 | +// Function Declarations |
| 9 | +// ============================================================================ |
| 10 | + |
| 11 | +// Simple function (no parameters) |
| 12 | +fn constant_value(): I32 = 42 |
| 13 | + |
| 14 | +// Function with parameters |
| 15 | +fn add(x: I32, y: I32): I32 = x + y |
| 16 | + |
| 17 | +// Function with let bindings (use 'in' keyword) |
| 18 | +fn compute(_unit: ()): I32 = |
| 19 | + let x = 10 in |
| 20 | + let y = 20 in |
| 21 | + x + y |
| 22 | + |
| 23 | +// Nested let bindings |
| 24 | +fn nested(_unit: ()): I32 = |
| 25 | + let a = 1 in |
| 26 | + let b = 2 in |
| 27 | + let c = a + b in |
| 28 | + let d = c * 2 in |
| 29 | + d |
| 30 | + |
| 31 | +// ============================================================================ |
| 32 | +// If Expressions |
| 33 | +// ============================================================================ |
| 34 | + |
| 35 | +fn conditional(x: I32): I32 = |
| 36 | + if x > 0 then x else 0 |
| 37 | + |
| 38 | +fn complex_conditional(_unit: ()): I32 = |
| 39 | + let x = 10 in |
| 40 | + let y = 20 in |
| 41 | + if x < y then |
| 42 | + let sum = x + y in |
| 43 | + sum |
| 44 | + else |
| 45 | + let diff = y - x in |
| 46 | + diff |
| 47 | + |
| 48 | +// ============================================================================ |
| 49 | +// Lambda Expressions |
| 50 | +// ============================================================================ |
| 51 | + |
| 52 | +fn use_lambda(_unit: ()): I32 = |
| 53 | + let f = fn (x: I32) -> x + 1 in |
| 54 | + f(5) |
| 55 | + |
| 56 | +// Note: Higher-order functions with function type parameters |
| 57 | +// require more complex type annotations - simplified for now |
| 58 | + |
| 59 | +// ============================================================================ |
| 60 | +// Product Types (Pairs) |
| 61 | +// ============================================================================ |
| 62 | + |
| 63 | +fn make_pair(_unit: ()): (I32, I32) = (10, 20) |
| 64 | + |
| 65 | +fn pair_operations(_unit: ()): I32 = |
| 66 | + let p = (5, 3) in |
| 67 | + let x = p.0 in |
| 68 | + let y = p.1 in |
| 69 | + x + y |
| 70 | + |
| 71 | +// ============================================================================ |
| 72 | +// Linear Types (with regions) |
| 73 | +// ============================================================================ |
| 74 | + |
| 75 | +// Linear values must be consumed exactly once |
| 76 | +fn linear_example(_unit: ()): I32 = |
| 77 | + region r { |
| 78 | + let s = String.new@r("hello") in |
| 79 | + let len = String.len(&s) in |
| 80 | + let _ = drop(s) in |
| 81 | + len |
| 82 | + } |
| 83 | + |
| 84 | +// ============================================================================ |
| 85 | +// Main Entry Point |
| 86 | +// ============================================================================ |
| 87 | + |
| 88 | +fn main(_unit: ()): I32 = |
| 89 | + let result = compute(()) in |
| 90 | + let cond_result = conditional(5) in |
| 91 | + let lambda_result = use_lambda(()) in |
| 92 | + result + cond_result + lambda_result |
0 commit comments