agency-lang 0.0.5 → 0.0.6

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.
@@ -43,5 +43,6 @@ export declare class AgencyGenerator extends BaseGenerator {
43
43
  protected processGraphNode(node: GraphNodeDefinition): string;
44
44
  protected processTool(node: FunctionDefinition): string;
45
45
  protected processUsesTool(node: UsesTool): string;
46
+ private indentStr;
46
47
  }
47
48
  export declare function generateAgency(program: AgencyProgram): string;
@@ -32,17 +32,17 @@ export class AgencyGenerator extends BaseGenerator {
32
32
  processTypeAlias(node) {
33
33
  this.typeAliases[node.aliasName] = node.aliasedType;
34
34
  const aliasedTypeStr = variableTypeToString(node.aliasedType, this.typeAliases);
35
- return `${this.indent()}type ${node.aliasName} = ${aliasedTypeStr}\n`;
35
+ return this.indentStr(`type ${node.aliasName} = ${aliasedTypeStr}\n`);
36
36
  }
37
37
  processTypeHint(node) {
38
38
  this.typeHints[node.variableName] = node.variableType;
39
39
  const typeStr = variableTypeToString(node.variableType, this.typeAliases);
40
- return `${this.indent()}${node.variableName} :: ${typeStr}\n`;
40
+ return this.indentStr(`${node.variableName} :: ${typeStr}\n`);
41
41
  }
42
42
  // Assignment and literals
43
43
  processAssignment(node) {
44
44
  const valueCode = this.processNode(node.value).trim();
45
- return `${this.indent()}${node.variableName} = ${valueCode}\n`;
45
+ return this.indentStr(`${node.variableName} = ${valueCode}\n\n`);
46
46
  }
47
47
  generateLiteral(literal) {
48
48
  switch (literal.type) {
@@ -86,7 +86,7 @@ export class AgencyGenerator extends BaseGenerator {
86
86
  // Build parameter list
87
87
  const params = parameters.join(", ");
88
88
  // Start function definition
89
- let result = `${this.indent()}def ${functionName}(${params}) {\n`;
89
+ let result = this.indentStr(`def ${functionName}(${params}) {\n`);
90
90
  // Process body with increased indentation
91
91
  this.increaseIndent();
92
92
  this.functionScopedVariables = [...parameters];
@@ -96,12 +96,12 @@ export class AgencyGenerator extends BaseGenerator {
96
96
  this.functionScopedVariables = [];
97
97
  this.decreaseIndent();
98
98
  // Close function
99
- result += `${this.indent()}}\n`;
99
+ result += this.indentStr(`}\n\n`);
100
100
  return result;
101
101
  }
102
102
  processFunctionCall(node) {
103
103
  const expr = this.generateFunctionCallExpression(node);
104
- return `${this.indent()}${expr}\n`;
104
+ return this.indentStr(`${expr}\n`);
105
105
  }
106
106
  generateFunctionCallExpression(node) {
107
107
  const args = node.arguments.map((arg) => {
@@ -151,7 +151,7 @@ export class AgencyGenerator extends BaseGenerator {
151
151
  // Control flow
152
152
  processMatchBlock(node) {
153
153
  const exprCode = this.processNode(node.expression).trim();
154
- let result = `${this.indent()}match(${exprCode}) {\n`;
154
+ let result = this.indentStr(`match(${exprCode}) {\n`);
155
155
  this.increaseIndent();
156
156
  for (const caseNode of node.cases) {
157
157
  // Handle comments within cases
@@ -165,39 +165,39 @@ export class AgencyGenerator extends BaseGenerator {
165
165
  : this.processNode(caseNode.caseValue).trim();
166
166
  // Format body (action)
167
167
  const bodyCode = this.processNode(caseNode.body).trim();
168
- result += `${this.indent()}${pattern} => ${bodyCode}\n`;
168
+ result += this.indentStr(`${pattern} => ${bodyCode}\n`);
169
169
  }
170
170
  this.decreaseIndent();
171
- result += `${this.indent()}}\n`;
171
+ result += this.indentStr(`}\n`);
172
172
  return result;
173
173
  }
174
174
  processWhileLoop(node) {
175
175
  const conditionCode = this.processNode(node.condition).trim();
176
- let result = `${this.indent()}while (${conditionCode}) {\n`;
176
+ let result = this.indentStr(`while (${conditionCode}) {\n`);
177
177
  this.increaseIndent();
178
178
  for (const stmt of node.body) {
179
179
  result += this.processNode(stmt);
180
180
  }
181
181
  this.decreaseIndent();
182
- result += `${this.indent()}}\n`;
182
+ result += this.indentStr(`}\n`);
183
183
  return result;
184
184
  }
185
185
  processReturnStatement(node) {
186
186
  const valueCode = this.processNode(node.value).trim();
187
- return `${this.indent()}return ${valueCode}\n`;
187
+ return this.indentStr(`return ${valueCode}\n`);
188
188
  }
189
189
  // Utility methods
190
190
  processComment(node) {
191
- return `${this.indent()}// ${node.content}\n`;
191
+ return this.indentStr(`// ${node.content}\n`);
192
192
  }
193
193
  processImportStatement(node) {
194
- return `${this.indent()}import {${node.importedNames}} from "${node.modulePath}"\n`;
194
+ return this.indentStr(`import {${node.importedNames}} from "${node.modulePath}"\n`);
195
195
  }
196
196
  processGraphNode(node) {
197
197
  // Graph nodes use similar syntax to functions
198
198
  const { nodeName, body, parameters } = node;
199
199
  const params = parameters.join(", ");
200
- let result = `${this.indent()}node ${nodeName}(${params}) {\n`;
200
+ let result = this.indentStr(`node ${nodeName}(${params}) {\n`);
201
201
  this.increaseIndent();
202
202
  this.functionScopedVariables = [...parameters];
203
203
  for (const stmt of body) {
@@ -205,7 +205,7 @@ export class AgencyGenerator extends BaseGenerator {
205
205
  }
206
206
  this.functionScopedVariables = [];
207
207
  this.decreaseIndent();
208
- result += `${this.indent()}}\n`;
208
+ result += this.indentStr(`}\n`);
209
209
  return result;
210
210
  }
211
211
  processTool(node) {
@@ -216,7 +216,10 @@ export class AgencyGenerator extends BaseGenerator {
216
216
  processUsesTool(node) {
217
217
  // Track tool usage but don't generate code
218
218
  this.toolsUsed.push(node.toolName);
219
- return "";
219
+ return this.indentStr(`+${node.toolName}\n`);
220
+ }
221
+ indentStr(str) {
222
+ return `${this.indent()}${str}`;
220
223
  }
221
224
  }
222
225
  export function generateAgency(program) {
@@ -4,4 +4,4 @@ export declare const agencyNode: Parser<AgencyNode[]>;
4
4
  export declare const agencyParser: Parser<AgencyProgram>;
5
5
  export declare const _multilineCommentParser: Parser<string[]>;
6
6
  export declare const multilineCommentParser: Parser<string[][]>;
7
- export declare function parseAgency(input: string): ParserResult<AgencyProgram>;
7
+ export declare function parseAgency(input: string, verbose?: boolean): ParserResult<AgencyProgram>;
@@ -9,6 +9,7 @@ import { importStatmentParser } from "./parsers/importStatement.js";
9
9
  import { matchBlockParser } from "./parsers/matchBlock.js";
10
10
  import { returnStatementParser } from "./parsers/returnStatement.js";
11
11
  import { usesToolParser } from "./parsers/tools.js";
12
+ import { EgonLog } from "egonlog";
12
13
  export const agencyNode = (input) => {
13
14
  const parser = sepBy(spaces, trace("agencyParser", or(usesToolParser, importStatmentParser, graphNodeParser, typeAliasParser, whileLoopParser, typeHintParser, matchBlockParser, functionParser, returnStatementParser, accessExpressionParser, assignmentParser, functionCallParser, commentParser)));
14
15
  return parser(input);
@@ -16,9 +17,15 @@ export const agencyNode = (input) => {
16
17
  export const agencyParser = seqC(set("type", "agencyProgram"), capture(agencyNode, "nodes"), eof);
17
18
  export const _multilineCommentParser = between(str("/*"), str("*/"), anyChar);
18
19
  export const multilineCommentParser = search(_multilineCommentParser);
19
- export function parseAgency(input) {
20
+ export function parseAgency(input, verbose = false) {
21
+ const logger = new EgonLog({ level: verbose ? "debug" : "warn" });
20
22
  let normalized = input;
23
+ logger.debug("Starting to parse agency program");
24
+ logger.debug(`Input: ${input}`);
25
+ logger.debug("================================");
21
26
  const comments = multilineCommentParser(normalized);
27
+ logger.debug(`Multiline comments: ${JSON.stringify(comments)}`);
28
+ logger.debug("================================");
22
29
  // get rid of all multiline comments
23
30
  normalized = comments.rest
24
31
  .split("\n")
@@ -33,6 +40,8 @@ export function parseAgency(input) {
33
40
  nodes: [],
34
41
  }, "");
35
42
  }
43
+ logger.debug(`Normalized input: ${normalized}`);
44
+ logger.debug("================================");
36
45
  const result = agencyParser(normalized);
37
46
  return result;
38
47
  }
@@ -44,7 +44,7 @@ describe("importStatmentParser", () => {
44
44
  result: {
45
45
  type: "importStatement",
46
46
  importedNames: "foo ",
47
- modulePath: '"./local/path"',
47
+ modulePath: '"./local/path.js"',
48
48
  },
49
49
  },
50
50
  },
@@ -55,7 +55,7 @@ describe("importStatmentParser", () => {
55
55
  result: {
56
56
  type: "importStatement",
57
57
  importedNames: "bar ",
58
- modulePath: '"@/utils/helper"',
58
+ modulePath: '"../utils/helper.js"',
59
59
  },
60
60
  },
61
61
  },
@@ -89,7 +89,7 @@ describe("importStatmentParser", () => {
89
89
  result: {
90
90
  type: "importStatement",
91
91
  importedNames: "test ",
92
- modulePath: "'./module'",
92
+ modulePath: "'./module.js'",
93
93
  },
94
94
  },
95
95
  },
@@ -240,7 +240,7 @@ describe("importStatmentParser", () => {
240
240
  result: {
241
241
  type: "importStatement",
242
242
  importedNames: "foo ",
243
- modulePath: '"../../../utils/helpers"',
243
+ modulePath: '"../../../utils/helpers.js"',
244
244
  },
245
245
  },
246
246
  },
@@ -7,7 +7,7 @@ export declare const angleBracketsArrayTypeParser: Parser<ArrayType>;
7
7
  export declare const stringLiteralTypeParser: Parser<StringLiteralType>;
8
8
  export declare const numberLiteralTypeParser: Parser<NumberLiteralType>;
9
9
  export declare const booleanLiteralTypeParser: Parser<BooleanLiteralType>;
10
- export declare const objectPropertyDelimiter: Parser<(string[] | ";")[]>;
10
+ export declare const objectPropertyDelimiter: Parser<"\n" | (string | string[])[]>;
11
11
  export declare const objectPropertyParser: Parser<ObjectProperty>;
12
12
  export declare const objectPropertyDescriptionParser: Parser<{
13
13
  description: string;
@@ -1,5 +1,5 @@
1
1
  import { optionalSpaces, varNameChar } from "./utils.js";
2
- import { capture, captureCaptures, char, count, digit, many1Till, many1WithJoin, or, sepBy, seqC, seqR, set, space, spaces, str, trace, } from "tarsec";
2
+ import { capture, captureCaptures, char, count, digit, many1Till, many1WithJoin, oneOf, or, sepBy, seqC, seqR, set, space, spaces, str, trace, } from "tarsec";
3
3
  import { optionalSemicolon } from "./parserUtils.js";
4
4
  export const primitiveTypeParser = trace("primitiveTypeParser", seqC(set("type", "primitiveType"), capture(or(str("number"), str("string"), str("boolean")), "value")));
5
5
  export const typeAliasVariableParser = trace("typeAliasVariableParser", seqC(set("type", "typeAliasVariable"), capture(many1WithJoin(varNameChar), "aliasName")));
@@ -27,12 +27,12 @@ export const angleBracketsArrayTypeParser = trace("angleBracketsArrayTypeParser"
27
27
  export const stringLiteralTypeParser = trace("stringLiteralTypeParser", seqC(set("type", "stringLiteralType"), char('"'), capture(many1Till(char('"')), "value"), char('"')));
28
28
  export const numberLiteralTypeParser = trace("numberLiteralTypeParser", seqC(set("type", "numberLiteralType"), capture(many1WithJoin(or(char("-"), char("."), digit)), "value")));
29
29
  export const booleanLiteralTypeParser = trace("booleanLiteralTypeParser", seqC(set("type", "booleanLiteralType"), capture(or(str("true"), str("false")), "value")));
30
- export const objectPropertyDelimiter = seqR(optionalSpaces, char(";"), optionalSpaces);
30
+ export const objectPropertyDelimiter = or(char("\n"), seqR(optionalSpaces, oneOf(",;"), optionalSpaces));
31
31
  export const objectPropertyParser = trace("objectPropertyParser", (input) => {
32
32
  const parser = seqC(capture(many1WithJoin(varNameChar), "key"), optionalSpaces, char(":"), optionalSpaces, capture(variableTypeParser, "value"));
33
33
  return parser(input);
34
34
  });
35
- export const objectPropertyDescriptionParser = seqC(char("#"), optionalSpaces, capture(many1Till(char(";")), "description"));
35
+ export const objectPropertyDescriptionParser = seqC(char("#"), optionalSpaces, capture(many1Till(oneOf(",;\n")), "description"));
36
36
  export const objectPropertyWithDescriptionParser = trace("objectPropertyWithDescriptionParser", seqC(captureCaptures(objectPropertyParser), spaces, captureCaptures(objectPropertyDescriptionParser)));
37
37
  export const objectTypeParser = trace("objectTypeParser", (input) => {
38
38
  const parser = seqC(set("type", "objectType"), char("{"), optionalSpaces, capture(sepBy(objectPropertyDelimiter, or(objectPropertyWithDescriptionParser, objectPropertyParser)), "properties"), optionalSpaces, char("}"));
@@ -1012,6 +1012,24 @@ describe("objectPropertyDescriptionParser", () => {
1012
1012
  },
1013
1013
  },
1014
1014
  },
1015
+ {
1016
+ input: "# comma,",
1017
+ expected: {
1018
+ success: true,
1019
+ result: {
1020
+ description: "comma",
1021
+ },
1022
+ },
1023
+ },
1024
+ {
1025
+ input: "# newline",
1026
+ expected: {
1027
+ success: true,
1028
+ result: {
1029
+ description: "newline",
1030
+ },
1031
+ },
1032
+ },
1015
1033
  {
1016
1034
  input: "# with extra spaces ;",
1017
1035
  expected: {
@@ -1022,11 +1040,11 @@ describe("objectPropertyDescriptionParser", () => {
1022
1040
  },
1023
1041
  },
1024
1042
  {
1025
- input: "# description with, punctuation!;",
1043
+ input: "# description with punctuation!;",
1026
1044
  expected: {
1027
1045
  success: true,
1028
1046
  result: {
1029
- description: "description with, punctuation!",
1047
+ description: "description with punctuation!",
1030
1048
  },
1031
1049
  },
1032
1050
  },
@@ -13,6 +13,7 @@ Usage:
13
13
  agency compile <input> [output] Compile .agency file to TypeScript
14
14
  agency run <input> [output] Compile and run .agency file
15
15
  agency format <input> Format .agency file in place
16
+ agency parse <input> Parse .agency file and show AST
16
17
  agency <input> Compile and run .agency file (shorthand)
17
18
 
18
19
  Arguments:
@@ -20,32 +21,39 @@ Arguments:
20
21
  output Path to output .ts file (optional)
21
22
  Default: <input-name>.ts
22
23
 
24
+ Flags:
25
+ -v, --verbose Enable verbose logging during parsing
26
+
23
27
  Examples:
24
28
  agency help Show help
25
29
  agency compile script.agency Compile to script.ts
26
30
  agency compile script.agency out.ts Compile to out.ts
27
31
  agency run script.agency Compile and run script.agency
32
+ agency -v parse script.agency Parse with verbose logging
28
33
  agency script.agency Compile and run (shorthand)
29
34
  `);
30
35
  }
31
- function compile(inputFile, outputFile) {
36
+ function parse(inputFile, verbose = false) {
32
37
  // Validate input file
33
38
  if (!fs.existsSync(inputFile)) {
34
39
  console.error(`Error: Input file '${inputFile}' not found`);
35
40
  process.exit(1);
36
41
  }
37
- // Determine output file name
38
- const output = outputFile || inputFile.replace(".agency", ".ts");
39
42
  // Read and parse the Agency file
40
43
  const contents = fs.readFileSync(inputFile, "utf-8");
41
- const parseResult = parseAgency(contents);
44
+ const parseResult = parseAgency(contents, verbose);
42
45
  // Check if parsing was successful
43
46
  if (!parseResult.success) {
44
47
  console.error("Parse error:");
45
48
  console.error(parseResult);
46
49
  process.exit(1);
47
50
  }
48
- const parsedProgram = parseResult.result;
51
+ return parseResult.result;
52
+ }
53
+ function compile(inputFile, outputFile, verbose = false) {
54
+ // Determine output file name
55
+ const output = outputFile || inputFile.replace(".agency", ".ts");
56
+ const parsedProgram = parse(inputFile, verbose);
49
57
  // Generate TypeScript code
50
58
  const generatedCode = generateGraph(parsedProgram);
51
59
  // Write to output file
@@ -53,9 +61,9 @@ function compile(inputFile, outputFile) {
53
61
  console.log(`Generated ${output} from ${inputFile}`);
54
62
  return output;
55
63
  }
56
- function run(inputFile, outputFile) {
64
+ function run(inputFile, outputFile, verbose = false) {
57
65
  // Compile the file
58
- const output = compile(inputFile, outputFile);
66
+ const output = compile(inputFile, outputFile, verbose);
59
67
  // Run the generated TypeScript file with Node.js
60
68
  console.log(`Running ${output}...`);
61
69
  console.log("---");
@@ -73,22 +81,8 @@ function run(inputFile, outputFile) {
73
81
  }
74
82
  });
75
83
  }
76
- function format(inputFile) {
77
- // Validate input file
78
- if (!fs.existsSync(inputFile)) {
79
- console.error(`Error: Input file '${inputFile}' not found`);
80
- process.exit(1);
81
- }
82
- // Read and parse the Agency file
83
- const contents = fs.readFileSync(inputFile, "utf-8");
84
- const parseResult = parseAgency(contents);
85
- // Check if parsing was successful
86
- if (!parseResult.success) {
87
- console.error("Parse error:");
88
- console.error(parseResult);
89
- process.exit(1);
90
- }
91
- const parsedProgram = parseResult.result;
84
+ function format(inputFile, verbose = false) {
85
+ const parsedProgram = parse(inputFile, verbose);
92
86
  // Generate TypeScript code
93
87
  const generatedCode = generateAgency(parsedProgram);
94
88
  // Write to output file
@@ -105,7 +99,12 @@ function main() {
105
99
  help();
106
100
  return;
107
101
  }
108
- const command = args[0];
102
+ // Extract verbose flag
103
+ const verboseIndex = args.findIndex((arg) => arg === "-v" || arg === "--verbose");
104
+ const verbose = verboseIndex !== -1;
105
+ // Remove verbose flag from args
106
+ const filteredArgs = args.filter((arg) => arg !== "-v" && arg !== "--verbose");
107
+ const command = filteredArgs[0];
109
108
  switch (command) {
110
109
  case "help":
111
110
  case "--help":
@@ -113,34 +112,43 @@ function main() {
113
112
  help();
114
113
  break;
115
114
  case "compile":
116
- if (args.length < 2) {
115
+ if (filteredArgs.length < 2) {
117
116
  console.error("Error: 'compile' command requires an input file");
118
117
  console.error("Usage: agency compile <input> [output]");
119
118
  process.exit(1);
120
119
  }
121
- compile(args[1], args[2]);
120
+ compile(filteredArgs[1], filteredArgs[2], verbose);
122
121
  break;
123
122
  case "run":
124
- if (args.length < 2) {
123
+ if (filteredArgs.length < 2) {
125
124
  console.error("Error: 'run' command requires an input file");
126
125
  console.error("Usage: agency run <input> [output]");
127
126
  process.exit(1);
128
127
  }
129
- run(args[1], args[2]);
128
+ run(filteredArgs[1], filteredArgs[2], verbose);
130
129
  break;
131
130
  case "fmt":
132
131
  case "format":
133
- if (args.length < 1) {
132
+ if (filteredArgs.length < 1) {
134
133
  console.error("Error: 'format' command requires an input file");
135
134
  console.error("Usage: agency format <input>");
136
135
  process.exit(1);
137
136
  }
138
- format(args[1]);
137
+ format(filteredArgs[1], verbose);
138
+ break;
139
+ case "parse":
140
+ if (filteredArgs.length < 1) {
141
+ console.error("Error: 'parse' command requires an input file");
142
+ console.error("Usage: agency parse <input>");
143
+ process.exit(1);
144
+ }
145
+ const result = parse(filteredArgs[1], verbose);
146
+ console.log(JSON.stringify(result, null, 2));
139
147
  break;
140
148
  default:
141
149
  // If first arg is not a recognized command, treat it as a file to run
142
150
  if (command.endsWith(".agency") || fs.existsSync(command)) {
143
- run(command, args[1]);
151
+ run(command, filteredArgs[1], verbose);
144
152
  }
145
153
  else {
146
154
  console.error(`Error: Unknown command '${command}'`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agency-lang",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "The Agency language",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -39,6 +39,7 @@
39
39
  },
40
40
  "homepage": "https://github.com/egonSchiele/agency-lang#readme",
41
41
  "dependencies": {
42
+ "egonlog": "^0.0.2",
42
43
  "nanoid": "^5.1.6",
43
44
  "openai": "^6.15.0",
44
45
  "simplemachine": "github:egonSchiele/simplemachine",