serez-cobol
A COBOL ⇆ Serez (.sz) source-to-source translator and reverse compiler, written entirely in pure Serez Code. It translates COBOL programs into clean Serez-Code scripts, and can reconstruct COBOL code back from compiled Serez scripts.
Install
sz install serez-cobolserez-cobol runs entirely in user-space as a command-line utility. It requires the File permission to read and write source files, and Env for system interactions.
Exact Fixed-Point Arithmetic
The core reason for serez-cobol's accuracy is Serez-Code's native dec type. COBOL arithmetic expects strict decimal fixed-point rounding (e.g., PIC 9V99 COMPUTE ... ROUNDED). Standard binary floats (f64) introduce rounding drift, which is unacceptable for financial calculations. Serez's exact decimal type mirrors this behavior faithfully:
For example, the COBOL statement:
COMPUTE WS-TAX = WS-SUBTOTAL * 0.21 ROUNDED.Is translated directly into Serez Code as:
WS_TAX = ((WS_SUBTOTAL * 0.21m) + 0m).setScale(2, "half-up");This ensures that calculation results perfectly match standard mainframe COBOL runtimes.
Usage
Once installed, the package exposes a single convert command (via sz run) that routes the direction automatically from the file extension — no sub-commands to remember:
sz run convert program.cob // Translate COBOL (.cob/.cbl) → Serez (.sz)
sz run convert program.sz // Reverse translate Serez (.sz) → COBOL (.cob)
sz run convert notes.txt // Invalid extension raises an errorsz run convertforwards the file to the package's entry point (index.sz), which locates its engines next to itself. Running that entry directly also works — handy when developing inside the repo:
sz index.sz program.cob // same routing, run from the repoYou can also invoke the individual translation engines directly:
// 1. Translate COBOL to Serez
sz cobol.sz examples/invoice.cob // Emits examples/invoice.sz
// 2. Run the translated program
sz examples/invoice.sz
// 3. Decompile Serez back to COBOL
sz serez.sz program.sz // Emits program.cobRound-Trip & Source Maps
Since Serez Code's grammar does not natively represent COBOL structures (like level numbers, exact picture clauses, divisions, and paragraphs), the translator embeds these declarations as comment annotations (//@) in the generated Serez code.
These annotations function as a lightweight source map. They are ignored when the Serez program runs, but the reverse translator (serez.sz) reads them to reconstruct the original IDENTIFICATION, ENVIRONMENT, and DATA divisions, translating PROCEDURE statements back to clean COBOL.
Supported Features (v2.0)
| Category | Details |
|---|---|
| Divisions & Format | Supports IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE. Autodetects fixed-format (cols 1-72) and free-format layout. Resolves COPY copybooks. |
| Data Types | Translates PIC 9(n) to int, PIC 9(n)V9(m) / S9 to dec, and PIC X(n) / A(n) to string. Supports VALUE clauses, level 77 variables, and level 88 condition names. |
| Group Items & Tables | Supports group items (nested structural dicts) and OCCURS tables (arrays) accessed via subscripts like T(i). Supports PIC editing (Z, commas, decimals, signs, asterisks). |
| Control Flow | Translates IF/ELSE/END-IF, EVALUATE (including multi-subject ALSO tables), and PERFORM forms (UNTIL, TIMES, VARYING). Supports GO TO using a program-counter loop driving paragraph functions. |
| Arithmetic Stmts | Supports COMPUTE [ROUNDED], ADD, SUBTRACT, MULTIPLY, DIVIDE (with TO, FROM, BY, INTO, GIVING, and REMAINDER) supporting multiple receivers. |
| Subprograms | Compiles nested units and END PROGRAM blocks. Translates CALL ... USING by reference, returning modified variables via tuples. |
| Strings & Files | Reference modification X(p:len), STRING, UNSTRING, and INSPECT (TALLYING / REPLACING). File I/O (LINE SEQUENTIAL) with SELECT, FD, OPEN, READ, WRITE, and CLOSE. |
Example Output
Here is a brief demonstration of how a simple paragraph loop translates.
Original COBOL source:
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM LOOP-PARA VARYING I FROM 1 BY 1 UNTIL I > 3.
STOP RUN.
LOOP-PARA.
DISPLAY "Iteration: " I.Generated Serez Code:
// Globals & Working-Storage
let I = 0;
// Paragraphs compiled as functions
fn void MAIN_PARA() {
I = 1;
while (!(I > 3)) {
LOOP_PARA();
I = I + 1;
}
System.exit(0);
}
fn void LOOP_PARA() {
out "Iteration: " + I.toString();
}
// Drive sequence starting from the first paragraph
MAIN_PARA();Not Yet Supported (Roadmap)
- Inter-file calls (CALL to subprograms defined in separate source files).
- REDEFINES clause byte-overlay logic.
- COMP / COMP-3 packed binary numbers and EBCDIC character encodings.
- SORT / MERGE verbs, Report Writer, and Screen Section layouts.