06_typescript_emitter

TypeScript Emitter

Writing TypeScript and Node.js code

Purpose

The TypeScript Emitter is a backend component that turns your ARES logic into modern TypeScript code. It is designed for programs that need to connect to the web, handle real-time data, or run as background services. This choice is usually made for tasks where connectivity and ease of integration are more important than Raw execution speed.

Why it exists

Creating web services or handling real-time data streams in C++ can be incredibly complex. TypeScript and Node.js are built for these exact tasks. The TypeScript Emitter exists to allow you to build these services using the clear logic of ARES. It handles all the difficult "async" and networking boilerplate for you, so you can focus on your logic while still being able to deploy your code to modern web systems.

How it works

The system organizes your code so it is safe to run in a modern Node.js environment.

  1. Async wrapper. The whole program is placed inside an "async" function. This ensures that the code can wait for network tasks or data reading without stopping the entire program.
  2. Type conversion. ARES types are converted into standard TypeScript types. For example, a vector of integers is turned into a number[] array.
  3. Blocking input. Since ARES scripts are written to read data one step at a time, the system uses specific commands to read from the console in a way that wait for the user to type.
  4. Web service integration. When you define an API endpoint or a server task, the system translates those into standard framework calls like those used in the Express or WS libraries.
  5. Modern standard. The system writes code that follows modern ECMASCRIPT and TypeScript rules, ensuring it is compatible with standard development tools.

Intuition

Think of the TypeScript Emitter like a digital network engineer. You give it the logic for how your information should move (your ARES script), and the engineer sets up all the wires, switches, and connections (the Node.js and TypeScript boilerplate) to make it work. You don't need to know how the network protocol or the async timing works; you just need to explain the flow of information, and the engineer ensures it reaches its destination.

Implementation details

The logic for writing TypeScript is located in src/codegen/ts_emitter.ts.

  • Main Entry: Every program is wrapped in a async function main() { ... } block to handle all tasks safely.
  • Prints: Every "print" command is turned into a console.log() call.

Trace example

This is what happens when the system processes an API endpoint define server GET "/api":

  1. Framework setup: The system adds the necessary Express library imports.
  2. Route creation: It writes the code to listen for "GET" requests at the /api address.
  3. Response logic: It creates an async callback function that will run your logic every time someone visits that address.

Related entities

  • 04_cpp_emitter.md: The high-performance alternative for tasks that don't need web connectivity.
  • src/codegen/ts_emitter.ts: The actual code that generates the TypeScript representation.
ARES