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