@irrelon/battlescript 3.1.5 → 3.1.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.
@@ -9,16 +9,29 @@ import {
9
9
  fromSourceCodeToMiniAst,
10
10
  miniAstFromBinaryBuffer
11
11
  } from "@/services";
12
+ import type {SourceResolverAsync, SourceResolverSync} from "@/types";
12
13
  import type {ByteCodeJson} from "@/types/byteCode/ByteCodeJson";
13
14
  import {version} from "../package.json";
14
15
  import {readFileSync} from "fs";
15
16
  import {writeFileSync} from "node:fs";
16
17
 
18
+ const sourceResolverAsync: SourceResolverAsync = (filePath: string) => {
19
+ return readFile(filePath, "utf-8")
20
+ };
21
+
22
+ const sourceResolverSync: SourceResolverSync = (filePath: string): string => {
23
+ return readFileSync(filePath, "utf-8")
24
+ };
25
+
17
26
  export async function toAst(inputPath: string, outputPath?: string) {
18
27
  console.log(`[BattleScript ${version}] toAst: ${inputPath}`);
19
28
  try {
20
- const sourceCode = await readFile(inputPath, "utf-8");
21
- const ast = fromSourceCodeToFullAst(sourceCode, {filePath: inputPath, includeMetaData: true});
29
+ const sourceCode = await sourceResolverAsync(inputPath);
30
+ const ast = fromSourceCodeToFullAst(sourceCode, {
31
+ filePath: inputPath,
32
+ includeMetaData: true,
33
+ sourceResolver: sourceResolverSync
34
+ });
22
35
 
23
36
  if (outputPath) {
24
37
  await writeFile(outputPath, JSON.stringify(ast, null, 2));
@@ -35,8 +48,12 @@ export async function toAst(inputPath: string, outputPath?: string) {
35
48
  export function toAstSync(inputPath: string, outputPath?: string) {
36
49
  console.log(`[BattleScript ${version}] toAstSync: ${inputPath}`);
37
50
  try {
38
- const sourceCode = readFileSync(inputPath, "utf-8");
39
- const ast = fromSourceCodeToFullAst(sourceCode, {filePath: inputPath, includeMetaData: true});
51
+ const sourceCode = sourceResolverSync(inputPath);
52
+ const ast = fromSourceCodeToFullAst(sourceCode, {
53
+ filePath: inputPath,
54
+ includeMetaData: true,
55
+ sourceResolver: sourceResolverSync
56
+ });
40
57
 
41
58
  if (outputPath) {
42
59
  writeFileSync(outputPath, JSON.stringify(ast, null, 2));
@@ -53,7 +70,7 @@ export function toAstSync(inputPath: string, outputPath?: string) {
53
70
  export async function fromAst(inputPath: string, outputPath: string) {
54
71
  console.log(`[BattleScript ${version}] fromAst: ${inputPath}`);
55
72
  try {
56
- const astString = await readFile(inputPath, "utf-8");
73
+ const astString = await sourceResolverAsync(inputPath);
57
74
  const ast = JSON.parse(astString);
58
75
  const code = fromFullAstToSourceCode(ast);
59
76
  await writeFile(outputPath, code);
@@ -67,8 +84,12 @@ export async function fromAst(inputPath: string, outputPath: string) {
67
84
  export async function toMiniAst(inputPath: string, outputPath: string) {
68
85
  console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
69
86
  try {
70
- const sourceCode = await readFile(inputPath, "utf-8");
71
- const ast = fromSourceCodeToMiniAst(sourceCode, {filePath: inputPath, includeMetaData: true});
87
+ const sourceCode = await sourceResolverAsync(inputPath);
88
+ const ast = fromSourceCodeToMiniAst(sourceCode, {
89
+ filePath: inputPath,
90
+ includeMetaData: true,
91
+ sourceResolver: sourceResolverSync
92
+ });
72
93
 
73
94
  await writeFile(outputPath, JSON.stringify(ast));
74
95
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -82,10 +103,15 @@ export async function toBinary(inputPath: string, outputPath: string) {
82
103
  console.log(`[BattleScript ${version}] toBinary: ${inputPath}`);
83
104
  try {
84
105
  console.log(`[BattleScript ${version}] Reading file contents: ${inputPath}`);
85
- const sourceCode = await readFile(inputPath, "utf-8");
106
+ const sourceCode = await sourceResolverAsync(inputPath);
86
107
 
87
108
  console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
88
- const binary = fromSourceCodeToBinary(sourceCode, {includeMetaData: true, flatten: true, filePath: inputPath});
109
+ const binary = fromSourceCodeToBinary(sourceCode, {
110
+ includeMetaData: true,
111
+ flatten: true,
112
+ filePath: inputPath,
113
+ sourceResolver: sourceResolverSync
114
+ });
89
115
 
90
116
  console.log(`[BattleScript ${version}] Writing file contents: ${outputPath}`);
91
117
  await writeFile(outputPath, Buffer.concat(binary))
@@ -99,11 +125,12 @@ export async function toBinary(inputPath: string, outputPath: string) {
99
125
 
100
126
  export async function miniAstToBinary(inputPath: string, outputPath: string) {
101
127
  try {
102
- const sourceCode = await readFile(inputPath, "utf-8");
128
+ const sourceCode = await sourceResolverAsync(inputPath);
103
129
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
104
130
  includeMetaData: true,
105
131
  flatten: true,
106
- filePath: inputPath
132
+ filePath: inputPath,
133
+ sourceResolver: sourceResolverSync
107
134
  });
108
135
  await writeFile(outputPath, Buffer.concat(binary))
109
136
 
@@ -128,7 +155,7 @@ export async function fromBinary(inputPath: string, outputPath: string) {
128
155
 
129
156
  // async function toByteCode(inputPath: string, outputPath: string) {
130
157
  // try {
131
- // const sourceCode = await readFile(inputPath, "utf-8");
158
+ // const sourceCode = await sourceResolverAsync(inputPath);
132
159
  // const bcodeJson = fromSourceCodeToByteCode(sourceCode, {includeMetaData: true, flatten: true});
133
160
  // await writeFile(outputPath, JSON.stringify(bcodeJson).replace(/\[\[/g, "[\n[").replace(/]]/g, "]\n]").replace(/],/g, "],\n"));
134
161
  // console.log(`Compiled to ${outputPath}`);
@@ -140,7 +167,7 @@ export async function fromBinary(inputPath: string, outputPath: string) {
140
167
 
141
168
  export async function fromByteCode(inputPath: string, outputPath: string) {
142
169
  try {
143
- const byteCodeJsonString = await readFile(inputPath, "utf-8");
170
+ const byteCodeJsonString = await sourceResolverAsync(inputPath);
144
171
  const byteCodeJson: ByteCodeJson = JSON.parse(byteCodeJsonString);
145
172
  const code = disassembleBytecode(byteCodeJson);
146
173
  await writeFile(outputPath, code.join("\n"));
package/bin/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
- import { readFile, writeFile } from 'fs/promises';
2
+ import { writeFile, readFile } from 'fs/promises';
3
3
  import { join, dirname, basename, extname } from 'path';
4
4
  import require$$0 from 'assert';
5
5
  import require$$0$1 from 'util';
6
6
  import { readFileSync } from 'fs';
7
- import { writeFileSync } from 'node:fs';
7
+ import 'node:fs';
8
8
 
9
9
  var ATNDeserializer = {};
10
10
 
@@ -26265,7 +26265,7 @@ var body$2 = [
26265
26265
  kind: "FUNCTION_DECLARATION",
26266
26266
  returnType: {
26267
26267
  kind: "DATA_TYPE",
26268
- value: "float",
26268
+ value: "void",
26269
26269
  meta: {
26270
26270
  start: {
26271
26271
  line: 1,
@@ -26274,28 +26274,60 @@ var body$2 = [
26274
26274
  },
26275
26275
  stop: {
26276
26276
  line: 1,
26277
- column: 12,
26278
- offset: 11
26277
+ column: 11,
26278
+ offset: 10
26279
26279
  }
26280
26280
  }
26281
26281
  },
26282
26282
  id: {
26283
26283
  kind: "IDENTIFIER",
26284
- value: "std::gpio.readPin",
26284
+ value: "std::gpio.setPinMode",
26285
26285
  meta: {
26286
26286
  start: {
26287
26287
  line: 1,
26288
- column: 13,
26289
- offset: 13
26288
+ column: 12,
26289
+ offset: 12
26290
26290
  },
26291
26291
  stop: {
26292
26292
  line: 1,
26293
- column: 30,
26294
- offset: 29
26293
+ column: 32,
26294
+ offset: 31
26295
26295
  }
26296
26296
  }
26297
26297
  },
26298
26298
  params: [
26299
+ {
26300
+ kind: "IDENTIFIER",
26301
+ value: "newMode",
26302
+ meta: {
26303
+ start: {
26304
+ line: 1,
26305
+ column: 38,
26306
+ offset: 38
26307
+ },
26308
+ stop: {
26309
+ line: 1,
26310
+ column: 45,
26311
+ offset: 44
26312
+ }
26313
+ },
26314
+ type: {
26315
+ kind: "DATA_TYPE",
26316
+ value: "int",
26317
+ meta: {
26318
+ start: {
26319
+ line: 1,
26320
+ column: 34,
26321
+ offset: 34
26322
+ },
26323
+ stop: {
26324
+ line: 1,
26325
+ column: 37,
26326
+ offset: 36
26327
+ }
26328
+ }
26329
+ }
26330
+ }
26299
26331
  ],
26300
26332
  isNative: true,
26301
26333
  isPure: false,
@@ -26307,8 +26339,8 @@ var body$2 = [
26307
26339
  },
26308
26340
  stop: {
26309
26341
  line: 1,
26310
- column: 34,
26311
- offset: 33
26342
+ column: 47,
26343
+ offset: 46
26312
26344
  }
26313
26345
  }
26314
26346
  },
@@ -26316,17 +26348,68 @@ var body$2 = [
26316
26348
  kind: "FUNCTION_DECLARATION",
26317
26349
  returnType: {
26318
26350
  kind: "DATA_TYPE",
26319
- value: "void",
26351
+ value: "float",
26320
26352
  meta: {
26321
26353
  start: {
26322
26354
  line: 2,
26323
26355
  column: 7,
26324
- offset: 42
26356
+ offset: 55
26325
26357
  },
26326
26358
  stop: {
26327
26359
  line: 2,
26360
+ column: 12,
26361
+ offset: 59
26362
+ }
26363
+ }
26364
+ },
26365
+ id: {
26366
+ kind: "IDENTIFIER",
26367
+ value: "std::gpio.readPin",
26368
+ meta: {
26369
+ start: {
26370
+ line: 2,
26371
+ column: 13,
26372
+ offset: 61
26373
+ },
26374
+ stop: {
26375
+ line: 2,
26376
+ column: 30,
26377
+ offset: 77
26378
+ }
26379
+ }
26380
+ },
26381
+ params: [
26382
+ ],
26383
+ isNative: true,
26384
+ isPure: false,
26385
+ meta: {
26386
+ start: {
26387
+ line: 2,
26388
+ column: 0,
26389
+ offset: 48
26390
+ },
26391
+ stop: {
26392
+ line: 2,
26393
+ column: 34,
26394
+ offset: 81
26395
+ }
26396
+ }
26397
+ },
26398
+ {
26399
+ kind: "FUNCTION_DECLARATION",
26400
+ returnType: {
26401
+ kind: "DATA_TYPE",
26402
+ value: "void",
26403
+ meta: {
26404
+ start: {
26405
+ line: 3,
26406
+ column: 7,
26407
+ offset: 90
26408
+ },
26409
+ stop: {
26410
+ line: 3,
26328
26411
  column: 11,
26329
- offset: 45
26412
+ offset: 93
26330
26413
  }
26331
26414
  }
26332
26415
  },
@@ -26335,14 +26418,14 @@ var body$2 = [
26335
26418
  value: "std::gpio.writePin",
26336
26419
  meta: {
26337
26420
  start: {
26338
- line: 2,
26421
+ line: 3,
26339
26422
  column: 12,
26340
- offset: 47
26423
+ offset: 95
26341
26424
  },
26342
26425
  stop: {
26343
- line: 2,
26426
+ line: 3,
26344
26427
  column: 30,
26345
- offset: 64
26428
+ offset: 112
26346
26429
  }
26347
26430
  }
26348
26431
  },
@@ -26352,14 +26435,14 @@ var body$2 = [
26352
26435
  value: "newVal",
26353
26436
  meta: {
26354
26437
  start: {
26355
- line: 2,
26438
+ line: 3,
26356
26439
  column: 36,
26357
- offset: 71
26440
+ offset: 119
26358
26441
  },
26359
26442
  stop: {
26360
- line: 2,
26443
+ line: 3,
26361
26444
  column: 42,
26362
- offset: 76
26445
+ offset: 124
26363
26446
  }
26364
26447
  },
26365
26448
  type: {
@@ -26367,14 +26450,14 @@ var body$2 = [
26367
26450
  value: "int",
26368
26451
  meta: {
26369
26452
  start: {
26370
- line: 2,
26453
+ line: 3,
26371
26454
  column: 32,
26372
- offset: 67
26455
+ offset: 115
26373
26456
  },
26374
26457
  stop: {
26375
- line: 2,
26458
+ line: 3,
26376
26459
  column: 35,
26377
- offset: 69
26460
+ offset: 117
26378
26461
  }
26379
26462
  }
26380
26463
  }
@@ -26384,14 +26467,14 @@ var body$2 = [
26384
26467
  isPure: false,
26385
26468
  meta: {
26386
26469
  start: {
26387
- line: 2,
26470
+ line: 3,
26388
26471
  column: 0,
26389
- offset: 35
26472
+ offset: 83
26390
26473
  },
26391
26474
  stop: {
26392
- line: 2,
26475
+ line: 3,
26393
26476
  column: 44,
26394
- offset: 78
26477
+ offset: 126
26395
26478
  }
26396
26479
  }
26397
26480
  }
@@ -27902,7 +27985,7 @@ function getAllNativeLibraryFunctions(rootNode) {
27902
27985
  ];
27903
27986
  }
27904
27987
 
27905
- var version = "3.1.5";
27988
+ var version = "3.1.7";
27906
27989
  var binaryVersion = 7;
27907
27990
 
27908
27991
  const RootNodeLayout = [{
@@ -27942,7 +28025,12 @@ const rootToAstNode = (instance, ctx) => {
27942
28025
  // Check we can locate the file
27943
28026
  console.log(`[BattleScript ${version}] Processing import compilation unit: `, importNode.source.value);
27944
28027
  const filePath = join(dirname(instance._filePath), importNode.source.value);
27945
- const importedAst = toAstSync(filePath);
28028
+ const importedSourceCode = instance.resolveSourceCode(filePath);
28029
+ const importedAst = fromSourceCodeToFullAst(importedSourceCode, {
28030
+ filePath,
28031
+ includeMetaData: true,
28032
+ sourceResolver: instance._sourceResolver
28033
+ });
27946
28034
  return importedAst.body;
27947
28035
  }).concat(standardLibraryDeclarations).filter((astNode) => {
27948
28036
  // TODO: We should move this to just before we complete the AST generation
@@ -28187,7 +28275,7 @@ function getMetaData(ctx, extra) {
28187
28275
  };
28188
28276
  }
28189
28277
  class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTreeVisitor {
28190
- constructor(filePath, sourceCode) {
28278
+ constructor(filePath, sourceCode, sourceResolver) {
28191
28279
  super();
28192
28280
  this._options = {
28193
28281
  includeMetaData: true
@@ -28205,6 +28293,16 @@ class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTre
28205
28293
  return;
28206
28294
  this._sourceCode = newSourceCode;
28207
28295
  };
28296
+ this.resolveSourceCode = (filePath) => {
28297
+ if (this._sourceResolver) {
28298
+ const resolved = this._sourceResolver(filePath);
28299
+ if (resolved !== undefined) {
28300
+ return resolved;
28301
+ }
28302
+ throw new Error(`Source resolver could not resolve source code for file: ${filePath}`);
28303
+ }
28304
+ throw new Error(`Could not resolve source code for file: ${filePath}. No source resolver provided.`);
28305
+ };
28208
28306
  this.extractInlineComment = (ctx) => {
28209
28307
  if (!ctx)
28210
28308
  return;
@@ -28434,6 +28532,7 @@ class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTre
28434
28532
  };
28435
28533
  this._filePath = filePath;
28436
28534
  this._sourceCode = sourceCode;
28535
+ this._sourceResolver = sourceResolver;
28437
28536
  }
28438
28537
  visit(node, options) {
28439
28538
  if (options) {
@@ -28865,28 +28964,26 @@ class BattleScriptToMiniAST {
28865
28964
  return requiredCount;
28866
28965
  }, 0);
28867
28966
  if (functionArgs.length < requiredCount) {
28868
- functionParams.reduce((requiredCount, param, index) => {
28869
- if (param.kind === "IDENTIFIER") {
28870
- if (!functionArgs[index]) {
28871
- throw new Error(formatCompilerError(this._filePath, this._sourceCode, "error", node.meta, `Call to function ${prettyPrintFunctionDeclaration(functionNode)} is missing required argument \`${this.nodeResolver.visit(param)}\``));
28872
- }
28873
- requiredCount++;
28967
+ functionParams.forEach((param, index) => {
28968
+ if (functionArgs[index]) {
28874
28969
  finalArgs.push(functionArgs[index]);
28970
+ return;
28971
+ }
28972
+ if (param.kind === "IDENTIFIER") {
28973
+ throw new Error(formatCompilerError(this._filePath, this._sourceCode, "error", node.meta, `Call to function ${prettyPrintFunctionDeclaration(functionNode)} is missing required argument \`${this.nodeResolver.visit(param)}\``));
28875
28974
  }
28876
28975
  else if (param.kind === "ASSIGNMENT_PATTERN") {
28877
- if (!functionArgs[index]) {
28878
- // We don't have a value but we do have a default, use it
28879
- // We use clone here because we want to create an entirely new node entry in the node array
28880
- // just for this one argument so that we don't point at a node index (the default argument
28881
- // in the function definition) because that would generate a non-contiguous start index
28882
- // and count for the function call arguments. However, when the meta array is compiled,
28883
- // the cloned node will still have the same meta data so will highlight the original
28884
- // function definition parameter which shows where the value came from.
28885
- finalArgs.push(clone(param.right));
28886
- }
28976
+ // We don't have a value but we do have a default, use it
28977
+ // We use clone here because we want to create an entirely new node entry in the node array
28978
+ // just for this one argument so that we don't point at a node index (the default argument
28979
+ // in the function definition) because that would generate a non-contiguous start index
28980
+ // and count for the function call arguments. However, when the meta array is compiled,
28981
+ // the cloned node will still have the same meta data so will highlight the original
28982
+ // function definition parameter which shows where the value came from.
28983
+ finalArgs.push(clone(param.right));
28887
28984
  }
28888
- return requiredCount;
28889
- }, 0);
28985
+ return;
28986
+ });
28890
28987
  }
28891
28988
  else {
28892
28989
  return functionArgs;
@@ -38796,9 +38893,7 @@ function fromSourceCodeToFullAst(inputText, options) {
38796
38893
  const parser = getParser(inputText);
38797
38894
  const tree = parser.root();
38798
38895
  console.log(`[BattleScript ${version}] Transforming source code -> AST...`);
38799
- const transformer = new BattleScriptToAST(options?.filePath || "-", inputText);
38800
- transformer.filePath(options?.filePath);
38801
- transformer.sourceCode(inputText);
38896
+ const transformer = new BattleScriptToAST(options?.filePath || "-", inputText, options?.sourceResolver);
38802
38897
  return transformer.visit(tree, options);
38803
38898
  }
38804
38899
  function fromFullAstToSourceCode(ast, options = { includeMetaData: true }) {
@@ -38810,8 +38905,6 @@ function fromSourceCodeToMiniAst(source, options = { includeMetaData: true }) {
38810
38905
  const ast = fromSourceCodeToFullAst(source, options);
38811
38906
  console.log(`[BattleScript ${version}] Transforming AST -> MiniAST...`);
38812
38907
  const transformer = new BattleScriptToMiniAST(options?.filePath || "-", source);
38813
- transformer.filePath(options?.filePath);
38814
- transformer.sourceCode(source); // Add for debugging purposes
38815
38908
  return transformer.transform(ast, options);
38816
38909
  }
38817
38910
  function fromMiniAstToBinary(miniAst, options = { includeMetaData: true }) {
@@ -38913,11 +39006,21 @@ function disassembleBytecode([stringTable, bytecode]) {
38913
39006
  return output;
38914
39007
  }
38915
39008
 
39009
+ const sourceResolverAsync = (filePath) => {
39010
+ return readFile(filePath, "utf-8");
39011
+ };
39012
+ const sourceResolverSync = (filePath) => {
39013
+ return readFileSync(filePath, "utf-8");
39014
+ };
38916
39015
  async function toAst(inputPath, outputPath) {
38917
39016
  console.log(`[BattleScript ${version}] toAst: ${inputPath}`);
38918
39017
  try {
38919
- const sourceCode = await readFile(inputPath, "utf-8");
38920
- const ast = fromSourceCodeToFullAst(sourceCode, { filePath: inputPath, includeMetaData: true });
39018
+ const sourceCode = await sourceResolverAsync(inputPath);
39019
+ const ast = fromSourceCodeToFullAst(sourceCode, {
39020
+ filePath: inputPath,
39021
+ includeMetaData: true,
39022
+ sourceResolver: sourceResolverSync
39023
+ });
38921
39024
  if (outputPath) {
38922
39025
  await writeFile(outputPath, JSON.stringify(ast, null, 2));
38923
39026
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -38931,25 +39034,10 @@ async function toAst(inputPath, outputPath) {
38931
39034
  process.exit(1);
38932
39035
  }
38933
39036
  }
38934
- function toAstSync(inputPath, outputPath) {
38935
- console.log(`[BattleScript ${version}] toAstSync: ${inputPath}`);
38936
- try {
38937
- const sourceCode = readFileSync(inputPath, "utf-8");
38938
- const ast = fromSourceCodeToFullAst(sourceCode, { filePath: inputPath, includeMetaData: true });
38939
- if (outputPath) ;
38940
- else {
38941
- return ast;
38942
- }
38943
- }
38944
- catch (error) {
38945
- console.error(`[BattleScript ${version}] Error:`, error.stack);
38946
- process.exit(1);
38947
- }
38948
- }
38949
39037
  async function fromAst(inputPath, outputPath) {
38950
39038
  console.log(`[BattleScript ${version}] fromAst: ${inputPath}`);
38951
39039
  try {
38952
- const astString = await readFile(inputPath, "utf-8");
39040
+ const astString = await sourceResolverAsync(inputPath);
38953
39041
  const ast = JSON.parse(astString);
38954
39042
  const code = fromFullAstToSourceCode(ast);
38955
39043
  await writeFile(outputPath, code);
@@ -38963,8 +39051,12 @@ async function fromAst(inputPath, outputPath) {
38963
39051
  async function toMiniAst(inputPath, outputPath) {
38964
39052
  console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
38965
39053
  try {
38966
- const sourceCode = await readFile(inputPath, "utf-8");
38967
- const ast = fromSourceCodeToMiniAst(sourceCode, { filePath: inputPath, includeMetaData: true });
39054
+ const sourceCode = await sourceResolverAsync(inputPath);
39055
+ const ast = fromSourceCodeToMiniAst(sourceCode, {
39056
+ filePath: inputPath,
39057
+ includeMetaData: true,
39058
+ sourceResolver: sourceResolverSync
39059
+ });
38968
39060
  await writeFile(outputPath, JSON.stringify(ast));
38969
39061
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
38970
39062
  }
@@ -38977,9 +39069,14 @@ async function toBinary(inputPath, outputPath) {
38977
39069
  console.log(`[BattleScript ${version}] toBinary: ${inputPath}`);
38978
39070
  try {
38979
39071
  console.log(`[BattleScript ${version}] Reading file contents: ${inputPath}`);
38980
- const sourceCode = await readFile(inputPath, "utf-8");
39072
+ const sourceCode = await sourceResolverAsync(inputPath);
38981
39073
  console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
38982
- const binary = fromSourceCodeToBinary(sourceCode, { includeMetaData: true, flatten: true, filePath: inputPath });
39074
+ const binary = fromSourceCodeToBinary(sourceCode, {
39075
+ includeMetaData: true,
39076
+ flatten: true,
39077
+ filePath: inputPath,
39078
+ sourceResolver: sourceResolverSync
39079
+ });
38983
39080
  console.log(`[BattleScript ${version}] Writing file contents: ${outputPath}`);
38984
39081
  await writeFile(outputPath, Buffer.concat(binary));
38985
39082
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -38991,11 +39088,12 @@ async function toBinary(inputPath, outputPath) {
38991
39088
  }
38992
39089
  async function miniAstToBinary(inputPath, outputPath) {
38993
39090
  try {
38994
- const sourceCode = await readFile(inputPath, "utf-8");
39091
+ const sourceCode = await sourceResolverAsync(inputPath);
38995
39092
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
38996
39093
  includeMetaData: true,
38997
39094
  flatten: true,
38998
- filePath: inputPath
39095
+ filePath: inputPath,
39096
+ sourceResolver: sourceResolverSync
38999
39097
  });
39000
39098
  await writeFile(outputPath, Buffer.concat(binary));
39001
39099
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -39019,7 +39117,7 @@ async function fromBinary(inputPath, outputPath) {
39019
39117
  }
39020
39118
  // async function toByteCode(inputPath: string, outputPath: string) {
39021
39119
  // try {
39022
- // const sourceCode = await readFile(inputPath, "utf-8");
39120
+ // const sourceCode = await sourceResolverAsync(inputPath);
39023
39121
  // const bcodeJson = fromSourceCodeToByteCode(sourceCode, {includeMetaData: true, flatten: true});
39024
39122
  // await writeFile(outputPath, JSON.stringify(bcodeJson).replace(/\[\[/g, "[\n[").replace(/]]/g, "]\n]").replace(/],/g, "],\n"));
39025
39123
  // console.log(`Compiled to ${outputPath}`);
@@ -39030,7 +39128,7 @@ async function fromBinary(inputPath, outputPath) {
39030
39128
  // }
39031
39129
  async function fromByteCode(inputPath, outputPath) {
39032
39130
  try {
39033
- const byteCodeJsonString = await readFile(inputPath, "utf-8");
39131
+ const byteCodeJsonString = await sourceResolverAsync(inputPath);
39034
39132
  const byteCodeJson = JSON.parse(byteCodeJsonString);
39035
39133
  const code = disassembleBytecode(byteCodeJson);
39036
39134
  await writeFile(outputPath, code.join("\n"));