Tidying up the code
Purpose
The Code Formatter is the final step in building your program. It is a system that takes the code written by ARES and applies standard style rules to it. This makes the code easy for humans to read and ensures that it matches the way professional developers write their own code.
Why it exists
When a computer writes code, it often cares more about correctness than how the code looks. The code might be all on one line, or it might have inconsistent spacing. This makes it very difficult for humans to understand or debug. The formatter exists to fix this problem. It ensures that every bracket is in the right place and every line is properly indented, turning a messy machine-written script into a professional program.
How it works
The system is a simple post-processor that runs after your code is already written.
- Detecting the language. The system identifies which language was used (C++, Python, or TypeScript).
- Calling professional tools. It then calls a well-known industry tool to fix the style. For example, it uses
clang-formatfor C++ andblackfor Python. - Applying standards. It uses standard rules, like the Google style for C++ or the PEP 8 rules for Python.
- Automatic fallback. If you do not have these tools installed on your computer, the system simply skips this step. Your program will still work perfectly; it just won't look as tidy.
Intuition
Think of the code formatter like a professional dry cleaner for your clothes. You might have finished building your outfit (writing your code), but it might be wrinkled or have some dust on it. You send it to the dry cleaner (the formatter), and they return it to you perfectly pressed and cleaned. The clothes are the same, but now you can wear them to a professional meeting with confidence.
Implementation details
The formatter is managed in src/codegen/formatter.ts. It uses a simple system command to run the external tools on the files that are already saved to your disk.
- Commands: It runs commands like
clang-format -idirectly from your console. - Silence: The system handles these tasks silently so they don't interrupt your work.
Trace example
This is what happens when the formatter sees a messy line of C++:
Input: int main(){int x;cin>>x;cout<<x<<endl;return 0;}
- Analysis: The system matches the line against the modern C++ rules.
- Cleanup: It adds spaces around symbols like
>>and<<. - Indentation: It breaks the code onto new lines and adds spaces inside the
main()function.
Output:
cppint main() { int x; cin >> x; cout << x << endl; return 0; }
Related entities
04_cpp_emitter.md: The tool that writes the initial C++ code.05_python_emitter.md: The tool that writes the initial Python code.