dirac-lang 0.1.35 → 0.1.37

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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  integrate
3
- } from "./chunk-HPGONBNW.js";
3
+ } from "./chunk-MVMLG434.js";
4
4
  import {
5
5
  DiracParser
6
6
  } from "./chunk-HRHAMPOB.js";
@@ -27,7 +27,7 @@ var BraKetParser = class {
27
27
  break;
28
28
  }
29
29
  if (line.type === "bra") {
30
- const attrs = line.attrs ? ` ${this.convertAttributes(line.attrs)}` : "";
30
+ const attrs = line.attrs ? ` ${this.convertBraAttributes(line.attrs)}` : "";
31
31
  output.push(`${" ".repeat(line.indent)}<subroutine name="${line.tag}"${attrs}>`);
32
32
  this.currentLine++;
33
33
  this.parseBlock(output, line.indent);
@@ -36,7 +36,7 @@ var BraKetParser = class {
36
36
  }
37
37
  if (line.type === "ket") {
38
38
  const indent = " ".repeat(line.indent);
39
- const attrs = line.attrs ? ` ${this.convertAttributes(line.attrs)}` : "";
39
+ const attrs = line.attrs ? ` ${this.convertKetAttributes(line.attrs, line.tag || "")}` : "";
40
40
  const nextLine = this.currentLine + 1 < this.lines.length ? this.parseLine(this.lines[this.currentLine + 1]) : null;
41
41
  if (nextLine && nextLine.indent > line.indent && nextLine.type !== "empty") {
42
42
  output.push(`${indent}<${line.tag}${attrs}>`);
@@ -149,6 +149,134 @@ var BraKetParser = class {
149
149
  return `${name}="${value}"`;
150
150
  }).join(" ");
151
151
  }
152
+ /**
153
+ * Convert bra-ket attribute syntax to XML for subroutine definitions
154
+ * Automatically converts parameter attributes to param-* format
155
+ * Reserved attributes (description, extends, visible) are kept as-is
156
+ *
157
+ * Examples:
158
+ * name=String → param-name="String"
159
+ * x=number y=string → param-x="number" param-y="string"
160
+ * description=Adds → description="Adds" (reserved, no prefix)
161
+ */
162
+ convertBraAttributes(attrs) {
163
+ if (!attrs) return "";
164
+ const RESERVED = /* @__PURE__ */ new Set(["description", "extends", "visible"]);
165
+ const parts = [];
166
+ let current = "";
167
+ let inQuotes = false;
168
+ let quoteChar = "";
169
+ for (let i = 0; i < attrs.length; i++) {
170
+ const char = attrs[i];
171
+ if ((char === '"' || char === "'") && (i === 0 || attrs[i - 1] !== "\\")) {
172
+ if (!inQuotes) {
173
+ inQuotes = true;
174
+ quoteChar = char;
175
+ current += char;
176
+ } else if (char === quoteChar) {
177
+ inQuotes = false;
178
+ current += char;
179
+ } else {
180
+ current += char;
181
+ }
182
+ } else if (char === " " && !inQuotes) {
183
+ if (current.trim()) {
184
+ parts.push(current.trim());
185
+ current = "";
186
+ }
187
+ } else {
188
+ current += char;
189
+ }
190
+ }
191
+ if (current.trim()) {
192
+ parts.push(current.trim());
193
+ }
194
+ return parts.map((part) => {
195
+ const match = part.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.+)$/);
196
+ if (!match) return part;
197
+ const [, name, value] = match;
198
+ const isReserved = RESERVED.has(name);
199
+ const attrName = isReserved ? name : `param-${name}`;
200
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
201
+ return `${attrName}=${value}`;
202
+ }
203
+ return `${attrName}="${value}"`;
204
+ }).join(" ");
205
+ }
206
+ /**
207
+ * Convert ket attribute syntax to XML, supporting positional arguments
208
+ * Detects unnamed values and marks them as _positional-N for runtime resolution
209
+ *
210
+ * Examples:
211
+ * |greeting zhi> → <call name="greeting" _positional-0="zhi"/>
212
+ * |add 5 10> → <call name="add" _positional-0="5" _positional-1="10"/>
213
+ * |add x=5 y=10> → <call name="add" x="5" y="10"/> (named, no change)
214
+ * |output>Hello → <output>Hello</output> (text content, no params)
215
+ */
216
+ convertKetAttributes(attrs, tagName) {
217
+ if (!attrs) return "";
218
+ const parts = this.parseAttributeParts(attrs);
219
+ const hasPositional = parts.some((part) => !part.includes("="));
220
+ if (!hasPositional) {
221
+ return this.convertAttributes(attrs);
222
+ }
223
+ let positionalIndex = 0;
224
+ return parts.map((part) => {
225
+ const match = part.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.+)$/);
226
+ if (match) {
227
+ const [, name, value] = match;
228
+ const quotedValue = this.quoteValue(value);
229
+ return `${name}=${quotedValue}`;
230
+ } else {
231
+ const quotedValue = this.quoteValue(part);
232
+ return `_positional-${positionalIndex++}=${quotedValue}`;
233
+ }
234
+ }).join(" ");
235
+ }
236
+ /**
237
+ * Parse attribute string into parts, respecting quotes
238
+ */
239
+ parseAttributeParts(attrs) {
240
+ const parts = [];
241
+ let current = "";
242
+ let inQuotes = false;
243
+ let quoteChar = "";
244
+ for (let i = 0; i < attrs.length; i++) {
245
+ const char = attrs[i];
246
+ if ((char === '"' || char === "'") && (i === 0 || attrs[i - 1] !== "\\")) {
247
+ if (!inQuotes) {
248
+ inQuotes = true;
249
+ quoteChar = char;
250
+ current += char;
251
+ } else if (char === quoteChar) {
252
+ inQuotes = false;
253
+ current += char;
254
+ } else {
255
+ current += char;
256
+ }
257
+ } else if (char === " " && !inQuotes) {
258
+ if (current.trim()) {
259
+ parts.push(current.trim());
260
+ current = "";
261
+ }
262
+ } else {
263
+ current += char;
264
+ }
265
+ }
266
+ if (current.trim()) {
267
+ parts.push(current.trim());
268
+ }
269
+ return parts;
270
+ }
271
+ /**
272
+ * Quote a value if not already quoted
273
+ */
274
+ quoteValue(value) {
275
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
276
+ return value;
277
+ }
278
+ return `"${value}"`;
279
+ }
152
280
  /**
153
281
  * Convert inline kets within text content
154
282
  * Example: "Hello |variable name=x> world" → "Hello <variable name="x"/> world"
@@ -308,6 +308,7 @@ async function executeCallInternal(session, subroutine, callElement, isExtendExe
308
308
  for (const [key, value] of Object.entries(callElement.attributes)) {
309
309
  substitutedElement.attributes[key] = substituteAttribute(session, value);
310
310
  }
311
+ resolvePositionalArguments(substitutedElement, subroutine);
311
312
  pushParameters(session, [substitutedElement]);
312
313
  try {
313
314
  for (const [attrName, attrValue] of Object.entries(subroutine.attributes)) {
@@ -316,8 +317,8 @@ async function executeCallInternal(session, subroutine, callElement, isExtendExe
316
317
  const alreadySet = session.variables.slice(session.varBoundary).some((v) => v.name === paramName);
317
318
  if (!alreadySet) {
318
319
  let value = "";
319
- if (callElement.attributes && callElement.attributes[paramName] !== void 0) {
320
- value = substituteAttribute(session, callElement.attributes[paramName]);
320
+ if (substitutedElement.attributes && substitutedElement.attributes[paramName] !== void 0) {
321
+ value = substitutedElement.attributes[paramName];
321
322
  } else {
322
323
  const parts = attrValue.split(":");
323
324
  if (parts.length > 3) {
@@ -376,6 +377,27 @@ async function bindParameters(session, subroutine, callParams) {
376
377
  }
377
378
  }
378
379
  }
380
+ function resolvePositionalArguments(callElement, subroutine) {
381
+ const positionalAttrs = Object.keys(callElement.attributes).filter((key) => key.startsWith("_positional-")).sort();
382
+ if (positionalAttrs.length === 0) {
383
+ return;
384
+ }
385
+ const paramNames = [];
386
+ for (const [attrName] of Object.entries(subroutine.attributes)) {
387
+ if (attrName.startsWith("param-")) {
388
+ paramNames.push(attrName.substring(6));
389
+ }
390
+ }
391
+ for (let i = 0; i < positionalAttrs.length; i++) {
392
+ const positionalKey = positionalAttrs[i];
393
+ const value = callElement.attributes[positionalKey];
394
+ if (i < paramNames.length) {
395
+ callElement.attributes[paramNames[i]] = value;
396
+ } else {
397
+ }
398
+ delete callElement.attributes[positionalKey];
399
+ }
400
+ }
379
401
 
380
402
  // src/tags/loop.ts
381
403
  async function executeLoop(session, element) {
@@ -425,12 +447,12 @@ async function executeIf(session, element) {
425
447
  const condition = await evaluatePredicate(session, conditionElement);
426
448
  if (condition) {
427
449
  if (thenElement) {
428
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
450
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
429
451
  await integrateChildren2(session, thenElement);
430
452
  }
431
453
  } else {
432
454
  if (elseElement) {
433
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
455
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
434
456
  await integrateChildren2(session, elseElement);
435
457
  }
436
458
  }
@@ -443,7 +465,7 @@ async function evaluatePredicate(session, predicateElement) {
443
465
  return await evaluateCondition(session, predicateElement);
444
466
  }
445
467
  const outputLengthBefore = session.output.length;
446
- const { integrate: integrate2 } = await import("./interpreter-F65BCAYX.js");
468
+ const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
447
469
  await integrate2(session, predicateElement);
448
470
  const newOutputChunks = session.output.slice(outputLengthBefore);
449
471
  const result = newOutputChunks.join("").trim();
@@ -466,11 +488,11 @@ async function evaluateCondition(session, condElement) {
466
488
  }
467
489
  const outputLengthBefore = session.output.length;
468
490
  const args = [];
469
- const { integrate: integrate2 } = await import("./interpreter-F65BCAYX.js");
491
+ const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
470
492
  for (const child of condElement.children) {
471
493
  if (child.tag.toLowerCase() === "arg") {
472
494
  const argOutputStart = session.output.length;
473
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
495
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
474
496
  await integrateChildren2(session, child);
475
497
  const newChunks = session.output.slice(argOutputStart);
476
498
  const argValue = newChunks.join("");
@@ -1355,7 +1377,7 @@ async function executeTagCheck(session, element) {
1355
1377
  const executeTag = correctedTag || tagName;
1356
1378
  console.error(`[tag-check] Executing <${executeTag}/> as all checks passed and execute=true.`);
1357
1379
  const elementToExecute = correctedTag ? { ...child, tag: correctedTag } : child;
1358
- const { integrate: integrate2 } = await import("./interpreter-F65BCAYX.js");
1380
+ const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
1359
1381
  await integrate2(session, elementToExecute);
1360
1382
  }
1361
1383
  }
@@ -1364,7 +1386,7 @@ async function executeTagCheck(session, element) {
1364
1386
  // src/tags/throw.ts
1365
1387
  async function executeThrow(session, element) {
1366
1388
  const exceptionName = element.attributes?.name || "exception";
1367
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
1389
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1368
1390
  const exceptionDom = {
1369
1391
  tag: "exception-content",
1370
1392
  attributes: { name: exceptionName },
@@ -1377,7 +1399,7 @@ async function executeThrow(session, element) {
1377
1399
  // src/tags/try.ts
1378
1400
  async function executeTry(session, element) {
1379
1401
  setExceptionBoundary(session);
1380
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
1402
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1381
1403
  await integrateChildren2(session, element);
1382
1404
  unsetExceptionBoundary(session);
1383
1405
  }
@@ -1387,7 +1409,7 @@ async function executeCatch(session, element) {
1387
1409
  const exceptionName = element.attributes?.name || "exception";
1388
1410
  const caughtCount = lookupException(session, exceptionName);
1389
1411
  if (caughtCount > 0) {
1390
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
1412
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1391
1413
  await integrateChildren2(session, element);
1392
1414
  }
1393
1415
  flushCurrentException(session);
@@ -1396,7 +1418,7 @@ async function executeCatch(session, element) {
1396
1418
  // src/tags/exception.ts
1397
1419
  async function executeException(session, element) {
1398
1420
  const exceptions = getCurrentExceptions(session);
1399
- const { integrateChildren: integrateChildren2 } = await import("./interpreter-F65BCAYX.js");
1421
+ const { integrateChildren: integrateChildren2 } = await import("./interpreter-GLWAQHAW.js");
1400
1422
  for (const exceptionDom of exceptions) {
1401
1423
  await integrateChildren2(session, exceptionDom);
1402
1424
  }
@@ -1536,7 +1558,7 @@ async function executeForeach(session, element) {
1536
1558
  const parser2 = new DiracParser2();
1537
1559
  try {
1538
1560
  const fromElement = parser2.parse(fromAttr);
1539
- const { integrate: integrate2 } = await import("./interpreter-F65BCAYX.js");
1561
+ const { integrate: integrate2 } = await import("./interpreter-GLWAQHAW.js");
1540
1562
  await integrate2(session, fromElement);
1541
1563
  } catch (e) {
1542
1564
  session.output = savedOutput;
package/dist/cli.js CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  BraKetParser
4
- } from "./chunk-DRPHX3WX.js";
4
+ } from "./chunk-E66LTAOE.js";
5
5
  import {
6
6
  execute
7
- } from "./chunk-4LLFMVOW.js";
8
- import "./chunk-HPGONBNW.js";
7
+ } from "./chunk-4J2UJXJZ.js";
8
+ import "./chunk-MVMLG434.js";
9
9
  import "./chunk-HRHAMPOB.js";
10
10
  import "./chunk-4QLTSCDG.js";
11
11
  import "./chunk-GLXVY235.js";
@@ -16,7 +16,7 @@ import "dotenv/config";
16
16
  // package.json
17
17
  var package_default = {
18
18
  name: "dirac-lang",
19
- version: "0.1.35",
19
+ version: "0.1.37",
20
20
  description: "LLM-Augmented Declarative Execution",
21
21
  type: "module",
22
22
  main: "dist/index.js",
@@ -95,7 +95,7 @@ async function main() {
95
95
  process.exit(0);
96
96
  }
97
97
  if (args[0] === "shell") {
98
- const { DiracShell } = await import("./shell-6ANKVL77.js");
98
+ const { DiracShell } = await import("./shell-B5RBRWK2.js");
99
99
  const shellConfig = { debug: false };
100
100
  for (let i = 1; i < args.length; i++) {
101
101
  const arg = args[i];
package/dist/index.js CHANGED
@@ -2,10 +2,10 @@ import {
2
2
  createLLMAdapter,
3
3
  execute,
4
4
  executeUserCommand
5
- } from "./chunk-4LLFMVOW.js";
5
+ } from "./chunk-4J2UJXJZ.js";
6
6
  import {
7
7
  integrate
8
- } from "./chunk-HPGONBNW.js";
8
+ } from "./chunk-MVMLG434.js";
9
9
  import {
10
10
  DiracParser
11
11
  } from "./chunk-HRHAMPOB.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  integrate,
3
3
  integrateChildren
4
- } from "./chunk-HPGONBNW.js";
4
+ } from "./chunk-MVMLG434.js";
5
5
  import "./chunk-HRHAMPOB.js";
6
6
  import "./chunk-4QLTSCDG.js";
7
7
  import "./chunk-GLXVY235.js";
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  BraKetParser
4
- } from "./chunk-DRPHX3WX.js";
4
+ } from "./chunk-E66LTAOE.js";
5
5
  import {
6
6
  integrate
7
- } from "./chunk-HPGONBNW.js";
7
+ } from "./chunk-MVMLG434.js";
8
8
  import {
9
9
  DiracParser
10
10
  } from "./chunk-HRHAMPOB.js";
@@ -88,6 +88,11 @@ var DiracShell = class {
88
88
  this.rl.prompt();
89
89
  return;
90
90
  }
91
+ if (this.inputBuffer.length === 0 && !this.isDiracSyntax(input)) {
92
+ await this.executeShellCommand(input);
93
+ this.rl.prompt();
94
+ return;
95
+ }
91
96
  const indent = this.getIndent(input);
92
97
  if (this.inputBuffer.length === 0) {
93
98
  this.inputBuffer.push(input);
@@ -258,6 +263,61 @@ Examples:
258
263
  console.log(`Unknown command: ${command}. Type :help for available commands.`);
259
264
  }
260
265
  }
266
+ /**
267
+ * Check if input contains Dirac bra-ket syntax
268
+ * Must be careful not to match shell redirects (>, <, <<, >>)
269
+ */
270
+ isDiracSyntax(input) {
271
+ const trimmed = input.trim();
272
+ if (trimmed.match(/^<[a-zA-Z_][a-zA-Z0-9_-]*[^<>]*\|$/)) {
273
+ return true;
274
+ }
275
+ if (trimmed.match(/^\|[a-zA-Z_][a-zA-Z0-9_-]*[^|]*>/)) {
276
+ return true;
277
+ }
278
+ if (trimmed.match(/^<\/[a-zA-Z_][a-zA-Z0-9_-]*>$/)) {
279
+ return true;
280
+ }
281
+ if (trimmed.match(/^<(output|variable|defvar|llm|call|subroutine|if|foreach|test-if)\b/)) {
282
+ return true;
283
+ }
284
+ return false;
285
+ }
286
+ /**
287
+ * Execute a Unix shell command
288
+ */
289
+ async executeShellCommand(command) {
290
+ const trimmed = command.trim();
291
+ const cdMatch = trimmed.match(/^cd\s+(.*)$/);
292
+ if (cdMatch) {
293
+ const targetDir = cdMatch[1].trim() || process.env.HOME || "~";
294
+ try {
295
+ const expandedDir = targetDir.startsWith("~") ? targetDir.replace(/^~/, process.env.HOME || "~") : targetDir;
296
+ process.chdir(expandedDir);
297
+ } catch (err) {
298
+ console.error(`cd: ${err.message}`);
299
+ }
300
+ return;
301
+ }
302
+ const { spawn } = await import("child_process");
303
+ this.rl.pause();
304
+ return new Promise((resolve) => {
305
+ const shell = process.env.SHELL || "/bin/sh";
306
+ const child = spawn(shell, ["-c", command], {
307
+ stdio: "inherit",
308
+ cwd: process.cwd()
309
+ });
310
+ child.on("close", () => {
311
+ this.rl.resume();
312
+ resolve();
313
+ });
314
+ child.on("error", (err) => {
315
+ console.error(`Shell error: ${err.message}`);
316
+ this.rl.resume();
317
+ resolve();
318
+ });
319
+ });
320
+ }
261
321
  start() {
262
322
  console.log("Dirac Shell v0.1.0");
263
323
  console.log("Type :help for commands, :exit to quit\n");
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  integrate
3
- } from "./chunk-HPGONBNW.js";
3
+ } from "./chunk-MVMLG434.js";
4
4
  import {
5
5
  DiracParser
6
6
  } from "./chunk-HRHAMPOB.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dirac-lang",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "description": "LLM-Augmented Declarative Execution",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",