@irrelon/battlescript 3.1.5 → 3.1.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.
@@ -9,15 +9,24 @@ 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");
29
+ const sourceCode = await sourceResolverAsync(inputPath);
21
30
  const ast = fromSourceCodeToFullAst(sourceCode, {filePath: inputPath, includeMetaData: true});
22
31
 
23
32
  if (outputPath) {
@@ -35,8 +44,12 @@ export async function toAst(inputPath: string, outputPath?: string) {
35
44
  export function toAstSync(inputPath: string, outputPath?: string) {
36
45
  console.log(`[BattleScript ${version}] toAstSync: ${inputPath}`);
37
46
  try {
38
- const sourceCode = readFileSync(inputPath, "utf-8");
39
- const ast = fromSourceCodeToFullAst(sourceCode, {filePath: inputPath, includeMetaData: true});
47
+ const sourceCode = sourceResolverSync(inputPath);
48
+ const ast = fromSourceCodeToFullAst(sourceCode, {
49
+ filePath: inputPath,
50
+ includeMetaData: true,
51
+ sourceResolver: sourceResolverSync
52
+ });
40
53
 
41
54
  if (outputPath) {
42
55
  writeFileSync(outputPath, JSON.stringify(ast, null, 2));
@@ -53,7 +66,7 @@ export function toAstSync(inputPath: string, outputPath?: string) {
53
66
  export async function fromAst(inputPath: string, outputPath: string) {
54
67
  console.log(`[BattleScript ${version}] fromAst: ${inputPath}`);
55
68
  try {
56
- const astString = await readFile(inputPath, "utf-8");
69
+ const astString = await sourceResolverAsync(inputPath);
57
70
  const ast = JSON.parse(astString);
58
71
  const code = fromFullAstToSourceCode(ast);
59
72
  await writeFile(outputPath, code);
@@ -67,7 +80,7 @@ export async function fromAst(inputPath: string, outputPath: string) {
67
80
  export async function toMiniAst(inputPath: string, outputPath: string) {
68
81
  console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
69
82
  try {
70
- const sourceCode = await readFile(inputPath, "utf-8");
83
+ const sourceCode = await sourceResolverAsync(inputPath);
71
84
  const ast = fromSourceCodeToMiniAst(sourceCode, {filePath: inputPath, includeMetaData: true});
72
85
 
73
86
  await writeFile(outputPath, JSON.stringify(ast));
@@ -82,7 +95,7 @@ export async function toBinary(inputPath: string, outputPath: string) {
82
95
  console.log(`[BattleScript ${version}] toBinary: ${inputPath}`);
83
96
  try {
84
97
  console.log(`[BattleScript ${version}] Reading file contents: ${inputPath}`);
85
- const sourceCode = await readFile(inputPath, "utf-8");
98
+ const sourceCode = await sourceResolverAsync(inputPath);
86
99
 
87
100
  console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
88
101
  const binary = fromSourceCodeToBinary(sourceCode, {includeMetaData: true, flatten: true, filePath: inputPath});
@@ -99,7 +112,7 @@ export async function toBinary(inputPath: string, outputPath: string) {
99
112
 
100
113
  export async function miniAstToBinary(inputPath: string, outputPath: string) {
101
114
  try {
102
- const sourceCode = await readFile(inputPath, "utf-8");
115
+ const sourceCode = await sourceResolverAsync(inputPath);
103
116
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
104
117
  includeMetaData: true,
105
118
  flatten: true,
@@ -128,7 +141,7 @@ export async function fromBinary(inputPath: string, outputPath: string) {
128
141
 
129
142
  // async function toByteCode(inputPath: string, outputPath: string) {
130
143
  // try {
131
- // const sourceCode = await readFile(inputPath, "utf-8");
144
+ // const sourceCode = await sourceResolverAsync(inputPath);
132
145
  // const bcodeJson = fromSourceCodeToByteCode(sourceCode, {includeMetaData: true, flatten: true});
133
146
  // await writeFile(outputPath, JSON.stringify(bcodeJson).replace(/\[\[/g, "[\n[").replace(/]]/g, "]\n]").replace(/],/g, "],\n"));
134
147
  // console.log(`Compiled to ${outputPath}`);
@@ -140,7 +153,7 @@ export async function fromBinary(inputPath: string, outputPath: string) {
140
153
 
141
154
  export async function fromByteCode(inputPath: string, outputPath: string) {
142
155
  try {
143
- const byteCodeJsonString = await readFile(inputPath, "utf-8");
156
+ const byteCodeJsonString = await sourceResolverAsync(inputPath);
144
157
  const byteCodeJson: ByteCodeJson = JSON.parse(byteCodeJsonString);
145
158
  const code = disassembleBytecode(byteCodeJson);
146
159
  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
- import { readFileSync } from 'fs';
7
- import { writeFileSync } from 'node:fs';
6
+ import 'fs';
7
+ import 'node:fs';
8
8
 
9
9
  var ATNDeserializer = {};
10
10
 
@@ -27902,7 +27902,7 @@ function getAllNativeLibraryFunctions(rootNode) {
27902
27902
  ];
27903
27903
  }
27904
27904
 
27905
- var version = "3.1.5";
27905
+ var version = "3.1.6";
27906
27906
  var binaryVersion = 7;
27907
27907
 
27908
27908
  const RootNodeLayout = [{
@@ -27942,7 +27942,12 @@ const rootToAstNode = (instance, ctx) => {
27942
27942
  // Check we can locate the file
27943
27943
  console.log(`[BattleScript ${version}] Processing import compilation unit: `, importNode.source.value);
27944
27944
  const filePath = join(dirname(instance._filePath), importNode.source.value);
27945
- const importedAst = toAstSync(filePath);
27945
+ const importedSourceCode = instance.resolveSourceCode(filePath);
27946
+ const importedAst = fromSourceCodeToFullAst(importedSourceCode, {
27947
+ filePath,
27948
+ includeMetaData: true,
27949
+ sourceResolver: instance._sourceResolver
27950
+ });
27946
27951
  return importedAst.body;
27947
27952
  }).concat(standardLibraryDeclarations).filter((astNode) => {
27948
27953
  // TODO: We should move this to just before we complete the AST generation
@@ -28187,7 +28192,7 @@ function getMetaData(ctx, extra) {
28187
28192
  };
28188
28193
  }
28189
28194
  class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTreeVisitor {
28190
- constructor(filePath, sourceCode) {
28195
+ constructor(filePath, sourceCode, sourceResolver) {
28191
28196
  super();
28192
28197
  this._options = {
28193
28198
  includeMetaData: true
@@ -28205,6 +28210,16 @@ class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTre
28205
28210
  return;
28206
28211
  this._sourceCode = newSourceCode;
28207
28212
  };
28213
+ this.resolveSourceCode = (filePath) => {
28214
+ if (this._sourceResolver) {
28215
+ const resolved = this._sourceResolver(filePath);
28216
+ if (resolved !== undefined) {
28217
+ return resolved;
28218
+ }
28219
+ throw new Error(`Source resolver could not resolve source code for file: ${filePath}`);
28220
+ }
28221
+ throw new Error(`Could not resolve source code for file: ${filePath}. No source resolver provided.`);
28222
+ };
28208
28223
  this.extractInlineComment = (ctx) => {
28209
28224
  if (!ctx)
28210
28225
  return;
@@ -28434,6 +28449,7 @@ class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTre
28434
28449
  };
28435
28450
  this._filePath = filePath;
28436
28451
  this._sourceCode = sourceCode;
28452
+ this._sourceResolver = sourceResolver;
28437
28453
  }
28438
28454
  visit(node, options) {
28439
28455
  if (options) {
@@ -38796,7 +38812,7 @@ function fromSourceCodeToFullAst(inputText, options) {
38796
38812
  const parser = getParser(inputText);
38797
38813
  const tree = parser.root();
38798
38814
  console.log(`[BattleScript ${version}] Transforming source code -> AST...`);
38799
- const transformer = new BattleScriptToAST(options?.filePath || "-", inputText);
38815
+ const transformer = new BattleScriptToAST(options?.filePath || "-", inputText, options?.sourceResolver);
38800
38816
  transformer.filePath(options?.filePath);
38801
38817
  transformer.sourceCode(inputText);
38802
38818
  return transformer.visit(tree, options);
@@ -38913,10 +38929,13 @@ function disassembleBytecode([stringTable, bytecode]) {
38913
38929
  return output;
38914
38930
  }
38915
38931
 
38932
+ const sourceResolverAsync = (filePath) => {
38933
+ return readFile(filePath, "utf-8");
38934
+ };
38916
38935
  async function toAst(inputPath, outputPath) {
38917
38936
  console.log(`[BattleScript ${version}] toAst: ${inputPath}`);
38918
38937
  try {
38919
- const sourceCode = await readFile(inputPath, "utf-8");
38938
+ const sourceCode = await sourceResolverAsync(inputPath);
38920
38939
  const ast = fromSourceCodeToFullAst(sourceCode, { filePath: inputPath, includeMetaData: true });
38921
38940
  if (outputPath) {
38922
38941
  await writeFile(outputPath, JSON.stringify(ast, null, 2));
@@ -38931,25 +38950,10 @@ async function toAst(inputPath, outputPath) {
38931
38950
  process.exit(1);
38932
38951
  }
38933
38952
  }
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
38953
  async function fromAst(inputPath, outputPath) {
38950
38954
  console.log(`[BattleScript ${version}] fromAst: ${inputPath}`);
38951
38955
  try {
38952
- const astString = await readFile(inputPath, "utf-8");
38956
+ const astString = await sourceResolverAsync(inputPath);
38953
38957
  const ast = JSON.parse(astString);
38954
38958
  const code = fromFullAstToSourceCode(ast);
38955
38959
  await writeFile(outputPath, code);
@@ -38963,7 +38967,7 @@ async function fromAst(inputPath, outputPath) {
38963
38967
  async function toMiniAst(inputPath, outputPath) {
38964
38968
  console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
38965
38969
  try {
38966
- const sourceCode = await readFile(inputPath, "utf-8");
38970
+ const sourceCode = await sourceResolverAsync(inputPath);
38967
38971
  const ast = fromSourceCodeToMiniAst(sourceCode, { filePath: inputPath, includeMetaData: true });
38968
38972
  await writeFile(outputPath, JSON.stringify(ast));
38969
38973
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -38977,7 +38981,7 @@ async function toBinary(inputPath, outputPath) {
38977
38981
  console.log(`[BattleScript ${version}] toBinary: ${inputPath}`);
38978
38982
  try {
38979
38983
  console.log(`[BattleScript ${version}] Reading file contents: ${inputPath}`);
38980
- const sourceCode = await readFile(inputPath, "utf-8");
38984
+ const sourceCode = await sourceResolverAsync(inputPath);
38981
38985
  console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
38982
38986
  const binary = fromSourceCodeToBinary(sourceCode, { includeMetaData: true, flatten: true, filePath: inputPath });
38983
38987
  console.log(`[BattleScript ${version}] Writing file contents: ${outputPath}`);
@@ -38991,7 +38995,7 @@ async function toBinary(inputPath, outputPath) {
38991
38995
  }
38992
38996
  async function miniAstToBinary(inputPath, outputPath) {
38993
38997
  try {
38994
- const sourceCode = await readFile(inputPath, "utf-8");
38998
+ const sourceCode = await sourceResolverAsync(inputPath);
38995
38999
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
38996
39000
  includeMetaData: true,
38997
39001
  flatten: true,
@@ -39019,7 +39023,7 @@ async function fromBinary(inputPath, outputPath) {
39019
39023
  }
39020
39024
  // async function toByteCode(inputPath: string, outputPath: string) {
39021
39025
  // try {
39022
- // const sourceCode = await readFile(inputPath, "utf-8");
39026
+ // const sourceCode = await sourceResolverAsync(inputPath);
39023
39027
  // const bcodeJson = fromSourceCodeToByteCode(sourceCode, {includeMetaData: true, flatten: true});
39024
39028
  // await writeFile(outputPath, JSON.stringify(bcodeJson).replace(/\[\[/g, "[\n[").replace(/]]/g, "]\n]").replace(/],/g, "],\n"));
39025
39029
  // console.log(`Compiled to ${outputPath}`);
@@ -39030,7 +39034,7 @@ async function fromBinary(inputPath, outputPath) {
39030
39034
  // }
39031
39035
  async function fromByteCode(inputPath, outputPath) {
39032
39036
  try {
39033
- const byteCodeJsonString = await readFile(inputPath, "utf-8");
39037
+ const byteCodeJsonString = await sourceResolverAsync(inputPath);
39034
39038
  const byteCodeJson = JSON.parse(byteCodeJsonString);
39035
39039
  const code = disassembleBytecode(byteCodeJson);
39036
39040
  await writeFile(outputPath, code.join("\n"));