agency-lang 0.0.15 → 0.0.16

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 (38) hide show
  1. package/dist/lib/backends/agencyGenerator.js +20 -3
  2. package/dist/lib/backends/agencyGenerator.test.d.ts +1 -0
  3. package/dist/lib/backends/agencyGenerator.test.js +205 -0
  4. package/dist/lib/backends/baseGenerator.js +3 -1
  5. package/dist/lib/backends/graphGenerator.js +7 -5
  6. package/dist/lib/backends/typescriptGenerator.d.ts +3 -3
  7. package/dist/lib/backends/typescriptGenerator.js +14 -12
  8. package/dist/lib/parsers/access.d.ts +3 -5
  9. package/dist/lib/parsers/access.js +54 -13
  10. package/dist/lib/parsers/access.test.js +40 -500
  11. package/dist/lib/parsers/assignment.js +2 -2
  12. package/dist/lib/parsers/assignment.test.d.ts +1 -0
  13. package/dist/lib/parsers/assignment.test.js +279 -0
  14. package/dist/lib/parsers/dataStructures.js +3 -3
  15. package/dist/lib/parsers/function.d.ts +3 -1
  16. package/dist/lib/parsers/function.js +6 -4
  17. package/dist/lib/parsers/function.test.js +653 -8
  18. package/dist/lib/parsers/functionCall.js +3 -2
  19. package/dist/lib/parsers/functionCall.test.js +310 -0
  20. package/dist/lib/parsers/literals.js +11 -2
  21. package/dist/lib/parsers/literals.test.js +7 -7
  22. package/dist/lib/parsers/matchBlock.js +2 -2
  23. package/dist/lib/parsers/parserUtils.test.d.ts +1 -0
  24. package/dist/lib/parsers/parserUtils.test.js +46 -0
  25. package/dist/lib/parsers/returnStatement.js +2 -2
  26. package/dist/lib/parsers/returnStatement.test.d.ts +1 -0
  27. package/dist/lib/parsers/returnStatement.test.js +268 -0
  28. package/dist/lib/parsers/specialVar.test.d.ts +1 -0
  29. package/dist/lib/parsers/specialVar.test.js +219 -0
  30. package/dist/lib/types/access.d.ts +5 -5
  31. package/dist/lib/types/access.js +6 -1
  32. package/dist/lib/types/dataStructures.d.ts +3 -3
  33. package/dist/lib/types/function.d.ts +10 -4
  34. package/dist/lib/types/matchBlock.d.ts +2 -2
  35. package/dist/lib/types/returnStatement.d.ts +2 -2
  36. package/dist/lib/types/whileLoop.d.ts +2 -2
  37. package/dist/lib/types.d.ts +3 -3
  38. package/package.json +3 -3
