|
| 1 | +### Introducing **Lingua**: A Minimalist, Expressive Language for Creative Coding |
| 2 | +*(Inspired by Lisp, Python, and Forth)* |
| 3 | + |
| 4 | +Lingua combines symbolic expressiveness with functional simplicity. Here’s the full specification: |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +#### **1. Core Principles** |
| 9 | +- **Everything is an atom or a list**: `(operator arg1 arg2 ...)` |
| 10 | +- **No keywords**: Operations are defined by symbols. |
| 11 | +- **Dynamic typing**: Values carry their type. |
| 12 | +- **Concise syntax**: Minimal punctuation. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +#### **2. Syntax & Data Types** |
| 17 | +```lisp |
| 18 | + |
| 19 | +; Atoms (immutable) |
| 20 | + |
| 21 | +42 ; int |
| 22 | + |
| 23 | +3.14 ; float |
| 24 | + |
| 25 | +"hello" ; string |
| 26 | + |
| 27 | +:quit ; symbol (interned) |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +; Lists (evaluated as operations) |
| 32 | + |
| 33 | +(+ 1 2) ; → 3 |
| 34 | + |
| 35 | +(print "Hi") ; Output: Hi |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +#### **3. Special Forms** |
| 42 | +| Form | Meaning | Example | |
| 43 | + |
| 44 | +|---------------|----------------------------------|-----------------------------| |
| 45 | + |
| 46 | +| `(def x 10)` | Define variable | `x` → 10 | |
| 47 | + |
| 48 | +| `(fn [a b] ...)` | Define function | `((fn [a b] (+ a b)) 2 3)` → 5 | |
| 49 | + |
| 50 | +| `(if cond T F)` | Conditional | `(if (> 3 2) "Y" "N")` → "Y" | |
| 51 | + |
| 52 | +| `(quote x)` | Prevent evaluation | `(quote (+ 1 2))` → `(+ 1 2)` | |
| 53 | + |
| 54 | +| `'(a b c)` | Shorthand for `(quote (a b c))` | `'(1 2 3)` → list `(1 2 3)` | |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +#### **4. Functions** |
| 61 | +```lisp |
| 62 | + |
| 63 | +; Named function |
| 64 | + |
| 65 | +(def sq (fn [x] (* x x))) |
| 66 | + |
| 67 | +(sq 4) ; → 16 |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +; Anonymous function |
| 72 | + |
| 73 | +(map (fn [x] (* x 2)) '(1 2 3)) ; → (2 4 6) |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +#### **5. Conventions** |
| 80 | +- **Arithmetic**: `(+ 1 2 3)` → 6 (n-ary) |
| 81 | +- **Comparison**: `(> 5 3)` → `:true` |
| 82 | +- **Logic**: `(and :true :false)` → `:false` |
| 83 | +- **Lists**: |
| 84 | + - `(head '(1 2 3))` → 1 |
| 85 | + - `(tail '(1 2 3))` → `(2 3)` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +#### **6. Memory Management** |
| 90 | +- **Garbage-collected**: No manual memory handling. |
| 91 | +- **Persistent data structures**: All values immutable. |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +#### **7. Standard Library** |
| 96 | +| Function | Action | |
| 97 | + |
| 98 | +|--------------|----------------------------| |
| 99 | + |
| 100 | +| `(range 5)` | → `(0 1 2 3 4)` | |
| 101 | + |
| 102 | +| `(zip a b)` | Pair lists: `(zip '(1 2) '(a b))` → `((1 a) (2 b))` | |
| 103 | + |
| 104 | +| `(fold + 0 '(1 2 3))` | → `6` | |
| 105 | + |
| 106 | +| `(io/write "file.txt" "data")` | Write to file | |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +#### **8. Sample Program: Fibonacci** |
| 113 | +```lisp |
| 114 | + |
| 115 | +(def fib |
| 116 | + |
| 117 | + (fn [n] |
| 118 | + |
| 119 | + (if (< n 2) |
| 120 | + |
| 121 | + n |
| 122 | + |
| 123 | + (+ (fib (- n 1)) (fib (- n 2)))))) |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +(print (fib 10)) ; → 55 |
| 128 | + |
| 129 | +``` |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +#### **9. Unique Features** |
| 134 | +1. **Homoiconicity**: Code is data (manipulate code as lists). |
| 135 | +2. **REPL-Driven**: Interactive development. |
| 136 | +3. **Macro System**: |
| 137 | + ```lisp |
| 138 | + |
| 139 | + (defmacro unless [cond body] |
| 140 | + |
| 141 | + (list 'if (list 'not cond) body)) |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + (unless (> 3 4) (print "Safe!")) ; Prints "Safe!" |
| 146 | + |
| 147 | + ``` |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +### **Why Lingua?** |
| 152 | +- **Learn in 10 minutes**: Syntax fits on a notecard. |
| 153 | +- **Extensible**: Add features via macros. |
| 154 | +- **Embeddable**: <200 KB runtime. |
| 155 | + |
| 156 | +Example interpreter (Python): |
| 157 | +```python |
| 158 | + |
| 159 | +def eval(expr, env): |
| 160 | + |
| 161 | + if isinstance(expr, int): return expr |
| 162 | + |
| 163 | + if expr[0] == '+': return eval(expr[1], env) + eval(expr[2], env) |
| 164 | + |
| 165 | + # ... |
| 166 | + |
| 167 | +``` |
| 168 | + |
| 169 | +**Run it**: `lingua script.lg` |
| 170 | + |
| 171 | +--- |
| 172 | +*Design your own dialects, explore symbolic programming, and create freely!* 🚀 |
0 commit comments