c-next 0.1.62 → 0.1.63

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.
Files changed (55) hide show
  1. package/README.md +86 -63
  2. package/package.json +1 -1
  3. package/src/transpiler/Transpiler.ts +3 -2
  4. package/src/transpiler/__tests__/DualCodePaths.test.ts +1 -1
  5. package/src/transpiler/__tests__/Transpiler.coverage.test.ts +1 -1
  6. package/src/transpiler/__tests__/Transpiler.test.ts +0 -23
  7. package/src/transpiler/logic/symbols/cnext/collectors/StructCollector.ts +156 -70
  8. package/src/transpiler/logic/symbols/cnext/collectors/VariableCollector.ts +31 -6
  9. package/src/transpiler/logic/symbols/cnext/utils/TypeUtils.ts +43 -11
  10. package/src/transpiler/output/codegen/CodeGenState.ts +811 -0
  11. package/src/transpiler/output/codegen/CodeGenerator.ts +817 -1377
  12. package/src/transpiler/output/codegen/TypeResolver.ts +193 -149
  13. package/src/transpiler/output/codegen/TypeValidator.ts +148 -370
  14. package/src/transpiler/output/codegen/__tests__/CodeGenState.test.ts +446 -0
  15. package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +326 -60
  16. package/src/transpiler/output/codegen/__tests__/TrackVariableTypeHelpers.test.ts +1 -1
  17. package/src/transpiler/output/codegen/__tests__/TypeResolver.test.ts +435 -196
  18. package/src/transpiler/output/codegen/__tests__/TypeValidator.resolution.test.ts +51 -67
  19. package/src/transpiler/output/codegen/__tests__/TypeValidator.test.ts +495 -471
  20. package/src/transpiler/output/codegen/analysis/MemberChainAnalyzer.ts +39 -43
  21. package/src/transpiler/output/codegen/analysis/StringLengthCounter.ts +52 -55
  22. package/src/transpiler/output/codegen/analysis/__tests__/MemberChainAnalyzer.test.ts +122 -62
  23. package/src/transpiler/output/codegen/analysis/__tests__/StringLengthCounter.test.ts +101 -144
  24. package/src/transpiler/output/codegen/assignment/AssignmentClassifier.ts +143 -126
  25. package/src/transpiler/output/codegen/assignment/__tests__/AssignmentClassifier.test.ts +287 -320
  26. package/src/transpiler/output/codegen/generators/GeneratorRegistry.ts +12 -0
  27. package/src/transpiler/output/codegen/generators/__tests__/GeneratorRegistry.test.ts +28 -1
  28. package/src/transpiler/output/codegen/generators/declarationGenerators/ArrayDimensionUtils.ts +67 -0
  29. package/src/transpiler/output/codegen/generators/declarationGenerators/ScopeGenerator.ts +121 -51
  30. package/src/transpiler/output/codegen/generators/declarationGenerators/StructGenerator.ts +100 -23
  31. package/src/transpiler/output/codegen/generators/declarationGenerators/__tests__/ArrayDimensionUtils.test.ts +125 -0
  32. package/src/transpiler/output/codegen/generators/declarationGenerators/__tests__/ScopeGenerator.test.ts +157 -4
  33. package/src/transpiler/output/codegen/generators/support/HelperGenerator.ts +23 -22
  34. package/src/transpiler/output/codegen/helpers/ArrayInitHelper.ts +54 -61
  35. package/src/transpiler/output/codegen/helpers/AssignmentExpectedTypeResolver.ts +21 -30
  36. package/src/transpiler/output/codegen/helpers/AssignmentValidator.ts +56 -53
  37. package/src/transpiler/output/codegen/helpers/CppModeHelper.ts +22 -30
  38. package/src/transpiler/output/codegen/helpers/EnumAssignmentValidator.ts +108 -50
  39. package/src/transpiler/output/codegen/helpers/FloatBitHelper.ts +16 -31
  40. package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +103 -96
  41. package/src/transpiler/output/codegen/helpers/TypeGenerationHelper.ts +9 -0
  42. package/src/transpiler/output/codegen/helpers/__tests__/ArrayInitHelper.test.ts +58 -103
  43. package/src/transpiler/output/codegen/helpers/__tests__/AssignmentExpectedTypeResolver.test.ts +97 -40
  44. package/src/transpiler/output/codegen/helpers/__tests__/AssignmentValidator.test.ts +223 -128
  45. package/src/transpiler/output/codegen/helpers/__tests__/CppModeHelper.test.ts +68 -41
  46. package/src/transpiler/output/codegen/helpers/__tests__/EnumAssignmentValidator.test.ts +198 -47
  47. package/src/transpiler/output/codegen/helpers/__tests__/FloatBitHelper.test.ts +39 -37
  48. package/src/transpiler/output/codegen/helpers/__tests__/StringDeclHelper.test.ts +191 -453
  49. package/src/transpiler/output/codegen/resolution/EnumTypeResolver.ts +229 -0
  50. package/src/transpiler/output/codegen/resolution/ScopeResolver.ts +60 -0
  51. package/src/transpiler/output/codegen/resolution/SizeofResolver.ts +177 -0
  52. package/src/transpiler/output/codegen/resolution/__tests__/EnumTypeResolver.test.ts +336 -0
  53. package/src/transpiler/output/codegen/resolution/__tests__/SizeofResolver.test.ts +201 -0
  54. package/src/transpiler/output/codegen/types/ITypeResolverDeps.ts +0 -23
  55. package/src/transpiler/output/codegen/types/ITypeValidatorDeps.ts +0 -53
