Skip to content

Scratch to C++ transpiler experiment - #695

Draft
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1792234428-cpp-transpiler
Draft

devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1792234428-cpp-transpiler

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Rewrites the C++ backend on top of cpp_architecture, using C++20 coroutines. The C++17 state machine is gone.

Backend (src/engine/internal/codegen/cpp)

  • CppCodeBuilder outputs one class per script. The class derives from CppExecutableCode and overrides runScript / runReporter / runPredicate / runProcedure. It registers itself (CppCompilerContext::registerCode) under the FNV-1a hash of its source.
  • build() hashes the source and creates the registered class. If there is none, it reports the source through CppCompilerContext::setSourceCallback and returns a no-op code.
  • Pointers (constants, variables, lists) are passed in at runtime as a pointer table, so the source is the same in every process.
  • Scripts and procedures that can suspend are C++20 coroutines (CppCoroutine). A yield is co_await std::suspend_always{}, and the end-thread flag is the coroutine's return value. Warp code never suspends. kill() keeps the coroutine alive until reset().
  • Function calls follow the LLVM convention: strings are returned through a StringPtr * first parameter. Externs are declared type-erased and cast at the call site, because the tests call one function with different signatures.

Tests

  • The shared codebuilder_test and operator tests now also run for cpp. codegen_test_generator runs them once to collect the generated code. It maps ASSERT to EXPECT and restarts the test process after a crash, so every builder in a test is reached. It writes 16 shards, which are compiled into codegen_test.
  • cpp_test mock updated for CppCoroutine.

Codebase generation

  • scratchcpp-transpiler <project.sb3> <output dir> [name] (cpp backend only) writes project.cpp, project_data.cpp (the embedded .sb3), main.cpp and a C++20 CMakeLists.txt.

Also adds a missing <bit> include in string_functions.cpp, which GCC 11 needs for std::bit_ceil.

Still not done: #682 (inlining) and type analysis. Variables stay ValueData.

@devin-ai-integration

Copy link
Copy Markdown
Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@adazem009
adazem009 marked this pull request as draft October 17, 2026 14:08
@adazem009 adazem009 changed the title Add Scratch to C++ transpiler backend Scratch to C++ transpiler experiment Oct 17, 2026
- CppCodeBuilder emits a C++ class per script deriving from CppExecutableCode,
  registered by the FNV-1a hash of its source
- scripts and procedures are C++20 coroutines (CppCoroutine)
- run the shared codegen tests with the cpp backend: codegen_test_generator
  collects the generated code which is compiled into codegen_test
- add the codebase generator and scratchcpp-transpiler tool

Co-Authored-By: zemanadam893@gmail.com <zemanadam893@gmail.com>
@devin-ai-integration
devin-ai-integration Bot force-pushed the devin/1792234428-cpp-transpiler branch from da3bb98 to e01f83c Compare October 28, 2026 22:12
@devin-ai-integration
devin-ai-integration Bot changed the base branch from master to cpp_architecture October 28, 2026 22:12
Base automatically changed from cpp_architecture to master October 29, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant