agency-lang 0.0.6 → 0.0.7
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.
- package/dist/scripts/agency.js +50 -29
- package/package.json +1 -1
package/dist/scripts/agency.js
CHANGED
|
@@ -12,12 +12,12 @@ Usage:
|
|
|
12
12
|
agency help Show this help message
|
|
13
13
|
agency compile <input> [output] Compile .agency file to TypeScript
|
|
14
14
|
agency run <input> [output] Compile and run .agency file
|
|
15
|
-
agency format
|
|
16
|
-
agency parse
|
|
15
|
+
agency format [input] Format .agency file (reads from stdin if no input)
|
|
16
|
+
agency parse [input] Parse .agency file and show AST (reads from stdin if no input)
|
|
17
17
|
agency <input> Compile and run .agency file (shorthand)
|
|
18
18
|
|
|
19
19
|
Arguments:
|
|
20
|
-
input Path to .agency input file
|
|
20
|
+
input Path to .agency input file (or omit to read from stdin for format/parse)
|
|
21
21
|
output Path to output .ts file (optional)
|
|
22
22
|
Default: <input-name>.ts
|
|
23
23
|
|
|
@@ -30,17 +30,27 @@ Examples:
|
|
|
30
30
|
agency compile script.agency out.ts Compile to out.ts
|
|
31
31
|
agency run script.agency Compile and run script.agency
|
|
32
32
|
agency -v parse script.agency Parse with verbose logging
|
|
33
|
+
cat script.agency | agency format Format from stdin
|
|
34
|
+
echo "x = 5" | agency parse Parse from stdin
|
|
33
35
|
agency script.agency Compile and run (shorthand)
|
|
34
36
|
`);
|
|
35
37
|
}
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
process.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
function readStdin() {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
let data = "";
|
|
41
|
+
process.stdin.setEncoding("utf-8");
|
|
42
|
+
process.stdin.on("data", (chunk) => {
|
|
43
|
+
data += chunk;
|
|
44
|
+
});
|
|
45
|
+
process.stdin.on("end", () => {
|
|
46
|
+
resolve(data);
|
|
47
|
+
});
|
|
48
|
+
process.stdin.on("error", (err) => {
|
|
49
|
+
reject(err);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function parse(contents, verbose = false) {
|
|
44
54
|
const parseResult = parseAgency(contents, verbose);
|
|
45
55
|
// Check if parsing was successful
|
|
46
56
|
if (!parseResult.success) {
|
|
@@ -50,10 +60,21 @@ function parse(inputFile, verbose = false) {
|
|
|
50
60
|
}
|
|
51
61
|
return parseResult.result;
|
|
52
62
|
}
|
|
63
|
+
function readFile(inputFile) {
|
|
64
|
+
// Validate input file
|
|
65
|
+
if (!fs.existsSync(inputFile)) {
|
|
66
|
+
console.error(`Error: Input file '${inputFile}' not found`);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
// Read and parse the Agency file
|
|
70
|
+
const contents = fs.readFileSync(inputFile, "utf-8");
|
|
71
|
+
return contents;
|
|
72
|
+
}
|
|
53
73
|
function compile(inputFile, outputFile, verbose = false) {
|
|
54
74
|
// Determine output file name
|
|
55
75
|
const output = outputFile || inputFile.replace(".agency", ".ts");
|
|
56
|
-
const
|
|
76
|
+
const contents = readFile(inputFile);
|
|
77
|
+
const parsedProgram = parse(contents, verbose);
|
|
57
78
|
// Generate TypeScript code
|
|
58
79
|
const generatedCode = generateGraph(parsedProgram);
|
|
59
80
|
// Write to output file
|
|
@@ -81,18 +102,14 @@ function run(inputFile, outputFile, verbose = false) {
|
|
|
81
102
|
}
|
|
82
103
|
});
|
|
83
104
|
}
|
|
84
|
-
function format(
|
|
85
|
-
const parsedProgram = parse(
|
|
86
|
-
// Generate TypeScript code
|
|
105
|
+
async function format(contents, verbose = false) {
|
|
106
|
+
const parsedProgram = parse(contents, verbose);
|
|
87
107
|
const generatedCode = generateAgency(parsedProgram);
|
|
88
|
-
// Write to output file
|
|
89
|
-
//fs.writeFileSync(inputFile, generatedCode, "utf-8");
|
|
90
|
-
// console.log(`Generated ${output} from ${inputFile}`);
|
|
91
108
|
console.log(generatedCode);
|
|
92
109
|
return generatedCode;
|
|
93
110
|
}
|
|
94
111
|
// Main CLI logic
|
|
95
|
-
function main() {
|
|
112
|
+
async function main() {
|
|
96
113
|
const args = process.argv.slice(2);
|
|
97
114
|
// No arguments - show help
|
|
98
115
|
if (args.length === 0) {
|
|
@@ -129,20 +146,24 @@ function main() {
|
|
|
129
146
|
break;
|
|
130
147
|
case "fmt":
|
|
131
148
|
case "format":
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
process.exit(1);
|
|
149
|
+
let fmtContents;
|
|
150
|
+
if (filteredArgs.length < 2) {
|
|
151
|
+
fmtContents = await readStdin();
|
|
136
152
|
}
|
|
137
|
-
|
|
153
|
+
else {
|
|
154
|
+
fmtContents = readFile(filteredArgs[1]);
|
|
155
|
+
}
|
|
156
|
+
format(fmtContents, verbose);
|
|
138
157
|
break;
|
|
139
158
|
case "parse":
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
159
|
+
let contents;
|
|
160
|
+
if (filteredArgs.length < 2) {
|
|
161
|
+
contents = await readStdin();
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
contents = readFile(filteredArgs[1]);
|
|
144
165
|
}
|
|
145
|
-
const result = parse(
|
|
166
|
+
const result = parse(contents, verbose);
|
|
146
167
|
console.log(JSON.stringify(result, null, 2));
|
|
147
168
|
break;
|
|
148
169
|
default:
|