Universal Code Converter
Build or refine the converter as a staged translation pipeline. Optimize for semantic fidelity, diagnostics, and incremental delivery instead of "convert everything" claims.
Run This Workflow
- 1. Clarify the scope.
- Capture the source language, target language, runtime constraints, supported constructs, and quality bar.
- Ask for or extract at least 3 representative snippets before designing the pipeline.
- Define what "done" means: compilable output, behavior-preserving output, migration scaffold, or partial assisted conversion.
- 2. Verify the parser frontend.
- Confirm the required Tree-sitter grammars exist and are actively usable.
- Prefer official prebuilt bindings or grammar packages for the first frontend probe. Compile grammars manually only when official artifacts are missing or the task needs grammar changes.
- Inspect actual node types and field names before hardcoding visitors.
- Capture one reusable parser-probe artifact from real fixtures: root node type, critical field names, and one query/capture example. Keep it in tests or fixtures so later work does not repeat manual spelunking.
- If Tree-sitter is part of the plan, run one incremental reparse check with
old_tree to prove the grammar is usable as an editing frontend. Treat this as a capability check, not a brittle microbenchmark.
- Treat Tree-sitter as a concrete syntax tree frontend with incremental parsing, not as a full semantic compiler.
- 3. Separate the pipeline into explicit passes.
- Use this default flow:
CODEBLOCK0
- Keep normalization separate from semantic analysis.
- Keep shared IR logic separate from pair-specific lowering rules.
- Keep code formatting separate from translation logic.
- 4. Design the IR around semantics, not surface syntax.
- Model declarations, scopes, bindings, literals, calls, control flow, imports, types, and diagnostics.
- Represent lossy or unsupported constructs explicitly with diagnostic nodes or status flags.
- Remove syntax sugar early when it simplifies downstream lowering.
- 5. Implement a narrow vertical slice first.
- Start with one source-target pair.
- Start with modules, functions, parameters, literals, identifiers, returns, calls, and simple conditionals.
- Add tests for each slice before adding more syntax.
- 6. Handle semantic gaps intentionally.
- Classify every feature as one of: direct mapping, desugaring, runtime helper, manual rewrite required, or unsupported.
- Emit warnings for behavior changes or lossy rewrites.
- Never silently drop exceptions, mutability rules, async semantics, or type expectations.
- 7. Generate target code from a structured model.
- Prefer a typed target-language model or emitter API over raw string replacement.
- Centralize naming, escaping, and indentation in the emitter layer.
- Preserve comments or source maps only when the task explicitly requires them.
- 8. Validate the output at multiple levels.
- Validate frontend assumptions separately from lowering assumptions.
- Reparse representative source fixtures and assert the parser-probe contract stays stable.
- Reparse the generated code.
- Compile or type-check the generated code when tooling is available.
- Run fixture-based execution tests for behavior-preserving conversions.
- Include at least one explicit failure-path fixture that must emit diagnostics.
- Snapshot IR and emitted output for regression coverage.
- 9. Deliver a bounded result.
- Return the supported feature matrix, known gaps, assumptions, warnings, and next steps.
- Say clearly when the result is a scaffold, partial converter, or production-ready slice.
Produce These Deliverables
- - For architecture requests, return: scope, pipeline, IR outline, feature mapping taxonomy, validation plan, and risk list.
- For implementation requests, code the smallest working slice first, add executable tests with it, and record the exact frontend dependency/version used for the parser probe.
- For review requests, prioritize semantic drift, unsupported constructs, parser-shape assumptions, and missing validation.
Read Additional References Only When Needed
- - Read
references/architecture-blueprint.md for module layout, feature mapping taxonomy, and IR design prompts. - Read
references/validation-checklist.md for test strategy, regression gates, and release criteria.
Apply These Guardrails
- - Do not call the converter "universal" unless a supported-language and supported-feature matrix exists.
- Do not equate CST shape with program meaning.
- Do not put all translation rules in one transformer file.
- Do not promise idiomatic target code before correctness and diagnostics exist.
- Do not add a new language pair without representative fixtures and validation.
Start With This Repo Shape
CODEBLOCK1
通用代码转换器
将转换器构建或优化为分阶段翻译流水线。优化语义保真度、诊断能力和增量交付,而非追求转换一切的宣称。
运行此工作流
- 1. 明确范围。
- 捕获源语言、目标语言、运行时约束、支持的构造和质量标准。
- 在设计流水线前,要求或提取至少3个代表性代码片段。
- 定义完成的含义:可编译输出、行为保持输出、迁移脚手架或部分辅助转换。
- 2. 验证解析器前端。
- 确认所需的Tree-sitter语法存在且可主动使用。
- 首次前端探测时优先使用官方预构建绑定或语法包。仅在官方产物缺失或任务需要语法修改时手动编译语法。
- 在硬编码访问器前检查实际节点类型和字段名。
- 从真实测试用例中捕获一个可复用的解析器探测产物:根节点类型、关键字段名和一个查询/捕获示例。将其保留在测试或测试用例中,避免后续工作重复手动探索。
- 如果Tree-sitter是计划的一部分,使用old_tree运行一次增量重解析检查,证明语法可用作编辑前端。将其视为能力检查,而非脆弱的微基准测试。
- 将Tree-sitter视为具有增量解析能力的具体语法树前端,而非完整的语义编译器。
- 3. 将流水线分离为显式阶段。
- 使用以下默认流程:
text
解析 -> 标准化 -> 语义增强 -> IR -> 降级 -> 生成 -> 验证
- 将标准化与语义分析分离。
- 将共享的IR逻辑与特定语言对的降级规则分离。
- 将代码格式化与翻译逻辑分离。
- 4. 围绕语义而非表面语法设计IR。
- 建模声明、作用域、绑定、字面量、调用、控制流、导入、类型和诊断。
- 使用诊断节点或状态标志显式表示有损或不支持的构造。
- 在简化下游降级时尽早移除语法糖。
- 5. 首先实现一个狭窄的垂直切片。
- 从一个源-目标语言对开始。
- 从模块、函数、参数、字面量、标识符、返回、调用和简单条件语句开始。
- 在添加更多语法前为每个切片添加测试。
- 6. 有意识地处理语义差距。
- 将每个特性分类为:直接映射、去糖化、运行时辅助、需要手动重写或不支持。
- 对行为变化或有损重写发出警告。
- 绝不静默丢弃异常、可变性规则、异步语义或类型期望。
- 7. 从结构化模型生成目标代码。
- 优先使用类型化的目标语言模型或生成器API,而非原始字符串替换。
- 在生成器层集中处理命名、转义和缩进。
- 仅当任务明确要求时保留注释或源码映射。
- 8. 在多个级别验证输出。
- 将前端假设验证与降级假设验证分离。
- 重新解析代表性源测试用例,断言解析器探测契约保持稳定。
- 重新解析生成的代码。
- 当工具可用时编译或类型检查生成的代码。
- 对行为保持转换运行基于测试用例的执行测试。
- 至少包含一个必须发出诊断的显式失败路径测试用例。
- 对IR和生成的输出进行快照,用于回归覆盖。
- 9. 交付有限的结果。
- 返回支持的特性矩阵、已知差距、假设、警告和后续步骤。
- 明确说明结果是脚手架、部分转换器还是生产就绪切片。
生成以下交付物
- - 对于架构请求,返回:范围、流水线、IR大纲、特性映射分类、验证计划和风险列表。
- 对于实现请求,首先编写最小的可工作切片,附带可执行测试,并记录解析器探测使用的确切前端依赖/版本。
- 对于审查请求,优先关注语义漂移、不支持的构造、解析器形状假设和缺失的验证。
仅在需要时阅读额外参考资料
- - 阅读references/architecture-blueprint.md了解模块布局、特性映射分类和IR设计提示。
- 阅读references/validation-checklist.md了解测试策略、回归门禁和发布标准。
应用这些防护措施
- - 除非存在支持的语言和特性矩阵,否则不要称转换器为通用。
- 不要将CST形状等同于程序含义。
- 不要将所有翻译规则放在一个转换器文件中。
- 在正确性和诊断存在之前,不要承诺地道的目标代码。
- 没有代表性测试用例和验证,不要添加新的语言对。
以此仓库结构开始
text
src/
frontends/
normalization/
semantic/
ir/
lowering/
emitters/
diagnostics/
tests/
fixtures/
golden/