@irrelon/battlescript 3.1.6 → 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.
@@ -27,7 +27,11 @@ export async function toAst(inputPath: string, outputPath?: string) {
27
27
  console.log(`[BattleScript ${version}] toAst: ${inputPath}`);
28
28
  try {
29
29
  const sourceCode = await sourceResolverAsync(inputPath);
30
- const ast = fromSourceCodeToFullAst(sourceCode, {filePath: inputPath, includeMetaData: true});
30
+ const ast = fromSourceCodeToFullAst(sourceCode, {
31
+ filePath: inputPath,
32
+ includeMetaData: true,
33
+ sourceResolver: sourceResolverSync
34
+ });
31
35
 
32
36
  if (outputPath) {
33
37
  await writeFile(outputPath, JSON.stringify(ast, null, 2));
@@ -81,7 +85,11 @@ export async function toMiniAst(inputPath: string, outputPath: string) {
81
85
  console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
82
86
  try {
83
87
  const sourceCode = await sourceResolverAsync(inputPath);
84
- const ast = fromSourceCodeToMiniAst(sourceCode, {filePath: inputPath, includeMetaData: true});
88
+ const ast = fromSourceCodeToMiniAst(sourceCode, {
89
+ filePath: inputPath,
90
+ includeMetaData: true,
91
+ sourceResolver: sourceResolverSync
92
+ });
85
93
 
86
94
  await writeFile(outputPath, JSON.stringify(ast));
87
95
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -98,7 +106,12 @@ export async function toBinary(inputPath: string, outputPath: string) {
98
106
  const sourceCode = await sourceResolverAsync(inputPath);
99
107
 
100
108
  console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
101
- 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
+ });
102
115
 
103
116
  console.log(`[BattleScript ${version}] Writing file contents: ${outputPath}`);
104
117
  await writeFile(outputPath, Buffer.concat(binary))
@@ -116,7 +129,8 @@ export async function miniAstToBinary(inputPath: string, outputPath: string) {
116
129
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
117
130
  includeMetaData: true,
118
131
  flatten: true,
119
- filePath: inputPath
132
+ filePath: inputPath,
133
+ sourceResolver: sourceResolverSync
120
134
  });
121
135
  await writeFile(outputPath, Buffer.concat(binary))
122
136
 
package/bin/index.js CHANGED
@@ -3,7 +3,7 @@ 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 'fs';
6
+ import { readFileSync } from 'fs';
7
7
  import 'node:fs';
8
8
 
9
9
  var ATNDeserializer = {};
@@ -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.6";
27988
+ var version = "3.1.7";
27906
27989
  var binaryVersion = 7;
27907
27990
 
27908
27991
  const RootNodeLayout = [{
@@ -28881,28 +28964,26 @@ class BattleScriptToMiniAST {
28881
28964
  return requiredCount;
28882
28965
  }, 0);
28883
28966
  if (functionArgs.length < requiredCount) {
28884
- functionParams.reduce((requiredCount, param, index) => {
28885
- if (param.kind === "IDENTIFIER") {
28886
- if (!functionArgs[index]) {
28887
- throw new Error(formatCompilerError(this._filePath, this._sourceCode, "error", node.meta, `Call to function ${prettyPrintFunctionDeclaration(functionNode)} is missing required argument \`${this.nodeResolver.visit(param)}\``));
28888
- }
28889
- requiredCount++;
28967
+ functionParams.forEach((param, index) => {
28968
+ if (functionArgs[index]) {
28890
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)}\``));
28891
28974
  }
28892
28975
  else if (param.kind === "ASSIGNMENT_PATTERN") {
28893
- if (!functionArgs[index]) {
28894
- // We don't have a value but we do have a default, use it
28895
- // We use clone here because we want to create an entirely new node entry in the node array
28896
- // just for this one argument so that we don't point at a node index (the default argument
28897
- // in the function definition) because that would generate a non-contiguous start index
28898
- // and count for the function call arguments. However, when the meta array is compiled,
28899
- // the cloned node will still have the same meta data so will highlight the original
28900
- // function definition parameter which shows where the value came from.
28901
- finalArgs.push(clone(param.right));
28902
- }
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));
28903
28984
  }
28904
- return requiredCount;
28905
- }, 0);
28985
+ return;
28986
+ });
28906
28987
  }
