@fraczak/k 6.1.1 → 6.1.3

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 (79) hide show
  1. package/.codex +0 -0
  2. package/Code-derivation-tests/pattern-envelope.mjs +26 -0
  3. package/DOCS/BIG_PICTURE.md +10 -10
  4. package/DOCS/DIVERGENCE_DETECTION.md +1 -1
  5. package/DOCS/REPL.md +33 -0
  6. package/DOCS/TYPE_DERIVATION.md +12 -12
  7. package/DOCS/a.html +398 -0
  8. package/DOCS/book/01-introduction.md +2 -2
  9. package/DOCS/book/02-syntax-and-values.md +8 -8
  10. package/DOCS/book/03-types-as-finite-automata.md +2 -2
  11. package/DOCS/book/04-partial-functions-and-composition.md +2 -2
  12. package/DOCS/book/05-typing-filters-and-normalization.md +3 -3
  13. package/DOCS/book/06-values-in-memory.md +3 -3
  14. package/DOCS/book/07-the-partial-function-abi.md +1 -1
  15. package/DOCS/book/08-operational-semantics-and-execution.md +1 -1
  16. package/DOCS/book/09-compiler-architecture.md +2 -2
  17. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +1 -0
  18. package/DOCS/dependency-graph.html +140 -0
  19. package/DOCS/dependency-graph.md +144 -0
  20. package/NAT.k~ +452 -0
  21. package/README.md +7 -8
  22. package/UU/ta.k +1 -0
  23. package/UU/ta.k~ +0 -0
  24. package/UU/tb.k +0 -0
  25. package/UU/tc.k +0 -0
  26. package/a.k +138 -0
  27. package/a.klib +1 -0
  28. package/codecs/BINARY_FORMAT.html +471 -0
  29. package/codecs/BINARY_FORMAT.md +1 -1
  30. package/codecs/POLYMORPHIC_BINARY_FORMAT.html +355 -0
  31. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +1 -1
  32. package/codecs/ieee.mjs +19 -4
  33. package/codecs/int.mjs +11 -4
  34. package/codecs/json.mjs +7 -4
  35. package/codecs/k-parse.mjs +7 -4
  36. package/codecs/k-print.mjs +7 -4
  37. package/codecs/runtime/cli-entry.mjs +13 -0
  38. package/codecs/show.mjs +7 -4
  39. package/codecs/unit.mjs +23 -8
  40. package/codecs/utf16.mjs +27 -5
  41. package/codecs/utf8.mjs +27 -5
  42. package/codes.mjs +14 -33
  43. package/core.json +1 -0
  44. package/core.klib +1 -0
  45. package/core.klib~ +0 -0
  46. package/export.mjs +3 -3
  47. package/ieee.klib +1 -0
  48. package/k-vscode/package-lock.json +4176 -0
  49. package/kkk.k +197 -0
  50. package/kkk.klib +0 -0
  51. package/kkk.klib~ +0 -0
  52. package/nak.klib +1 -0
  53. package/nak2.klib +1 -0
  54. package/nat.k +10 -0
  55. package/nat2.k +9 -0
  56. package/object.mjs +20 -7
  57. package/p.json +5475 -0
  58. package/package.json +3 -2
  59. package/repl-codecs.mjs +173 -0
  60. package/repl.mjs +215 -5
  61. package/run.mjs +16 -2
  62. package/scripts/benchmark-idempotency.mjs +72 -0
  63. package/test-ieee-arithmetic.mjs +93 -0
  64. package/test-k-object.mjs +19 -0
  65. package/test-repl.mjs +60 -1
  66. package/tests.sh +10 -1
  67. package/type_registry/registry.mjs~ +105 -0
  68. package/type_registry/typeParser.jison~ +132 -0
  69. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  70. package/.idea/copilot.data.migration.agent.xml +0 -6
  71. package/.idea/copilot.data.migration.ask.xml +0 -6
  72. package/.idea/copilot.data.migration.ask2agent.xml +0 -6
  73. package/.idea/copilot.data.migration.edit.xml +0 -6
  74. package/.idea/k.iml +0 -9
  75. package/.idea/misc.xml +0 -6
  76. package/.idea/modules.xml +0 -8
  77. package/.idea/vcs.xml +0 -6
  78. package/k_compiler/runtime.c +0 -205
  79. package/k_compiler/runtime.h +0 -34
