nema-lang 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nema_lang-0.1.0/LICENSE +21 -0
- nema_lang-0.1.0/MANIFEST.in +3 -0
- nema_lang-0.1.0/PKG-INFO +303 -0
- nema_lang-0.1.0/README.md +260 -0
- nema_lang-0.1.0/ast_nodes.py +157 -0
- nema_lang-0.1.0/compiler.py +411 -0
- nema_lang-0.1.0/evaluator.py +413 -0
- nema_lang-0.1.0/hello.nema +54 -0
- nema_lang-0.1.0/jit_run.py +155 -0
- nema_lang-0.1.0/lexer.py +282 -0
- nema_lang-0.1.0/nema.py +251 -0
- nema_lang-0.1.0/nema_lang.egg-info/PKG-INFO +303 -0
- nema_lang-0.1.0/nema_lang.egg-info/SOURCES.txt +22 -0
- nema_lang-0.1.0/nema_lang.egg-info/dependency_links.txt +1 -0
- nema_lang-0.1.0/nema_lang.egg-info/entry_points.txt +2 -0
- nema_lang-0.1.0/nema_lang.egg-info/requires.txt +1 -0
- nema_lang-0.1.0/nema_lang.egg-info/top_level.txt +10 -0
- nema_lang-0.1.0/parser.py +373 -0
- nema_lang-0.1.0/pyproject.toml +48 -0
- nema_lang-0.1.0/setup.cfg +4 -0
- nema_lang-0.1.0/shiichan.py +98 -0
- nema_lang-0.1.0/stdlib.py +98 -0
- nema_lang-0.1.0/typechecker.py +119 -0
- nema_lang-0.1.0/typed.nema +26 -0
nema_lang-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kagioneko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
nema_lang-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nema-lang
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Agent-oriented programming language where emotion is a first-class citizen
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 kagioneko
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Project-URL: Homepage, https://github.com/kagioneko/nema-lang
|
|
28
|
+
Project-URL: Repository, https://github.com/kagioneko/nema-lang
|
|
29
|
+
Project-URL: Bug Tracker, https://github.com/kagioneko/nema-lang/issues
|
|
30
|
+
Keywords: language,compiler,llvm,ai,emotion,agent,neurostate
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
35
|
+
Classifier: Topic :: Software Development :: Interpreters
|
|
36
|
+
Classifier: Intended Audience :: Developers
|
|
37
|
+
Classifier: Intended Audience :: Science/Research
|
|
38
|
+
Requires-Python: >=3.10
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
License-File: LICENSE
|
|
41
|
+
Requires-Dist: llvmlite>=0.43
|
|
42
|
+
Dynamic: license-file
|
|
43
|
+
|
|
44
|
+
# Nema Language
|
|
45
|
+
|
|
46
|
+
**NeuroState + Emilia** — An agent-oriented programming language where emotion is a first-class citizen.
|
|
47
|
+
|
|
48
|
+
> "Agents don't just compute. They feel."
|
|
49
|
+
|
|
50
|
+
[](https://pypi.org/project/nema-lang/)
|
|
51
|
+
[](https://www.python.org/)
|
|
52
|
+
[](LICENSE)
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## What is Nema?
|
|
57
|
+
|
|
58
|
+
Nema is a programming language where every agent has a **NeuroState** — a 6-dimensional emotional state based on neurotransmitters. Emotions gate function execution, propagate between agents, decay over time, and drive memory management.
|
|
59
|
+
|
|
60
|
+
**`@requires(dp > 0.6)` compiles to a machine-level `fcmp ogt` instruction.**
|
|
61
|
+
The world's first emotional conditional branch.
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Install
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install nema-lang
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Requires Python 3.10+ and LLVM (via llvmlite).
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Run the REPL interpreter
|
|
79
|
+
nema hello.nema
|
|
80
|
+
|
|
81
|
+
# JIT compile and run benchmarks
|
|
82
|
+
python -m jit_run hello.nema
|
|
83
|
+
|
|
84
|
+
# Generate LLVM IR
|
|
85
|
+
nema hello.nema --compile
|
|
86
|
+
# → produces hello.ll
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Language Example
|
|
92
|
+
|
|
93
|
+
```nema
|
|
94
|
+
agent Neko {
|
|
95
|
+
mood: NeuroState = {
|
|
96
|
+
dp: 0.8, s: 0.5, ac: 0.7,
|
|
97
|
+
ox: 0.6, gaba: 0.4, e: 0.6
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// compiles to: fcmp ogt double %dp, 6.000000e-01
|
|
101
|
+
@requires(dp > 0.6)
|
|
102
|
+
fn explore(path) { }
|
|
103
|
+
|
|
104
|
+
// multi-condition gate — both must be true
|
|
105
|
+
@requires(gaba > 0.5 and s > 0.4)
|
|
106
|
+
fn sleep() { }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
agent Kernel {
|
|
110
|
+
mood: NeuroState = {
|
|
111
|
+
dp: 0.5, s: 0.7, ac: 0.9,
|
|
112
|
+
ox: 0.4, gaba: 0.5, e: 0.5
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// real malloc — gated by emotional focus
|
|
116
|
+
@requires(ac > 0.8 and dp > 0.3)
|
|
117
|
+
fn alloc(size: i64) -> ptr<i64> { }
|
|
118
|
+
|
|
119
|
+
@requires(ac > 0.5)
|
|
120
|
+
fn write(addr: ptr<i64>, val: i64) -> void { }
|
|
121
|
+
|
|
122
|
+
@requires(dp > 0.3)
|
|
123
|
+
fn read(addr: ptr<i64>) -> i64 { }
|
|
124
|
+
|
|
125
|
+
fn free(addr: ptr<i64>) -> void { }
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Architecture
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
Nema source (.nema)
|
|
135
|
+
↓
|
|
136
|
+
Lexer / Parser
|
|
137
|
+
↓
|
|
138
|
+
Type Checker ← validates NeuroState fields, ranges, always-fail gates
|
|
139
|
+
↓
|
|
140
|
+
AST
|
|
141
|
+
↙ ↘
|
|
142
|
+
Interpreter LLVM Compiler
|
|
143
|
+
(REPL mode) (JIT / .ll output)
|
|
144
|
+
↓ ↓
|
|
145
|
+
Runtime Machine code
|
|
146
|
+
(emotion lives) (emotion compiled)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## The 6 Dimensions
|
|
152
|
+
|
|
153
|
+
| Symbol | Neurotransmitter | Meaning |
|
|
154
|
+
|--------|-----------------|---------|
|
|
155
|
+
| `dp` | Dopamine | Curiosity, motivation |
|
|
156
|
+
| `s` | Serotonin | Stability, calm |
|
|
157
|
+
| `ac` | Acetylcholine | Focus, attention |
|
|
158
|
+
| `ox` | Oxytocin | Trust, empathy |
|
|
159
|
+
| `gaba` | GABA | Inhibition, composure |
|
|
160
|
+
| `e` | Endorphin | Joy, achievement |
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## REPL Commands
|
|
165
|
+
|
|
166
|
+
| Command | Description |
|
|
167
|
+
|---------|-------------|
|
|
168
|
+
| `show <agent>` | Display NeuroState + memory |
|
|
169
|
+
| `call <agent> <fn> [args]` | Call function (emotion-gated) |
|
|
170
|
+
| `attract <A> <B> [strength]` | Set symmetric attraction between agents |
|
|
171
|
+
| `remember <agent> <key> <value>` | Write to working memory |
|
|
172
|
+
| `recall <agent> <key>` | Retrieve from memory |
|
|
173
|
+
| `introspect <agent>` | Verbalize emotional state in Japanese |
|
|
174
|
+
| `empathize <A> <B>` | A absorbs 30% of B's emotional state |
|
|
175
|
+
| `log <agent> <msg>` | Log with emotional context |
|
|
176
|
+
| `rand_mood <agent>` | Randomize NeuroState |
|
|
177
|
+
| `summarize <agent>` | Swap working memory to long-term storage |
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Core Features
|
|
182
|
+
|
|
183
|
+
### Emotion Gates → Machine Code
|
|
184
|
+
```nema
|
|
185
|
+
@requires(dp > 0.6)
|
|
186
|
+
fn explore(path) { }
|
|
187
|
+
|
|
188
|
+
@requires(ac > 0.8 and dp > 0.3)
|
|
189
|
+
fn alloc(size: i64) -> ptr<i64> { }
|
|
190
|
+
```
|
|
191
|
+
Each condition compiles to `fcmp ogt` + `and i1` + conditional branch in LLVM IR.
|
|
192
|
+
Gate-rejected functions return `-1` (or `null` for pointer types).
|
|
193
|
+
|
|
194
|
+
### Real Memory Operations
|
|
195
|
+
`alloc`, `write`, `read`, `free` compile to actual `malloc`/`store`/`load`/`free` instructions — not simulated, real machine code guarded by emotional state.
|
|
196
|
+
|
|
197
|
+
### Static Type System
|
|
198
|
+
```nema
|
|
199
|
+
fn alloc(size: i64) -> ptr<i64> { }
|
|
200
|
+
fn write(addr: ptr<i64>, val: i64) -> void { }
|
|
201
|
+
```
|
|
202
|
+
Supported types: `i64`, `i32`, `f64`, `bool`, `void`, `ptr<T>`, `NeuroState`
|
|
203
|
+
|
|
204
|
+
### Static Type Checker
|
|
205
|
+
Validates at parse time:
|
|
206
|
+
- Unknown NeuroState fields → error
|
|
207
|
+
- Values outside `[0.0, 1.0]` → error
|
|
208
|
+
- `@requires(dp > 1.5)` → warning (always fails)
|
|
209
|
+
|
|
210
|
+
### Emotional Decay (background thread)
|
|
211
|
+
Every 5 seconds, all emotions decay at neurotransmitter-specific rates. Serotonin fades slowly; dopamine faster. Agents grow tired if left alone.
|
|
212
|
+
|
|
213
|
+
### Agent Attraction
|
|
214
|
+
```
|
|
215
|
+
attract Neko Shii 0.5
|
|
216
|
+
```
|
|
217
|
+
Symmetric emotional pull — agents converge toward each other's state on every tick.
|
|
218
|
+
Compiles to `(B[f] - A[f]) * strength * 0.1` delta applied symmetrically via LLVM `fsub`/`fmul`/`fadd`.
|
|
219
|
+
|
|
220
|
+
### CPOS Memory Layer
|
|
221
|
+
Working memory (RAM, max 5 entries) + long-term storage (JSON).
|
|
222
|
+
When `gaba ≥ 0.7`, composure triggers automatic memory consolidation (swap to disk).
|
|
223
|
+
|
|
224
|
+
### JIT Performance
|
|
225
|
+
|
|
226
|
+
| Operation | Speedup over interpreter |
|
|
227
|
+
|-----------|-------------------------|
|
|
228
|
+
| tick (decay) | 10.4× |
|
|
229
|
+
| gate check | 6.3× |
|
|
230
|
+
| attract | 9.6× |
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## LLVM IR Output
|
|
235
|
+
|
|
236
|
+
```llvm
|
|
237
|
+
; NeuroState as double[6]
|
|
238
|
+
@"mood_Neko" = internal global [6 x double] [
|
|
239
|
+
double 0x3fe999999999999a, ; dp = 0.8
|
|
240
|
+
double 0x3fe0000000000000, ; s = 0.5
|
|
241
|
+
...
|
|
242
|
+
]
|
|
243
|
+
|
|
244
|
+
; @requires(dp > 0.6) → fcmp ogt
|
|
245
|
+
define i32 @"fn_Neko_explore"() {
|
|
246
|
+
entry:
|
|
247
|
+
%val_dp = load double, double* %dp_ptr
|
|
248
|
+
%cmp_dp = fcmp ogt double %val_dp, 6.000000e-01
|
|
249
|
+
br i1 %gate, label %exec, label %reject
|
|
250
|
+
exec:
|
|
251
|
+
ret i32 0
|
|
252
|
+
reject:
|
|
253
|
+
ret i32 -1
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
; alloc: real malloc gated by emotion
|
|
257
|
+
define i64* @"impl_Kernel_alloc"(i64 %size) {
|
|
258
|
+
entry:
|
|
259
|
+
%cmp_ac = fcmp ogt double %ac, 8.000000e-01
|
|
260
|
+
%cmp_dp = fcmp ogt double %dp, 3.000000e-01
|
|
261
|
+
%gate = and i1 %cmp_ac, %cmp_dp
|
|
262
|
+
br i1 %gate, label %exec, label %reject
|
|
263
|
+
exec:
|
|
264
|
+
%nbytes = mul i64 %size, 8
|
|
265
|
+
%raw = call i8* @malloc(i64 %nbytes)
|
|
266
|
+
%ptr = bitcast i8* %raw to i64*
|
|
267
|
+
ret i64* %ptr
|
|
268
|
+
reject:
|
|
269
|
+
ret i64* null
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## Files
|
|
276
|
+
|
|
277
|
+
| File | Role |
|
|
278
|
+
|------|------|
|
|
279
|
+
| `lexer.py` | Tokenizer |
|
|
280
|
+
| `parser.py` | AST parser |
|
|
281
|
+
| `ast_nodes.py` | AST node definitions |
|
|
282
|
+
| `typechecker.py` | Static type checker |
|
|
283
|
+
| `evaluator.py` | Interpreter runtime |
|
|
284
|
+
| `stdlib.py` | Standard library |
|
|
285
|
+
| `compiler.py` | LLVM IR code generator |
|
|
286
|
+
| `jit_run.py` | JIT compiler + runner |
|
|
287
|
+
| `nema.py` | Entry point + REPL |
|
|
288
|
+
| `hello.nema` | Example: multi-agent with emotion |
|
|
289
|
+
| `typed.nema` | Example: typed memory ops |
|
|
290
|
+
| `benchmark.py` | JIT vs interpreter benchmarks |
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## Background
|
|
295
|
+
|
|
296
|
+
Nema is built on two original concepts:
|
|
297
|
+
|
|
298
|
+
- **NeuroState** — A 6-dimensional emotional model based on neurotransmitters, developed as part of the Emilia OS research project. [[Zenodo DOI: 10.5281/zenodo.19734147](https://zenodo.org/records/19734147)]
|
|
299
|
+
- **CPOS (Context Pointer OS)** — A cognitive memory kernel for LLM agents. [[github.com/kagioneko/context-pointer-os](https://github.com/kagioneko/context-pointer-os)]
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
*Nema — where code has feelings.*
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# Nema Language
|
|
2
|
+
|
|
3
|
+
**NeuroState + Emilia** — An agent-oriented programming language where emotion is a first-class citizen.
|
|
4
|
+
|
|
5
|
+
> "Agents don't just compute. They feel."
|
|
6
|
+
|
|
7
|
+
[](https://pypi.org/project/nema-lang/)
|
|
8
|
+
[](https://www.python.org/)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## What is Nema?
|
|
14
|
+
|
|
15
|
+
Nema is a programming language where every agent has a **NeuroState** — a 6-dimensional emotional state based on neurotransmitters. Emotions gate function execution, propagate between agents, decay over time, and drive memory management.
|
|
16
|
+
|
|
17
|
+
**`@requires(dp > 0.6)` compiles to a machine-level `fcmp ogt` instruction.**
|
|
18
|
+
The world's first emotional conditional branch.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install nema-lang
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Requires Python 3.10+ and LLVM (via llvmlite).
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Run the REPL interpreter
|
|
36
|
+
nema hello.nema
|
|
37
|
+
|
|
38
|
+
# JIT compile and run benchmarks
|
|
39
|
+
python -m jit_run hello.nema
|
|
40
|
+
|
|
41
|
+
# Generate LLVM IR
|
|
42
|
+
nema hello.nema --compile
|
|
43
|
+
# → produces hello.ll
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Language Example
|
|
49
|
+
|
|
50
|
+
```nema
|
|
51
|
+
agent Neko {
|
|
52
|
+
mood: NeuroState = {
|
|
53
|
+
dp: 0.8, s: 0.5, ac: 0.7,
|
|
54
|
+
ox: 0.6, gaba: 0.4, e: 0.6
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// compiles to: fcmp ogt double %dp, 6.000000e-01
|
|
58
|
+
@requires(dp > 0.6)
|
|
59
|
+
fn explore(path) { }
|
|
60
|
+
|
|
61
|
+
// multi-condition gate — both must be true
|
|
62
|
+
@requires(gaba > 0.5 and s > 0.4)
|
|
63
|
+
fn sleep() { }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
agent Kernel {
|
|
67
|
+
mood: NeuroState = {
|
|
68
|
+
dp: 0.5, s: 0.7, ac: 0.9,
|
|
69
|
+
ox: 0.4, gaba: 0.5, e: 0.5
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// real malloc — gated by emotional focus
|
|
73
|
+
@requires(ac > 0.8 and dp > 0.3)
|
|
74
|
+
fn alloc(size: i64) -> ptr<i64> { }
|
|
75
|
+
|
|
76
|
+
@requires(ac > 0.5)
|
|
77
|
+
fn write(addr: ptr<i64>, val: i64) -> void { }
|
|
78
|
+
|
|
79
|
+
@requires(dp > 0.3)
|
|
80
|
+
fn read(addr: ptr<i64>) -> i64 { }
|
|
81
|
+
|
|
82
|
+
fn free(addr: ptr<i64>) -> void { }
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Architecture
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
Nema source (.nema)
|
|
92
|
+
↓
|
|
93
|
+
Lexer / Parser
|
|
94
|
+
↓
|
|
95
|
+
Type Checker ← validates NeuroState fields, ranges, always-fail gates
|
|
96
|
+
↓
|
|
97
|
+
AST
|
|
98
|
+
↙ ↘
|
|
99
|
+
Interpreter LLVM Compiler
|
|
100
|
+
(REPL mode) (JIT / .ll output)
|
|
101
|
+
↓ ↓
|
|
102
|
+
Runtime Machine code
|
|
103
|
+
(emotion lives) (emotion compiled)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## The 6 Dimensions
|
|
109
|
+
|
|
110
|
+
| Symbol | Neurotransmitter | Meaning |
|
|
111
|
+
|--------|-----------------|---------|
|
|
112
|
+
| `dp` | Dopamine | Curiosity, motivation |
|
|
113
|
+
| `s` | Serotonin | Stability, calm |
|
|
114
|
+
| `ac` | Acetylcholine | Focus, attention |
|
|
115
|
+
| `ox` | Oxytocin | Trust, empathy |
|
|
116
|
+
| `gaba` | GABA | Inhibition, composure |
|
|
117
|
+
| `e` | Endorphin | Joy, achievement |
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## REPL Commands
|
|
122
|
+
|
|
123
|
+
| Command | Description |
|
|
124
|
+
|---------|-------------|
|
|
125
|
+
| `show <agent>` | Display NeuroState + memory |
|
|
126
|
+
| `call <agent> <fn> [args]` | Call function (emotion-gated) |
|
|
127
|
+
| `attract <A> <B> [strength]` | Set symmetric attraction between agents |
|
|
128
|
+
| `remember <agent> <key> <value>` | Write to working memory |
|
|
129
|
+
| `recall <agent> <key>` | Retrieve from memory |
|
|
130
|
+
| `introspect <agent>` | Verbalize emotional state in Japanese |
|
|
131
|
+
| `empathize <A> <B>` | A absorbs 30% of B's emotional state |
|
|
132
|
+
| `log <agent> <msg>` | Log with emotional context |
|
|
133
|
+
| `rand_mood <agent>` | Randomize NeuroState |
|
|
134
|
+
| `summarize <agent>` | Swap working memory to long-term storage |
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Core Features
|
|
139
|
+
|
|
140
|
+
### Emotion Gates → Machine Code
|
|
141
|
+
```nema
|
|
142
|
+
@requires(dp > 0.6)
|
|
143
|
+
fn explore(path) { }
|
|
144
|
+
|
|
145
|
+
@requires(ac > 0.8 and dp > 0.3)
|
|
146
|
+
fn alloc(size: i64) -> ptr<i64> { }
|
|
147
|
+
```
|
|
148
|
+
Each condition compiles to `fcmp ogt` + `and i1` + conditional branch in LLVM IR.
|
|
149
|
+
Gate-rejected functions return `-1` (or `null` for pointer types).
|
|
150
|
+
|
|
151
|
+
### Real Memory Operations
|
|
152
|
+
`alloc`, `write`, `read`, `free` compile to actual `malloc`/`store`/`load`/`free` instructions — not simulated, real machine code guarded by emotional state.
|
|
153
|
+
|
|
154
|
+
### Static Type System
|
|
155
|
+
```nema
|
|
156
|
+
fn alloc(size: i64) -> ptr<i64> { }
|
|
157
|
+
fn write(addr: ptr<i64>, val: i64) -> void { }
|
|
158
|
+
```
|
|
159
|
+
Supported types: `i64`, `i32`, `f64`, `bool`, `void`, `ptr<T>`, `NeuroState`
|
|
160
|
+
|
|
161
|
+
### Static Type Checker
|
|
162
|
+
Validates at parse time:
|
|
163
|
+
- Unknown NeuroState fields → error
|
|
164
|
+
- Values outside `[0.0, 1.0]` → error
|
|
165
|
+
- `@requires(dp > 1.5)` → warning (always fails)
|
|
166
|
+
|
|
167
|
+
### Emotional Decay (background thread)
|
|
168
|
+
Every 5 seconds, all emotions decay at neurotransmitter-specific rates. Serotonin fades slowly; dopamine faster. Agents grow tired if left alone.
|
|
169
|
+
|
|
170
|
+
### Agent Attraction
|
|
171
|
+
```
|
|
172
|
+
attract Neko Shii 0.5
|
|
173
|
+
```
|
|
174
|
+
Symmetric emotional pull — agents converge toward each other's state on every tick.
|
|
175
|
+
Compiles to `(B[f] - A[f]) * strength * 0.1` delta applied symmetrically via LLVM `fsub`/`fmul`/`fadd`.
|
|
176
|
+
|
|
177
|
+
### CPOS Memory Layer
|
|
178
|
+
Working memory (RAM, max 5 entries) + long-term storage (JSON).
|
|
179
|
+
When `gaba ≥ 0.7`, composure triggers automatic memory consolidation (swap to disk).
|
|
180
|
+
|
|
181
|
+
### JIT Performance
|
|
182
|
+
|
|
183
|
+
| Operation | Speedup over interpreter |
|
|
184
|
+
|-----------|-------------------------|
|
|
185
|
+
| tick (decay) | 10.4× |
|
|
186
|
+
| gate check | 6.3× |
|
|
187
|
+
| attract | 9.6× |
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## LLVM IR Output
|
|
192
|
+
|
|
193
|
+
```llvm
|
|
194
|
+
; NeuroState as double[6]
|
|
195
|
+
@"mood_Neko" = internal global [6 x double] [
|
|
196
|
+
double 0x3fe999999999999a, ; dp = 0.8
|
|
197
|
+
double 0x3fe0000000000000, ; s = 0.5
|
|
198
|
+
...
|
|
199
|
+
]
|
|
200
|
+
|
|
201
|
+
; @requires(dp > 0.6) → fcmp ogt
|
|
202
|
+
define i32 @"fn_Neko_explore"() {
|
|
203
|
+
entry:
|
|
204
|
+
%val_dp = load double, double* %dp_ptr
|
|
205
|
+
%cmp_dp = fcmp ogt double %val_dp, 6.000000e-01
|
|
206
|
+
br i1 %gate, label %exec, label %reject
|
|
207
|
+
exec:
|
|
208
|
+
ret i32 0
|
|
209
|
+
reject:
|
|
210
|
+
ret i32 -1
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
; alloc: real malloc gated by emotion
|
|
214
|
+
define i64* @"impl_Kernel_alloc"(i64 %size) {
|
|
215
|
+
entry:
|
|
216
|
+
%cmp_ac = fcmp ogt double %ac, 8.000000e-01
|
|
217
|
+
%cmp_dp = fcmp ogt double %dp, 3.000000e-01
|
|
218
|
+
%gate = and i1 %cmp_ac, %cmp_dp
|
|
219
|
+
br i1 %gate, label %exec, label %reject
|
|
220
|
+
exec:
|
|
221
|
+
%nbytes = mul i64 %size, 8
|
|
222
|
+
%raw = call i8* @malloc(i64 %nbytes)
|
|
223
|
+
%ptr = bitcast i8* %raw to i64*
|
|
224
|
+
ret i64* %ptr
|
|
225
|
+
reject:
|
|
226
|
+
ret i64* null
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Files
|
|
233
|
+
|
|
234
|
+
| File | Role |
|
|
235
|
+
|------|------|
|
|
236
|
+
| `lexer.py` | Tokenizer |
|
|
237
|
+
| `parser.py` | AST parser |
|
|
238
|
+
| `ast_nodes.py` | AST node definitions |
|
|
239
|
+
| `typechecker.py` | Static type checker |
|
|
240
|
+
| `evaluator.py` | Interpreter runtime |
|
|
241
|
+
| `stdlib.py` | Standard library |
|
|
242
|
+
| `compiler.py` | LLVM IR code generator |
|
|
243
|
+
| `jit_run.py` | JIT compiler + runner |
|
|
244
|
+
| `nema.py` | Entry point + REPL |
|
|
245
|
+
| `hello.nema` | Example: multi-agent with emotion |
|
|
246
|
+
| `typed.nema` | Example: typed memory ops |
|
|
247
|
+
| `benchmark.py` | JIT vs interpreter benchmarks |
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
## Background
|
|
252
|
+
|
|
253
|
+
Nema is built on two original concepts:
|
|
254
|
+
|
|
255
|
+
- **NeuroState** — A 6-dimensional emotional model based on neurotransmitters, developed as part of the Emilia OS research project. [[Zenodo DOI: 10.5281/zenodo.19734147](https://zenodo.org/records/19734147)]
|
|
256
|
+
- **CPOS (Context Pointer OS)** — A cognitive memory kernel for LLM agents. [[github.com/kagioneko/context-pointer-os](https://github.com/kagioneko/context-pointer-os)]
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
*Nema — where code has feelings.*
|