@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.
@@ -1 +1 @@
1
- {"version":3,"file":"functionality.d.ts","sourceRoot":"","sources":["../../bin/functionality.ts"],"names":[],"mappings":"AAgBA,wBAAsB,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,gBAgBjE;AAED,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,OAgB/D;AAED,wBAAsB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAYlE;AAED,wBAAsB,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAYpE;AAED,wBAAsB,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAiBnE;AAED,wBAAsB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAe1E;AAED,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAUrE;AAcD,wBAAsB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAWvE;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIxE"}
1
+ {"version":3,"file":"functionality.d.ts","sourceRoot":"","sources":["../../bin/functionality.ts"],"names":[],"mappings":"AAyBA,wBAAsB,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,gBAoBjE;AAED,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,OAoB/D;AAED,wBAAsB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAYlE;AAED,wBAAsB,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAgBpE;AAED,wBAAsB,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAsBnE;AAED,wBAAsB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAgB1E;AAED,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAUrE;AAcD,wBAAsB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAWvE;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIxE"}
package/dist/index.js CHANGED
@@ -1,9 +1,6 @@
1
1
  import require$$0 from 'assert';
2
2
  import require$$0$1 from 'util';
3
3
  import { join, dirname } from 'path';
4
- import 'fs/promises';
5
- import { readFileSync } from 'fs';
6
- import { writeFileSync } from 'node:fs';
7
4
 
8
5
  var ATNDeserializer = {};
9
6
 
@@ -26300,7 +26297,7 @@ var body$2 = [
26300
26297
  kind: "FUNCTION_DECLARATION",
26301
26298
  returnType: {
26302
26299
  kind: "DATA_TYPE",
26303
- value: "float",
26300
+ value: "void",
26304
26301
  meta: {
26305
26302
  start: {
26306
26303
  line: 1,
@@ -26309,28 +26306,60 @@ var body$2 = [
26309
26306
  },
26310
26307
  stop: {
26311
26308
  line: 1,
26312
- column: 12,
26313
- offset: 11
26309
+ column: 11,
26310
+ offset: 10
26314
26311
  }
26315
26312
  }
26316
26313
  },
26317
26314
  id: {
26318
26315
  kind: "IDENTIFIER",
26319
- value: "std::gpio.readPin",
26316
+ value: "std::gpio.setPinMode",
26320
26317
  meta: {
26321
26318
  start: {
26322
26319
  line: 1,
26323
- column: 13,
26324
- offset: 13
26320
+ column: 12,
26321
+ offset: 12
26325
26322
  },
26326
26323
  stop: {
26327
26324
  line: 1,
26328
- column: 30,
26329
- offset: 29
26325
+ column: 32,
26326
+ offset: 31
26330
26327
  }
26331
26328
  }
26332
26329
  },
26333
26330
  params: [
26331
+ {
26332
+ kind: "IDENTIFIER",
26333
+ value: "newMode",
26334
+ meta: {
26335
+ start: {
26336
+ line: 1,
26337
+ column: 38,
26338
+ offset: 38
26339
+ },
26340
+ stop: {
26341
+ line: 1,
26342
+ column: 45,
26343
+ offset: 44
26344
+ }
26345
+ },
26346
+ type: {
26347
+ kind: "DATA_TYPE",
26348
+ value: "int",
26349
+ meta: {
26350
+ start: {
26351
+ line: 1,
26352
+ column: 34,
26353
+ offset: 34
26354
+ },
26355
+ stop: {
26356
+ line: 1,
26357
+ column: 37,
26358
+ offset: 36
26359
+ }
26360
+ }
26361
+ }
26362
+ }
26334
26363
  ],
26335
26364
  isNative: true,
26336
26365
  isPure: false,
@@ -26342,8 +26371,8 @@ var body$2 = [
26342
26371
  },
26343
26372
  stop: {
26344
26373
  line: 1,
26345
- column: 34,
26346
- offset: 33
26374
+ column: 47,
26375
+ offset: 46
26347
26376
  }
26348
26377
  }
26349
26378
  },
