c-next 0.1.61 → 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.
- package/README.md +86 -63
- package/grammar/CNext.g4 +3 -17
- package/package.json +1 -1
- package/src/cli/serve/ServeCommand.ts +57 -45
- package/src/lib/__tests__/parseCHeader.mocked.test.ts +145 -0
- package/src/transpiler/Transpiler.ts +603 -613
- package/src/transpiler/__tests__/DualCodePaths.test.ts +5 -1
- package/src/transpiler/__tests__/Transpiler.coverage.test.ts +2 -99
- package/src/transpiler/__tests__/Transpiler.test.ts +3 -26
- package/src/transpiler/data/IncludeTreeWalker.ts +1 -1
- package/src/transpiler/logic/analysis/InitializationAnalyzer.ts +23 -52
- package/src/transpiler/logic/parser/grammar/CNext.interp +1 -3
- package/src/transpiler/logic/parser/grammar/CNextListener.ts +0 -22
- package/src/transpiler/logic/parser/grammar/CNextParser.ts +665 -1084
- package/src/transpiler/logic/parser/grammar/CNextVisitor.ts +0 -14
- package/src/transpiler/logic/symbols/CppSymbolCollector.ts +67 -43
- 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 +1410 -2587
- 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 +2082 -52
- 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 +227 -66
- package/src/transpiler/output/codegen/analysis/StringLengthCounter.ts +55 -58
- package/src/transpiler/output/codegen/analysis/__tests__/MemberChainAnalyzer.test.ts +288 -275
- package/src/transpiler/output/codegen/analysis/__tests__/StringLengthCounter.test.ts +101 -144
- package/src/transpiler/output/codegen/assignment/AssignmentClassifier.ts +195 -133
- package/src/transpiler/output/codegen/assignment/AssignmentContextBuilder.ts +24 -74
- package/src/transpiler/output/codegen/assignment/AssignmentKind.ts +3 -0
- package/src/transpiler/output/codegen/assignment/IAssignmentContext.ts +3 -0
- package/src/transpiler/output/codegen/assignment/__tests__/AssignmentClassifier.test.ts +290 -320
- package/src/transpiler/output/codegen/assignment/handlers/BitAccessHandlers.ts +42 -0
- package/src/transpiler/output/codegen/assignment/handlers/__tests__/BitAccessHandlers.test.ts +76 -2
- package/src/transpiler/output/codegen/generators/GeneratorRegistry.ts +12 -0
- package/src/transpiler/output/codegen/generators/IOrchestrator.ts +5 -1
- 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/RegisterGenerator.ts +11 -24
- package/src/transpiler/output/codegen/generators/declarationGenerators/RegisterMacroGenerator.ts +64 -0
- package/src/transpiler/output/codegen/generators/declarationGenerators/ScopeGenerator.ts +137 -61
- package/src/transpiler/output/codegen/generators/declarationGenerators/ScopedRegisterGenerator.ts +18 -27
- 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/expressions/PostfixExpressionGenerator.ts +5 -1
- package/src/transpiler/output/codegen/generators/statements/ControlFlowGenerator.ts +1 -17
- package/src/transpiler/output/codegen/generators/support/HelperGenerator.ts +23 -22
- package/src/transpiler/output/codegen/helpers/ArrayAccessHelper.ts +129 -0
- package/src/transpiler/output/codegen/helpers/ArrayInitHelper.ts +54 -61
- package/src/transpiler/output/codegen/helpers/AssignmentExpectedTypeResolver.ts +40 -44
- package/src/transpiler/output/codegen/helpers/AssignmentTargetExtractor.ts +17 -45
- package/src/transpiler/output/codegen/helpers/AssignmentValidator.ts +83 -78
- 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/MemberSeparatorResolver.ts +10 -3
- package/src/transpiler/output/codegen/helpers/StringDeclHelper.ts +103 -96
- package/src/transpiler/output/codegen/helpers/SymbolLookupHelper.ts +44 -0
- package/src/transpiler/output/codegen/helpers/TypeGenerationHelper.ts +9 -0
- package/src/transpiler/output/codegen/helpers/__tests__/ArrayAccessHelper.test.ts +479 -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__/MemberSeparatorResolver.test.ts +1 -0
- package/src/transpiler/output/codegen/helpers/__tests__/StringDeclHelper.test.ts +191 -453
- package/src/transpiler/output/codegen/helpers/__tests__/SymbolLookupHelper.test.ts +201 -0
- package/src/transpiler/output/codegen/helpers/__tests__/TypeGenerationHelper.test.ts +50 -0
- 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/IArrayAccessDeps.ts +23 -0
- package/src/transpiler/output/codegen/types/IArrayAccessInfo.ts +26 -0
- package/src/transpiler/output/codegen/types/IMemberSeparatorDeps.ts +7 -0
- package/src/transpiler/output/codegen/utils/CodegenParserUtils.ts +98 -0
- package/src/transpiler/output/codegen/utils/ExpressionUnwrapper.ts +22 -22
- package/src/transpiler/output/codegen/utils/__tests__/CodegenParserUtils.test.ts +228 -0
- package/src/transpiler/types/IFileResult.ts +0 -4
- package/src/transpiler/types/IPipelineFile.ts +27 -0
- package/src/transpiler/types/IPipelineInput.ts +23 -0
- package/src/transpiler/types/TranspilerState.ts +1 -1
- package/src/utils/FormatUtils.ts +28 -2
- package/src/utils/MapUtils.ts +25 -0
- package/src/utils/PostfixAnalysisUtils.ts +48 -0
- package/src/utils/__tests__/FormatUtils.test.ts +42 -0
- package/src/utils/__tests__/MapUtils.test.ts +85 -0
- package/src/utils/constants/OperatorMappings.ts +19 -0
- package/src/transpiler/logic/StandaloneContextBuilder.ts +0 -150
- package/src/transpiler/logic/__tests__/StandaloneContextBuilder.test.ts +0 -647
- package/src/transpiler/output/codegen/types/ITypeResolverDeps.ts +0 -23
- package/src/transpiler/output/codegen/types/ITypeValidatorDeps.ts +0 -53
- package/src/transpiler/types/ITranspileContext.ts +0 -49
- package/src/transpiler/types/ITranspileContribution.ts +0 -32
|
@@ -7,21 +7,7 @@
|
|
|
7
7
|
import * as Parser from "../../../logic/parser/grammar/CNextParser";
|
|
8
8
|
import IAssignmentContext from "./IAssignmentContext";
|
|
9
9
|
import TTypeInfo from "../types/TTypeInfo";
|
|
10
|
-
|
|
11
|
-
/** Operator mapping from C-Next to C */
|
|
12
|
-
const ASSIGNMENT_OPERATOR_MAP: Record<string, string> = {
|
|
13
|
-
"<-": "=",
|
|
14
|
-
"+<-": "+=",
|
|
15
|
-
"-<-": "-=",
|
|
16
|
-
"*<-": "*=",
|
|
17
|
-
"/<-": "/=",
|
|
18
|
-
"%<-": "%=",
|
|
19
|
-
"&<-": "&=",
|
|
20
|
-
"|<-": "|=",
|
|
21
|
-
"^<-": "^=",
|
|
22
|
-
"<<<-": "<<=",
|
|
23
|
-
">><-": ">>=",
|
|
24
|
-
};
|
|
10
|
+
import ASSIGNMENT_OPERATOR_MAP from "../../../../utils/constants/OperatorMappings";
|
|
25
11
|
|
|
26
12
|
/**
|
|
27
13
|
* Dependencies for building context.
|
|
@@ -42,57 +28,23 @@ interface ITargetExtraction {
|
|
|
42
28
|
subscripts: Parser.ExpressionContext[];
|
|
43
29
|
hasMemberAccess: boolean;
|
|
44
30
|
hasArrayAccess: boolean;
|
|
31
|
+
/** Number of expressions in the last subscript operation */
|
|
32
|
+
lastSubscriptExprCount: number;
|
|
45
33
|
}
|
|
46
34
|
|
|
47
35
|
/**
|
|
48
|
-
* Extract
|
|
49
|
-
*
|
|
36
|
+
* Extract base identifier from assignment target.
|
|
37
|
+
* With unified grammar, all patterns use IDENTIFIER postfixTargetOp*.
|
|
50
38
|
*/
|
|
51
|
-
function
|
|
39
|
+
function extractBaseIdentifier(
|
|
52
40
|
targetCtx: Parser.AssignmentTargetContext,
|
|
53
41
|
): ITargetExtraction {
|
|
54
42
|
const identifiers: string[] = [];
|
|
55
43
|
const subscripts: Parser.ExpressionContext[] = [];
|
|
56
44
|
|
|
57
|
-
|
|
58
|
-
const legacyArrayAccess = targetCtx.arrayAccess();
|
|
59
|
-
|
|
45
|
+
// All patterns now have a base IDENTIFIER
|
|
60
46
|
if (targetCtx.IDENTIFIER()) {
|
|
61
47
|
identifiers.push(targetCtx.IDENTIFIER()!.getText());
|
|
62
|
-
return {
|
|
63
|
-
identifiers,
|
|
64
|
-
subscripts,
|
|
65
|
-
hasMemberAccess: legacyMemberAccess !== null,
|
|
66
|
-
hasArrayAccess: legacyArrayAccess !== null,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (legacyMemberAccess) {
|
|
71
|
-
for (const id of legacyMemberAccess.IDENTIFIER()) {
|
|
72
|
-
identifiers.push(id.getText());
|
|
73
|
-
}
|
|
74
|
-
for (const expr of legacyMemberAccess.expression()) {
|
|
75
|
-
subscripts.push(expr);
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
identifiers,
|
|
79
|
-
subscripts,
|
|
80
|
-
hasMemberAccess: true,
|
|
81
|
-
hasArrayAccess: subscripts.length > 0,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (legacyArrayAccess) {
|
|
86
|
-
identifiers.push(legacyArrayAccess.IDENTIFIER().getText());
|
|
87
|
-
for (const expr of legacyArrayAccess.expression()) {
|
|
88
|
-
subscripts.push(expr);
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
identifiers,
|
|
92
|
-
subscripts,
|
|
93
|
-
hasMemberAccess: false,
|
|
94
|
-
hasArrayAccess: true,
|
|
95
|
-
};
|
|
96
48
|
}
|
|
97
49
|
|
|
98
50
|
return {
|
|
@@ -100,6 +52,7 @@ function extractFromLegacyAccess(
|
|
|
100
52
|
subscripts,
|
|
101
53
|
hasMemberAccess: false,
|
|
102
54
|
hasArrayAccess: false,
|
|
55
|
+
lastSubscriptExprCount: 0,
|
|
103
56
|
};
|
|
104
57
|
}
|
|
105
58
|
|
|
@@ -116,10 +69,13 @@ function processPostfixOps(
|
|
|
116
69
|
extraction.identifiers.push(op.IDENTIFIER()!.getText());
|
|
117
70
|
extraction.hasMemberAccess = true;
|
|
118
71
|
} else {
|
|
119
|
-
|
|
72
|
+
const exprs = op.expression();
|
|
73
|
+
for (const expr of exprs) {
|
|
120
74
|
extraction.subscripts.push(expr);
|
|
121
75
|
}
|
|
122
76
|
extraction.hasArrayAccess = true;
|
|
77
|
+
// Track the expression count of the last subscript operation
|
|
78
|
+
extraction.lastSubscriptExprCount = exprs.length;
|
|
123
79
|
}
|
|
124
80
|
}
|
|
125
81
|
}
|
|
@@ -149,16 +105,17 @@ function buildAssignmentContext(
|
|
|
149
105
|
const hasThis = targetCtx.THIS() !== null;
|
|
150
106
|
const postfixOps = targetCtx.postfixTargetOp();
|
|
151
107
|
|
|
152
|
-
//
|
|
153
|
-
const
|
|
154
|
-
const legacyArrayAccess = targetCtx.arrayAccess();
|
|
155
|
-
|
|
156
|
-
// Extract identifiers and subscripts using helper functions
|
|
157
|
-
const extraction = extractFromLegacyAccess(targetCtx);
|
|
108
|
+
// Extract base identifier and process postfix operations
|
|
109
|
+
const extraction = extractBaseIdentifier(targetCtx);
|
|
158
110
|
processPostfixOps(postfixOps, extraction);
|
|
159
111
|
|
|
160
|
-
const {
|
|
161
|
-
|
|
112
|
+
const {
|
|
113
|
+
identifiers,
|
|
114
|
+
subscripts,
|
|
115
|
+
hasMemberAccess,
|
|
116
|
+
hasArrayAccess,
|
|
117
|
+
lastSubscriptExprCount,
|
|
118
|
+
} = extraction;
|
|
162
119
|
|
|
163
120
|
// Get first identifier type info
|
|
164
121
|
const firstId = identifiers[0] ?? "";
|
|
@@ -175,17 +132,9 @@ function buildAssignmentContext(
|
|
|
175
132
|
!hasArrayAccess &&
|
|
176
133
|
identifiers.length === 1;
|
|
177
134
|
|
|
178
|
-
const isSimpleThisAccess =
|
|
179
|
-
hasThis &&
|
|
180
|
-
postfixOps.length === 0 &&
|
|
181
|
-
!legacyMemberAccess &&
|
|
182
|
-
!legacyArrayAccess;
|
|
135
|
+
const isSimpleThisAccess = hasThis && postfixOps.length === 0;
|
|
183
136
|
|
|
184
|
-
const isSimpleGlobalAccess =
|
|
185
|
-
hasGlobal &&
|
|
186
|
-
postfixOps.length === 0 &&
|
|
187
|
-
!legacyMemberAccess &&
|
|
188
|
-
!legacyArrayAccess;
|
|
137
|
+
const isSimpleGlobalAccess = hasGlobal && postfixOps.length === 0;
|
|
189
138
|
|
|
190
139
|
return {
|
|
191
140
|
statementCtx: ctx,
|
|
@@ -206,6 +155,7 @@ function buildAssignmentContext(
|
|
|
206
155
|
firstIdTypeInfo,
|
|
207
156
|
memberAccessDepth,
|
|
208
157
|
subscriptDepth,
|
|
158
|
+
lastSubscriptExprCount,
|
|
209
159
|
isSimpleIdentifier,
|
|
210
160
|
isSimpleThisAccess,
|
|
211
161
|
isSimpleGlobalAccess,
|
|
@@ -51,6 +51,9 @@ enum AssignmentKind {
|
|
|
51
51
|
/** item.byte[7] <- true (bit access on struct member) */
|
|
52
52
|
STRUCT_MEMBER_BIT,
|
|
53
53
|
|
|
54
|
+
/** devices[0].control[0, 4] <- 15 (bit range through struct chain) */
|
|
55
|
+
STRUCT_CHAIN_BIT_RANGE,
|
|
56
|
+
|
|
54
57
|
/** matrix[i][j][FIELD_BIT] <- false (bit on multi-dim array element) */
|
|
55
58
|
ARRAY_ELEMENT_BIT,
|
|
56
59
|
|
|
@@ -80,6 +80,9 @@ interface IAssignmentContext {
|
|
|
80
80
|
/** Number of [index] accesses in the chain */
|
|
81
81
|
readonly subscriptDepth: number;
|
|
82
82
|
|
|
83
|
+
/** Number of expressions in the last subscript operation (1=element, 2=slice/bit-range) */
|
|
84
|
+
readonly lastSubscriptExprCount: number;
|
|
85
|
+
|
|
83
86
|
/** True if target is a simple identifier (no prefix, no postfix) */
|
|
84
87
|
readonly isSimpleIdentifier: boolean;
|
|
85
88
|
|