c-next 0.2.16 → 0.2.17
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.
- package/README.md +16 -0
- package/dist/index.js +1347 -414
- package/dist/index.js.map +3 -3
- package/grammar/CNext.g4 +4 -0
- package/package.json +5 -1
- package/src/transpiler/Transpiler.ts +90 -22
- package/src/transpiler/__tests__/DualCodePaths.test.ts +1 -1
- package/src/transpiler/logic/analysis/FunctionCallAnalyzer.ts +57 -10
- package/src/transpiler/logic/analysis/InitializationAnalyzer.ts +186 -14
- package/src/transpiler/logic/analysis/SignedShiftAnalyzer.ts +124 -12
- package/src/transpiler/logic/analysis/__tests__/FunctionCallAnalyzer.test.ts +200 -0
- package/src/transpiler/logic/analysis/__tests__/InitializationAnalyzer.test.ts +386 -1
- package/src/transpiler/logic/analysis/__tests__/SignedShiftAnalyzer.test.ts +211 -0
- package/src/transpiler/logic/parser/grammar/CNext.interp +1 -1
- package/src/transpiler/logic/parser/grammar/CNextParser.ts +154 -86
- package/src/transpiler/logic/symbols/SymbolTable.ts +17 -2
- package/src/transpiler/logic/symbols/__tests__/SymbolTable.test.ts +6 -4
- package/src/transpiler/logic/symbols/cnext/collectors/VariableCollector.ts +15 -2
- package/src/transpiler/logic/symbols/cnext/utils/TypeUtils.ts +64 -50
- package/src/transpiler/output/codegen/CodeGenerator.ts +151 -94
- package/src/transpiler/output/codegen/__tests__/CodeGenerator.coverage.test.ts +165 -17
- package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +150 -34
- package/src/transpiler/output/codegen/__tests__/TrackVariableTypeHelpers.test.ts +2 -2
- package/src/transpiler/output/codegen/__tests__/TypeValidator.test.ts +11 -0
- package/src/transpiler/output/codegen/generators/declarationGenerators/ScopeGenerator.ts +26 -7
- package/src/transpiler/output/codegen/generators/declarationGenerators/__tests__/ScopeGenerator.test.ts +86 -0
- package/src/transpiler/output/codegen/generators/expressions/BinaryExprGenerator.ts +43 -24
- package/src/transpiler/output/codegen/generators/expressions/CallExprGenerator.ts +48 -43
- package/src/transpiler/output/codegen/generators/expressions/ExpressionGenerator.ts +9 -2
- package/src/transpiler/output/codegen/generators/expressions/__tests__/CallExprGenerator.test.ts +44 -0
- package/src/transpiler/output/codegen/generators/expressions/__tests__/ExpressionGenerator.test.ts +82 -1
- package/src/transpiler/output/codegen/helpers/ParameterInputAdapter.ts +17 -3
- package/src/transpiler/output/codegen/helpers/ParameterSignatureBuilder.ts +17 -4
- package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +227 -32
- package/src/transpiler/output/codegen/helpers/SymbolLookupHelper.ts +0 -21
- package/src/transpiler/output/codegen/helpers/TypeGenerationHelper.ts +60 -39
- package/src/transpiler/output/codegen/helpers/TypeRegistrationEngine.ts +170 -36
- package/src/transpiler/output/codegen/helpers/VariableDeclHelper.ts +37 -39
- package/src/transpiler/output/codegen/helpers/__tests__/ParameterInputAdapter.test.ts +117 -0
- package/src/transpiler/output/codegen/helpers/__tests__/ParameterSignatureBuilder.test.ts +94 -2
- package/src/transpiler/output/codegen/helpers/__tests__/StringDeclHelper.test.ts +268 -1
- package/src/transpiler/output/codegen/helpers/__tests__/SymbolLookupHelper.test.ts +0 -64
- package/src/transpiler/output/codegen/helpers/__tests__/TypeRegistrationEngine.test.ts +101 -0
- package/src/transpiler/output/codegen/helpers/__tests__/VariableDeclHelper.test.ts +29 -5
- package/src/transpiler/output/codegen/types/ICallbackTypeInfo.ts +2 -1
- package/src/transpiler/output/codegen/types/IParameterInput.ts +7 -0
- package/src/transpiler/output/headers/BaseHeaderGenerator.ts +8 -0
- package/src/transpiler/output/headers/HeaderGeneratorUtils.ts +75 -0
- package/src/transpiler/output/headers/__tests__/HeaderGeneratorUtils.test.ts +280 -0
- package/src/transpiler/output/headers/generators/IHeaderTypeInput.ts +13 -0
- package/src/transpiler/output/headers/generators/generateStructHeader.ts +48 -28
- package/src/transpiler/state/CodeGenState.ts +71 -6
- package/src/transpiler/state/__tests__/CodeGenState.test.ts +253 -11
- package/src/utils/LiteralUtils.ts +23 -0
- package/src/utils/__tests__/LiteralUtils.test.ts +101 -0
- package/src/utils/types/IParameterSymbol.ts +1 -0
|
@@ -648,12 +648,15 @@ describe("CodeGenerator Coverage Tests", () => {
|
|
|
648
648
|
// Issue #834: generateStructInitializer with named struct tags
|
|
649
649
|
// ==========================================================================
|
|
650
650
|
describe("generateStructInitializer() with named struct tags", () => {
|
|
651
|
-
it("should include struct keyword in compound literal for named struct tags", () => {
|
|
652
|
-
// Test the fix for issue #834: named struct tags need 'struct' prefix in cast
|
|
651
|
+
it("should include struct keyword in compound literal for named struct tags (assignment context)", () => {
|
|
652
|
+
// Test the fix for issue #834: named struct tags need 'struct' prefix in cast.
|
|
653
|
+
// In declaration context, no compound literal is emitted.
|
|
654
|
+
// In assignment (expression) context, the struct keyword must be present.
|
|
653
655
|
const source = `
|
|
654
656
|
struct NamedPoint { i32 x; i32 y; }
|
|
655
657
|
void test() {
|
|
656
|
-
NamedPoint p <- {x:
|
|
658
|
+
NamedPoint p <- {x: 0, y: 0};
|
|
659
|
+
p <- {x: 10, y: 20};
|
|
657
660
|
}
|
|
658
661
|
`;
|
|
659
662
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
@@ -673,10 +676,10 @@ describe("CodeGenerator Coverage Tests", () => {
|
|
|
673
676
|
cppMode: false,
|
|
674
677
|
});
|
|
675
678
|
|
|
676
|
-
//
|
|
677
|
-
expect(code).toContain("
|
|
678
|
-
|
|
679
|
-
expect(code).toContain(".y = 20");
|
|
679
|
+
// Declaration uses plain designated initializer (no compound literal)
|
|
680
|
+
expect(code).toContain("p = { .x = 0, .y = 0 }");
|
|
681
|
+
// Assignment (expression context) must keep compound literal with struct keyword
|
|
682
|
+
expect(code).toContain("(struct NamedPoint){ .x = 10, .y = 20 }");
|
|
680
683
|
});
|
|
681
684
|
|
|
682
685
|
it("should include struct keyword in empty initializer via return statement", () => {
|
|
@@ -708,12 +711,14 @@ describe("CodeGenerator Coverage Tests", () => {
|
|
|
708
711
|
expect(code).toContain("(struct ReturnStruct){ 0 }");
|
|
709
712
|
});
|
|
710
713
|
|
|
711
|
-
it("should NOT include struct keyword for typedef'd structs in
|
|
712
|
-
// This tests the branch where checkNeedsStructKeyword returns false
|
|
714
|
+
it("should NOT include struct keyword for typedef'd structs in assignment context", () => {
|
|
715
|
+
// This tests the branch where checkNeedsStructKeyword returns false.
|
|
716
|
+
// Compound literals (expression context) for typedef'd structs must NOT have 'struct'.
|
|
713
717
|
const source = `
|
|
714
718
|
struct TypedefPoint { i32 x; i32 y; }
|
|
715
719
|
void test() {
|
|
716
|
-
TypedefPoint p <- {x:
|
|
720
|
+
TypedefPoint p <- {x: 0, y: 0};
|
|
721
|
+
p <- {x: 1, y: 2};
|
|
717
722
|
}
|
|
718
723
|
`;
|
|
719
724
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
@@ -732,22 +737,23 @@ describe("CodeGenerator Coverage Tests", () => {
|
|
|
732
737
|
cppMode: false,
|
|
733
738
|
});
|
|
734
739
|
|
|
735
|
-
//
|
|
740
|
+
// Assignment (expression context) should use plain type, no 'struct' keyword
|
|
736
741
|
expect(code).not.toContain("(struct TypedefPoint)");
|
|
737
|
-
expect(code).toContain("(TypedefPoint)");
|
|
742
|
+
expect(code).toContain("(TypedefPoint){ .x = 1, .y = 2 }");
|
|
738
743
|
});
|
|
739
744
|
|
|
740
|
-
it("should NOT include struct keyword in C++ mode", () => {
|
|
745
|
+
it("should NOT include struct keyword in C++ mode (assignment context)", () => {
|
|
746
|
+
// Even if marked, C++ mode should not use struct keyword in compound literals.
|
|
741
747
|
const source = `
|
|
742
748
|
struct CppPoint { i32 x; i32 y; }
|
|
743
749
|
void test() {
|
|
744
|
-
CppPoint p <- {x:
|
|
750
|
+
CppPoint p <- {x: 0, y: 0};
|
|
751
|
+
p <- {x: 5, y: 10};
|
|
745
752
|
}
|
|
746
753
|
`;
|
|
747
754
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
748
755
|
|
|
749
756
|
const symbolTable = new SymbolTable();
|
|
750
|
-
// Even if marked, C++ mode should not use struct keyword
|
|
751
757
|
symbolTable.markNeedsStructKeyword("CppPoint");
|
|
752
758
|
|
|
753
759
|
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
@@ -761,9 +767,9 @@ describe("CodeGenerator Coverage Tests", () => {
|
|
|
761
767
|
cppMode: true,
|
|
762
768
|
});
|
|
763
769
|
|
|
764
|
-
//
|
|
770
|
+
// Assignment (expression context) in C++ mode must not use 'struct' keyword
|
|
765
771
|
expect(code).not.toContain("(struct CppPoint)");
|
|
766
|
-
expect(code).toContain("(CppPoint)");
|
|
772
|
+
expect(code).toContain("(CppPoint){ .x = 5, .y = 10 }");
|
|
767
773
|
});
|
|
768
774
|
});
|
|
769
775
|
|
|
@@ -1339,4 +1345,146 @@ describe("CodeGenerator Coverage Tests", () => {
|
|
|
1339
1345
|
expect(code).not.toContain("widget_t* w");
|
|
1340
1346
|
});
|
|
1341
1347
|
});
|
|
1348
|
+
|
|
1349
|
+
// ==========================================================================
|
|
1350
|
+
// PR: _generateScopeVariable with struct initializer (line 3214)
|
|
1351
|
+
// Covers withDeclarationInit wrapping in scope variable declarations
|
|
1352
|
+
// ==========================================================================
|
|
1353
|
+
describe("_generateScopeVariable() with struct initializer", () => {
|
|
1354
|
+
it("should use plain designated initializer for scope struct variable", () => {
|
|
1355
|
+
const source = `
|
|
1356
|
+
struct Settings { i32 timeout; i32 retries; }
|
|
1357
|
+
scope Config {
|
|
1358
|
+
public Settings defaults <- {timeout: 30, retries: 3};
|
|
1359
|
+
}
|
|
1360
|
+
`;
|
|
1361
|
+
const { code } = setupGenerator(source);
|
|
1362
|
+
// Scope variable initializer uses withDeclarationInit, producing plain designated init
|
|
1363
|
+
expect(code).toContain(".timeout = 30");
|
|
1364
|
+
expect(code).toContain(".retries = 3");
|
|
1365
|
+
// Should NOT have compound literal prefix in declaration context
|
|
1366
|
+
expect(code).not.toContain("(Settings){ .timeout");
|
|
1367
|
+
});
|
|
1368
|
+
|
|
1369
|
+
it("should use plain designated initializer for private scope struct variable", () => {
|
|
1370
|
+
const source = `
|
|
1371
|
+
struct Point { i32 x; i32 y; }
|
|
1372
|
+
scope Drawing {
|
|
1373
|
+
Point origin <- {x: 0, y: 0};
|
|
1374
|
+
}
|
|
1375
|
+
`;
|
|
1376
|
+
const { code } = setupGenerator(source);
|
|
1377
|
+
expect(code).toContain(
|
|
1378
|
+
"static Point Drawing_origin = { .x = 0, .y = 0 }",
|
|
1379
|
+
);
|
|
1380
|
+
});
|
|
1381
|
+
});
|
|
1382
|
+
|
|
1383
|
+
// ==========================================================================
|
|
1384
|
+
// PR: formatStructInitializer with inDeclarationInit (line 3544)
|
|
1385
|
+
// Covers the plain designated initializer path in declaration context
|
|
1386
|
+
// ==========================================================================
|
|
1387
|
+
describe("formatStructInitializer() in declaration context", () => {
|
|
1388
|
+
it("should use plain designated init (no compound literal) for global struct variable", () => {
|
|
1389
|
+
const source = `
|
|
1390
|
+
struct Point { i32 x; i32 y; }
|
|
1391
|
+
Point origin <- {x: 0, y: 0};
|
|
1392
|
+
`;
|
|
1393
|
+
const { code } = setupGenerator(source);
|
|
1394
|
+
// Global declaration: plain designated init, no compound literal prefix
|
|
1395
|
+
expect(code).toContain("Point origin = { .x = 0, .y = 0 }");
|
|
1396
|
+
expect(code).not.toContain("(Point){ .x");
|
|
1397
|
+
});
|
|
1398
|
+
|
|
1399
|
+
it("should use compound literal for struct in assignment context", () => {
|
|
1400
|
+
const source = `
|
|
1401
|
+
struct Point { i32 x; i32 y; }
|
|
1402
|
+
void main() {
|
|
1403
|
+
Point p <- {x: 0, y: 0};
|
|
1404
|
+
p <- {x: 10, y: 20};
|
|
1405
|
+
}
|
|
1406
|
+
`;
|
|
1407
|
+
const { code } = setupGenerator(source);
|
|
1408
|
+
// Declaration: plain init
|
|
1409
|
+
expect(code).toContain("Point p = { .x = 0, .y = 0 }");
|
|
1410
|
+
// Assignment: compound literal with type cast
|
|
1411
|
+
expect(code).toContain("(Point){ .x = 10, .y = 20 }");
|
|
1412
|
+
});
|
|
1413
|
+
});
|
|
1414
|
+
|
|
1415
|
+
// ==========================================================================
|
|
1416
|
+
// PR: _resolveFieldType with underscore field types (lines 3577-3581)
|
|
1417
|
+
// Covers the C++ underscore-to-:: conversion path
|
|
1418
|
+
// ==========================================================================
|
|
1419
|
+
describe("_resolveFieldType() with underscore types", () => {
|
|
1420
|
+
it("should convert underscore type to :: when first part is a C++ namespace", () => {
|
|
1421
|
+
const source = `
|
|
1422
|
+
struct Outer { i32 dummy; }
|
|
1423
|
+
void main() {
|
|
1424
|
+
Outer o <- {dummy: 1};
|
|
1425
|
+
}
|
|
1426
|
+
`;
|
|
1427
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
1428
|
+
|
|
1429
|
+
const symbolTable = new SymbolTable();
|
|
1430
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
1431
|
+
symbolTable.addTSymbols(tSymbols);
|
|
1432
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
1433
|
+
|
|
1434
|
+
// Register a C++ namespace so isCppScopeSymbol("SeaDash") returns true
|
|
1435
|
+
symbolTable.addCppSymbol({
|
|
1436
|
+
kind: "namespace",
|
|
1437
|
+
name: "SeaDash",
|
|
1438
|
+
sourceFile: "SeaDash.h",
|
|
1439
|
+
sourceLine: 1,
|
|
1440
|
+
sourceLanguage: ESourceLanguage.Cpp,
|
|
1441
|
+
isExported: true,
|
|
1442
|
+
});
|
|
1443
|
+
|
|
1444
|
+
// Register struct field type with underscore (simulates C++ imported struct)
|
|
1445
|
+
symbolTable.addStructField("Outer", "dummy", "SeaDash_Parse_Result");
|
|
1446
|
+
|
|
1447
|
+
const generator = new CodeGenerator();
|
|
1448
|
+
CodeGenState.symbolTable = symbolTable;
|
|
1449
|
+
const code = generator.generate(tree, tokenStream, {
|
|
1450
|
+
symbolInfo: symbols,
|
|
1451
|
+
sourcePath: "test.cnx",
|
|
1452
|
+
cppMode: false,
|
|
1453
|
+
});
|
|
1454
|
+
|
|
1455
|
+
// The field type should be converted from SeaDash_Parse_Result to SeaDash::Parse::Result
|
|
1456
|
+
// This exercises _resolveFieldType lines 3577-3579
|
|
1457
|
+
expect(code).toBeDefined();
|
|
1458
|
+
});
|
|
1459
|
+
|
|
1460
|
+
it("should keep underscore type when first part is not a C++ namespace", () => {
|
|
1461
|
+
const source = `
|
|
1462
|
+
struct Data { i32 value; }
|
|
1463
|
+
void main() {
|
|
1464
|
+
Data d <- {value: 42};
|
|
1465
|
+
}
|
|
1466
|
+
`;
|
|
1467
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
1468
|
+
|
|
1469
|
+
const symbolTable = new SymbolTable();
|
|
1470
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
1471
|
+
symbolTable.addTSymbols(tSymbols);
|
|
1472
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
1473
|
+
|
|
1474
|
+
// Register struct field type with underscore but NOT a C++ namespace
|
|
1475
|
+
symbolTable.addStructField("Data", "value", "some_plain_type");
|
|
1476
|
+
|
|
1477
|
+
const generator = new CodeGenerator();
|
|
1478
|
+
CodeGenState.symbolTable = symbolTable;
|
|
1479
|
+
const code = generator.generate(tree, tokenStream, {
|
|
1480
|
+
symbolInfo: symbols,
|
|
1481
|
+
sourcePath: "test.cnx",
|
|
1482
|
+
cppMode: false,
|
|
1483
|
+
});
|
|
1484
|
+
|
|
1485
|
+
// The field type should remain unchanged (not a C++ namespace)
|
|
1486
|
+
// This exercises _resolveFieldType line 3581
|
|
1487
|
+
expect(code).toBeDefined();
|
|
1488
|
+
});
|
|
1489
|
+
});
|
|
1342
1490
|
});
|
|
@@ -1504,7 +1504,7 @@ describe("CodeGenerator", () => {
|
|
|
1504
1504
|
|
|
1505
1505
|
it("should generate multi-dimensional array", () => {
|
|
1506
1506
|
const source = `
|
|
1507
|
-
u32[3]
|
|
1507
|
+
u32[3][3] matrix;
|
|
1508
1508
|
void main() { }
|
|
1509
1509
|
`;
|
|
1510
1510
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
@@ -2944,7 +2944,7 @@ describe("CodeGenerator", () => {
|
|
|
2944
2944
|
describe("Inferred array size", () => {
|
|
2945
2945
|
it("should generate array with inferred size from initializer", () => {
|
|
2946
2946
|
const source = `
|
|
2947
|
-
u32
|
|
2947
|
+
u32[] values <- [1, 2, 3, 4, 5];
|
|
2948
2948
|
void main() { }
|
|
2949
2949
|
`;
|
|
2950
2950
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
@@ -3799,7 +3799,7 @@ describe("CodeGenerator", () => {
|
|
|
3799
3799
|
describe("Nested array initializers", () => {
|
|
3800
3800
|
it("should generate 2D array initializer", () => {
|
|
3801
3801
|
const source = `
|
|
3802
|
-
u32[2]
|
|
3802
|
+
u32[2][3] matrix <- [[1, 2, 3], [4, 5, 6]];
|
|
3803
3803
|
`;
|
|
3804
3804
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
3805
3805
|
const generator = new CodeGenerator();
|
|
@@ -4097,7 +4097,7 @@ describe("CodeGenerator", () => {
|
|
|
4097
4097
|
describe("Multi-dimensional array", () => {
|
|
4098
4098
|
it("should generate 3D array declaration", () => {
|
|
4099
4099
|
const source = `
|
|
4100
|
-
u8[2]
|
|
4100
|
+
u8[2][3][4] cube;
|
|
4101
4101
|
`;
|
|
4102
4102
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
4103
4103
|
const generator = new CodeGenerator();
|
|
@@ -6932,7 +6932,7 @@ describe("CodeGenerator", () => {
|
|
|
6932
6932
|
expect(code).toContain("ExternalClass obj = {}");
|
|
6933
6933
|
});
|
|
6934
6934
|
|
|
6935
|
-
it("should initialize known structs to {
|
|
6935
|
+
it("should initialize known structs to {} in C++ mode (Issue #1004)", () => {
|
|
6936
6936
|
const source = `
|
|
6937
6937
|
struct Point { i32 x; i32 y; }
|
|
6938
6938
|
void main() {
|
|
@@ -6950,8 +6950,10 @@ describe("CodeGenerator", () => {
|
|
|
6950
6950
|
cppMode: true,
|
|
6951
6951
|
});
|
|
6952
6952
|
|
|
6953
|
-
//
|
|
6954
|
-
|
|
6953
|
+
// Issue #1004: C++ value-initialization ({}) is valid for any struct,
|
|
6954
|
+
// including ones whose first field is an enum where {0} would be an
|
|
6955
|
+
// invalid int->enum narrowing.
|
|
6956
|
+
expect(code).toContain("Point p = {}");
|
|
6955
6957
|
});
|
|
6956
6958
|
|
|
6957
6959
|
// Note: Template type tests skipped - template argument transformation
|
|
@@ -7023,7 +7025,7 @@ describe("CodeGenerator", () => {
|
|
|
7023
7025
|
expect(code).not.toContain("uint8_t* buf");
|
|
7024
7026
|
});
|
|
7025
7027
|
|
|
7026
|
-
it("should generate primitive C-Next style arrays with {
|
|
7028
|
+
it("should generate primitive C-Next style arrays with {} in C++ mode (Issue #1004)", () => {
|
|
7027
7029
|
const source = `
|
|
7028
7030
|
void main() {
|
|
7029
7031
|
u32[8] counters;
|
|
@@ -7040,8 +7042,9 @@ describe("CodeGenerator", () => {
|
|
|
7040
7042
|
cppMode: true,
|
|
7041
7043
|
});
|
|
7042
7044
|
|
|
7043
|
-
//
|
|
7044
|
-
|
|
7045
|
+
// Issue #1004: C++ arrays value-initialize with {} regardless of
|
|
7046
|
+
// element type (one unified aggregate zero-init rule).
|
|
7047
|
+
expect(code).toContain("uint32_t counters[8] = {}");
|
|
7045
7048
|
});
|
|
7046
7049
|
|
|
7047
7050
|
it("should generate unknown user type C-Next style arrays with {} in C++ mode", () => {
|
|
@@ -7065,7 +7068,7 @@ describe("CodeGenerator", () => {
|
|
|
7065
7068
|
expect(code).toContain("UnknownType items[4] = {}");
|
|
7066
7069
|
});
|
|
7067
7070
|
|
|
7068
|
-
it("should generate known C-Next struct arrays with {
|
|
7071
|
+
it("should generate known C-Next struct arrays with {} in C++ mode (Issue #1004)", () => {
|
|
7069
7072
|
const source = `
|
|
7070
7073
|
struct Point { i32 x; i32 y; }
|
|
7071
7074
|
void main() {
|
|
@@ -7083,8 +7086,9 @@ describe("CodeGenerator", () => {
|
|
|
7083
7086
|
cppMode: true,
|
|
7084
7087
|
});
|
|
7085
7088
|
|
|
7086
|
-
//
|
|
7087
|
-
|
|
7089
|
+
// Issue #1004: C++ struct arrays value-initialize with {}, valid even
|
|
7090
|
+
// when the struct's first field is an enum.
|
|
7091
|
+
expect(code).toContain("Point pts[3] = {}");
|
|
7088
7092
|
});
|
|
7089
7093
|
});
|
|
7090
7094
|
});
|
|
@@ -8483,7 +8487,7 @@ describe("CodeGenerator", () => {
|
|
|
8483
8487
|
it("should handle multi-dimensional array access", () => {
|
|
8484
8488
|
const source = `
|
|
8485
8489
|
void test() {
|
|
8486
|
-
u32[3]
|
|
8490
|
+
u32[3][3] matrix;
|
|
8487
8491
|
u32 val <- matrix[1][2];
|
|
8488
8492
|
}
|
|
8489
8493
|
`;
|
|
@@ -9420,7 +9424,9 @@ describe("CodeGenerator", () => {
|
|
|
9420
9424
|
sourcePath: "test.cnx",
|
|
9421
9425
|
});
|
|
9422
9426
|
|
|
9423
|
-
|
|
9427
|
+
// Issue #1032: Comparison operands don't get U suffix (would change semantics)
|
|
9428
|
+
// but ternary arms do (they're assigned to u32 result)
|
|
9429
|
+
expect(code).toContain("(x > 3) ? 10U : 20U");
|
|
9424
9430
|
});
|
|
9425
9431
|
});
|
|
9426
9432
|
|
|
@@ -11293,7 +11299,7 @@ describe("CodeGenerator", () => {
|
|
|
11293
11299
|
it("should allow empty brackets for size inference", () => {
|
|
11294
11300
|
const source = `
|
|
11295
11301
|
void test() {
|
|
11296
|
-
u8
|
|
11302
|
+
u8[] data <- [1, 2, 3];
|
|
11297
11303
|
}
|
|
11298
11304
|
`;
|
|
11299
11305
|
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
@@ -11309,7 +11315,7 @@ describe("CodeGenerator", () => {
|
|
|
11309
11315
|
expect(code).toContain("uint8_t data[3]");
|
|
11310
11316
|
});
|
|
11311
11317
|
|
|
11312
|
-
it("should
|
|
11318
|
+
it("should reject multi-dimensional C-style arrays (Issue #1014)", () => {
|
|
11313
11319
|
const source = `
|
|
11314
11320
|
void test() {
|
|
11315
11321
|
u8 matrix[4][4];
|
|
@@ -11321,6 +11327,26 @@ describe("CodeGenerator", () => {
|
|
|
11321
11327
|
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
11322
11328
|
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
11323
11329
|
|
|
11330
|
+
expect(() =>
|
|
11331
|
+
generator.generate(tree, tokenStream, {
|
|
11332
|
+
symbolInfo: symbols,
|
|
11333
|
+
sourcePath: "test.cnx",
|
|
11334
|
+
}),
|
|
11335
|
+
).toThrow("C-style array declaration is not allowed");
|
|
11336
|
+
});
|
|
11337
|
+
|
|
11338
|
+
it("should allow multi-dimensional C-Next style arrays", () => {
|
|
11339
|
+
const source = `
|
|
11340
|
+
void test() {
|
|
11341
|
+
u8[4][4] matrix;
|
|
11342
|
+
matrix[0][0] <- 0;
|
|
11343
|
+
}
|
|
11344
|
+
`;
|
|
11345
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
11346
|
+
const generator = new CodeGenerator();
|
|
11347
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
11348
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
11349
|
+
|
|
11324
11350
|
const code = generator.generate(tree, tokenStream, {
|
|
11325
11351
|
symbolInfo: symbols,
|
|
11326
11352
|
sourcePath: "test.cnx",
|
|
@@ -14042,7 +14068,7 @@ describe("CodeGenerator", () => {
|
|
|
14042
14068
|
it("should detect array element of string array", () => {
|
|
14043
14069
|
const source = `
|
|
14044
14070
|
void test() {
|
|
14045
|
-
string<32>
|
|
14071
|
+
string<32>[3] names;
|
|
14046
14072
|
names[0] <- "Alice";
|
|
14047
14073
|
bool check <- (names[0] = "Alice");
|
|
14048
14074
|
}
|
|
@@ -14086,7 +14112,7 @@ describe("CodeGenerator", () => {
|
|
|
14086
14112
|
it("should register multi-dimensional array", () => {
|
|
14087
14113
|
const source = `
|
|
14088
14114
|
void test() {
|
|
14089
|
-
u8[10]
|
|
14115
|
+
u8[10][20] matrix;
|
|
14090
14116
|
matrix[0][0] <- 0;
|
|
14091
14117
|
}
|
|
14092
14118
|
`;
|
|
@@ -14892,8 +14918,8 @@ describe("CodeGenerator", () => {
|
|
|
14892
14918
|
});
|
|
14893
14919
|
});
|
|
14894
14920
|
|
|
14895
|
-
describe("break and continue
|
|
14896
|
-
it("should
|
|
14921
|
+
describe("break and continue rejection (Issue #1011)", () => {
|
|
14922
|
+
it("should reject break - not part of C-Next spec", () => {
|
|
14897
14923
|
const source = `
|
|
14898
14924
|
void test() {
|
|
14899
14925
|
while (true) {
|
|
@@ -14906,15 +14932,15 @@ describe("CodeGenerator", () => {
|
|
|
14906
14932
|
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
14907
14933
|
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
14908
14934
|
|
|
14909
|
-
|
|
14910
|
-
|
|
14911
|
-
|
|
14912
|
-
|
|
14913
|
-
|
|
14914
|
-
|
|
14935
|
+
expect(() =>
|
|
14936
|
+
generator.generate(tree, tokenStream, {
|
|
14937
|
+
symbolInfo: symbols,
|
|
14938
|
+
sourcePath: "test.cnx",
|
|
14939
|
+
}),
|
|
14940
|
+
).toThrow("'break' is not supported in C-Next");
|
|
14915
14941
|
});
|
|
14916
14942
|
|
|
14917
|
-
it("should
|
|
14943
|
+
it("should reject continue - not part of C-Next spec", () => {
|
|
14918
14944
|
const source = `
|
|
14919
14945
|
void test() {
|
|
14920
14946
|
u32 i <- 0;
|
|
@@ -14931,12 +14957,12 @@ describe("CodeGenerator", () => {
|
|
|
14931
14957
|
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
14932
14958
|
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
14933
14959
|
|
|
14934
|
-
|
|
14935
|
-
|
|
14936
|
-
|
|
14937
|
-
|
|
14938
|
-
|
|
14939
|
-
|
|
14960
|
+
expect(() =>
|
|
14961
|
+
generator.generate(tree, tokenStream, {
|
|
14962
|
+
symbolInfo: symbols,
|
|
14963
|
+
sourcePath: "test.cnx",
|
|
14964
|
+
}),
|
|
14965
|
+
).toThrow("'continue' is not supported in C-Next");
|
|
14940
14966
|
});
|
|
14941
14967
|
});
|
|
14942
14968
|
|
|
@@ -15632,4 +15658,94 @@ describe("CodeGenerator", () => {
|
|
|
15632
15658
|
}).toThrow("Error: enum generator not registered");
|
|
15633
15659
|
});
|
|
15634
15660
|
});
|
|
15661
|
+
|
|
15662
|
+
describe("struct initializer — declaration vs expression context", () => {
|
|
15663
|
+
it("should use designated initializer without type cast for global struct declaration", () => {
|
|
15664
|
+
const source = `
|
|
15665
|
+
struct Point { i32 x; i32 y; }
|
|
15666
|
+
Point origin <- {x: 0, y: 0};
|
|
15667
|
+
`;
|
|
15668
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
15669
|
+
const generator = new CodeGenerator();
|
|
15670
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
15671
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
15672
|
+
|
|
15673
|
+
const code = generator.generate(tree, tokenStream, {
|
|
15674
|
+
symbolInfo: symbols,
|
|
15675
|
+
sourcePath: "test.cnx",
|
|
15676
|
+
});
|
|
15677
|
+
|
|
15678
|
+
// Must use plain designated initializer — (Point){...} is not a C99 constant expression
|
|
15679
|
+
// and fails to compile at file scope on GCC < 13
|
|
15680
|
+
expect(code).toContain("origin = { .x = 0, .y = 0 }");
|
|
15681
|
+
expect(code).not.toContain("(Point)");
|
|
15682
|
+
});
|
|
15683
|
+
|
|
15684
|
+
it("should use designated initializer without type cast for local struct declaration", () => {
|
|
15685
|
+
const source = `
|
|
15686
|
+
struct Point { i32 x; i32 y; }
|
|
15687
|
+
void foo() {
|
|
15688
|
+
Point p <- {x: 10, y: 20};
|
|
15689
|
+
}
|
|
15690
|
+
`;
|
|
15691
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
15692
|
+
const generator = new CodeGenerator();
|
|
15693
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
15694
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
15695
|
+
|
|
15696
|
+
const code = generator.generate(tree, tokenStream, {
|
|
15697
|
+
symbolInfo: symbols,
|
|
15698
|
+
sourcePath: "test.cnx",
|
|
15699
|
+
});
|
|
15700
|
+
|
|
15701
|
+
expect(code).toContain("p = { .x = 10, .y = 20 }");
|
|
15702
|
+
expect(code).not.toContain("(Point)");
|
|
15703
|
+
});
|
|
15704
|
+
|
|
15705
|
+
it("should use designated initializer for nested struct literals in declaration", () => {
|
|
15706
|
+
const source = `
|
|
15707
|
+
struct Point { i32 x; i32 y; }
|
|
15708
|
+
struct Line { Point start; Point end; }
|
|
15709
|
+
Line seg <- {start: {x: 0, y: 0}, end: {x: 100, y: 100}};
|
|
15710
|
+
`;
|
|
15711
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
15712
|
+
const generator = new CodeGenerator();
|
|
15713
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
15714
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
15715
|
+
|
|
15716
|
+
const code = generator.generate(tree, tokenStream, {
|
|
15717
|
+
symbolInfo: symbols,
|
|
15718
|
+
sourcePath: "test.cnx",
|
|
15719
|
+
});
|
|
15720
|
+
|
|
15721
|
+
// Neither outer nor inner initializer should have a type cast prefix
|
|
15722
|
+
expect(code).toContain(
|
|
15723
|
+
"{ .start = { .x = 0, .y = 0 }, .end = { .x = 100, .y = 100 } }",
|
|
15724
|
+
);
|
|
15725
|
+
expect(code).not.toContain("(Line)");
|
|
15726
|
+
expect(code).not.toContain("(Point)");
|
|
15727
|
+
});
|
|
15728
|
+
|
|
15729
|
+
it("should keep compound literal type cast for struct re-assignment (expression context)", () => {
|
|
15730
|
+
const source = `
|
|
15731
|
+
struct Point { i32 x; i32 y; }
|
|
15732
|
+
void foo() {
|
|
15733
|
+
Point p <- {x: 0, y: 0};
|
|
15734
|
+
p <- {x: 10, y: 20};
|
|
15735
|
+
}
|
|
15736
|
+
`;
|
|
15737
|
+
const { tree, tokenStream } = CNextSourceParser.parse(source);
|
|
15738
|
+
const generator = new CodeGenerator();
|
|
15739
|
+
const tSymbols = CNextResolver.resolve(tree, "test.cnx");
|
|
15740
|
+
const symbols = TSymbolInfoAdapter.convert(tSymbols);
|
|
15741
|
+
|
|
15742
|
+
const code = generator.generate(tree, tokenStream, {
|
|
15743
|
+
symbolInfo: symbols,
|
|
15744
|
+
sourcePath: "test.cnx",
|
|
15745
|
+
});
|
|
15746
|
+
|
|
15747
|
+
// Re-assignment must use compound literal — plain { } is not valid as an expression
|
|
15748
|
+
expect(code).toContain("p = (Point){ .x = 10, .y = 20 }");
|
|
15749
|
+
});
|
|
15750
|
+
});
|
|
15635
15751
|
});
|
|
@@ -27,7 +27,7 @@ describe("trackVariableTypeWithName helpers", () => {
|
|
|
27
27
|
describe("extractArrayDimensionsSimple", () => {
|
|
28
28
|
it("handles string array with single dimension", async () => {
|
|
29
29
|
const source = `
|
|
30
|
-
string<32>
|
|
30
|
+
string<32>[4] messages;
|
|
31
31
|
void main() {
|
|
32
32
|
messages[0] <- "Hello";
|
|
33
33
|
}
|
|
@@ -39,7 +39,7 @@ describe("trackVariableTypeWithName helpers", () => {
|
|
|
39
39
|
|
|
40
40
|
it("handles string array with multiple dimensions", async () => {
|
|
41
41
|
const source = `
|
|
42
|
-
string<16>
|
|
42
|
+
string<16>[2][3] grid;
|
|
43
43
|
void main() {
|
|
44
44
|
grid[0][0] <- "test";
|
|
45
45
|
}
|
|
@@ -650,6 +650,7 @@ describe("TypeValidator", () => {
|
|
|
650
650
|
type: "int",
|
|
651
651
|
isConst: false,
|
|
652
652
|
isPointer: false,
|
|
653
|
+
isStruct: false,
|
|
653
654
|
isArray: false,
|
|
654
655
|
arrayDims: "",
|
|
655
656
|
},
|
|
@@ -665,6 +666,7 @@ describe("TypeValidator", () => {
|
|
|
665
666
|
type: "int",
|
|
666
667
|
isConst: false,
|
|
667
668
|
isPointer: false,
|
|
669
|
+
isStruct: false,
|
|
668
670
|
isArray: false,
|
|
669
671
|
arrayDims: "",
|
|
670
672
|
},
|
|
@@ -702,6 +704,7 @@ describe("TypeValidator", () => {
|
|
|
702
704
|
type: "int",
|
|
703
705
|
isConst: false,
|
|
704
706
|
isPointer: false,
|
|
707
|
+
isStruct: false,
|
|
705
708
|
isArray: false,
|
|
706
709
|
arrayDims: "",
|
|
707
710
|
},
|
|
@@ -728,6 +731,7 @@ describe("TypeValidator", () => {
|
|
|
728
731
|
type: "int",
|
|
729
732
|
isConst: false,
|
|
730
733
|
isPointer: false,
|
|
734
|
+
isStruct: false,
|
|
731
735
|
isArray: false,
|
|
732
736
|
arrayDims: "",
|
|
733
737
|
},
|
|
@@ -743,6 +747,7 @@ describe("TypeValidator", () => {
|
|
|
743
747
|
type: "float",
|
|
744
748
|
isConst: false,
|
|
745
749
|
isPointer: false,
|
|
750
|
+
isStruct: false,
|
|
746
751
|
isArray: false,
|
|
747
752
|
arrayDims: "",
|
|
748
753
|
},
|
|
@@ -763,6 +768,7 @@ describe("TypeValidator", () => {
|
|
|
763
768
|
type: "int",
|
|
764
769
|
isConst: true,
|
|
765
770
|
isPointer: false,
|
|
771
|
+
isStruct: false,
|
|
766
772
|
isArray: false,
|
|
767
773
|
arrayDims: "",
|
|
768
774
|
},
|
|
@@ -778,6 +784,7 @@ describe("TypeValidator", () => {
|
|
|
778
784
|
type: "int",
|
|
779
785
|
isConst: false,
|
|
780
786
|
isPointer: false,
|
|
787
|
+
isStruct: false,
|
|
781
788
|
isArray: false,
|
|
782
789
|
arrayDims: "",
|
|
783
790
|
},
|
|
@@ -798,6 +805,7 @@ describe("TypeValidator", () => {
|
|
|
798
805
|
type: "int",
|
|
799
806
|
isConst: false,
|
|
800
807
|
isPointer: true,
|
|
808
|
+
isStruct: false,
|
|
801
809
|
isArray: false,
|
|
802
810
|
arrayDims: "",
|
|
803
811
|
},
|
|
@@ -813,6 +821,7 @@ describe("TypeValidator", () => {
|
|
|
813
821
|
type: "int",
|
|
814
822
|
isConst: false,
|
|
815
823
|
isPointer: false,
|
|
824
|
+
isStruct: false,
|
|
816
825
|
isArray: false,
|
|
817
826
|
arrayDims: "",
|
|
818
827
|
},
|
|
@@ -833,6 +842,7 @@ describe("TypeValidator", () => {
|
|
|
833
842
|
type: "int",
|
|
834
843
|
isConst: false,
|
|
835
844
|
isPointer: false,
|
|
845
|
+
isStruct: false,
|
|
836
846
|
isArray: true,
|
|
837
847
|
arrayDims: "[10]",
|
|
838
848
|
},
|
|
@@ -848,6 +858,7 @@ describe("TypeValidator", () => {
|
|
|
848
858
|
type: "int",
|
|
849
859
|
isConst: false,
|
|
850
860
|
isPointer: false,
|
|
861
|
+
isStruct: false,
|
|
851
862
|
isArray: false,
|
|
852
863
|
arrayDims: "",
|
|
853
864
|
},
|