agency-lang 0.0.17 → 0.0.19

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.
@@ -112,8 +112,11 @@ export class AgencyGenerator extends BaseGenerator {
112
112
  }
113
113
  })
114
114
  .join(", ");
115
+ const returnTypeStr = node.returnType
116
+ ? ": " + variableTypeToString(node.returnType, this.typeAliases)
117
+ : "";
115
118
  // Start function definition
116
- let result = this.indentStr(`def ${functionName}(${params}) {\n`);
119
+ let result = this.indentStr(`def ${functionName}(${params})${returnTypeStr} {\n`);
117
120
  // Process body with increased indentation
118
121
  this.increaseIndent();
119
122
  if (node.docString) {
@@ -234,7 +237,10 @@ export class AgencyGenerator extends BaseGenerator {
234
237
  // Graph nodes use similar syntax to functions
235
238
  const { nodeName, body, parameters } = node;
236
239
  const params = parameters.join(", ");
237
- let result = this.indentStr(`node ${nodeName}(${params}) {\n`);
240
+ const returnTypeStr = node.returnType
241
+ ? ": " + variableTypeToString(node.returnType, this.typeAliases)
242
+ : "";
243
+ let result = this.indentStr(`node ${nodeName}(${params})${returnTypeStr} {\n`);
238
244
  this.increaseIndent();
239
245
  this.functionScopedVariables = [...parameters];
240
246
  const lines = [];
@@ -245,7 +251,7 @@ export class AgencyGenerator extends BaseGenerator {
245
251
  result += bodyCode;
246
252
  this.functionScopedVariables = [];
247
253
  this.decreaseIndent();
248
- result += this.indentStr(`}\n`);
254
+ result += this.indentStr(`}\n\n`);
249
255
  return result;
250
256
  }
251
257
  processTool(node) {
@@ -11,7 +11,7 @@ import { UsesTool } from "../types/tools.js";
11
11
  import { WhileLoop } from "../types/whileLoop.js";
12
12
  export declare class BaseGenerator {
13
13
  protected typeHints: TypeHintMap;
14
- protected graphNodes: string[];
14
+ protected graphNodes: GraphNodeDefinition[];
15
15
  protected generatedStatements: string[];
16
16
  protected generatedTypeAliases: string[];
17
17
  protected functionScopedVariables: string[];
@@ -7,6 +7,7 @@ import * as renderStartNode from "../templates/backends/graphGenerator/startNode
7
7
  import * as renderRunNodeFunction from "../templates/backends/graphGenerator/runNodeFunction.js";
8
8
  import { TypeScriptGenerator } from "./typescriptGenerator.js";
9
9
  import { mapFunctionName } from "./typescriptGenerator/builtins.js";
10
+ import { variableTypeToString } from "./typescriptGenerator/typeToString.js";
10
11
  export class GraphGenerator extends TypeScriptGenerator {
11
12
  typeHints = {};
12
13
  generatedStatements = [];
@@ -203,7 +204,7 @@ export class GraphGenerator extends TypeScriptGenerator {
203
204
  else {
204
205
  const returnCode = this.processNode(node.value);
205
206
  if (node.value.type === "functionCall" &&
206
- this.graphNodes.includes(node.value.functionName)) {
207
+ this.graphNodes.map((n) => n.nodeName).includes(node.value.functionName)) {
207
208
  // we're going to return a goToNode call, so just return that directly
208
209
  return `return ${returnCode}\n`;
209
210
  }
@@ -211,7 +212,7 @@ export class GraphGenerator extends TypeScriptGenerator {
211
212
  }
212
213
  }
213
214
  processGraphNodeName(node) {
214
- this.graphNodes.push(node.nodeName);
215
+ this.graphNodes.push(node);
215
216
  }
216
217
  processGraphNode(node) {
217
218
  const { nodeName, body, parameters } = node;
@@ -234,13 +235,16 @@ export class GraphGenerator extends TypeScriptGenerator {
234
235
  this.isInsideGraphNode = false;
235
236
  return graphNode.default({
236
237
  name: nodeName,
238
+ /* returnType: node.returnType
239
+ ? variableTypeToString(node.returnType, this.typeAliases)
240
+ : "any", */
237
241
  body: bodyCode.join("\n"),
238
242
  hasParam: parameters.length > 0,
239
243
  paramName: parameters[0] || "input",
240
244
  });
241
245
  }
242
246
  processFunctionCall(node) {
243
- if (this.graphNodes.includes(node.functionName)) {
247
+ if (this.graphNodes.map((n) => n.nodeName).includes(node.functionName)) {
244
248
  this.currentAdjacentNodes.push(node.functionName);
245
249
  this.functionsUsed.add(node.functionName);
246
250
  const functionCallCode = this.generateNodeCallExpression(node);
@@ -258,10 +262,11 @@ export class GraphGenerator extends TypeScriptGenerator {
258
262
  this.functionsUsed.add(arg.functionName);
259
263
  return this.generateFunctionCallExpression(arg);
260
264
  /* } else if (arg.type === "accessExpression") {
261
- return this.processAccessExpression(arg);
262
- } else if (arg.type === "indexAccess") {
263
- return this.processIndexAccess(arg);
264
- */ }
265
+ return this.processAccessExpression(arg);
266
+ } else if (arg.type === "indexAccess") {
267
+ return this.processIndexAccess(arg);
268
+ */
269
+ }
265
270
  else {
266
271
  return this.processNode(arg);
267
272
  // return this.generateLiteral(arg);
@@ -279,7 +284,9 @@ export class GraphGenerator extends TypeScriptGenerator {
279
284
  } */
280
285
  generateImports() {
281
286
  let arr = [
282
- renderImports.default({ nodes: JSON.stringify(this.graphNodes) }),
287
+ renderImports.default({
288
+ nodes: JSON.stringify(this.graphNodes.map((n) => n.nodeName)),
289
+ }),
283
290
  ];
284
291
  arr.push(builtinTools.default({}));
285
292
  return arr.join("\n");
@@ -299,14 +306,17 @@ export class GraphGenerator extends TypeScriptGenerator {
299
306
  toNodes: JSON.stringify(adjacent),
300
307
  }));
301
308
  });
302
- if (this.graphNodes.includes("main")) {
309
+ if (this.graphNodes.map((n) => n.nodeName).includes("main")) {
303
310
  lines.push(renderStartNode.default({
304
311
  startNode: "main",
305
312
  }));
306
313
  }
307
314
  for (const node of this.graphNodes) {
308
315
  lines.push(renderRunNodeFunction.default({
309
- nodeName: node,
316
+ nodeName: node.nodeName,
317
+ returnType: node.returnType
318
+ ? variableTypeToString(node.returnType, this.typeAliases)
319
+ : "any",
310
320
  }));
311
321
  }
312
322
  lines.push("export default graph;");
@@ -4,6 +4,10 @@ export const DEFAULT_SCHEMA = "z.string()";
4
4
  * Maps Agency types to Zod schema strings
5
5
  */
6
6
  export function mapTypeToZodSchema(variableType, typeAliases) {
7
+ console.log({ variableType, typeAliases });
8
+ if (!variableType) {
9
+ throw new Error("Variable type is undefined");
10
+ }
7
11
  if (variableType.type === "primitiveType") {
8
12
  switch (variableType.value.toLowerCase()) {
9
13
  case "number":
@@ -153,7 +153,7 @@ export class TypeScriptGenerator extends BaseGenerator {
153
153
  }
154
154
  processTool(node) {
155
155
  const { functionName, body, parameters } = node;
156
- if (this.graphNodes.includes(functionName)) {
156
+ if (this.graphNodes.map((n) => n.nodeName).includes(functionName)) {
157
157
  throw new Error(`There is already a node named '${functionName}'. Functions can't have the same name as an existing node.`);
158
158
  }
159
159
  const properties = {};
@@ -162,6 +162,7 @@ export class TypeScriptGenerator extends BaseGenerator {
162
162
  type: "primitiveType",
163
163
  value: "string",
164
164
  };
165
+ console.log({ typeHint, typeAliases: this.typeAliases });
165
166
  const tsType = mapTypeToZodSchema(typeHint, this.typeAliases);
166
167
  properties[param.name] = tsType;
167
168
  });
@@ -194,6 +195,9 @@ export class TypeScriptGenerator extends BaseGenerator {
194
195
  return renderFunctionDefinition.default({
195
196
  functionName,
196
197
  args: "{" + args + "}",
198
+ returnType: node.returnType
199
+ ? variableTypeToString(node.returnType, this.typeAliases)
200
+ : "any",
197
201
  functionBody: bodyCode.join("\n"),
198
202
  });
199
203
  }
@@ -303,6 +307,7 @@ export class TypeScriptGenerator extends BaseGenerator {
303
307
  type: "primitiveType",
304
308
  value: "string",
305
309
  };
310
+ console.log(">>>", { variableType });
306
311
  const zodSchema = mapTypeToZodSchema(variableType, this.typeAliases);
307
312
  //console.log("Generated Zod schema for variable", variableName, "Variable type:", variableType, ":", zodSchema, "aliases:", this.typeAliases, "hints:", this.typeHints);
308
313
  const typeString = variableTypeToString(variableType, this.typeAliases);
@@ -1,4 +1,4 @@
1
- import { AgencyNode, DocString, FunctionDefinition, FunctionParameter } from "../types.js";
1
+ import { AgencyNode, DocString, FunctionDefinition, FunctionParameter, VariableType } from "../types.js";
2
2
  import { Parser } from "tarsec";
3
3
  import { GraphNodeDefinition } from "../types/graphNode.js";
4
4
  import { WhileLoop } from "../types/whileLoop.js";
@@ -7,5 +7,6 @@ export declare const bodyParser: Parser<AgencyNode[]>;
7
7
  export declare const whileLoopParser: Parser<WhileLoop>;
8
8
  export declare const functionParameterParserWithTypeHint: Parser<FunctionParameter>;
9
9
  export declare const functionParameterParser: Parser<FunctionParameter>;
10
+ export declare const functionReturnTypeParser: Parser<VariableType>;
10
11
  export declare const functionParser: Parser<FunctionDefinition>;
11
12
  export declare const graphNodeParser: Parser<GraphNodeDefinition>;
@@ -1,4 +1,4 @@
1
- import { capture, char, debug, many1, many1Till, many1WithJoin, map, or, sepBy, seqC, set, space, spaces, str, succeed, trace, } from "tarsec";
1
+ import { capture, captureCaptures, char, debug, many1, many1Till, many1WithJoin, map, optional, or, sepBy, seqC, set, space, spaces, str, succeed, trace, } from "tarsec";
2
2
  import { accessExpressionParser, indexAccessParser } from "./access.js";
3
3
  import { assignmentParser } from "./assignment.js";
4
4
  import { commentParser } from "./comment.js";
@@ -21,5 +21,6 @@ export const bodyParser = trace("functionBodyParser", (input) => {
21
21
  export const whileLoopParser = trace("whileLoopParser", seqC(set("type", "whileLoop"), str("while"), optionalSpaces, char("("), optionalSpaces, capture(or(indexAccessParser, functionCallParser, accessExpressionParser, literalParser), "condition"), optionalSpaces, char(")"), optionalSpaces, char("{"), spaces, capture(bodyParser, "body"), optionalSpaces, char("}")));
22
22
  export const functionParameterParserWithTypeHint = trace("functionParameterParserWithTypeHint", seqC(set("type", "functionParameter"), capture(many1WithJoin(varNameChar), "name"), optionalSpaces, char(":"), optionalSpaces, capture(variableTypeParser, "typeHint")));
23
23
  export const functionParameterParser = trace("functionParameterParser", seqC(set("type", "functionParameter"), capture(many1WithJoin(varNameChar), "name")));
24
- export const functionParser = trace("functionParser", seqC(set("type", "function"), str("def"), many1(space), capture(many1Till(char("(")), "functionName"), char("("), optionalSpaces, capture(sepBy(comma, or(functionParameterParserWithTypeHint, functionParameterParser)), "parameters"), optionalSpaces, char(")"), optionalSpaces, char("{"), optionalSpaces, capture(or(docStringParser, succeed(undefined)), "docString"), optionalSpaces, capture(bodyParser, "body"), optionalSpaces, char("}"), optionalSemicolon));
25
- export const graphNodeParser = trace("graphNodeParser", seqC(set("type", "graphNode"), str("node"), many1(space), capture(many1Till(char("(")), "nodeName"), char("("), optionalSpaces, capture(or(sepBy(comma, many1WithJoin(varNameChar)), succeed([])), "parameters"), optionalSpaces, char(")"), optionalSpaces, char("{"), optionalSpaces, capture(bodyParser, "body"), optionalSpaces, char("}"), optionalSemicolon));
24
+ export const functionReturnTypeParser = trace("functionReturnTypeParser", seqC(char(":"), optionalSpaces, captureCaptures(variableTypeParser)));
25
+ export const functionParser = trace("functionParser", seqC(set("type", "function"), str("def"), many1(space), capture(many1Till(char("(")), "functionName"), char("("), optionalSpaces, capture(sepBy(comma, or(functionParameterParserWithTypeHint, functionParameterParser)), "parameters"), optionalSpaces, char(")"), optionalSpaces, capture(optional(functionReturnTypeParser), "returnType"), optionalSpaces, char("{"), optionalSpaces, capture(or(docStringParser, succeed(undefined)), "docString"), optionalSpaces, capture(bodyParser, "body"), optionalSpaces, char("}"), optionalSemicolon));
26
+ export const graphNodeParser = trace("graphNodeParser", seqC(set("type", "graphNode"), str("node"), many1(space), capture(many1Till(char("(")), "nodeName"), char("("), optionalSpaces, capture(or(sepBy(comma, many1WithJoin(varNameChar)), succeed([])), "parameters"), optionalSpaces, char(")"), optionalSpaces, capture(optional(functionReturnTypeParser), "returnType"), optionalSpaces, char("{"), optionalSpaces, capture(bodyParser, "body"), optionalSpaces, char("}"), optionalSemicolon));