@@ -1,205 +0,0 @@
1
- #include "runtime.h"
2
-
3
- #include <stdio.h>
4
- #include <stdlib.h>
5
- #include <string.h>
6
-
7
- typedef enum {
8
- K_NODE_PRODUCT = 1,
9
- K_NODE_VARIANT = 2
10
- } KNodeKind;
11
-
12
- typedef struct {
13
- const char *label;
14
- KValue value;
15
- } KField;
16
-
17
- struct KNode {
18
- KNodeKind kind;
19
- const char *type_id;
20
- union {
21
- struct {
22
- int field_count;
23
- KField *fields;
24
- } product;
25
- struct {
26
- const char *tag;
27
- KValue child;
28
- } variant;
29
- } as;
30
- };
31
-
32
- static char *krt_strdup(const char *value) {
33
- size_t len = strlen(value);
34
- char *result = (char *)malloc(len + 1);
35
- memcpy(result, value, len + 1);
36
- return result;
37
- }
38
-
39
- static const char *krt_infer_variant_type(const char *label, KValue child) {
40
- if (child == NULL) return "";
41
- if (child->type_id != NULL && child->type_id[0] != '\0') {
42
- return child->type_id;
43
- }
44
- if (strcmp(label, "zero") == 0 || strcmp(label, "succ") == 0) {
45
- return "@w8iSHeQQE738vEmWNGja3FQWk3XuExQKZ2pbm8ApEdkF";
46
- }
47
- return "";
48
- }
49
-
50
- static KOpt krt_ok(KValue value) {
51
- KOpt result;
52
- result.ok = 1;
53
- result.value = value;
54
- return result;
55
- }
56
-
57
- static const char *json_extract_string(const char *json, const char *key) {
58
- static char buffer[256];
59
- char pattern[64];
60
- const char *cursor;
61
- const char *start;
62
- size_t length = 0;
63
-
64
- snprintf(pattern, sizeof(pattern), "\"%s\":\"", key);
65
- cursor = strstr(json, pattern);
66
- if (cursor == NULL) return NULL;
67
- start = cursor + strlen(pattern);
68
-
69
- while (start[length] != '\0' && start[length] != '"') {
70
- if (start[length] == '\\' && start[length + 1] != '\0') {
71
- length += 2;
72
- continue;
73
- }
74
- length += 1;
75
- }
76
-
77
- if (length >= sizeof(buffer)) {
78
- length = sizeof(buffer) - 1;
79
- }
80
- memcpy(buffer, start, length);
81
- buffer[length] = '\0';
82
- return buffer;
83
- }
84
-
85
- static KValue product_field(KValue input, const char *label) {
86
- int index;
87
- if (input == NULL || input->kind != K_NODE_PRODUCT) return NULL;
88
- for (index = 0; index < input->as.product.field_count; index += 1) {
89
- if (strcmp(input->as.product.fields[index].label, label) == 0) {
90
- return input->as.product.fields[index].value;
91
- }
92
- }
93
- return NULL;
94
- }
95
-
96
- KOpt krt_fail(void) {
97
- KOpt result;
98
- result.ok = 0;
99
- result.value = NULL;
100
- return result;
101
- }
102
-
103
- KOpt krt_return_input(KValue input) {
104
- return krt_ok(input);
105
- }
106
-
107
- KValue krt_new_product(const char *type_id, int field_count, const char *const *labels, KValue const *values) {
108
- int index;
109
- KNode *node = (KNode *)calloc(1, sizeof(KNode));
110
- node->kind = K_NODE_PRODUCT;
111
- node->type_id = krt_strdup(type_id == NULL ? "" : type_id);
112
- node->as.product.field_count = field_count;
113
- node->as.product.fields = field_count > 0 ? (KField *)calloc((size_t)field_count, sizeof(KField)) : NULL;
114
-
115
- for (index = 0; index < field_count; index += 1) {
116
- node->as.product.fields[index].label = krt_strdup(labels[index]);
117
- node->as.product.fields[index].value = values[index];
118
- }
119
- return node;
120
- }
121
-
122
- KValue krt_new_variant(const char *type_id, const char *label, KValue child) {
123
- KNode *node = (KNode *)calloc(1, sizeof(KNode));
124
- node->kind = K_NODE_VARIANT;
125
- node->type_id = krt_strdup(type_id == NULL ? "" : type_id);
126
- node->as.variant.tag = krt_strdup(label);
127
- node->as.variant.child = child;
128
- return node;
129
- }
130
-
131
- KOpt krt_guard_code(KValue input, const char *type_id) {
132
- if (input == NULL) return krt_fail();
133
- if (type_id == NULL || type_id[0] == '\0') return krt_ok(input);
134
- if (strcmp(input->type_id, type_id) == 0) return krt_ok(input);
135
- return krt_fail();
136
- }
137
-
138
- KOpt krt_guard_type_var(KValue input, const char *var_name) {
139
- (void)var_name;
140
- if (input == NULL) return krt_fail();
141
- return krt_ok(input);
142
- }
143
-
144
- KOpt krt_guard_shape(KValue input, const char *shape_descriptor_json) {
145
- const char *shape;
146
- if (input == NULL) return krt_fail();
147
- shape = json_extract_string(shape_descriptor_json, "shape");
148
- if (shape == NULL || strcmp(shape, "any") == 0) return krt_ok(input);
149
- if (strcmp(shape, "product") == 0 && input->kind == K_NODE_PRODUCT) return krt_ok(input);
150
- if (strcmp(shape, "union") == 0 && input->kind == K_NODE_VARIANT) return krt_ok(input);
151
- return krt_fail();
152
- }
153
-
154
- KOpt krt_project_field(KValue input, const char *projection_descriptor_json) {
155
- const char *label = json_extract_string(projection_descriptor_json, "label");
156
- KValue child = product_field(input, label);
157
- if (label == NULL || child == NULL) return krt_fail();
158
- return krt_ok(child);
159
- }
160
-
161
- KOpt krt_project_variant(KValue input, const char *projection_descriptor_json) {
162
- const char *label = json_extract_string(projection_descriptor_json, "label");
163
- if (label == NULL || input == NULL || input->kind != K_NODE_VARIANT) return krt_fail();
164
- if (strcmp(input->as.variant.tag, label) != 0) return krt_fail();
165
- return krt_ok(input->as.variant.child);
166
- }
167
-
168
- KOpt krt_make_product(const char *type_id, int field_count, const char *const *labels, KValue const *values) {
169
- return krt_ok(krt_new_product(type_id, field_count, labels, values));
170
- }
171
-
172
- KOpt krt_make_variant(KValue child, const char *label, const char *type_id) {
173
- const char *resolved_type = type_id;
174
- if (resolved_type == NULL || resolved_type[0] == '\0') {
175
- resolved_type = krt_infer_variant_type(label, child);
176
- }
177
- return krt_ok(krt_new_variant(resolved_type, label, child));
178
- }
179
-
180
- void krt_print_value(KValue value) {
181
- int index;
182
- if (value == NULL) {
183
- printf("<null>");
184
- return;
185
- }
186
-
187
- if (value->kind == K_NODE_PRODUCT) {
188
- if (value->as.product.field_count == 0) {
189
- printf("{}");
190
- return;
191
- }
192
- printf("{");
193
- for (index = 0; index < value->as.product.field_count; index += 1) {
194
- if (index > 0) printf(", ");
195
- printf("%s:", value->as.product.fields[index].label);
196
- krt_print_value(value->as.product.fields[index].value);
197
- }
198
- printf("}");
199
- return;
200
- }
201
-
202
- printf("%s(", value->as.variant.tag);
203
- krt_print_value(value->as.variant.child);
204
- printf(")");
205
- }
@@ -1,34 +0,0 @@
1
- #ifndef K_COMPILER_RUNTIME_H
2
- #define K_COMPILER_RUNTIME_H
3
-
4
- #ifdef __cplusplus
5
- extern "C" {
6
- #endif
7
-
8
- typedef struct KNode KNode;
9
- typedef KNode *KValue;
10
-
11
- typedef struct {
12
- int ok;
13
- KValue value;
14
- } KOpt;
15
-
16
- KOpt krt_fail(void);
17
- KOpt krt_return_input(KValue input);
18
- KOpt krt_guard_code(KValue input, const char *type_id);
19
- KOpt krt_guard_type_var(KValue input, const char *var_name);
20
- KOpt krt_guard_shape(KValue input, const char *shape_descriptor_json);
21
- KOpt krt_project_field(KValue input, const char *projection_descriptor_json);
22
- KOpt krt_project_variant(KValue input, const char *projection_descriptor_json);
23
- KOpt krt_make_product(const char *type_id, int field_count, const char *const *labels, KValue const *values);
24
- KOpt krt_make_variant(KValue child, const char *label, const char *type_id);
25
-
26
- KValue krt_new_product(const char *type_id, int field_count, const char *const *labels, KValue const *values);
27
- KValue krt_new_variant(const char *type_id, const char *label, KValue child);
28
- void krt_print_value(KValue value);
29
-
30
- #ifdef __cplusplus
31
- }
32
- #endif
33
-
34
- #endif