@@ -1466,7 +1466,7 @@ describe("CodeGenerator", () => {
1466
1466
  describe("Array declarations", () => {
1467
1467
  it("should generate array declaration", () => {
1468
1468
  const source = `
1469
- u32 arr[10];
1469
+ u32[10] arr;
1470
1470
  void main() { }
1471
1471
  `;
1472
1472
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -1485,7 +1485,7 @@ describe("CodeGenerator", () => {
1485
1485
 
1486
1486
  it("should generate array with initializer", () => {
1487
1487
  const source = `
1488
- u32 arr[3] <- [1, 2, 3];
1488
+ u32[3] arr <- [1, 2, 3];
1489
1489
  void main() { }
1490
1490
  `;
1491
1491
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -1505,7 +1505,7 @@ describe("CodeGenerator", () => {
1505
1505
 
1506
1506
  it("should generate multi-dimensional array", () => {
1507
1507
  const source = `
1508
- u32 matrix[3][3];
1508
+ u32[3] matrix[3];
1509
1509
  void main() { }
1510
1510
  `;
1511
1511
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -2586,7 +2586,7 @@ describe("CodeGenerator", () => {
2586
2586
  describe("Array element assignment", () => {
2587
2587
  it("should generate array element assignment", () => {
2588
2588
  const source = `
2589
- wrap u32 arr[10];
2589
+ wrap u32[10] arr;
2590
2590
  void main() {
2591
2591
  arr[0] <- 42;
2592
2592
  arr[5] <- 100;
@@ -2734,7 +2734,7 @@ describe("CodeGenerator", () => {
2734
2734
  it("should generate struct with array field", () => {
2735
2735
  const source = `
2736
2736
  struct Buffer {
2737
- u8 data[64];
2737
+ u8[64] data;
2738
2738
  u32 length;
2739
2739
  }
2740
2740
  `;
@@ -3297,7 +3297,7 @@ describe("CodeGenerator", () => {
3297
3297
  it("should generate struct with array member access", () => {
3298
3298
  const source = `
3299
3299
  struct Buffer {
3300
- u8 data[64];
3300
+ u8[64] data;
3301
3301
  }
3302
3302
  Buffer buf;
3303
3303
  void main() {
@@ -3427,7 +3427,7 @@ describe("CodeGenerator", () => {
3427
3427
  describe("Const array", () => {
3428
3428
  it("should generate const array", () => {
3429
3429
  const source = `
3430
- const u32 LOOKUP[4] <- [10, 20, 30, 40];
3430
+ const u32[4] LOOKUP <- [10, 20, 30, 40];
3431
3431
  void main() { }
3432
3432
  `;
3433
3433
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -3853,7 +3853,7 @@ describe("CodeGenerator", () => {
3853
3853
  describe("Array fill-all syntax", () => {
3854
3854
  it("should generate fill-all array [0*]", () => {
3855
3855
  const source = `
3856
- u32 data[10] <- [0*];
3856
+ u32[10] data <- [0*];
3857
3857
  `;
3858
3858
  const { tree, tokenStream } = CNextSourceParser.parse(source);
3859
3859
  const generator = new CodeGenerator();
@@ -3872,7 +3872,7 @@ describe("CodeGenerator", () => {
3872
3872
 
3873
3873
  it("should generate fill-all array with non-zero value", () => {
3874
3874
  const source = `
3875
- u8 buffer[4] <- [255*];
3875
+ u8[4] buffer <- [255*];
3876
3876
  `;
3877
3877
  const { tree, tokenStream } = CNextSourceParser.parse(source);
3878
3878
  const generator = new CodeGenerator();
@@ -3893,7 +3893,7 @@ describe("CodeGenerator", () => {
3893
3893
  describe("Nested array initializers", () => {
3894
3894
  it("should generate 2D array initializer", () => {
3895
3895
  const source = `
3896
- u32 matrix[2][3] <- [[1, 2, 3], [4, 5, 6]];
3896
+ u32[2] matrix[3] <- [[1, 2, 3], [4, 5, 6]];
3897
3897
  `;
3898
3898
  const { tree, tokenStream } = CNextSourceParser.parse(source);
3899
3899
  const generator = new CodeGenerator();
@@ -3914,7 +3914,7 @@ describe("CodeGenerator", () => {
3914
3914
  it("should generate array of struct initializers", () => {
3915
3915
  const source = `
3916
3916
  struct Point { i32 x; i32 y; }
3917
- Point points[2] <- [{x: 1, y: 2}, {x: 3, y: 4}];
3917
+ Point[2] points <- [{x: 1, y: 2}, {x: 3, y: 4}];
3918
3918
  `;
3919
3919
  const { tree, tokenStream } = CNextSourceParser.parse(source);
3920
3920
  const generator = new CodeGenerator();
@@ -4184,7 +4184,7 @@ describe("CodeGenerator", () => {
4184
4184
  it("should resolve const as array dimension at file scope", () => {
4185
4185
  const source = `
4186
4186
  const u32 SIZE <- 10;
4187
- u32 data[SIZE];
4187
+ u32[SIZE] data;
4188
4188
  `;
4189
4189
  const { tree, tokenStream } = CNextSourceParser.parse(source);
4190
4190
  const generator = new CodeGenerator();
@@ -4205,7 +4205,7 @@ describe("CodeGenerator", () => {
4205
4205
  describe("Multi-dimensional array", () => {
4206
4206
  it("should generate 3D array declaration", () => {
4207
4207
  const source = `
4208
- u8 cube[2][3][4];
4208
+ u8[2] cube[3][4];
4209
4209
  `;
4210
4210
  const { tree, tokenStream } = CNextSourceParser.parse(source);
4211
4211
  const generator = new CodeGenerator();
@@ -4686,7 +4686,7 @@ describe("CodeGenerator", () => {
4686
4686
  describe("Array indexing with expressions", () => {
4687
4687
  it("should generate array index with arithmetic", () => {
4688
4688
  const source = `
4689
- u32 data[10];
4689
+ u32[10] data;
4690
4690
  wrap u32 i <- 2;
4691
4691
  void main() {
4692
4692
  data[i * 2 + 1] <- 42;
@@ -4877,7 +4877,7 @@ describe("CodeGenerator", () => {
4877
4877
  describe("Array length", () => {
4878
4878
  it("should generate array length using sizeof", () => {
4879
4879
  const source = `
4880
- u32 data[10];
4880
+ u32[10] data;
4881
4881
  u32 len <- data.length;
4882
4882
  `;
4883
4883
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -5285,7 +5285,7 @@ describe("CodeGenerator", () => {
5285
5285
  it("should pass array without address-of operator", () => {
5286
5286
  const source = `
5287
5287
  void processData(u32 data[]) { }
5288
- u32 myData[5];
5288
+ u32[5] myData;
5289
5289
  void main() {
5290
5290
  processData(myData);
5291
5291
  }
@@ -5310,7 +5310,7 @@ describe("CodeGenerator", () => {
5310
5310
  it("should generate struct with array field", () => {
5311
5311
  const source = `
5312
5312
  struct Buffer {
5313
- u8 data[64];
5313
+ u8[64] data;
5314
5314
  u32 length;
5315
5315
  }
5316
5316
  `;
@@ -5694,7 +5694,7 @@ describe("CodeGenerator", () => {
5694
5694
  describe("Array bounds checking", () => {
5695
5695
  it("should throw error for out-of-bounds constant index", () => {
5696
5696
  const source = `
5697
- u32 data[5];
5697
+ u32[5] data;
5698
5698
  void main() {
5699
5699
  data[10] <- 1;
5700
5700
  }
@@ -5899,7 +5899,7 @@ describe("CodeGenerator", () => {
5899
5899
  describe("Sizeof variable", () => {
5900
5900
  it("should generate sizeof for variable", () => {
5901
5901
  const source = `
5902
- u32 arr[10];
5902
+ u32[10] arr;
5903
5903
  u32 size <- sizeof(arr);
5904
5904
  `;
5905
5905
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -5921,7 +5921,7 @@ describe("CodeGenerator", () => {
5921
5921
  it("should resolve const to literal for file-scope array", () => {
5922
5922
  const source = `
5923
5923
  const u32 BUFFER_SIZE <- 256;
5924
- u8 buffer[BUFFER_SIZE];
5924
+ u8[BUFFER_SIZE] buffer;
5925
5925
  `;
5926
5926
  const { tree, tokenStream } = CNextSourceParser.parse(source);
5927
5927
  const generator = new CodeGenerator();
@@ -6056,7 +6056,7 @@ describe("CodeGenerator", () => {
6056
6056
  it("should generate struct with array field access", () => {
6057
6057
  const source = `
6058
6058
  struct Message {
6059
- u8 data[8];
6059
+ u8[8] data;
6060
6060
  u8 length;
6061
6061
  }
6062
6062
  Message msg;
@@ -6166,7 +6166,7 @@ describe("CodeGenerator", () => {
6166
6166
  const source = `
6167
6167
  void setup() {
6168
6168
  const u32 SIZE <- 5;
6169
- u32 data[SIZE];
6169
+ u32[SIZE] data;
6170
6170
  }
6171
6171
  `;
6172
6172
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -6181,7 +6181,8 @@ describe("CodeGenerator", () => {
6181
6181
  });
6182
6182
 
6183
6183
  expect(code).toContain("SIZE");
6184
- expect(code).toContain("[SIZE]");
6184
+ // Const value is evaluated at compile time
6185
+ expect(code).toContain("data[5]");
6185
6186
  });
6186
6187
  });
6187
6188
 
@@ -6666,7 +6667,7 @@ describe("CodeGenerator", () => {
6666
6667
 
6667
6668
  describe("Array with literal size", () => {
6668
6669
  it("should generate array with integer literal size", () => {
6669
- const source = `u32 buffer[100];`;
6670
+ const source = `u32[100] buffer;`;
6670
6671
  const { tree, tokenStream } = CNextSourceParser.parse(source);
6671
6672
  const generator = new CodeGenerator();
6672
6673
  const symbolTable = new SymbolTable();
@@ -6793,7 +6794,7 @@ describe("CodeGenerator", () => {
6793
6794
 
6794
6795
  describe("Array initialization with values", () => {
6795
6796
  it("should generate array with initialization list", () => {
6796
- const source = `u8 data[4] <- [1, 2, 3, 4];`;
6797
+ const source = `u8[4] data <- [1, 2, 3, 4];`;
6797
6798
  const { tree, tokenStream } = CNextSourceParser.parse(source);
6798
6799
  const generator = new CodeGenerator();
6799
6800
  const symbolTable = new SymbolTable();
@@ -6814,7 +6815,7 @@ describe("CodeGenerator", () => {
6814
6815
  const source = `
6815
6816
  void process(u8 arr[]) { }
6816
6817
  void main() {
6817
- u8 local[10];
6818
+ u8[10] local;
6818
6819
  process(local);
6819
6820
  }
6820
6821
  `;
@@ -7081,7 +7082,7 @@ describe("CodeGenerator", () => {
7081
7082
  it("should initialize POD arrays to {0}", () => {
7082
7083
  const source = `
7083
7084
  void main() {
7084
- u32 values[10];
7085
+ u32[10] values;
7085
7086
  }
7086
7087
  `;
7087
7088
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -7102,7 +7103,7 @@ describe("CodeGenerator", () => {
7102
7103
  const source = `
7103
7104
  struct Point { i32 x; i32 y; }
7104
7105
  void main() {
7105
- Point points[5];
7106
+ Point[5] points;
7106
7107
  }
7107
7108
  `;
7108
7109
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -7347,7 +7348,7 @@ describe("CodeGenerator", () => {
7347
7348
  it("should generate struct array member write", () => {
7348
7349
  const source = `
7349
7350
  struct Item { u32 value; }
7350
- Item items[3];
7351
+ Item[3] items;
7351
7352
  void main() {
7352
7353
  items[0].value <- 42;
7353
7354
  }
@@ -7932,7 +7933,7 @@ describe("CodeGenerator", () => {
7932
7933
  const source = `
7933
7934
  void callee(u32 arr[10]) { }
7934
7935
  void test() {
7935
- u32 data[10];
7936
+ u32[10] data;
7936
7937
  callee(data);
7937
7938
  }
7938
7939
  `;
@@ -7976,7 +7977,7 @@ describe("CodeGenerator", () => {
7976
7977
  const source = `
7977
7978
  void callee(u32 val) { }
7978
7979
  void test() {
7979
- u32 arr[10];
7980
+ u32[10] arr;
7980
7981
  callee(arr[0]);
7981
7982
  }
7982
7983
  `;
@@ -8426,7 +8427,7 @@ describe("CodeGenerator", () => {
8426
8427
  it("should handle sizeof on variable", () => {
8427
8428
  const source = `
8428
8429
  void test() {
8429
- u32 arr[10];
8430
+ u32[10] arr;
8430
8431
  u32 size <- sizeof(arr);
8431
8432
  }
8432
8433
  `;
@@ -8491,7 +8492,7 @@ describe("CodeGenerator", () => {
8491
8492
  it("should handle single index array access", () => {
8492
8493
  const source = `
8493
8494
  void test() {
8494
- u32 arr[10];
8495
+ u32[10] arr;
8495
8496
  u32 val <- arr[5];
8496
8497
  }
8497
8498
  `;
@@ -8575,7 +8576,7 @@ describe("CodeGenerator", () => {
8575
8576
  it("should handle C++ mode array access", () => {
8576
8577
  const source = `
8577
8578
  void test() {
8578
- u32 arr[10];
8579
+ u32[10] arr;
8579
8580
  u32 val <- arr[5];
8580
8581
  }
8581
8582
  `;
@@ -8597,7 +8598,7 @@ describe("CodeGenerator", () => {
8597
8598
  it("should handle multi-dimensional array access", () => {
8598
8599
  const source = `
8599
8600
  void test() {
8600
- u32 matrix[3][3];
8601
+ u32[3] matrix[3];
8601
8602
  u32 val <- matrix[1][2];
8602
8603
  }
8603
8604
  `;
@@ -8778,7 +8779,7 @@ describe("CodeGenerator", () => {
8778
8779
  const source = `
8779
8780
  struct Point { u32 x; u32 y; }
8780
8781
  void test() {
8781
- Point points[10];
8782
+ Point[10] points;
8782
8783
  u32 x <- points[0].x;
8783
8784
  }
8784
8785
  `;
@@ -9141,7 +9142,7 @@ describe("CodeGenerator", () => {
9141
9142
  it("should resolve const values in expressions", () => {
9142
9143
  const source = `
9143
9144
  const u32 MAX_SIZE <- 100;
9144
- u32 buffer[MAX_SIZE];
9145
+ u32[MAX_SIZE] buffer;
9145
9146
  `;
9146
9147
  const { tree, tokenStream } = CNextSourceParser.parse(source);
9147
9148
  const generator = new CodeGenerator();
@@ -9210,7 +9211,7 @@ describe("CodeGenerator", () => {
9210
9211
  it("should handle array access in assignment target", () => {
9211
9212
  const source = `
9212
9213
  void test() {
9213
- u32 arr[10];
9214
+ u32[10] arr;
9214
9215
  arr[5] <- 42;
9215
9216
  }
9216
9217
  `;
@@ -9716,7 +9717,7 @@ describe("CodeGenerator", () => {
9716
9717
  it("should generate struct with array member", () => {
9717
9718
  const source = `
9718
9719
  struct Buffer {
9719
- u8 data[64];
9720
+ u8[64] data;
9720
9721
  u32 size;
9721
9722
  }
9722
9723
  Buffer buf;
@@ -9833,7 +9834,7 @@ describe("CodeGenerator", () => {
9833
9834
  it("should generate array with initializer list", () => {
9834
9835
  const source = `
9835
9836
  void test() {
9836
- u32 arr[5] <- [1, 2, 3, 4, 5];
9837
+ u32[5] arr <- [1, 2, 3, 4, 5];
9837
9838
  }
9838
9839
  `;
9839
9840
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -11130,7 +11131,7 @@ describe("CodeGenerator", () => {
11130
11131
  data[0] <- 1;
11131
11132
  }
11132
11133
  void test() {
11133
- u32 arr[10];
11134
+ u32[10] arr;
11134
11135
  process(arr);
11135
11136
  }
11136
11137
  `;
@@ -11436,7 +11437,7 @@ describe("CodeGenerator", () => {
11436
11437
  it("should handle array with size in declaration", () => {
11437
11438
  const source = `
11438
11439
  void test() {
11439
- u8 buffer[10];
11440
+ u8[10] buffer;
11440
11441
  buffer[0] <- 0xFF;
11441
11442
  }
11442
11443
  `;
@@ -11458,7 +11459,7 @@ describe("CodeGenerator", () => {
11458
11459
  const source = `
11459
11460
  const u32 BUFFER_SIZE <- 64;
11460
11461
  void test() {
11461
- u8 buffer[BUFFER_SIZE];
11462
+ u8[BUFFER_SIZE] buffer;
11462
11463
  buffer[0] <- 0;
11463
11464
  }
11464
11465
  `;
@@ -11475,6 +11476,90 @@ describe("CodeGenerator", () => {
11475
11476
 
11476
11477
  expect(code).toContain("BUFFER_SIZE");
11477
11478
  });
11479
+
11480
+ it("should reject C-style array declaration for primitive types", () => {
11481
+ const source = `
11482
+ void test() {
11483
+ u8 buffer[10];
11484
+ }
11485
+ `;
11486
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
11487
+ const generator = new CodeGenerator();
11488
+ const symbolTable = new SymbolTable();
11489
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
11490
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
11491
+
11492
+ expect(() => {
11493
+ generator.generate(tree, symbolTable, tokenStream, {
11494
+ symbolInfo: symbols,
11495
+ sourcePath: "test.cnx",
11496
+ });
11497
+ }).toThrow(/C-style array declaration is not allowed/);
11498
+ });
11499
+
11500
+ it("should reject C-style array declaration for user types", () => {
11501
+ const source = `
11502
+ struct Data {
11503
+ u32 value;
11504
+ }
11505
+ void test() {
11506
+ Data items[5];
11507
+ }
11508
+ `;
11509
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
11510
+ const generator = new CodeGenerator();
11511
+ const symbolTable = new SymbolTable();
11512
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
11513
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
11514
+
11515
+ expect(() => {
11516
+ generator.generate(tree, symbolTable, tokenStream, {
11517
+ symbolInfo: symbols,
11518
+ sourcePath: "test.cnx",
11519
+ });
11520
+ }).toThrow(/C-style array declaration is not allowed/);
11521
+ });
11522
+
11523
+ it("should allow empty brackets for size inference", () => {
11524
+ const source = `
11525
+ void test() {
11526
+ u8 data[] <- [1, 2, 3];
11527
+ }
11528
+ `;
11529
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
11530
+ const generator = new CodeGenerator();
11531
+ const symbolTable = new SymbolTable();
11532
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
11533
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
11534
+
11535
+ const code = generator.generate(tree, symbolTable, tokenStream, {
11536
+ symbolInfo: symbols,
11537
+ sourcePath: "test.cnx",
11538
+ });
11539
+
11540
+ expect(code).toContain("uint8_t data[3]");
11541
+ });
11542
+
11543
+ it("should allow multi-dimensional C-style arrays", () => {
11544
+ const source = `
11545
+ void test() {
11546
+ u8 matrix[4][4];
11547
+ matrix[0][0] <- 0;
11548
+ }
11549
+ `;
11550
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
11551
+ const generator = new CodeGenerator();
11552
+ const symbolTable = new SymbolTable();
11553
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
11554
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
11555
+
11556
+ const code = generator.generate(tree, symbolTable, tokenStream, {
11557
+ symbolInfo: symbols,
11558
+ sourcePath: "test.cnx",
11559
+ });
11560
+
11561
+ expect(code).toContain("uint8_t matrix[4][4]");
11562
+ });
11478
11563
  });
11479
11564
 
11480
11565
  describe("string expression type checking", () => {
@@ -12208,7 +12293,7 @@ describe("CodeGenerator", () => {
12208
12293
  it("should handle sizeof on local array", () => {
12209
12294
  const source = `
12210
12295
  void test() {
12211
- u8 buffer[16];
12296
+ u8[16] buffer;
12212
12297
  u32 size <- sizeof(buffer);
12213
12298
  }
12214
12299
  `;
@@ -12368,7 +12453,7 @@ describe("CodeGenerator", () => {
12368
12453
  it("should resolve array element access in expression", () => {
12369
12454
  const source = `
12370
12455
  void test() {
12371
- u32 arr[10];
12456
+ u32[10] arr;
12372
12457
  u32 val <- arr[5];
12373
12458
  }
12374
12459
  `;
@@ -12389,7 +12474,7 @@ describe("CodeGenerator", () => {
12389
12474
  it("should resolve array element with variable index", () => {
12390
12475
  const source = `
12391
12476
  void test() {
12392
- u32 arr[10];
12477
+ u32[10] arr;
12393
12478
  u32 i <- 3;
12394
12479
  u32 val <- arr[i];
12395
12480
  }
@@ -12802,7 +12887,7 @@ describe("CodeGenerator", () => {
12802
12887
  const source = `
12803
12888
  struct Item { u32 id; }
12804
12889
  void test() {
12805
- Item items[10];
12890
+ Item[10] items;
12806
12891
  items[0].id <- 42;
12807
12892
  }
12808
12893
  `;
@@ -13071,7 +13156,7 @@ describe("CodeGenerator", () => {
13071
13156
  it("should handle array type with user-defined struct", () => {
13072
13157
  const source = `
13073
13158
  struct Point { i32 x; i32 y; }
13074
- Point points[10];
13159
+ Point[10] points;
13075
13160
  `;
13076
13161
  const { tree, tokenStream } = CNextSourceParser.parse(source);
13077
13162
  const generator = new CodeGenerator();
@@ -13089,8 +13174,8 @@ describe("CodeGenerator", () => {
13089
13174
 
13090
13175
  it("should handle array type with primitive types", () => {
13091
13176
  const source = `
13092
- u8 buffer[256];
13093
- i32 numbers[10];
13177
+ u8[256] buffer;
13178
+ i32[10] numbers;
13094
13179
  `;
13095
13180
  const { tree, tokenStream } = CNextSourceParser.parse(source);
13096
13181
  const generator = new CodeGenerator();
@@ -13590,7 +13675,7 @@ describe("CodeGenerator", () => {
13590
13675
  describe("array access resolution helpers", () => {
13591
13676
  it("should resolve simple array access", () => {
13592
13677
  const source = `
13593
- u8 buffer[10];
13678
+ u8[10] buffer;
13594
13679
  void test() {
13595
13680
  u8 val <- buffer[5];
13596
13681
  }
@@ -13611,7 +13696,7 @@ describe("CodeGenerator", () => {
13611
13696
 
13612
13697
  it("should handle array access with variable index", () => {
13613
13698
  const source = `
13614
- u8 data[20];
13699
+ u8[20] data;
13615
13700
  void test() {
13616
13701
  u32 idx <- 3;
13617
13702
  u8 val <- data[idx];
@@ -14098,7 +14183,7 @@ describe("CodeGenerator", () => {
14098
14183
  it("should register array type correctly", () => {
14099
14184
  const source = `
14100
14185
  void test() {
14101
- u8 buffer[10];
14186
+ u8[10] buffer;
14102
14187
  buffer[0] <- 0xFF;
14103
14188
  }
14104
14189
  `;
@@ -14119,7 +14204,7 @@ describe("CodeGenerator", () => {
14119
14204
  it("should register multi-dimensional array", () => {
14120
14205
  const source = `
14121
14206
  void test() {
14122
- u8 matrix[10][20];
14207
+ u8[10] matrix[20];
14123
14208
  matrix[0][0] <- 0;
14124
14209
  }
14125
14210
  `;
@@ -14140,7 +14225,7 @@ describe("CodeGenerator", () => {
14140
14225
  it("should handle i32 array type", () => {
14141
14226
  const source = `
14142
14227
  void test() {
14143
- i32 values[5];
14228
+ i32[5] values;
14144
14229
  values[0] <- 100;
14145
14230
  }
14146
14231
  `;
@@ -14161,7 +14246,7 @@ describe("CodeGenerator", () => {
14161
14246
  it("should handle const array type", () => {
14162
14247
  const source = `
14163
14248
  void test() {
14164
- const u8 data[4] <- [1, 2, 3, 4];
14249
+ const u8[4] data <- [1, 2, 3, 4];
14165
14250
  }
14166
14251
  `;
14167
14252
  const { tree, tokenStream } = CNextSourceParser.parse(source);
@@ -14177,6 +14262,111 @@ describe("CodeGenerator", () => {
14177
14262
 
14178
14263
  expect(code).toContain("const uint8_t data[4]");
14179
14264
  });
14265
+
14266
+ it("should handle struct array with C-Next syntax", () => {
14267
+ const source = `
14268
+ struct Point {
14269
+ i32 x;
14270
+ i32 y;
14271
+ }
14272
+ void test() {
14273
+ Point[3] points;
14274
+ points[0].x <- 10;
14275
+ }
14276
+ `;
14277
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
14278
+ const generator = new CodeGenerator();
14279
+ const symbolTable = new SymbolTable();
14280
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
14281
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
14282
+
14283
+ const code = generator.generate(tree, symbolTable, tokenStream, {
14284
+ symbolInfo: symbols,
14285
+ sourcePath: "test.cnx",
14286
+ });
14287
+
14288
+ expect(code).toContain("Point points[3]");
14289
+ expect(code).toContain("points[0].x = 10");
14290
+ });
14291
+
14292
+ it("should handle bitmap array with C-Next syntax", () => {
14293
+ const source = `
14294
+ bitmap8 Flags {
14295
+ active,
14296
+ ready,
14297
+ error,
14298
+ mode[3],
14299
+ priority[2]
14300
+ }
14301
+ void test() {
14302
+ Flags[4] flags;
14303
+ flags[0].active <- true;
14304
+ }
14305
+ `;
14306
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
14307
+ const generator = new CodeGenerator();
14308
+ const symbolTable = new SymbolTable();
14309
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
14310
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
14311
+
14312
+ const code = generator.generate(tree, symbolTable, tokenStream, {
14313
+ symbolInfo: symbols,
14314
+ sourcePath: "test.cnx",
14315
+ });
14316
+
14317
+ expect(code).toContain("Flags flags[4]");
14318
+ // Bitmap field access should generate bit manipulation
14319
+ expect(code).toContain("flags[0]");
14320
+ });
14321
+
14322
+ it("should handle enum array with C-Next syntax", () => {
14323
+ const source = `
14324
+ enum Color {
14325
+ RED,
14326
+ GREEN,
14327
+ BLUE
14328
+ }
14329
+ void test() {
14330
+ Color[3] colors;
14331
+ colors[0] <- RED;
14332
+ }
14333
+ `;
14334
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
14335
+ const generator = new CodeGenerator();
14336
+ const symbolTable = new SymbolTable();
14337
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
14338
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
14339
+
14340
+ const code = generator.generate(tree, symbolTable, tokenStream, {
14341
+ symbolInfo: symbols,
14342
+ sourcePath: "test.cnx",
14343
+ });
14344
+
14345
+ expect(code).toContain("Color colors[3]");
14346
+ expect(code).toContain("colors[0] = Color_RED");
14347
+ });
14348
+
14349
+ it("should handle struct array initializer with C-Next syntax", () => {
14350
+ const source = `
14351
+ struct Item {
14352
+ u8 id;
14353
+ }
14354
+ const Item[2] items <- [{id: 1}, {id: 2}];
14355
+ `;
14356
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
14357
+ const generator = new CodeGenerator();
14358
+ const symbolTable = new SymbolTable();
14359
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
14360
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
14361
+
14362
+ const code = generator.generate(tree, symbolTable, tokenStream, {
14363
+ symbolInfo: symbols,
14364
+ sourcePath: "test.cnx",
14365
+ });
14366
+
14367
+ expect(code).toContain("const Item items[2]");
14368
+ expect(code).toContain(".id = 1");
14369
+ });
14180
14370
  });
14181
14371
 
14182
14372
  describe("C++ mode member conversion", () => {
@@ -14211,7 +14401,7 @@ describe("CodeGenerator", () => {
14211
14401
  u32 id;
14212
14402
  }
14213
14403
  void process() {
14214
- Item items[3];
14404
+ Item[3] items;
14215
14405
  u32 first <- items[0].id;
14216
14406
  }
14217
14407
  `;
@@ -14610,7 +14800,7 @@ describe("CodeGenerator", () => {
14610
14800
  it("should generate array element write", () => {
14611
14801
  const source = `
14612
14802
  void test() {
14613
- u32 arr[10];
14803
+ u32[10] arr;
14614
14804
  arr[5] <- 42;
14615
14805
  }
14616
14806
  `;
@@ -15384,7 +15574,7 @@ describe("CodeGenerator", () => {
15384
15574
  describe("array access via ArrayAccessHelper", () => {
15385
15575
  it("should generate single-index array access", () => {
15386
15576
  const source = `
15387
- u32 arr[10];
15577
+ u32[10] arr;
15388
15578
  void test() {
15389
15579
  u32 val <- arr[5];
15390
15580
  }
@@ -15405,7 +15595,7 @@ describe("CodeGenerator", () => {
15405
15595
 
15406
15596
  it("should generate array access with variable index", () => {
15407
15597
  const source = `
15408
- u8 data[100];
15598
+ u8[100] data;
15409
15599
  void test(u32 idx) {
15410
15600
  u8 val <- data[idx];
15411
15601
  }
@@ -15546,4 +15736,80 @@ describe("CodeGenerator", () => {
15546
15736
  expect(code).not.toContain("Bar_OFFSET");
15547
15737
  });
15548
15738
  });
15739
+
15740
+ describe("Generator registration error paths", () => {
15741
+ it("throws error when struct generator is not registered", () => {
15742
+ // First call initializes the registry
15743
+ const initSource = `u8 x;`;
15744
+ const { tree: initTree, tokenStream: initTokenStream } =
15745
+ CNextSourceParser.parse(initSource);
15746
+ const generator = new CodeGenerator();
15747
+ const initSymbolTable = new SymbolTable();
15748
+ const initTSymbols = CNextResolver.resolve(initTree, "init.cnx");
15749
+ const initSymbols = TSymbolInfoAdapter.convert(initTSymbols);
15750
+ generator.generate(initTree, initSymbolTable, initTokenStream, {
15751
+ symbolInfo: initSymbols,
15752
+ sourcePath: "init.cnx",
15753
+ });
15754
+
15755
+ // Unregister the struct generator to test error path
15756
+ const registry = (
15757
+ generator as unknown as {
15758
+ registry: { unregisterDeclaration: (kind: string) => void };
15759
+ }
15760
+ ).registry;
15761
+ registry.unregisterDeclaration("struct");
15762
+
15763
+ // Second call should throw because struct generator is now missing
15764
+ const source = `struct Point { i32 x; i32 y; }`;
15765
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
15766
+ const symbolTable = new SymbolTable();
15767
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
15768
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
15769
+
15770
+ expect(() => {
15771
+ generator.generate(tree, symbolTable, tokenStream, {
15772
+ symbolInfo: symbols,
15773
+ sourcePath: "test.cnx",
15774
+ });
15775
+ }).toThrow("Error: struct generator not registered");
15776
+ });
15777
+
15778
+ it("throws error when enum generator is not registered", () => {
15779
+ // First call initializes the registry
15780
+ const initSource = `u8 x;`;
15781
+ const { tree: initTree, tokenStream: initTokenStream } =
15782
+ CNextSourceParser.parse(initSource);
15783
+ const generator = new CodeGenerator();
15784
+ const initSymbolTable = new SymbolTable();
15785
+ const initTSymbols = CNextResolver.resolve(initTree, "init.cnx");
15786
+ const initSymbols = TSymbolInfoAdapter.convert(initTSymbols);
15787
+ generator.generate(initTree, initSymbolTable, initTokenStream, {
15788
+ symbolInfo: initSymbols,
15789
+ sourcePath: "init.cnx",
15790
+ });
15791
+
15792
+ // Unregister the enum generator to test error path
15793
+ const registry = (
15794
+ generator as unknown as {
15795
+ registry: { unregisterDeclaration: (kind: string) => void };
15796
+ }
15797
+ ).registry;
15798
+ registry.unregisterDeclaration("enum");
15799
+
15800
+ // Second call should throw because enum generator is now missing
15801
+ const source = `enum State { IDLE, RUNNING }`;
15802
+ const { tree, tokenStream } = CNextSourceParser.parse(source);
15803
+ const symbolTable = new SymbolTable();
15804
+ const tSymbols = CNextResolver.resolve(tree, "test.cnx");
15805
+ const symbols = TSymbolInfoAdapter.convert(tSymbols);
15806
+
15807
+ expect(() => {
15808
+ generator.generate(tree, symbolTable, tokenStream, {
15809
+ symbolInfo: symbols,
15810
+ sourcePath: "test.cnx",
15811
+ });
15812
+ }).toThrow("Error: enum generator not registered");
15813
+ });
15814
+ });
15549
15815
  });