@fraczak/k 6.0.4 → 6.1.1

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 (59) hide show
  1. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  2. package/.idea/copilot.data.migration.agent.xml +6 -0
  3. package/.idea/copilot.data.migration.ask.xml +6 -0
  4. package/.idea/copilot.data.migration.ask2agent.xml +6 -0
  5. package/.idea/copilot.data.migration.edit.xml +6 -0
  6. package/.idea/k.iml +9 -0
  7. package/.idea/misc.xml +6 -0
  8. package/.idea/modules.xml +8 -0
  9. package/.idea/vcs.xml +6 -0
  10. package/AGENTS.md +2 -2
  11. package/CHANGELOG.md +22 -5
  12. package/CLAUDE.md +4 -4
  13. package/DOCS/BIG_PICTURE.md +5 -5
  14. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +3 -3
  15. package/DOCS/OBJECT_FILE_AND_PATTERN.md +9 -9
  16. package/DOCS/PATTERNS.md +6 -5
  17. package/DOCS/REPL.md +2 -2
  18. package/DOCS/book/06-values-in-memory.md +1 -1
  19. package/DOCS/book.md +20 -24
  20. package/README.md +200 -274
  21. package/codecs/README.md +21 -2
  22. package/codecs/example-pipeline.mjs +14 -0
  23. package/codecs/ieee.mjs +13 -3
  24. package/codecs/int.mjs +13 -3
  25. package/codecs/json.mjs +13 -3
  26. package/codecs/k-parse.mjs +5 -0
  27. package/codecs/k-print.mjs +16 -1
  28. package/codecs/show.mjs +15 -1
  29. package/codecs/unit.mjs +13 -3
  30. package/codecs/utf16.mjs +13 -3
  31. package/codecs/utf8.mjs +13 -3
  32. package/k-vscode/README.md +3 -2
  33. package/k.mjs +9 -1
  34. package/k_compiler/runtime.c +205 -0
  35. package/k_compiler/runtime.h +34 -0
  36. package/objects/README.md +11 -8
  37. package/package.json +13 -7
  38. package/{repl2.mjs → repl.mjs} +47 -9
  39. package/{test-repl2.mjs → test-repl.mjs} +16 -5
  40. package/.codex +0 -0
  41. package/DOCS/a.html +0 -398
  42. package/NAT.k~ +0 -452
  43. package/UU/ta.k +0 -1
  44. package/UU/ta.k~ +0 -0
  45. package/UU/tb.k +0 -0
  46. package/UU/tc.k +0 -0
  47. package/codecs/BINARY_FORMAT.html +0 -471
  48. package/codecs/POLYMORPHIC_BINARY_FORMAT.html +0 -355
  49. package/core.json +0 -1
  50. package/core.klib +0 -1
  51. package/core.klib~ +0 -0
  52. package/ieee.klib +0 -0
  53. package/k-vscode/package-lock.json +0 -4176
  54. package/kkk.k +0 -197
  55. package/kkk.klib +0 -0
  56. package/kkk.klib~ +0 -0
  57. package/p.json +0 -5475
  58. package/type_registry/registry.mjs~ +0 -105
  59. package/type_registry/typeParser.jison~ +0 -132
package/codecs/ieee.mjs CHANGED
@@ -6,6 +6,13 @@ import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
6
6
  import { FLOAT64_PATTERN } from "./runtime/ieee-pattern.mjs";
7
7
  const UNIT = new Product({});
8
8
 
9
+ function usage(stream = console.error) {
10
+ stream(`Usage: ${argv[1]} --parse | --print`);
11
+ stream(" --parse Read a float literal from stdin, write binary pattern+value stream.");
12
+ stream(" --print Read binary pattern+value stream from stdin, write a float literal.");
13
+ stream(" -h, --help Show this help.");
14
+ }
15
+
9
16
  function bitValue(bit) {
10
17
  return new Variant(bit === 0 ? "0" : "1", UNIT);
11
18
  }