@@ -26351,17 +26380,68 @@ var body$2 = [
26351
26380
  kind: "FUNCTION_DECLARATION",
26352
26381
  returnType: {
26353
26382
  kind: "DATA_TYPE",
26354
- value: "void",
26383
+ value: "float",
26355
26384
  meta: {
26356
26385
  start: {
26357
26386
  line: 2,
26358
26387
  column: 7,
26359
- offset: 42
26388
+ offset: 55
26389
+ },
26390
+ stop: {
26391
+ line: 2,
26392
+ column: 12,
26393
+ offset: 59
26394
+ }
26395
+ }
26396
+ },
26397
+ id: {
26398
+ kind: "IDENTIFIER",
26399
+ value: "std::gpio.readPin",
26400
+ meta: {
26401
+ start: {
26402
+ line: 2,
26403
+ column: 13,
26404
+ offset: 61
26360
26405
  },
26361
26406
  stop: {
26362
26407
  line: 2,
26408
+ column: 30,
26409
+ offset: 77
26410
+ }
26411
+ }
26412
+ },
26413
+ params: [
26414
+ ],
26415
+ isNative: true,
26416
+ isPure: false,
26417
+ meta: {
26418
+ start: {
26419
+ line: 2,
26420
+ column: 0,
26421
+ offset: 48
26422
+ },
26423
+ stop: {
26424
+ line: 2,
26425
+ column: 34,
26426
+ offset: 81
26427
+ }
26428
+ }
26429
+ },
26430
+ {
26431
+ kind: "FUNCTION_DECLARATION",
26432
+ returnType: {
26433
+ kind: "DATA_TYPE",
26434
+ value: "void",
26435
+ meta: {
26436
+ start: {
26437
+ line: 3,
26438
+ column: 7,
26439
+ offset: 90
26440
+ },
26441
+ stop: {
26442
+ line: 3,
26363
26443
  column: 11,
26364
- offset: 45
26444
+ offset: 93
26365
26445
  }
26366
26446
  }
26367
26447
  },
@@ -26370,14 +26450,14 @@ var body$2 = [
26370
26450
  value: "std::gpio.writePin",
26371
26451
  meta: {
26372
26452
  start: {
26373
- line: 2,
26453
+ line: 3,
26374
26454
  column: 12,
26375
- offset: 47
26455
+ offset: 95
26376
26456
  },
26377
26457
  stop: {
26378
- line: 2,
26458
+ line: 3,
26379
26459
  column: 30,
26380
- offset: 64
26460
+ offset: 112
26381
26461
  }
26382
26462
  }
26383
26463
  },
@@ -26387,14 +26467,14 @@ var body$2 = [
26387
26467
  value: "newVal",
26388
26468
  meta: {
26389
26469
  start: {
26390
- line: 2,
26470
+ line: 3,
26391
26471
  column: 36,
26392
- offset: 71
26472
+ offset: 119
26393
26473
  },
26394
26474
  stop: {
26395
- line: 2,
26475
+ line: 3,
26396
26476
  column: 42,
26397
- offset: 76
26477
+ offset: 124
26398
26478
  }
26399
26479
  },
26400
26480
  type: {
@@ -26402,14 +26482,14 @@ var body$2 = [
26402
26482
  value: "int",
26403
26483
  meta: {
26404
26484
  start: {
26405
- line: 2,
26485
+ line: 3,
26406
26486
  column: 32,
26407
- offset: 67
26487
+ offset: 115
26408
26488
  },
26409
26489
  stop: {
26410
- line: 2,
26490
+ line: 3,
26411
26491
  column: 35,
26412
- offset: 69
26492
+ offset: 117
26413
26493
  }
26414
26494
  }
26415
26495
  }
@@ -26419,14 +26499,14 @@ var body$2 = [
26419
26499
  isPure: false,
26420
26500
  meta: {
26421
26501
  start: {
26422
- line: 2,
26502
+ line: 3,
26423
26503
  column: 0,
26424
- offset: 35
26504
+ offset: 83
26425
26505
  },
26426
26506
  stop: {
26427
- line: 2,
26507
+ line: 3,
26428
26508
  column: 44,
26429
- offset: 78
26509
+ offset: 126
26430
26510
  }
26431
26511
  }
26432
26512
  }
@@ -27937,25 +28017,9 @@ function getAllNativeLibraryFunctions(rootNode) {
27937
28017
  ];
27938
28018
  }
27939
28019
 
27940
- var version = "3.1.5";
28020
+ var version = "3.1.7";
27941
28021
  var binaryVersion = 7;
27942
28022
 
27943
- function toAstSync(inputPath, outputPath) {
27944
- console.log(`[BattleScript ${version}] toAstSync: ${inputPath}`);
27945
- try {
27946
- const sourceCode = readFileSync(inputPath, "utf-8");
27947
- const ast = fromSourceCodeToFullAst(sourceCode, { filePath: inputPath, includeMetaData: true });
27948
- if (outputPath) ;
27949
- else {
27950
- return ast;
27951
- }
27952
- }
27953
- catch (error) {
27954
- console.error(`[BattleScript ${version}] Error:`, error.stack);
27955
- process.exit(1);
27956
- }
27957
- }
27958
-
27959
28023
  const RootNodeLayout = [{
27960
28024
  key: "kind",
27961
28025
  shape: [EnumNodeLayoutEntry.LAYOUT_POINTER_KIND],
@@ -27993,7 +28057,12 @@ const rootToAstNode = (instance, ctx) => {
27993
28057
  // Check we can locate the file
27994
28058
  console.log(`[BattleScript ${version}] Processing import compilation unit: `, importNode.source.value);
27995
28059
  const filePath = join(dirname(instance._filePath), importNode.source.value);
27996
- const importedAst = toAstSync(filePath);
28060
+ const importedSourceCode = instance.resolveSourceCode(filePath);
28061
+ const importedAst = fromSourceCodeToFullAst(importedSourceCode, {
28062
+ filePath,
28063
+ includeMetaData: true,
28064
+ sourceResolver: instance._sourceResolver
28065
+ });
27997
28066
  return importedAst.body;
27998
28067
  }).concat(standardLibraryDeclarations).filter((astNode) => {
27999
28068
  // TODO: We should move this to just before we complete the AST generation
@@ -28270,7 +28339,7 @@ function getMetaData(ctx, extra) {
28270
28339
  };
28271
28340
  }
28272
28341
  class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTreeVisitor {
28273
- constructor(filePath, sourceCode) {
28342
+ constructor(filePath, sourceCode, sourceResolver) {
28274
28343
  super();
28275
28344
  this._options = {
28276
28345
  includeMetaData: true
@@ -28288,6 +28357,16 @@ class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTre
28288
28357
  return;
28289
28358
  this._sourceCode = newSourceCode;
28290
28359
  };
28360
+ this.resolveSourceCode = (filePath) => {
28361
+ if (this._sourceResolver) {
28362
+ const resolved = this._sourceResolver(filePath);
28363
+ if (resolved !== undefined) {
28364
+ return resolved;
28365
+ }
28366
+ throw new Error(`Source resolver could not resolve source code for file: ${filePath}`);
28367
+ }
28368
+ throw new Error(`Could not resolve source code for file: ${filePath}. No source resolver provided.`);
28369
+ };
28291
28370
  this.extractInlineComment = (ctx) => {
28292
28371
  if (!ctx)
28293
28372
  return;
@@ -28517,6 +28596,7 @@ class BattleScriptToAST extends AbstractParseTreeVisitorExports.AbstractParseTre
28517
28596
  };
28518
28597
  this._filePath = filePath;
28519
28598
  this._sourceCode = sourceCode;
28599
+ this._sourceResolver = sourceResolver;
28520
28600
  }
28521
28601
  visit(node, options) {
28522
28602
  if (options) {
@@ -28972,28 +29052,26 @@ class BattleScriptToMiniAST {
28972
29052
  return requiredCount;
28973
29053
  }, 0);
28974
29054
  if (functionArgs.length < requiredCount) {
28975
- functionParams.reduce((requiredCount, param, index) => {
28976
- if (param.kind === "IDENTIFIER") {
28977
- if (!functionArgs[index]) {
28978
- throw new Error(formatCompilerError(this._filePath, this._sourceCode, "error", node.meta, `Call to function ${prettyPrintFunctionDeclaration(functionNode)} is missing required argument \`${this.nodeResolver.visit(param)}\``));
28979
- }
28980
- requiredCount++;
29055
+ functionParams.forEach((param, index) => {
29056
+ if (functionArgs[index]) {
28981
29057
  finalArgs.push(functionArgs[index]);
29058
+ return;
29059
+ }
29060
+ if (param.kind === "IDENTIFIER") {
29061
+ throw new Error(formatCompilerError(this._filePath, this._sourceCode, "error", node.meta, `Call to function ${prettyPrintFunctionDeclaration(functionNode)} is missing required argument \`${this.nodeResolver.visit(param)}\``));
28982
29062
  }
28983
29063
  else if (param.kind === "ASSIGNMENT_PATTERN") {
28984
- if (!functionArgs[index]) {
28985
- // We don't have a value but we do have a default, use it
28986
- // We use clone here because we want to create an entirely new node entry in the node array
28987
- // just for this one argument so that we don't point at a node index (the default argument
28988
- // in the function definition) because that would generate a non-contiguous start index
28989
- // and count for the function call arguments. However, when the meta array is compiled,
28990
- // the cloned node will still have the same meta data so will highlight the original
28991
- // function definition parameter which shows where the value came from.
28992
- finalArgs.push(clone(param.right));
28993
- }
29064
+ // We don't have a value but we do have a default, use it
29065
+ // We use clone here because we want to create an entirely new node entry in the node array
29066
+ // just for this one argument so that we don't point at a node index (the default argument
29067
+ // in the function definition) because that would generate a non-contiguous start index
29068
+ // and count for the function call arguments. However, when the meta array is compiled,
29069
+ // the cloned node will still have the same meta data so will highlight the original
29070
+ // function definition parameter which shows where the value came from.
29071
+ finalArgs.push(clone(param.right));
28994
29072
  }
28995
- return requiredCount;
28996
- }, 0);
29073
+ return;
29074
+ });
28997
29075
  }
28998
29076
  else {
28999
29077
  return functionArgs;
@@ -39079,9 +39157,7 @@ function fromSourceCodeToFullAst(inputText, options) {
39079
39157
  const parser = getParser(inputText);
39080
39158
  const tree = parser.root();
39081
39159
  console.log(`[BattleScript ${version}] Transforming source code -> AST...`);
39082
- const transformer = new BattleScriptToAST(options?.filePath || "-", inputText);
39083
- transformer.filePath(options?.filePath);
39084
- transformer.sourceCode(inputText);
39160
+ const transformer = new BattleScriptToAST(options?.filePath || "-", inputText, options?.sourceResolver);
39085
39161
  return transformer.visit(tree, options);
39086
39162
  }
39087
39163
  function fromFullAstToSourceCode(ast, options = { includeMetaData: true }) {
@@ -39093,8 +39169,6 @@ function fromSourceCodeToMiniAst(source, options = { includeMetaData: true }) {
39093
39169
  const ast = fromSourceCodeToFullAst(source, options);
39094
39170
  console.log(`[BattleScript ${version}] Transforming AST -> MiniAST...`);
39095
39171
  const transformer = new BattleScriptToMiniAST(options?.filePath || "-", source);
39096
- transformer.filePath(options?.filePath);
39097
- transformer.sourceCode(source); // Add for debugging purposes
39098
39172
  return transformer.transform(ast, options);
39099
39173
  }
39100
39174
  function fromMiniAstToBinary(miniAst, options = { includeMetaData: true }) {
@@ -39112,8 +39186,7 @@ function fromSourceCodeToByteCode(source, options = { includeMetaData: true }) {
39112
39186
  if (typeof source === "string") {
39113
39187
  const parser = getParser(source);
39114
39188
  const tree = parser.root();
39115
- const transformer = new BattleScriptToAST(options?.filePath || "-", source);
39116
- transformer.sourceCode(source);
39189
+ const transformer = new BattleScriptToAST(options?.filePath || "-", source, options?.sourceResolver);
39117
39190
  transformer.visit(tree, options);
39118
39191
  }
39119
39192
  // const transformer = new BattleScriptToByteCode();