@@ -0,0 +1,268 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { returnStatementParser } from "./returnStatement.js";
3
+ describe("returnStatementParser", () => {
4
+ const testCases = [
5
+ // Happy path - returning literals
6
+ {
7
+ input: "return 42",
8
+ expected: {
9
+ success: true,
10
+ result: {
11
+ type: "returnStatement",
12
+ value: { type: "number", value: "42" },
13
+ },
14
+ },
15
+ },
16
+ {
17
+ input: 'return "hello"',
18
+ expected: {
19
+ success: true,
20
+ result: {
21
+ type: "returnStatement",
22
+ value: { type: "string", value: "hello" },
23
+ },
24
+ },
25
+ },
26
+ {
27
+ input: "return `say hello`",
28
+ expected: {
29
+ success: true,
30
+ result: {
31
+ type: "returnStatement",
32
+ value: {
33
+ type: "prompt",
34
+ segments: [{ type: "text", value: "say hello" }],
35
+ },
36
+ },
37
+ },
38
+ },
39
+ {
40
+ input: "return myVar",
41
+ expected: {
42
+ success: true,
43
+ result: {
44
+ type: "returnStatement",
45
+ value: { type: "variableName", value: "myVar" },
46
+ },
47
+ },
48
+ },
49
+ // With optional semicolon
50
+ {
51
+ input: "return 123;",
52
+ expected: {
53
+ success: true,
54
+ result: {
55
+ type: "returnStatement",
56
+ value: { type: "number", value: "123" },
57
+ },
58
+ },
59
+ },
60
+ // With optional spaces
61
+ {
62
+ input: "return 42 ",
63
+ expected: {
64
+ success: true,
65
+ result: {
66
+ type: "returnStatement",
67
+ value: { type: "number", value: "42" },
68
+ },
69
+ },
70
+ },
71
+ {
72
+ input: "return 42",
73
+ expected: {
74
+ success: true,
75
+ result: {
76
+ type: "returnStatement",
77
+ value: { type: "number", value: "42" },
78
+ },
79
+ },
80
+ },
81
+ // Return function call
82
+ {
83
+ input: "return foo()",
84
+ expected: {
85
+ success: true,
86
+ result: {
87
+ type: "returnStatement",
88
+ value: {
89
+ type: "functionCall",
90
+ functionName: "foo",
91
+ arguments: [],
92
+ },
93
+ },
94
+ },
95
+ },
96
+ {
97
+ input: "return calculate(1, 2)",
98
+ expected: {
99
+ success: true,
100
+ result: {
101
+ type: "returnStatement",
102
+ value: {
103
+ type: "functionCall",
104
+ functionName: "calculate",
105
+ arguments: [
106
+ { type: "number", value: "1" },
107
+ { type: "number", value: "2" },
108
+ ],
109
+ },
110
+ },
111
+ },
112
+ },
113
+ // Return access expression
114
+ {
115
+ input: "return obj.property",
116
+ expected: {
117
+ success: true,
118
+ result: {
119
+ type: "returnStatement",
120
+ value: {
121
+ type: "accessExpression",
122
+ expression: {
123
+ type: "dotProperty",
124
+ object: { type: "variableName", value: "obj" },
125
+ propertyName: "property",
126
+ },
127
+ },
128
+ },
129
+ },
130
+ },
131
+ // Return array
132
+ {
133
+ input: "return [1, 2, 3]",
134
+ expected: {
135
+ success: true,
136
+ result: {
137
+ type: "returnStatement",
138
+ value: {
139
+ type: "agencyArray",
140
+ items: [
141
+ { type: "number", value: "1" },
142
+ { type: "number", value: "2" },
143
+ { type: "number", value: "3" },
144
+ ],
145
+ },
146
+ },
147
+ },
148
+ },
149
+ {
150
+ input: "return []",
151
+ expected: {
152
+ success: true,
153
+ result: {
154
+ type: "returnStatement",
155
+ value: {
156
+ type: "agencyArray",
157
+ items: [],
158
+ },
159
+ },
160
+ },
161
+ },
162
+ // Return object
163
+ {
164
+ input: 'return { key: "value" }',
165
+ expected: {
166
+ success: true,
167
+ result: {
168
+ type: "returnStatement",
169
+ value: {
170
+ type: "agencyObject",
171
+ entries: [
172
+ {
173
+ key: "key",
174
+ value: { type: "string", value: "value" },
175
+ },
176
+ ],
177
+ },
178
+ },
179
+ },
180
+ },
181
+ {
182
+ input: "return {}",
183
+ expected: {
184
+ success: true,
185
+ result: {
186
+ type: "returnStatement",
187
+ value: {
188
+ type: "agencyObject",
189
+ entries: [],
190
+ },
191
+ },
192
+ },
193
+ },
194
+ {
195
+ input: 'return { x: 1, y: 2 }',
196
+ expected: {
197
+ success: true,
198
+ result: {
199
+ type: "returnStatement",
200
+ value: {
201
+ type: "agencyObject",
202
+ entries: [
203
+ { key: "x", value: { type: "number", value: "1" } },
204
+ { key: "y", value: { type: "number", value: "2" } },
205
+ ],
206
+ },
207
+ },
208
+ },
209
+ },
210
+ // Negative numbers
211
+ {
212
+ input: "return -100",
213
+ expected: {
214
+ success: true,
215
+ result: {
216
+ type: "returnStatement",
217
+ value: { type: "number", value: "-100" },
218
+ },
219
+ },
220
+ },
221
+ // Decimal numbers
222
+ {
223
+ input: "return 3.14159",
224
+ expected: {
225
+ success: true,
226
+ result: {
227
+ type: "returnStatement",
228
+ value: { type: "number", value: "3.14159" },
229
+ },
230
+ },
231
+ },
232
+ // With semicolon and spaces
233
+ {
234
+ input: "return someValue ;",
235
+ expected: {
236
+ success: true,
237
+ result: {
238
+ type: "returnStatement",
239
+ value: { type: "variableName", value: "someValue" },
240
+ },
241
+ },
242
+ },
243
+ // Failure cases
244
+ { input: "return", expected: { success: false } },
245
+ { input: "return;", expected: { success: false } },
246
+ { input: "return ", expected: { success: false } },
247
+ { input: "retur 5", expected: { success: false } },
248
+ { input: "", expected: { success: false } },
249
+ { input: "42", expected: { success: false } },
250
+ ];
251
+ testCases.forEach(({ input, expected }) => {
252
+ if (expected.success) {
253
+ it(`should parse "${input}" successfully`, () => {
254
+ const result = returnStatementParser(input);
255
+ expect(result.success).toBe(true);
256
+ if (result.success) {
257
+ expect(result.result).toEqual(expected.result);
258
+ }
259
+ });
260
+ }
261
+ else {
262
+ it(`should fail to parse "${input}"`, () => {
263
+ const result = returnStatementParser(input);
264
+ expect(result.success).toBe(false);
265
+ });
266
+ }
267
+ });
268
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,219 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { specialVarParser } from "./specialVar.js";
3
+ describe("specialVar parsers", () => {
4
+ describe("specialVarParser", () => {
5
+ const testCases = [
6
+ // Happy path - assigning literals to special vars
7
+ {
8
+ input: '@model = "gpt-4"',
9
+ expected: {
10
+ success: true,
11
+ result: {
12
+ type: "specialVar",
13
+ name: "model",
14
+ value: { type: "string", value: "gpt-4" },
15
+ },
16
+ },
17
+ },
18
+ {
19
+ input: "@model = 42",
20
+ expected: {
21
+ success: true,
22
+ result: {
23
+ type: "specialVar",
24
+ name: "model",
25
+ value: { type: "number", value: "42" },
26
+ },
27
+ },
28
+ },
29
+ {
30
+ input: "@model = `the best model`",
31
+ expected: {
32
+ success: true,
33
+ result: {
34
+ type: "specialVar",
35
+ name: "model",
36
+ value: {
37
+ type: "prompt",
38
+ segments: [{ type: "text", value: "the best model" }],
39
+ },
40
+ },
41
+ },
42
+ },
43
+ {
44
+ input: "@model = myVariable",
45
+ expected: {
46
+ success: true,
47
+ result: {
48
+ type: "specialVar",
49
+ name: "model",
50
+ value: { type: "variableName", value: "myVariable" },
51
+ },
52
+ },
53
+ },
54
+ // With optional semicolon
55
+ {
56
+ input: '@model = "gpt-4";',
57
+ expected: {
58
+ success: true,
59
+ result: {
60
+ type: "specialVar",
61
+ name: "model",
62
+ value: { type: "string", value: "gpt-4" },
63
+ },
64
+ },
65
+ },
66
+ {
67
+ input: '@model="gpt-4"',
68
+ expected: {
69
+ success: true,
70
+ result: {
71
+ type: "specialVar",
72
+ name: "model",
73
+ value: { type: "string", value: "gpt-4" },
74
+ },
75
+ },
76
+ },
77
+ // Assigning arrays to special vars
78
+ {
79
+ input: "@model = []",
80
+ expected: {
81
+ success: true,
82
+ result: {
83
+ type: "specialVar",
84
+ name: "model",
85
+ value: {
86
+ type: "agencyArray",
87
+ items: [],
88
+ },
89
+ },
90
+ },
91
+ },
92
+ {
93
+ input: '@model = ["gpt-4", "claude"]',
94
+ expected: {
95
+ success: true,
96
+ result: {
97
+ type: "specialVar",
98
+ name: "model",
99
+ value: {
100
+ type: "agencyArray",
101
+ items: [
102
+ { type: "string", value: "gpt-4" },
103
+ { type: "string", value: "claude" },
104
+ ],
105
+ },
106
+ },
107
+ },
108
+ },
109
+ // Assigning objects to special vars
110
+ {
111
+ input: "@model = {}",
112
+ expected: {
113
+ success: true,
114
+ result: {
115
+ type: "specialVar",
116
+ name: "model",
117
+ value: {
118
+ type: "agencyObject",
119
+ entries: [],
120
+ },
121
+ },
122
+ },
123
+ },
124
+ {
125
+ input: '@model = { name: "gpt-4" }',
126
+ expected: {
127
+ success: true,
128
+ result: {
129
+ type: "specialVar",
130
+ name: "model",
131
+ value: {
132
+ type: "agencyObject",
133
+ entries: [
134
+ {
135
+ key: "name",
136
+ value: { type: "string", value: "gpt-4" },
137
+ },
138
+ ],
139
+ },
140
+ },
141
+ },
142
+ },
143
+ {
144
+ input: '@model = { name: "gpt-4", version: 4 }',
145
+ expected: {
146
+ success: true,
147
+ result: {
148
+ type: "specialVar",
149
+ name: "model",
150
+ value: {
151
+ type: "agencyObject",
152
+ entries: [
153
+ {
154
+ key: "name",
155
+ value: { type: "string", value: "gpt-4" },
156
+ },
157
+ {
158
+ key: "version",
159
+ value: { type: "number", value: "4" },
160
+ },
161
+ ],
162
+ },
163
+ },
164
+ },
165
+ },
166
+ // Negative numbers
167
+ {
168
+ input: "@model = -100",
169
+ expected: {
170
+ success: true,
171
+ result: {
172
+ type: "specialVar",
173
+ name: "model",
174
+ value: { type: "number", value: "-100" },
175
+ },
176
+ },
177
+ },
178
+ // Decimal numbers
179
+ {
180
+ input: "@model = 3.14",
181
+ expected: {
182
+ success: true,
183
+ result: {
184
+ type: "specialVar",
185
+ name: "model",
186
+ value: { type: "number", value: "3.14" },
187
+ },
188
+ },
189
+ },
190
+ // Failure cases
191
+ { input: "@model =", expected: { success: false } },
192
+ { input: '@notSpecial = "value"', expected: { success: false } },
193
+ { input: 'model = "value"', expected: { success: false } }, // missing @
194
+ { input: '@Model = "value"', expected: { success: false } }, // wrong case
195
+ { input: '@ model = "value"', expected: { success: false } }, // space after @
196
+ { input: "", expected: { success: false } },
197
+ { input: "@", expected: { success: false } },
198
+ { input: "@model", expected: { success: false } },
199
+ { input: '@"model" = "value"', expected: { success: false } },
200
+ ];
201
+ testCases.forEach(({ input, expected }) => {
202
+ if (expected.success) {
203
+ it(`should parse "${input}" successfully`, () => {
204
+ const result = specialVarParser(input);
205
+ expect(result.success).toBe(true);
206
+ if (result.success) {
207
+ expect(result.result).toEqual(expected.result);
208
+ }
209
+ });
210
+ }
211
+ else {
212
+ it(`should fail to parse "${input}"`, () => {
213
+ const result = specialVarParser(input);
214
+ expect(result.success).toBe(false);
215
+ });
216
+ }
217
+ });
218
+ });
219
+ });
@@ -1,22 +1,22 @@
1
- import { FunctionCall } from "../types.js";
1
+ import { AgencyNode, FunctionCall } from "../types.js";
2
2
  import { Literal } from "./literals.js";
3
- import { AgencyArray } from "./dataStructures.js";
4
3
  export type DotProperty = {
5
4
  type: "dotProperty";
6
- object: Literal | FunctionCall | AccessExpression;
5
+ object: AgencyNode;
7
6
  propertyName: string;
8
7
  };
9
8
  export type IndexAccess = {
10
9
  type: "indexAccess";
11
- array: Literal | FunctionCall | AccessExpression | AgencyArray;
10
+ array: AgencyNode;
12
11
  index: Literal | FunctionCall | AccessExpression;
13
12
  };
14
13
  export type DotFunctionCall = {
15
14
  type: "dotFunctionCall";
16
- object: Literal | FunctionCall | AccessExpression;
15
+ object: AgencyNode;
17
16
  functionCall: FunctionCall;
18
17
  };
19
18
  export type AccessExpression = {
20
19
  type: "accessExpression";
21
20
  expression: DotProperty | IndexAccess | DotFunctionCall;
22
21
  };
22
+ export declare function accessExpression(expression: DotProperty | IndexAccess | DotFunctionCall): AccessExpression;
@@ -1 +1,6 @@
1
- export {};
1
+ export function accessExpression(expression) {
2
+ return {
3
+ type: "accessExpression",
4
+ expression
5
+ };
6
+ }
@@ -1,12 +1,12 @@
1
- import { AccessExpression, Literal } from "../types.js";
1
+ import { AccessExpression, IndexAccess, Literal } from "../types.js";
2
2
  import { FunctionCall } from "./function.js";
3
3
  export type AgencyArray = {
4
4
  type: "agencyArray";
5
- items: (AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | AccessExpression)[];
5
+ items: (IndexAccess | AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | AccessExpression)[];
6
6
  };
7
7
  export type AgencyObjectKV = {
8
8
  key: string;
9
- value: AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | AccessExpression;
9
+ value: IndexAccess | AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | AccessExpression;
10
10
  };
11
11
  export type AgencyObject = {
12
12
  type: "agencyObject";
@@ -1,17 +1,23 @@
1
- import { AgencyNode } from "../types.js";
2
- import { AccessExpression } from "./access.js";
1
+ import { AgencyArray, AgencyNode, AgencyObject, VariableType } from "../types.js";
2
+ import { AccessExpression, IndexAccess } from "./access.js";
3
3
  import { Literal } from "./literals.js";
4
+ export type FunctionParameter = {
5
+ type: "functionParameter";
6
+ name: string;
7
+ typeHint?: VariableType;
8
+ };
4
9
  export type FunctionDefinition = {
5
10
  type: "function";
6
11
  functionName: string;
7
- parameters: string[];
12
+ parameters: FunctionParameter[];
8
13
  body: AgencyNode[];
14
+ returnType?: VariableType;
9
15
  docString?: DocString;
10
16
  };
11
17
  export type FunctionCall = {
12
18
  type: "functionCall";
13
19
  functionName: string;
14
- arguments: (Literal | AccessExpression | FunctionCall)[];
20
+ arguments: (AgencyArray | AgencyObject | IndexAccess | Literal | AccessExpression | FunctionCall)[];
15
21
  };
16
22
  export type DocString = {
17
23
  type: "docString";
@@ -1,5 +1,5 @@
1
1
  import { Assignment, AgencyComment } from "../types.js";
2
- import { AccessExpression } from "./access.js";
2
+ import { AccessExpression, IndexAccess } from "./access.js";
3
3
  import { Literal } from "./literals.js";
4
4
  import { FunctionCall } from "./function.js";
5
5
  import { AgencyArray, AgencyObject } from "./dataStructures.js";
@@ -7,7 +7,7 @@ import { ReturnStatement } from "./returnStatement.js";
7
7
  export type DefaultCase = "_";
8
8
  export type MatchBlockCase = {
9
9
  type: "matchBlockCase";
10
- caseValue: AccessExpression | Literal | DefaultCase;
10
+ caseValue: AccessExpression | Literal | DefaultCase | IndexAccess;
11
11
  body: Assignment | Literal | FunctionCall | AccessExpression | AgencyArray | AgencyObject | ReturnStatement;
12
12
  };
13
13
  export type MatchBlock = {
@@ -1,6 +1,6 @@
1
- import { AccessExpression, FunctionCall, Literal } from "../types.js";
1
+ import { AccessExpression, FunctionCall, IndexAccess, Literal } from "../types.js";
2
2
  import { AgencyArray, AgencyObject } from "./dataStructures.js";
3
3
  export type ReturnStatement = {
4
4
  type: "returnStatement";
5
- value: AccessExpression | FunctionCall | Literal | AgencyObject | AgencyArray;
5
+ value: AccessExpression | FunctionCall | Literal | AgencyObject | AgencyArray | IndexAccess;
6
6
  };
@@ -1,6 +1,6 @@
1
- import { AccessExpression, AgencyNode, FunctionCall, Literal } from "../types.js";
1
+ import { AccessExpression, AgencyNode, FunctionCall, IndexAccess, Literal } from "../types.js";
2
2
  export type WhileLoop = {
3
3
  type: "whileLoop";
4
- condition: FunctionCall | AccessExpression | Literal;
4
+ condition: IndexAccess | FunctionCall | AccessExpression | Literal;
5
5
  body: AgencyNode[];
6
6
  };
@@ -1,7 +1,7 @@
1
1
  import { Literal } from "./types/literals.js";
2
2
  import { TypeAlias, TypeHint, VariableType } from "./types/typeHints.js";
3
3
  import { MatchBlock } from "./types/matchBlock.js";
4
- import { AccessExpression } from "./types/access.js";
4
+ import { AccessExpression, DotProperty, IndexAccess } from "./types/access.js";
5
5
  import { FunctionCall, FunctionDefinition } from "./types/function.js";
6
6
  import { AgencyArray, AgencyObject } from "./types/dataStructures.js";
7
7
  import { GraphNodeDefinition } from "./types/graphNode.js";
@@ -24,13 +24,13 @@ export * from "./types/whileLoop.js";
24
24
  export type Assignment = {
25
25
  type: "assignment";
26
26
  variableName: string;
27
- value: AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray;
27
+ value: AccessExpression | Literal | FunctionCall | AgencyObject | AgencyArray | IndexAccess;
28
28
  };
29
29
  export type AgencyComment = {
30
30
  type: "comment";
31
31
  content: string;
32
32
  };
33
- export type AgencyNode = TypeHint | TypeAlias | UsesTool | GraphNodeDefinition | FunctionDefinition | Assignment | Literal | FunctionCall | MatchBlock | ReturnStatement | AccessExpression | AgencyComment | AgencyObject | AgencyArray | ImportStatement | WhileLoop | SpecialVar;
33
+ export type AgencyNode = TypeHint | TypeAlias | UsesTool | GraphNodeDefinition | FunctionDefinition | Assignment | Literal | FunctionCall | MatchBlock | ReturnStatement | AccessExpression | AgencyComment | AgencyObject | AgencyArray | ImportStatement | WhileLoop | SpecialVar | IndexAccess | DotProperty;
34
34
  export type AgencyProgram = {
35
35
  type: "agencyProgram";
36
36
  nodes: AgencyNode[];
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "agency-lang",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "The Agency language",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "test": "vitest",
8
8
  "test:run": "vitest run",
9
9
  "build": "rm -rf dist/ && tsc && tsc-alias",
10
- "start": "node dist/lib/index.js",
10
+ "start": "node dist/index.js",
11
11
  "templates": "typestache ./lib/templates",
12
12
  "typecheck": "tsc --noEmit",
13
13
  "agency": "node ./dist/scripts/agency.js",
@@ -55,4 +55,4 @@
55
55
  "typescript": "^5.9.3",
56
56
  "vitest": "^4.0.16"
57
57
  }
58
- }
58
+ }