nebulara 2.0.0

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.
Files changed (48) hide show
  1. package/Compiler/nbs-bootstrap.c +2229 -0
  2. package/Compiler/nbs_cli.c +1553 -0
  3. package/Compiler/neb-cli.exe +0 -0
  4. package/Compiler/neb-codegen.c +287 -0
  5. package/Compiler/neb-ffi.c +303 -0
  6. package/Compiler/neb-ir.c +307 -0
  7. package/Compiler/neb-knowledge.c +203 -0
  8. package/Compiler/neb-pipeline.c +787 -0
  9. package/Compiler/neb-semantic.c +261 -0
  10. package/Compiler/nebulara.exe +0 -0
  11. package/README.md +258 -0
  12. package/SPEC.md +267 -0
  13. package/bin/neb.js +36 -0
  14. package/build/neb-cli.exe +0 -0
  15. package/build/neb-codegen.exe +0 -0
  16. package/build/neb-ffi.exe +0 -0
  17. package/build/neb-knowledge.exe +0 -0
  18. package/build/neb-pipeline.exe +0 -0
  19. package/build/nebulara.exe +0 -0
  20. package/dist/index.js +131 -0
  21. package/package.json +39 -0
  22. package/std/collections.nbs +72 -0
  23. package/std/json.nbs +15 -0
  24. package/std/kanban.nbs +122 -0
  25. package/std/math.nbs +52 -0
  26. package/std/net.nbs +22 -0
  27. package/std/primitives.nbs +30 -0
  28. package/std/string.nbs +71 -0
  29. package/std/time.nbs +15 -0
  30. package/test/broken.nbs +2 -0
  31. package/test/hello.nbs +6 -0
  32. package/test/test-arrays.nbs +22 -0
  33. package/test/test-charat.nbs +7 -0
  34. package/test/test-cont-debug.nbs +8 -0
  35. package/test/test-cont-min.nbs +7 -0
  36. package/test/test-cont-parens.nbs +7 -0
  37. package/test/test-cont2.nbs +7 -0
  38. package/test/test-cont3.nbs +7 -0
  39. package/test/test-control.nbs +18 -0
  40. package/test/test-functions.nbs +13 -0
  41. package/test/test-loops.nbs +17 -0
  42. package/test/test-mod-for.nbs +6 -0
  43. package/test/test-mod.nbs +5 -0
  44. package/test/test-new-features.nbs +39 -0
  45. package/test/test-recursion.nbs +23 -0
  46. package/test/test-runtime.nbs +8 -0
  47. package/test/test-strings.nbs +14 -0
  48. package/test/test-trycatch.nbs +11 -0
