04_cpp_emitter

C++ Emitter

Writing C++ code

Purpose

The C++ Emitter is the primary tool for high-performance tasks within ARES. It is a backend component that turns your high-level logic into modern C++ code. This is the ideal choice for competitive programming, complex math, or any system task where execution speed and memory efficiency are the top priorities.

Why it exists

Some tasks are too difficult or too slow for languages like Python. If you are handling millions of data points or solving a complex puzzle in a very short time, you need the power of the machine. The C++ Emitter exists to give you that power without requiring you to write all the difficult C++ boilerplate yourself. It bridge the gap between your clear logic and the raw speed of the computer.

How it works

The system maps every part of your ARES script into a corresponding C++ structure.

  1. Header management. The system scans your code to see which algorithms you are using. It then automatically adds the right #include lines for the C++ Standard Template Library (STL), like <vector>, <algorithm>, and <queue>.
  2. Optimized startup. It adds professional boilerplate code, such as ios_base::sync_with_stdio(false), which makes reading and writing data significantly faster in C++.
  3. Algorithmic templates. When you use an elite algorithm like Dijkstra or a segment tree, the system pulls a pre-written, highly optimized C++ template from the registry and fills in your specific variable names.
  4. Type matching. ARES types are translated into their C++ counterparts. For example, a "vector of integers" becomes a std::vector<long long> to ensure enough space for large numbers.
  5. Modern syntax. The system uses modern C++ features (C++17 and C++20) to ensure the final code is clean and follows best practices.

Intuition

Think of the C++ Emitter like a master carpenter. You give it a rough sketch of a chair (your ARES logic), and the carpenter uses professional-grade tools and high-quality wood (C++ STL) to build a chair that is incredibly strong and perfectly finished. You don't need to know how to use the complex saws or joinery techniques; you just need to provide the sketch, and the master carpenter handles the rest.

Implementation details

The core logic for writing C++ is found in src/codegen/cpp_emitter.ts. It visits every part of your code tree and writes the corresponding text.

  • Variables: A simple let x = 10 is written as auto x = 10;.
  • Blocks: Code blocks are organized using curly braces {} and standard indentation.

Trace example

This is what happens when the system processes read data as vector<int>:

  1. Identification: The system sees you want to read a list of numbers.
  2. Type check: It identifies that a vector of int is needed.
  3. Code generation: It writes the C++ code to read the size of the list first, then creates a std::vector of that size.
  4. Input loop: it writes a for loop to read every individual number from the console into the vector.

Related entities

  • src/registry/approach_registry.ts: The database where the actual C++ code for algorithms is stored.
  • 05_python_emitter.md: The alternative emitter for tasks where speed is less important than being able to use Python libraries.
ARES