@irrelon/battlescript 3.1.4 → 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,14 +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) {
27
+ console.log(`[BattleScript ${version}] toAst: ${inputPath}`);
18
28
  try {
19
- const sourceCode = await readFile(inputPath, "utf-8");
29
+ const sourceCode = await sourceResolverAsync(inputPath);
20
30
  const ast = fromSourceCodeToFullAst(sourceCode, {filePath: inputPath, includeMetaData: true});
21
31
 
22
32
  if (outputPath) {
@@ -32,9 +42,14 @@ export async function toAst(inputPath: string, outputPath?: string) {
32
42
  }
33
43
 
34
44
  export function toAstSync(inputPath: string, outputPath?: string) {
45
+ console.log(`[BattleScript ${version}] toAstSync: ${inputPath}`);
35
46
  try {
36
- const sourceCode = readFileSync(inputPath, "utf-8");
37
- 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
+ });
38
53
 
39
54
  if (outputPath) {
40
55
  writeFileSync(outputPath, JSON.stringify(ast, null, 2));
@@ -49,8 +64,9 @@ export function toAstSync(inputPath: string, outputPath?: string) {
49
64
  }
50
65
 
51
66
  export async function fromAst(inputPath: string, outputPath: string) {
67
+ console.log(`[BattleScript ${version}] fromAst: ${inputPath}`);
52
68
  try {
53
- const astString = await readFile(inputPath, "utf-8");
69
+ const astString = await sourceResolverAsync(inputPath);
54
70
  const ast = JSON.parse(astString);
55
71
  const code = fromFullAstToSourceCode(ast);
56
72
  await writeFile(outputPath, code);
@@ -62,8 +78,9 @@ export async function fromAst(inputPath: string, outputPath: string) {
62
78
  }
63
79
 
64
80
  export async function toMiniAst(inputPath: string, outputPath: string) {
81
+ console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
65
82
  try {
66
- const sourceCode = await readFile(inputPath, "utf-8");
83
+ const sourceCode = await sourceResolverAsync(inputPath);
67
84
  const ast = fromSourceCodeToMiniAst(sourceCode, {filePath: inputPath, includeMetaData: true});
68
85
 
69
86
  await writeFile(outputPath, JSON.stringify(ast));
@@ -75,9 +92,15 @@ export async function toMiniAst(inputPath: string, outputPath: string) {
75
92
  }
76
93
 
77
94
  export async function toBinary(inputPath: string, outputPath: string) {
95
+ console.log(`[BattleScript ${version}] toBinary: ${inputPath}`);
78
96
  try {
79
- const sourceCode = await readFile(inputPath, "utf-8");
97
+ console.log(`[BattleScript ${version}] Reading file contents: ${inputPath}`);
98
+ const sourceCode = await sourceResolverAsync(inputPath);
99
+
100
+ console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
80
101
  const binary = fromSourceCodeToBinary(sourceCode, {includeMetaData: true, flatten: true, filePath: inputPath});
102
+
103
+ console.log(`[BattleScript ${version}] Writing file contents: ${outputPath}`);
81
104
  await writeFile(outputPath, Buffer.concat(binary))
82
105
 
83
106
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -89,7 +112,7 @@ export async function toBinary(inputPath: string, outputPath: string) {
89
112
 
90
113
  export async function miniAstToBinary(inputPath: string, outputPath: string) {
91
114
  try {
92
- const sourceCode = await readFile(inputPath, "utf-8");
115
+ const sourceCode = await sourceResolverAsync(inputPath);
93
116
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
94
117
  includeMetaData: true,
95
118
  flatten: true,
@@ -118,7 +141,7 @@ export async function fromBinary(inputPath: string, outputPath: string) {
118
141
 
119
142
  // async function toByteCode(inputPath: string, outputPath: string) {
120
143
  // try {
121
- // const sourceCode = await readFile(inputPath, "utf-8");
144
+ // const sourceCode = await sourceResolverAsync(inputPath);
122
145
  // const bcodeJson = fromSourceCodeToByteCode(sourceCode, {includeMetaData: true, flatten: true});
123
146
  // await writeFile(outputPath, JSON.stringify(bcodeJson).replace(/\[\[/g, "[\n[").replace(/]]/g, "]\n]").replace(/],/g, "],\n"));
124
147
  // console.log(`Compiled to ${outputPath}`);
@@ -130,7 +153,7 @@ export async function fromBinary(inputPath: string, outputPath: string) {
130
153
 
131
154
  export async function fromByteCode(inputPath: string, outputPath: string) {
132
155
  try {
133
- const byteCodeJsonString = await readFile(inputPath, "utf-8");
156
+ const byteCodeJsonString = await sourceResolverAsync(inputPath);
134
157
  const byteCodeJson: ByteCodeJson = JSON.parse(byteCodeJsonString);
135
158
  const code = disassembleBytecode(byteCodeJson);
136
159
  await writeFile(outputPath, code.join("\n"));