@@ -0,0 +1,307 @@
1
+ /*
2
+ * neb-ir.c - Intermediate Representation for Nebulara
3
+ *
4
+ * Three-address code IR with SSA-like properties:
5
+ * - Linear sequence of operations
6
+ * - Each instruction has at most 3 operands
7
+ * - Phi functions for control flow merge points
8
+ * - Basic blocks for CFG construction
9
+ *
10
+ * Build: gcc -o neb-ir.exe neb-ir.c -static -O2
11
+ */
12
+
13
+ #include <stdio.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+
17
+ /* IR Instruction Types */
18
+ typedef enum {
19
+ /* Arithmetic */
20
+ IR_ADD, IR_SUB, IR_MUL, IR_DIV, IR_MOD,
21
+ IR_NEG,
22
+
23
+ /* Comparison */
24
+ IR_EQ, IR_NEQ, IR_LT, IR_GT, IR_LTE, IR_GTE,
25
+
26
+ /* Logical */
27
+ IR_AND, IR_OR, IR_NOT,
28
+
29
+ /* Memory */
30
+ IR_LOAD_IMM, /* load immediate value */
31
+ IR_LOAD_VAR, /* load variable */
32
+ IR_STORE_VAR, /* store to variable */
33
+ IR_LOAD_ARRAY, /* array index */
34
+ IR_STORE_ARRAY, /* array store */
35
+
36
+ /* Control flow */
37
+ IR_LABEL, /* label target */
38
+ IR_GOTO, /* unconditional jump */
39
+ IR_IF_GOTO, /* conditional jump */
40
+ IR_CALL, /* function call */
41
+ IR_RET, /* return */
42
+
43
+ /* Special */
44
+ IR_PRINT, /* print to stdout */
45
+ IR_PHI, /* phi function for SSA */
46
+ IR_NOP,
47
+
48
+ /* String */
49
+ IR_STR_CONCAT, /* string concatenation */
50
+
51
+ /* Type */
52
+ IR_TYPEOF,
53
+ IR_TO_STRING,
54
+ IR_TO_NUMBER,
55
+
56
+ /* Array */
57
+ IR_ARRAY_LEN,
58
+ IR_ARRAY_NEW,
59
+
60
+ IR_OP_COUNT
61
+ } IROpcode;
62
+
63
+ typedef struct {
64
+ char *name;
65
+ int id;
66
+ } IRFunc;
67
+
68
+ typedef struct {
69
+ IROpcode op;
70
+ int line;
71
+ int col;
72
+
73
+ /* Operands (indices into symbol/value tables) */
74
+ int dst; /* destination register/variable */
75
+ int src1; /* first source operand */
76
+ int src2; /* second source operand */
77
+ int imm; /* immediate value */
78
+ char str[256]; /* string literal */
79
+
80
+ /* Labels */
81
+ int label_id;
82
+ int target_label;
83
+
84
+ /* Call info */
85
+ char func_name[256];
86
+ int arg_count;
87
+ int args[16]; /* argument operand indices */
88
+ } IRInstruction;
89
+
90
+ typedef struct {
91
+ char name[256];
92
+ int id;
93
+ int is_param;
94
+ } IRVariable;
95
+
96
+ typedef struct {
97
+ IRInstruction instructions[4096];
98
+ int count;
99
+
100
+ IRVariable variables[1024];
101
+ int var_count;
102
+
103
+ /* Basic blocks */
104
+ int block_starts[1024];
105
+ int block_ends[1024];
106
+ int block_count;
107
+
108
+ /* Label table */
109
+ int label_targets[1024];
110
+
111
+ int next_var;
112
+ int next_label;
113
+ } IRProgram;
114
+
115
+ /* IR Program */
116
+ static IRProgram ir_program;
117
+
118
+ /* Variable allocation */
119
+ int ir_alloc_var(const char *name) {
120
+ int id = ir_program.var_count;
121
+ strncpy(ir_program.variables[id].name, name, 255);
122
+ ir_program.variables[id].id = id;
123
+ ir_program.var_count++;
124
+ return id;
125
+ }
126
+
127
+ int ir_next_temp(void) {
128
+ char name[32];
129
+ snprintf(name, 32, "t%d", ir_program.next_var);
130
+ return ir_alloc_var(name);
131
+ }
132
+
133
+ /* Emit instructions */
134
+ void ir_emit(IROpcode op, int line, int col) {
135
+ IRInstruction *i = &ir_program.instructions[ir_program.count];
136
+ i->op = op;
137
+ i->line = line;
138
+ i->col = col;
139
+ i->dst = -1;
140
+ i->src1 = -1;
141
+ i->src2 = -1;
142
+ i->imm = 0;
143
+ i->label_id = -1;
144
+ i->target_label = -1;
145
+ i->arg_count = 0;
146
+ ir_program.count++;
147
+ }
148
+
149
+ void ir_emit_imm(int dst, int imm, int line, int col) {
150
+ ir_emit(IR_LOAD_IMM, line, col);
151
+ ir_program.instructions[ir_program.count - 1].dst = dst;
152
+ ir_program.instructions[ir_program.count - 1].imm = imm;
153
+ }
154
+
155
+ void ir_emit_binary(int dst, int src1, int src2, IROpcode op, int line, int col) {
156
+ ir_emit(op, line, col);
157
+ ir_program.instructions[ir_program.count - 1].dst = dst;
158
+ ir_program.instructions[ir_program.count - 1].src1 = src1;
159
+ ir_program.instructions[ir_program.count - 1].src2 = src2;
160
+ }
161
+
162
+ void ir_emit_label(int label_id) {
163
+ ir_emit(IR_LABEL, 0, 0);
164
+ ir_program.instructions[ir_program.count - 1].label_id = label_id;
165
+ ir_program.label_targets[label_id] = ir_program.count - 1;
166
+ }
167
+
168
+ void ir_emit_goto(int label_id, int line, int col) {
169
+ ir_emit(IR_GOTO, line, col);
170
+ ir_program.instructions[ir_program.count - 1].target_label = label_id;
171
+ }
172
+
173
+ void ir_emit_if_goto(int cond, int label_id, int line, int col) {
174
+ ir_emit(IR_IF_GOTO, line, col);
175
+ ir_program.instructions[ir_program.count - 1].src1 = cond;
176
+ ir_program.instructions[ir_program.count - 1].target_label = label_id;
177
+ }
178
+
179
+ int ir_new_label(void) {
180
+ return ir_program.next_label++;
181
+ }
182
+
183
+ /* Print IR */
184
+ const char *ir_op_name(IROpcode op) {
185
+ switch (op) {
186
+ case IR_ADD: return "ADD";
187
+ case IR_SUB: return "SUB";
188
+ case IR_MUL: return "MUL";
189
+ case IR_DIV: return "DIV";
190
+ case IR_MOD: return "MOD";
191
+ case IR_NEG: return "NEG";
192
+ case IR_EQ: return "EQ";
193
+ case IR_NEQ: return "NEQ";
194
+ case IR_LT: return "LT";
195
+ case IR_GT: return "GT";
196
+ case IR_LTE: return "LTE";
197
+ case IR_GTE: return "GTE";
198
+ case IR_AND: return "AND";
199
+ case IR_OR: return "OR";
200
+ case IR_NOT: return "NOT";
201
+ case IR_LOAD_IMM: return "LOAD_IMM";
202
+ case IR_LOAD_VAR: return "LOAD_VAR";
203
+ case IR_STORE_VAR: return "STORE_VAR";
204
+ case IR_LOAD_ARRAY: return "LOAD_ARRAY";
205
+ case IR_STORE_ARRAY:return "STORE_ARRAY";
206
+ case IR_LABEL: return "LABEL";
207
+ case IR_GOTO: return "GOTO";
208
+ case IR_IF_GOTO: return "IF_GOTO";
209
+ case IR_CALL: return "CALL";
210
+ case IR_RET: return "RET";
211
+ case IR_PRINT: return "PRINT";
212
+ case IR_PHI: return "PHI";
213
+ case IR_NOP: return "NOP";
214
+ case IR_STR_CONCAT: return "STR_CONCAT";
215
+ case IR_TYPEOF: return "TYPEOF";
216
+ case IR_TO_STRING: return "TO_STRING";
217
+ case IR_TO_NUMBER: return "TO_NUMBER";
218
+ case IR_ARRAY_LEN: return "ARRAY_LEN";
219
+ case IR_ARRAY_NEW: return "ARRAY_NEW";
220
+ default: return "UNKNOWN";
221
+ }
222
+ }
223
+
224
+ void ir_print(void) {
225
+ printf("=== IR Output ===\n");
226
+ for (int i = 0; i < ir_program.count; i++) {
227
+ IRInstruction *ins = &ir_program.instructions[i];
228
+ printf("%4d: ", i);
229
+
230
+ switch (ins->op) {
231
+ case IR_LABEL:
232
+ printf("L%d:", ins->label_id);
233
+ break;
234
+ case IR_LOAD_IMM:
235
+ printf(" %s t%d, %d", ir_op_name(ins->op), ins->dst, ins->imm);
236
+ break;
237
+ case IR_ADD: case IR_SUB: case IR_MUL: case IR_DIV: case IR_MOD:
238
+ case IR_EQ: case IR_NEQ: case IR_LT: case IR_GT: case IR_LTE: case IR_GTE:
239
+ case IR_AND: case IR_OR:
240
+ case IR_STR_CONCAT:
241
+ printf(" %s t%d, t%d, t%d", ir_op_name(ins->op), ins->dst, ins->src1, ins->src2);
242
+ break;
243
+ case IR_NOT: case IR_NEG:
244
+ printf(" %s t%d, t%d", ir_op_name(ins->op), ins->dst, ins->src1);
245
+ break;
246
+ case IR_GOTO:
247
+ printf(" GOTO L%d", ins->target_label);
248
+ break;
249
+ case IR_IF_GOTO:
250
+ printf(" IF t%d GOTO L%d", ins->src1, ins->target_label);
251
+ break;
252
+ case IR_PRINT:
253
+ printf(" PRINT t%d", ins->src1);
254
+ break;
255
+ case IR_RET:
256
+ printf(" RET t%d", ins->src1);
257
+ break;
258
+ case IR_STORE_VAR:
259
+ printf(" STORE %s, t%d", ir_program.variables[ins->dst].name, ins->src1);
260
+ break;
261
+ case IR_LOAD_VAR:
262
+ printf(" LOAD t%d, %s", ins->dst, ir_program.variables[ins->src1].name);
263
+ break;
264
+ default:
265
+ printf(" %s", ir_op_name(ins->op));
266
+ break;
267
+ }
268
+ printf("\n");
269
+ }
270
+ printf("=== End IR ===\n");
271
+ }
272
+
273
+ void ir_reset(void) {
274
+ ir_program.count = 0;
275
+ ir_program.var_count = 0;
276
+ ir_program.block_count = 0;
277
+ ir_program.next_var = 0;
278
+ ir_program.next_label = 0;
279
+ }
280
+
281
+ /* Demo */
282
+ #ifdef NEB_IR_TEST
283
+ int main(void) {
284
+ printf("=== Nebulara IR Module ===\n\n");
285
+
286
+ /* Generate IR for: LET x = 3 + 4; PRINT x */
287
+ int v_x = ir_alloc_var("x");
288
+ int t1 = ir_next_temp();
289
+ int t2 = ir_next_temp();
290
+ int t3 = ir_next_temp();
291
+
292
+ ir_emit_imm(t1, 3, 1, 9);
293
+ ir_emit_imm(t2, 4, 1, 13);
294
+ ir_emit_binary(t3, t1, t2, IR_ADD, 1, 11);
295
+ ir_emit(IR_STORE_VAR, 1, 5);
296
+ ir_program.instructions[ir_program.count - 1].dst = v_x;
297
+ ir_program.instructions[ir_program.count - 1].src1 = t3;
298
+ ir_emit(IR_PRINT, 2, 1);
299
+ ir_program.instructions[ir_program.count - 1].src1 = v_x;
300
+
301
+ ir_print();
302
+ ir_reset();
303
+
304
+ printf("\nIR module: OK\n");
305
+ return 0;
306
+ }
307
+ #endif
@@ -0,0 +1,203 @@
1
+ /*
2
+ * neb-knowledge.c - Knowledge Graph Module
3
+ *
4
+ * AI-native knowledge storage and retrieval:
5
+ * - Entity-Relationship model
6
+ * - Graph traversal
7
+ * - Semantic search
8
+ * - Context-aware retrieval
9
+ *
10
+ * Build: gcc -o neb-knowledge.exe neb-knowledge.c -static -O2
11
+ */
12
+
13
+ #include <stdio.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+
17
+ /* Knowledge types */
18
+ typedef enum {
19
+ KNOW_TYPE_ENTITY,
20
+ KNOW_TYPE_RELATION,
21
+ KNOW_TYPE_FACT,
22
+ KNOW_TYPE_RULE,
23
+ KNOW_TYPE_CONTEXT
24
+ } KnowType;
25
+
26
+ /* Entity in the knowledge graph */
27
+ typedef struct {
28
+ int id;
29
+ char name[256];
30
+ char type[128]; /* e.g., "function", "variable", "concept" */
31
+ char value[512];
32
+ int relations[64]; /* IDs of connected entities */
33
+ int relation_count;
34
+ int confidence; /* 0-100 */
35
+ } Entity;
36
+
37
+ /* Knowledge graph */
38
+ typedef struct {
39
+ Entity entities[4096];
40
+ int count;
41
+ } KnowledgeGraph;
42
+
43
+ static KnowledgeGraph graph;
44
+
45
+ /* Initialize the knowledge graph */
46
+ void kg_init(void) {
47
+ graph.count = 0;
48
+ }
49
+
50
+ /* Add an entity */
51
+ int kg_add_entity(const char *name, const char *type, const char *value, int confidence) {
52
+ if (graph.count >= 4096) return -1;
53
+
54
+ Entity *e = &graph.entities[graph.count];
55
+ e->id = graph.count;
56
+ strncpy(e->name, name, 255);
57
+ strncpy(e->type, type, 127);
58
+ strncpy(e->value, value, 511);
59
+ e->relation_count = 0;
60
+ e->confidence = confidence;
61
+ graph.count++;
62
+
63
+ return e->id;
64
+ }
65
+
66
+ /* Add a relation between two entities */
67
+ int kg_add_relation(int from_id, int to_id) {
68
+ if (from_id < 0 || from_id >= graph.count) return -1;
69
+ if (to_id < 0 || to_id >= graph.count) return -2;
70
+
71
+ Entity *e = &graph.entities[from_id];
72
+ if (e->relation_count >= 64) return -3;
73
+
74
+ e->relations[e->relation_count++] = to_id;
75
+ return 0;
76
+ }
77
+
78
+ /* Find entity by name */
79
+ Entity *kg_find(const char *name) {
80
+ for (int i = 0; i < graph.count; i++) {
81
+ if (strcmp(graph.entities[i].name, name) == 0) {
82
+ return &graph.entities[i];
83
+ }
84
+ }
85
+ return NULL;
86
+ }
87
+
88
+ /* Find all entities of a type */
89
+ int kg_find_by_type(const char *type, Entity **results, int max_results) {
90
+ int count = 0;
91
+ for (int i = 0; i < graph.count && count < max_results; i++) {
92
+ if (strcmp(graph.entities[i].type, type) == 0) {
93
+ results[count++] = &graph.entities[i];
94
+ }
95
+ }
96
+ return count;
97
+ }
98
+
99
+ /* Semantic search - find entities matching a query */
100
+ int kg_search(const char *query, Entity **results, int max_results) {
101
+ int count = 0;
102
+ for (int i = 0; i < graph.count && count < max_results; i++) {
103
+ if (strstr(graph.entities[i].name, query) ||
104
+ strstr(graph.entities[i].value, query)) {
105
+ results[count++] = &graph.entities[i];
106
+ }
107
+ }
108
+ return count;
109
+ }
110
+
111
+ /* Get related entities */
112
+ int kg_get_related(int entity_id, Entity **results, int max_results) {
113
+ if (entity_id < 0 || entity_id >= graph.count) return 0;
114
+
115
+ Entity *e = &graph.entities[entity_id];
116
+ int count = 0;
117
+ for (int i = 0; i < e->relation_count && count < max_results; i++) {
118
+ results[count++] = &graph.entities[e->relations[i]];
119
+ }
120
+ return count;
121
+ }
122
+
123
+ /* Get context - all entities in a scope/context */
124
+ int kg_get_context(const char *context, Entity **results, int max_results) {
125
+ int count = 0;
126
+ for (int i = 0; i < graph.count && count < max_results; i++) {
127
+ if (strcmp(graph.entities[i].type, context) == 0 ||
128
+ strstr(graph.entities[i].name, context)) {
129
+ results[count++] = &graph.entities[i];
130
+ }
131
+ }
132
+ return count;
133
+ }
134
+
135
+ /* Print entity */
136
+ void kg_print_entity(Entity *e) {
137
+ printf(" [%d] %s (%s) = %s (confidence: %d%%)\n",
138
+ e->id, e->name, e->type, e->value, e->confidence);
139
+ if (e->relation_count > 0) {
140
+ printf(" relations: ");
141
+ for (int i = 0; i < e->relation_count; i++) {
142
+ printf("%d ", e->relations[i]);
143
+ }
144
+ printf("\n");
145
+ }
146
+ }
147
+
148
+ /* Print the entire knowledge graph */
149
+ void kg_print(void) {
150
+ printf("=== Knowledge Graph (%d entities) ===\n", graph.count);
151
+ for (int i = 0; i < graph.count; i++) {
152
+ kg_print_entity(&graph.entities[i]);
153
+ }
154
+ printf("=== End Knowledge Graph ===\n");
155
+ }
156
+
157
+ /* Demo */
158
+ int main(int argc, char *argv[]) {
159
+ printf("=== Nebulara Knowledge Graph Module ===\n\n");
160
+
161
+ kg_init();
162
+
163
+ /* Add some entities */
164
+ int func_add = kg_add_entity("add", "function", "int add(int, int) -> int", 100);
165
+ int func_print = kg_add_entity("PRINT", "builtin", "print to stdout", 100);
166
+ int param_a = kg_add_entity("a", "parameter", "first argument to add", 100);
167
+ int param_b = kg_add_entity("b", "parameter", "second argument to add", 100);
168
+ int lang_neb = kg_add_entity("Nebulara", "language", "AI-native programming language", 100);
169
+
170
+ /* Add relations */
171
+ kg_add_relation(func_add, param_a);
172
+ kg_add_relation(func_add, param_b);
173
+ kg_add_relation(param_a, lang_neb);
174
+ kg_add_relation(param_b, lang_neb);
175
+
176
+ /* Print graph */
177
+ kg_print();
178
+
179
+ /* Search */
180
+ printf("\nSearch 'add':\n");
181
+ Entity *results[10];
182
+ int count = kg_search("add", results, 10);
183
+ for (int i = 0; i < count; i++) {
184
+ kg_print_entity(results[i]);
185
+ }
186
+
187
+ /* Find by type */
188
+ printf("\nAll functions:\n");
189
+ count = kg_find_by_type("function", results, 10);
190
+ for (int i = 0; i < count; i++) {
191
+ kg_print_entity(results[i]);
192
+ }
193
+
194
+ /* Get related */
195
+ printf("\nRelated to 'add':\n");
196
+ count = kg_get_related(func_add, results, 10);
197
+ for (int i = 0; i < count; i++) {
198
+ kg_print_entity(results[i]);
199
+ }
200
+
201
+ printf("\nKnowledge graph module: OK\n");
202
+ return 0;
203
+ }