flang the compiler proves your program cannot hang 0.7.41 GitHub Try in the browserTry it Русский

The TypeScript target

flang emit --target ts prints a program as TypeScript. Function bodies, the runner and the runtime are the same as for target js; the one addition is that every function, record and sum type carries its declared type.

модуль «Shapes»

тип «Фигура»
  вариант «Круг» с радиус: число
  вариант «Прямоугольник» с ширина: число и высота: число

объект «Точка»
  «х»: число
  «у»: число

тотальная функция «Площадь»
  принимает ф: «Фигура»
  возвращает число
  пример «Прямоугольник два на три»
    дано ф равно вариант «Прямоугольник» с ширина равным 2 и высота равным 3
    ожидается 6
  разбор ф
    случай вариант «Круг» с радиус как р
      то 3 умножить на р умножить на р
    случай вариант «Прямоугольник» с ширина как ш и высота как вы
      то ш умножить на вы

тотальная функция «Сдвиг»
  принимает х: число
  возвращает «Точка»
  запись «Точка» с «х» равным х и «у» равным 0
$ flang emit shapes.flang --target ts --out out-ts
напечатано файлов 5, байт 57146, в out-ts
…
$ ls out-ts
flang_cli.js  flang_runtime.js  package.json  shapes.ts  tsconfig.json

Declarations from the printed module:

export type Tochka = { х: number; у: number };
export function sozdatTochka(fields: Partial<Tochka> = {}): Tochka
export type Figura = $FlangVariant;
export function Krug(fields: { радиус: number }): $FlangVariant
export function Pryamougolnik(fields: { ширина: number; высота: number }): $FlangVariant
export function ploschad(f: Figura): number
export function sdvig(h: number): Tochka

What the output directory holds

filewhat it is
the .ts modulethe program: functions, record and sum types, the $PROGRAM table for the runner
flang_runtime.jsthe runtime; target js inlines it into the module, here it is a separate file
flang_cli.jsthe runner, the same as for target js: JSON in, JSON out
tsconfig.jsontsc settings: strict, allowJs, noEmit — checking needs no flags
package.jsonthe single line "type": "module"

Check and run

$ cd out-ts
$ tsc -p .
$ printf '%s\n' '{"fn":"Сдвиг","args":[{"n":"10"}]}' | node flang_cli.js ./shapes.ts
{"ok":true,"value":{"r":[["х",{"n":"10"}],["у",{"n":"0"}]]}}

tsc -p . is a strict type check; exit code 0 means the module holds. Node 23.6 and newer reads .ts directly by stripping types; Node 22.6–23.5 needs --experimental-strip-types. Only erasable syntax is printed — type annotations, type, as — so no build step is needed. To get .js files: tsc -p . --noEmit false --outDir dist.

How types map

flangTypeScript
число, нат, целое and their synonymsnumber
строкаstring
признакboolean
ничтоnull
список XArray<X>
объект «Имя»export type Imya = { поле: T; … } and a factory sozdatImya(fields: Partial<Imya> = {}); a missing field becomes null
тип «Имя» with variantsexport type Imya = $FlangVariant and one constructor per variant: (fields: { поле: T }): $FlangVariant
unknown typeany

Three limits:

How it differs from target js

The other nine targets: Print a program into C and JavaScript.