28907
28988
  else {
28908
28989
  return functionArgs;
@@ -38813,8 +38894,6 @@ function fromSourceCodeToFullAst(inputText, options) {
38813
38894
  const tree = parser.root();
38814
38895
  console.log(`[BattleScript ${version}] Transforming source code -> AST...`);
38815
38896
  const transformer = new BattleScriptToAST(options?.filePath || "-", inputText, options?.sourceResolver);
38816
- transformer.filePath(options?.filePath);
38817
- transformer.sourceCode(inputText);
38818
38897
  return transformer.visit(tree, options);
38819
38898
  }
38820
38899
  function fromFullAstToSourceCode(ast, options = { includeMetaData: true }) {
@@ -38826,8 +38905,6 @@ function fromSourceCodeToMiniAst(source, options = { includeMetaData: true }) {
38826
38905
  const ast = fromSourceCodeToFullAst(source, options);
38827
38906
  console.log(`[BattleScript ${version}] Transforming AST -> MiniAST...`);
38828
38907
  const transformer = new BattleScriptToMiniAST(options?.filePath || "-", source);
38829
- transformer.filePath(options?.filePath);
38830
- transformer.sourceCode(source); // Add for debugging purposes
38831
38908
  return transformer.transform(ast, options);
38832
38909
  }
38833
38910
  function fromMiniAstToBinary(miniAst, options = { includeMetaData: true }) {
@@ -38932,11 +39009,18 @@ function disassembleBytecode([stringTable, bytecode]) {
38932
39009
  const sourceResolverAsync = (filePath) => {
38933
39010
  return readFile(filePath, "utf-8");
38934
39011
  };
39012
+ const sourceResolverSync = (filePath) => {
39013
+ return readFileSync(filePath, "utf-8");
39014
+ };
38935
39015
  async function toAst(inputPath, outputPath) {
38936
39016
  console.log(`[BattleScript ${version}] toAst: ${inputPath}`);
38937
39017
  try {
38938
39018
  const sourceCode = await sourceResolverAsync(inputPath);
38939
- const ast = fromSourceCodeToFullAst(sourceCode, { filePath: inputPath, includeMetaData: true });
39019
+ const ast = fromSourceCodeToFullAst(sourceCode, {
39020
+ filePath: inputPath,
39021
+ includeMetaData: true,
39022
+ sourceResolver: sourceResolverSync
39023
+ });
38940
39024
  if (outputPath) {
38941
39025
  await writeFile(outputPath, JSON.stringify(ast, null, 2));
38942
39026
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -38968,7 +39052,11 @@ async function toMiniAst(inputPath, outputPath) {
38968
39052
  console.log(`[BattleScript ${version}] toMiniAst: ${inputPath}`);
38969
39053
  try {
38970
39054
  const sourceCode = await sourceResolverAsync(inputPath);
38971
- const ast = fromSourceCodeToMiniAst(sourceCode, { filePath: inputPath, includeMetaData: true });
39055
+ const ast = fromSourceCodeToMiniAst(sourceCode, {
39056
+ filePath: inputPath,
39057
+ includeMetaData: true,
39058
+ sourceResolver: sourceResolverSync
39059
+ });
38972
39060
  await writeFile(outputPath, JSON.stringify(ast));
38973
39061
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
38974
39062
  }
@@ -38983,7 +39071,12 @@ async function toBinary(inputPath, outputPath) {
38983
39071
  console.log(`[BattleScript ${version}] Reading file contents: ${inputPath}`);
38984
39072
  const sourceCode = await sourceResolverAsync(inputPath);
38985
39073
  console.log(`[BattleScript ${version}] fromSourceCodeToBinary: ${inputPath}`);
38986
- 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
+ });
38987
39080
  console.log(`[BattleScript ${version}] Writing file contents: ${outputPath}`);
38988
39081
  await writeFile(outputPath, Buffer.concat(binary));
38989
39082
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);
@@ -38999,7 +39092,8 @@ async function miniAstToBinary(inputPath, outputPath) {
38999
39092
  const binary = fromMiniAstToBinary(JSON.parse(sourceCode), {
39000
39093
  includeMetaData: true,
39001
39094
  flatten: true,
39002
- filePath: inputPath
39095
+ filePath: inputPath,
39096
+ sourceResolver: sourceResolverSync
39003
39097
  });
39004
39098
  await writeFile(outputPath, Buffer.concat(binary));
39005
39099
  console.log(`[BattleScript ${version}] Compiled to ${outputPath}`);