@@ -116,10 +123,13 @@ function readAll(stream) {
116
123
 
117
124
  async function main() {
118
125
  const args = argv.slice(2);
126
+ if (args.includes("-h") || args.includes("--help")) {
127
+ usage(console.log);
128
+ exit(0);
129
+ }
130
+
119
131
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
120
- console.error("Usage: ieee.mjs --parse | --print");
121
- console.error(" --parse read a float literal from stdin, write binary pattern+value stream");
122
- console.error(" --print read binary pattern+value stream from stdin, write a float literal");
132
+ usage();
123
133
  exit(1);
124
134
  }
125
135
 
package/codecs/int.mjs CHANGED
@@ -17,6 +17,13 @@ import { stdin, stdout, argv, exit } from "node:process";
17
17
  import { Product, Variant } from "../Value.mjs";
18
18
  import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
19
19
 
20
+ function usage(stream = console.error) {
21
+ stream(`Usage: ${argv[1]} --parse | --print`);
22
+ stream(" --parse Read a decimal integer from stdin, write binary pattern+value stream.");
23
+ stream(" --print Read binary pattern+value stream from stdin, write decimal integer.");
24
+ stream(" -h, --help Show this help.");
25
+ }
26
+
20
27
  // Closed pattern for $ int = < bits '+', bits '-' >
21
28
  // with $ bits = < {} _, bits 0, bits 1 >
22
29
  const INT_PATTERN = [
@@ -79,10 +86,13 @@ function printIntValue(value) {
79
86
 
80
87
  async function main() {
81
88
  const args = argv.slice(2);
89
+ if (args.includes("-h") || args.includes("--help")) {
90
+ usage(console.log);
91
+ exit(0);
92
+ }
93
+
82
94
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
83
- console.error("Usage: int.mjs --parse | --print");
84
- console.error(" --parse read a decimal integer from stdin, write binary pattern+value stream");
85
- console.error(" --print read binary pattern+value stream from stdin, write decimal integer");
95
+ usage();
86
96
  exit(1);
87
97
  }
88
98
 
package/codecs/json.mjs CHANGED
@@ -4,6 +4,13 @@ import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
5
5
  import { fromJsonValue, toJsonValue, patternFromJsonValue } from "./json-codec.mjs";
6
6
 
7
+ function usage(stream = console.error) {
8
+ stream(`Usage: ${argv[1]} --parse | --print`);
9
+ stream(" --parse Read JSON from stdin, write binary pattern+value stream.");
10
+ stream(" --print Read binary pattern+value stream from stdin, write JSON.");
11
+ stream(" -h, --help Show this help.");
12
+ }
13
+
7
14
  function readAll(stream) {
8
15
  return new Promise((resolve, reject) => {
9
16
  const chunks = [];
@@ -15,10 +22,13 @@ function readAll(stream) {
15
22
 
16
23
  async function main() {
17
24
  const args = argv.slice(2);
25
+ if (args.includes("-h") || args.includes("--help")) {
26
+ usage(console.log);
27
+ exit(0);
28
+ }
29
+
18
30
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
19
- console.error("Usage: json.mjs --parse | --print");
20
- console.error(" --parse read JSON from stdin, write binary pattern+value stream");
21
- console.error(" --print read binary pattern+value stream from stdin, write JSON");
31
+ usage();
22
32
  exit(1);
23
33
  }
24
34
 
@@ -49,6 +49,11 @@ async function main() {
49
49
  const prog = argv[1];
50
50
  const args = argv.slice(2);
51
51
 
52
+ if (args.includes("-h") || args.includes("--help")) {
53
+ usage(prog);
54
+ return exit(0);
55
+ }
56
+
52
57
  let inputTypeArg = null;
53
58
  let inputPatternArg = null;
54
59
  let valueFile = null;
@@ -4,6 +4,15 @@ import fs from "node:fs";
4
4
  import { argv, stdin, stdout } from "node:process";
5
5
  import { decodeWire } from "./runtime/prefix-codec.mjs";
6
6
 
7
+ function usage(stream = console.error) {
8
+ stream(`Usage: ${argv[1]} [--debug] [file]`);
9
+ stream(" Read a binary pattern+value stream and print the decoded textual value.");
10
+ stream("");
11
+ stream("Options:");
12
+ stream(" --debug Print decoded pattern and value as JSON.");
13
+ stream(" -h, --help Show this help.");
14
+ }
15
+
7
16
  function readAll(stream) {
8
17
  return new Promise((resolve, reject) => {
9
18
  const chunks = [];
@@ -15,6 +24,11 @@ function readAll(stream) {
15
24
 
16
25
  async function main() {
17
26
  const args = argv.slice(2);
27
+ if (args.includes("-h") || args.includes("--help")) {
28
+ usage(console.log);
29
+ return;
30
+ }
31
+
18
32
  let debug = false;
19
33
  let fileArg = null;
20
34
 
@@ -24,7 +38,8 @@ async function main() {
24
38
  } else if (fileArg == null) {
25
39
  fileArg = arg;
26
40
  } else {
27
- throw new Error("Usage: k-print [--debug] [file]");
41
+ usage();
42
+ process.exit(1);
28
43
  }
29
44
  }
30
45
 
package/codecs/show.mjs CHANGED
@@ -5,6 +5,15 @@ import { argv, stdin, stdout, stderr } from "node:process";
5
5
  import { decodeWire } from "./runtime/prefix-codec.mjs";
6
6
  import { propertyListToFilter, valueToK } from "./runtime/show-value.mjs";
7
7
 
8
+ function usage(stream = console.error) {
9
+ stream(`Usage: ${argv[1]} [wire-file]`);
10
+ stream(" Pass the wire stream through unchanged on stdout.");
11
+ stream(" Print the decoded value and filter to stderr.");
12
+ stream("");
13
+ stream("Options:");
14
+ stream(" -h, --help Show this help.");
15
+ }
16
+
8
17
  function readAll(stream) {
9
18
  return new Promise((resolve, reject) => {
10
19
  const chunks = [];
@@ -16,10 +25,15 @@ function readAll(stream) {
16
25
 
17
26
  async function main() {
18
27
  const args = argv.slice(2);
28
+ if (args.includes("-h") || args.includes("--help")) {
29
+ usage(console.log);
30
+ return;
31
+ }
32
+
19
33
  let fileArg = null;
20
34
  for (const arg of args) {
21
35
  if (fileArg == null) fileArg = arg;
22
- else { console.error("Usage: show.mjs [wire-file]"); process.exit(1); }
36
+ else { usage(); process.exit(1); }
23
37
  }
24
38
 
25
39
  const buffer = fileArg ? fs.readFileSync(fileArg) : await readAll(stdin);
package/codecs/unit.mjs CHANGED
@@ -4,6 +4,13 @@ import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { Product } from "../Value.mjs";
5
5
  import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
6
6
 
7
+ function usage(stream = console.error) {
8
+ stream(`Usage: ${argv[1]} --parse | --print`);
9
+ stream(" --parse Ignore stdin and write the unit binary pattern+value stream.");
10
+ stream(" --print Validate the unit binary pattern+value stream and write {}.");
11
+ stream(" -h, --help Show this help.");
12
+ }
13
+
7
14
  const UNIT_PATTERN = [
8
15
  ["closed-product", []]
9
16
  ];
@@ -23,10 +30,13 @@ function unitEncoding() {
23
30
 
24
31
  async function main() {
25
32
  const args = argv.slice(2);
33
+ if (args.includes("-h") || args.includes("--help")) {
34
+ usage(console.log);
35
+ exit(0);
36
+ }
37
+
26
38
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
27
- console.error("Usage: unit.mjs --parse | --print");
28
- console.error(" --parse ignore stdin and write the unit binary pattern+value stream");
29
- console.error(" --print validate the unit binary pattern+value stream and write {}");
39
+ usage();
30
40
  exit(1);
31
41
  }
32
42
 
package/codecs/utf16.mjs CHANGED
@@ -3,6 +3,13 @@
3
3
  import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { encodeText, decodeText } from "./string-codec.mjs";
5
5
 
6
+ function usage(stream = console.error) {
7
+ stream(`Usage: ${argv[1]} --parse | --print`);
8
+ stream(" --parse Read UTF-16 text from stdin (BOM-aware), write binary pattern+value stream of k string.");
9
+ stream(" --print Read binary pattern+value stream of k string, write UTF-16LE text with BOM.");
10
+ stream(" -h, --help Show this help.");
11
+ }
12
+
6
13
  function readAll(stream) {
7
14
  return new Promise((resolve, reject) => {
8
15
  const chunks = [];
@@ -43,10 +50,13 @@ function encodeUtf16Output(text) {
43
50
 
44
51
  async function main() {
45
52
  const args = argv.slice(2);
53
+ if (args.includes("-h") || args.includes("--help")) {
54
+ usage(console.log);
55
+ exit(0);
56
+ }
57
+
46
58
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
47
- console.error("Usage: utf16.mjs --parse | --print");
48
- console.error(" --parse read UTF-16 text from stdin (BOM-aware), write binary pattern+value stream of k string");
49
- console.error(" --print read binary pattern+value stream of k string, write UTF-16LE text with BOM");
59
+ usage();
50
60
  exit(1);
51
61
  }
52
62
 
package/codecs/utf8.mjs CHANGED
@@ -3,6 +3,13 @@
3
3
  import { stdin, stdout, argv, exit } from "node:process";
4
4
  import { encodeText, decodeText } from "./string-codec.mjs";
5
5
 
6
+ function usage(stream = console.error) {
7
+ stream(`Usage: ${argv[1]} --parse | --print`);
8
+ stream(" --parse Read UTF-8 text from stdin, write binary pattern+value stream of k string.");
9
+ stream(" --print Read binary pattern+value stream of k string, write UTF-8 text.");
10
+ stream(" -h, --help Show this help.");
11
+ }
12
+
6
13
  function readAll(stream) {
7
14
  return new Promise((resolve, reject) => {
8
15
  const chunks = [];
@@ -14,10 +21,13 @@ function readAll(stream) {
14
21
 
15
22
  async function main() {
16
23
  const args = argv.slice(2);
24
+ if (args.includes("-h") || args.includes("--help")) {
25
+ usage(console.log);
26
+ exit(0);
27
+ }
28
+
17
29
  if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
18
- console.error("Usage: utf8.mjs --parse | --print");
19
- console.error(" --parse read UTF-8 text from stdin, write binary pattern+value stream of k string");
20
- console.error(" --print read binary pattern+value stream of k string, write UTF-8 text");
30
+ usage();
21
31
  exit(1);
22
32
  }
23
33
 
@@ -1,7 +1,8 @@
1
1
  # k language — VS Code extension
2
2
 
3
- Syntax highlighting and snippets for the **k language**: a language for composing
4
- first-order partial functions over algebraic data types (products and tagged unions).
3
+ Syntax highlighting and snippets for the **k language**: a small language for
4
+ typed data transformations over algebraic data types (products and tagged
5
+ unions).
5
6
 
6
7
  ## Features
7
8
 
package/k.mjs CHANGED
@@ -13,7 +13,10 @@ let kScript, inputStream;
13
13
 
14
14
  function usage() {
15
15
  console.error(`Usage: ${prog} [ --lib lib-file ]... ( k-expr | -k file ) [ input-file ]`);
16
- console.error(` E.g., echo '["zebara","ela"]' | ./codecs/k-parse.mjs --input-type '$x=<{} zebara, {} ela>; $v={x 0, x 1}; $v' | ${prog} '{.1 0}'`);
16
+ console.error(` E.g., echo '["zebara","ela"]' | k-parse --input-type '$x=<{} zebara, {} ela>; $v={x 0, x 1}; $v' | ${prog} '{.1 0}'`);
17
+ console.error("Options:");
18
+ console.error(" --lib file Load a .klib dependency before compiling. May be repeated.");
19
+ console.error(" -h, --help Show this help.");
17
20
  }
18
21
 
19
22
  function compileFile(path, libraries) {
@@ -27,6 +30,11 @@ function compileFile(path, libraries) {
27
30
 
28
31
  ({ kScript, inputStream } = ((args) => {
29
32
  try {
33
+ if (args.includes("-h") || args.includes("--help")) {
34
+ usage();
35
+ return exit(0);
36
+ }
37
+
30
38
  const libraries = [];
31
39
  // Parse --lib flags
32
40
  while (args.length > 0 && args[0] === "--lib") {
@@ -0,0 +1,205 @@
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
+ }
@@ -0,0 +1,34 @@
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
package/objects/README.md CHANGED
@@ -4,17 +4,17 @@ CLI tools for compiling and inspecting k object files (`.ko`) and library files
4
4
 
5
5
  ## Tools
6
6
 
7
- ### compile.mjs
7
+ ### `k-compile`
8
8
 
9
9
  Compiles a `.k` source file into an executable object file (`.ko`).
10
10
  The output contains only the codes and relations reachable from `main`.
11
11
  Each stored relation includes `typeDerivation.status` for backend eligibility.
12
12
 
13
13
  ```bash
14
- ./objects/compile.mjs [--lib lib-file]... [k-file [object-file]]
14
+ k-compile [--lib lib-file]... [k-file [object-file]]
15
15
  ```
16
16
 
17
- ### compile-lib.mjs
17
+ ### `k-compile-lib`
18
18
 
19
19
  Compiles a `.k` source file into a library file (`.klib`).
20
20
  The output is plain JSON with no binary header. It contains the library closure
@@ -25,27 +25,30 @@ Each stored relation includes `typeDerivation.status`; libraries may contain a
25
25
  mix of converged and non-converged relations.
26
26
 
27
27
  ```bash
28
- ./objects/compile-lib.mjs [--lib lib-file]... [k-file [lib-file]]
28
+ k-compile-lib [--lib lib-file]... [k-file [lib-file]]
29
29
  ```
30
30
 
31
- ### decompile.mjs
31
+ ### `k-decompile`
32
32
 
33
33
  Decompiles a `.ko` or `.klib` back into human-readable k source.
34
34
 
35
35
  ```bash
36
- ./objects/decompile.mjs [object-file [k-file]]
36
+ k-decompile [object-file [k-file]]
37
37
  ```
38
38
 
39
- ### extract-aliases.mjs
39
+ ### `k-extract-aliases`
40
40
 
41
41
  Extracts metadata aliases from a `.ko` or `.klib` as a valid k definition
42
42
  snippet. Output is grouped by metadata type (`code`, then `rel`), then sorted by
43
43
  alias name and `compiledAt`.
44
44
 
45
45
  ```bash
46
- ./objects/extract-aliases.mjs [object-file [k-file]]
46
+ k-extract-aliases [object-file [k-file]]
47
47
  ```
48
48
 
49
+ The installed names are `k-` plus the source basename without `.mjs`. Every
50
+ installed object binary supports `-h` and `--help`.
51
+
49
52
  ## Current Format Notes
50
53
 
51
54
  - `.klib` files are plain UTF-8 JSON, not binary containers.
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "6.0.4",
3
+ "version": "6.1.1",
4
4
  "description": "k — a language for composing first-order partial functions over algebraic data types (products and tagged unions); includes runtime, type derivation, REPL, and codec toolchain",
5
5
  "main": "index.mjs",
6
6
  "scripts": {
7
7
  "prepare": "jiwson -m es6 parser.jison -o parser.mjs; jiwson -m es6 valueParser.jison -o valueParser.mjs; ",
8
- "test": "node test.mjs; for f in Code-derivation-tests/*.mjs; do echo $f; node $f; done; node test-fingerprint.mjs; node test-hash-fuzz.mjs; node test-hash-normalization.mjs; node test-k-object.mjs; node test-repl2.mjs; ./tests.sh",
8
+ "test": "node test.mjs; for f in Code-derivation-tests/*.mjs; do echo $f; node $f; done; node test-fingerprint.mjs; node test-hash-fuzz.mjs; node test-hash-normalization.mjs; node test-k-object.mjs; node test-repl.mjs; ./tests.sh",
9
9
  "deps:graph": "node scripts/generate-dependency-graph.mjs"
10
10
  },
11
11
  "repository": {
@@ -16,14 +16,20 @@
16
16
  "license": "ISC",
17
17
  "bin": {
18
18
  "k": "./k.mjs",
19
- "k-compile-object": "./objects/compile.mjs",
20
- "k-decompile-object": "./objects/decompile.mjs",
19
+ "k-compile": "./objects/compile.mjs",
20
+ "k-compile-lib": "./objects/compile-lib.mjs",
21
+ "k-decompile": "./objects/decompile.mjs",
21
22
  "k-extract-aliases": "./objects/extract-aliases.mjs",
23
+ "k-ieee": "./codecs/ieee.mjs",
24
+ "k-int": "./codecs/int.mjs",
25
+ "k-json": "./codecs/json.mjs",
22
26
  "k-parse": "./codecs/k-parse.mjs",
23
- "k-pattern": "./patterns/from-k.mjs",
24
27
  "k-print": "./codecs/k-print.mjs",
25
- "k-repl": "./repl2.mjs",
26
- "k-repl2": "./repl2.mjs"
28
+ "k-repl": "./repl.mjs",
29
+ "k-show": "./codecs/show.mjs",
30
+ "k-unit": "./codecs/unit.mjs",
31
+ "k-utf16": "./codecs/utf16.mjs",
32
+ "k-utf8": "./codecs/utf8.mjs"
27
33
  },
28
34
  "devDependencies": {
29
35
  "jiwson": "^0.1.2"