c-next 0.1.62 → 0.1.64
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 +86 -63
- package/package.json +1 -1
- package/src/transpiler/Transpiler.ts +3 -2
- package/src/transpiler/__tests__/DualCodePaths.test.ts +1 -1
- package/src/transpiler/__tests__/Transpiler.coverage.test.ts +1 -1
- package/src/transpiler/__tests__/Transpiler.test.ts +0 -23
- package/src/transpiler/logic/symbols/cnext/collectors/StructCollector.ts +156 -70
- package/src/transpiler/logic/symbols/cnext/collectors/VariableCollector.ts +31 -6
- package/src/transpiler/logic/symbols/cnext/utils/TypeUtils.ts +43 -11
- package/src/transpiler/output/codegen/CodeGenState.ts +811 -0
- package/src/transpiler/output/codegen/CodeGenerator.ts +848 -1382
- package/src/transpiler/output/codegen/TypeResolver.ts +193 -149
- package/src/transpiler/output/codegen/TypeValidator.ts +148 -370
- package/src/transpiler/output/codegen/__tests__/CodeGenState.test.ts +446 -0
- package/src/transpiler/output/codegen/__tests__/CodeGenerator.test.ts +462 -60
- package/src/transpiler/output/codegen/__tests__/TrackVariableTypeHelpers.test.ts +1 -1
- package/src/transpiler/output/codegen/__tests__/TypeResolver.test.ts +435 -196
- package/src/transpiler/output/codegen/__tests__/TypeValidator.resolution.test.ts +51 -67
- package/src/transpiler/output/codegen/__tests__/TypeValidator.test.ts +495 -471
- package/src/transpiler/output/codegen/analysis/MemberChainAnalyzer.ts +39 -43
- package/src/transpiler/output/codegen/analysis/StringLengthCounter.ts +52 -55
- package/src/transpiler/output/codegen/analysis/__tests__/MemberChainAnalyzer.test.ts +122 -62
- package/src/transpiler/output/codegen/analysis/__tests__/StringLengthCounter.test.ts +101 -144
- package/src/transpiler/output/codegen/assignment/AssignmentClassifier.ts +143 -126
- package/src/transpiler/output/codegen/assignment/__tests__/AssignmentClassifier.test.ts +287 -320
- package/src/transpiler/output/codegen/generators/GeneratorRegistry.ts +12 -0
- package/src/transpiler/output/codegen/generators/__tests__/GeneratorRegistry.test.ts +28 -1
- package/src/transpiler/output/codegen/generators/declarationGenerators/ArrayDimensionUtils.ts +67 -0
- package/src/transpiler/output/codegen/generators/declarationGenerators/ScopeGenerator.ts +121 -51
- package/src/transpiler/output/codegen/generators/declarationGenerators/StructGenerator.ts +100 -23
- package/src/transpiler/output/codegen/generators/declarationGenerators/__tests__/ArrayDimensionUtils.test.ts +125 -0
- package/src/transpiler/output/codegen/generators/declarationGenerators/__tests__/ScopeGenerator.test.ts +157 -4
- package/src/transpiler/output/codegen/generators/support/HelperGenerator.ts +23 -22
- package/src/transpiler/output/codegen/helpers/ArrayInitHelper.ts +54 -61
- package/src/transpiler/output/codegen/helpers/AssignmentExpectedTypeResolver.ts +21 -30
- package/src/transpiler/output/codegen/helpers/AssignmentValidator.ts +56 -53
- package/src/transpiler/output/codegen/helpers/CppModeHelper.ts +22 -30
- package/src/transpiler/output/codegen/helpers/EnumAssignmentValidator.ts +108 -50
- package/src/transpiler/output/codegen/helpers/FloatBitHelper.ts +16 -31
- package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +103 -96
- package/src/transpiler/output/codegen/helpers/TypeGenerationHelper.ts +9 -0
- package/src/transpiler/output/codegen/helpers/__tests__/ArrayInitHelper.test.ts +58 -103
- package/src/transpiler/output/codegen/helpers/__tests__/AssignmentExpectedTypeResolver.test.ts +97 -40
- package/src/transpiler/output/codegen/helpers/__tests__/AssignmentValidator.test.ts +223 -128
- package/src/transpiler/output/codegen/helpers/__tests__/CppModeHelper.test.ts +68 -41
- package/src/transpiler/output/codegen/helpers/__tests__/EnumAssignmentValidator.test.ts +198 -47
- package/src/transpiler/output/codegen/helpers/__tests__/FloatBitHelper.test.ts +39 -37
- package/src/transpiler/output/codegen/helpers/__tests__/StringDeclHelper.test.ts +191 -453
- package/src/transpiler/output/codegen/resolution/EnumTypeResolver.ts +229 -0
- package/src/transpiler/output/codegen/resolution/ScopeResolver.ts +60 -0
- package/src/transpiler/output/codegen/resolution/SizeofResolver.ts +177 -0
- package/src/transpiler/output/codegen/resolution/__tests__/EnumTypeResolver.test.ts +336 -0
- package/src/transpiler/output/codegen/resolution/__tests__/SizeofResolver.test.ts +201 -0
- package/src/transpiler/output/codegen/types/ITypeResolverDeps.ts +0 -23
- package/src/transpiler/output/codegen/types/ITypeValidatorDeps.ts +0 -53
|
@@ -1,34 +1,19 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach } from "vitest";
|
|
2
2
|
import TypeValidator from "../TypeValidator";
|
|
3
|
+
import CodeGenState from "../CodeGenState";
|
|
3
4
|
import type ICodeGenSymbols from "../../../types/ICodeGenSymbols";
|
|
4
|
-
import type TTypeInfo from "../types/TTypeInfo";
|
|
5
5
|
|
|
6
6
|
describe("TypeValidator.resolveBareIdentifier", () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
[
|
|
16
|
-
"globalCounter",
|
|
17
|
-
{ baseType: "u32", bitWidth: 32, isArray: false, isConst: false },
|
|
18
|
-
],
|
|
19
|
-
[
|
|
20
|
-
"Motor_speed",
|
|
21
|
-
{ baseType: "u32", bitWidth: 32, isArray: false, isConst: false },
|
|
22
|
-
],
|
|
23
|
-
]);
|
|
24
|
-
currentScope = "Motor";
|
|
25
|
-
mockSymbols = {
|
|
26
|
-
knownScopes: new Set(["Motor", "LED"]),
|
|
27
|
-
knownRegisters: new Set(["GPIO"]),
|
|
28
|
-
knownEnums: new Set(["State"]),
|
|
29
|
-
knownStructs: new Set(["Point"]),
|
|
7
|
+
const createMockSymbols = (
|
|
8
|
+
overrides: Partial<ICodeGenSymbols> = {},
|
|
9
|
+
): ICodeGenSymbols =>
|
|
10
|
+
({
|
|
11
|
+
knownScopes: new Set(),
|
|
12
|
+
knownRegisters: new Set(),
|
|
13
|
+
knownEnums: new Set(),
|
|
14
|
+
knownStructs: new Set(),
|
|
30
15
|
knownBitmaps: new Set(),
|
|
31
|
-
scopeMembers: new Map(
|
|
16
|
+
scopeMembers: new Map(),
|
|
32
17
|
scopeMemberVisibility: new Map(),
|
|
33
18
|
structFields: new Map(),
|
|
34
19
|
structFieldArrays: new Map(),
|
|
@@ -48,31 +33,40 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
48
33
|
functionReturnTypes: new Map(),
|
|
49
34
|
getSingleFunctionForVariable: () => null,
|
|
50
35
|
hasPublicSymbols: () => false,
|
|
51
|
-
|
|
52
|
-
|
|
36
|
+
...overrides,
|
|
37
|
+
}) as ICodeGenSymbols;
|
|
53
38
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
CodeGenState.reset();
|
|
41
|
+
CodeGenState.scopeMembers = new Map([
|
|
42
|
+
["Motor", new Set(["speed", "maxSpeed"])],
|
|
43
|
+
]);
|
|
44
|
+
CodeGenState.typeRegistry.set("globalCounter", {
|
|
45
|
+
baseType: "u32",
|
|
46
|
+
bitWidth: 32,
|
|
47
|
+
isArray: false,
|
|
48
|
+
isConst: false,
|
|
49
|
+
});
|
|
50
|
+
CodeGenState.typeRegistry.set("Motor_speed", {
|
|
51
|
+
baseType: "u32",
|
|
52
|
+
bitWidth: 32,
|
|
53
|
+
isArray: false,
|
|
54
|
+
isConst: false,
|
|
55
|
+
});
|
|
56
|
+
CodeGenState.currentScope = "Motor";
|
|
57
|
+
CodeGenState.symbols = createMockSymbols({
|
|
58
|
+
knownScopes: new Set(["Motor", "LED"]),
|
|
59
|
+
knownRegisters: new Set(["GPIO"]),
|
|
60
|
+
knownEnums: new Set(["State"]),
|
|
61
|
+
knownStructs: new Set(["Point"]),
|
|
62
|
+
scopeMembers: new Map([["Motor", new Set(["speed", "maxSpeed"])]]),
|
|
69
63
|
});
|
|
70
|
-
|
|
64
|
+
CodeGenState.knownFunctions = new Set(["globalFunc", "Motor_stop"]);
|
|
65
|
+
});
|
|
71
66
|
|
|
72
67
|
describe("inside a scope", () => {
|
|
73
68
|
it("returns null for local variables (no transformation needed)", () => {
|
|
74
|
-
const
|
|
75
|
-
const result = validator.resolveBareIdentifier(
|
|
69
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
76
70
|
"localVar",
|
|
77
71
|
true,
|
|
78
72
|
() => false,
|
|
@@ -81,8 +75,7 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
81
75
|
});
|
|
82
76
|
|
|
83
77
|
it("resolves scope member to prefixed name", () => {
|
|
84
|
-
const
|
|
85
|
-
const result = validator.resolveBareIdentifier(
|
|
78
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
86
79
|
"speed",
|
|
87
80
|
false,
|
|
88
81
|
() => false,
|
|
@@ -91,8 +84,7 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
91
84
|
});
|
|
92
85
|
|
|
93
86
|
it("resolves global variable to itself", () => {
|
|
94
|
-
const
|
|
95
|
-
const result = validator.resolveBareIdentifier(
|
|
87
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
96
88
|
"globalCounter",
|
|
97
89
|
false,
|
|
98
90
|
() => false,
|
|
@@ -101,8 +93,7 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
101
93
|
});
|
|
102
94
|
|
|
103
95
|
it("resolves global function to itself", () => {
|
|
104
|
-
const
|
|
105
|
-
const result = validator.resolveBareIdentifier(
|
|
96
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
106
97
|
"globalFunc",
|
|
107
98
|
false,
|
|
108
99
|
() => false,
|
|
@@ -111,8 +102,7 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
111
102
|
});
|
|
112
103
|
|
|
113
104
|
it("resolves scope function to prefixed name", () => {
|
|
114
|
-
const
|
|
115
|
-
const result = validator.resolveBareIdentifier(
|
|
105
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
116
106
|
"stop",
|
|
117
107
|
false,
|
|
118
108
|
() => false,
|
|
@@ -122,8 +112,7 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
122
112
|
});
|
|
123
113
|
|
|
124
114
|
it("returns null for unknown identifiers", () => {
|
|
125
|
-
const
|
|
126
|
-
const result = validator.resolveBareIdentifier(
|
|
115
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
127
116
|
"unknownName",
|
|
128
117
|
false,
|
|
129
118
|
() => false,
|
|
@@ -134,12 +123,11 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
134
123
|
|
|
135
124
|
describe("outside a scope", () => {
|
|
136
125
|
beforeEach(() => {
|
|
137
|
-
currentScope = null;
|
|
126
|
+
CodeGenState.currentScope = null;
|
|
138
127
|
});
|
|
139
128
|
|
|
140
129
|
it("returns null for local variables", () => {
|
|
141
|
-
const
|
|
142
|
-
const result = validator.resolveBareIdentifier(
|
|
130
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
143
131
|
"localVar",
|
|
144
132
|
true,
|
|
145
133
|
() => false,
|
|
@@ -148,8 +136,7 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
148
136
|
});
|
|
149
137
|
|
|
150
138
|
it("returns null for global variables (no transformation)", () => {
|
|
151
|
-
const
|
|
152
|
-
const result = validator.resolveBareIdentifier(
|
|
139
|
+
const result = TypeValidator.resolveBareIdentifier(
|
|
153
140
|
"globalCounter",
|
|
154
141
|
false,
|
|
155
142
|
() => false,
|
|
@@ -161,28 +148,25 @@ describe("TypeValidator.resolveBareIdentifier", () => {
|
|
|
161
148
|
describe("resolveForMemberAccess", () => {
|
|
162
149
|
it("prefers scope name over global variable for member access", () => {
|
|
163
150
|
// Setup: global variable 'LED' exists AND scope 'LED' exists
|
|
164
|
-
typeRegistry.set("LED", {
|
|
151
|
+
CodeGenState.typeRegistry.set("LED", {
|
|
165
152
|
baseType: "u8",
|
|
166
153
|
bitWidth: 8,
|
|
167
154
|
isArray: false,
|
|
168
155
|
isConst: false,
|
|
169
156
|
});
|
|
170
|
-
const validator = createValidator();
|
|
171
157
|
|
|
172
|
-
const result =
|
|
158
|
+
const result = TypeValidator.resolveForMemberAccess("LED");
|
|
173
159
|
expect(result).toBe("LED"); // Returns scope name, not transformed
|
|
174
160
|
expect(result).not.toBe("Motor_LED"); // Should NOT be scope-prefixed
|
|
175
161
|
});
|
|
176
162
|
|
|
177
163
|
it("returns scope name when it exists", () => {
|
|
178
|
-
const
|
|
179
|
-
const result = validator.resolveForMemberAccess("LED");
|
|
164
|
+
const result = TypeValidator.resolveForMemberAccess("LED");
|
|
180
165
|
expect(result).toBe("LED");
|
|
181
166
|
});
|
|
182
167
|
|
|
183
168
|
it("returns null for unknown identifiers", () => {
|
|
184
|
-
const
|
|
185
|
-
const result = validator.resolveForMemberAccess("Unknown");
|
|
169
|
+
const result = TypeValidator.resolveForMemberAccess("Unknown");
|
|
186
170
|
expect(result).toBeNull();
|
|
187
171
|
});
|
|
188
172
|
});
|