Skip to content

Cobol translation demo #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions COBOL/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
payroll
*.txt
76 changes: 76 additions & 0 deletions COBOL/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Code Modernization

Astonishingly, 70% of all existing business logic is in COBOL, and 800 billion lines of COBOL are still in use in industry today [1].
Many (perhaps all) companies that still use COBOL wish they could move off it, since it is massively antiquated and the only living engineers who still know it are in retirement homes. Obviously, LLMs present a compelling solution to this problem via automated translation. But how can you be sure the translation is correct? This demo is meant to illustrate how Benchify can fit into the analysis of a translation from COBOL to Python.

You can see the complete report [here](https://github.com/Benchify/benchify-examples/pull/134).

Example usage of the COBOL wrapper:
```
ipython3
Python 3.9.6 (default, Nov 11 2024, 03:15:38)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.18.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from script import *
...: install_cobc_and_compile_script()
...:
...: employee_records = [
...: EmployeeRecord(emp_id="12345", hours_worked=40, hourly_rate=20.0, tax_deduction=50.0),
...: EmployeeRecord(emp_id="67890", hours_worked=35, hourly_rate=15.0, tax_deduction=30.0),
...: EmployeeRecord(emp_id="54321", hours_worked=45, hourly_rate=25.0, tax_deduction=70.0)
...: ]
...:
...: payroll_results = process_payroll_cobol(employee_records)
...: payroll_results
GNU COBOL (cobc) is already installed.
Compiling payroll.cbl...
Compilation successful.
GNU COBOL (cobc) is already installed.
Compiling payroll.cbl...
Compilation successful.
=== Debug: Writing the following lines to input file ===
1234504000020000005000
6789003500015000003000
5432104500025000007000
=======================================================
Running the COBOL payroll program with file: temp_payroll_input_ceacb40a807b19a0.txt
Processing COBOL output...
=== Raw COBOL Output ===
EMP-ID,GROSS-PAY,NET-PAY
12345,0000800.00,0000750.00
67890,0000525.00,0000495.00
54321,0001125.00,0001055.00
=========================
Employee ID: 12345
Gross Pay: $800.00
Net Pay: $750.00
Employee ID: 67890
Gross Pay: $525.00
Net Pay: $495.00
Employee ID: 54321
Gross Pay: $1125.00
Net Pay: $1055.00
Out[1]: [(800.0, 750.0), (525.0, 495.0), (1125.0, 1055.0)]

In [2]: payroll_results_2 = process_payroll(employee_records)
Employee ID: 12345
Gross Pay: $800.00
Net Pay: $750.00
Employee ID: 67890
Gross Pay: $525.00
Net Pay: $495.00
Employee ID: 54321
Gross Pay: $1125.00
Net Pay: $1055.00

In [3]: payroll_results_2
Out[3]: [(800.0, 750.0), (525.0, 495.0), (1125.0, 1055.0)]

In [4]: payroll_results_2 == payroll_results
Out[4]: True

In [5]: exit()
```

[1]: https://cobolcowboys.com/cobol-today/#:~:text=70%25%20of%20all%20critical%20business,use%20today%20by%20various%20industries.
59 changes: 59 additions & 0 deletions COBOL/payroll.cbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
$set sourceformat"free"
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPLOYEE-FILE ASSIGN TO DYNAMIC-FILE-NAME
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS FILE-STATUS.

DATA DIVISION.
FILE SECTION.
FD EMPLOYEE-FILE
LABEL RECORDS ARE STANDARD
BLOCK CONTAINS 0 RECORDS
DATA RECORD IS EMPLOYEE-RECORD.
01 EMPLOYEE-RECORD.
05 EMP-ID PIC X(5).
05 HOURS-WORKED PIC 9(3).
05 HOURLY-RATE PIC 9(5)V99.
05 TAX-DEDUCTION PIC 9(5)V99.

WORKING-STORAGE SECTION.
01 GROSS-PAY PIC 9(7)V99.
01 NET-PAY PIC 9(7)V99.
01 END-OF-FILE PIC X(3) VALUE SPACES.
01 DYNAMIC-FILE-NAME PIC X(100).
01 FILE-STATUS PIC XX.

PROCEDURE DIVISION.

*> Retrieve the first argument using COMMAND-LINE
ACCEPT DYNAMIC-FILE-NAME FROM COMMAND-LINE

*> Now open the file using DYNAMIC-FILE-NAME
OPEN INPUT EMPLOYEE-FILE
IF FILE-STATUS NOT = "00"
DISPLAY "Error opening file: " DYNAMIC-FILE-NAME
STOP RUN
END-IF

*> Output CSV Header
DISPLAY "EMP-ID,GROSS-PAY,NET-PAY"

*> Read until EOF
PERFORM UNTIL END-OF-FILE = "EOF"
READ EMPLOYEE-FILE INTO EMPLOYEE-RECORD
AT END
MOVE "EOF" TO END-OF-FILE
NOT AT END
COMPUTE GROSS-PAY = HOURS-WORKED * HOURLY-RATE
COMPUTE NET-PAY = GROSS-PAY - TAX-DEDUCTION
DISPLAY EMP-ID "," GROSS-PAY "," NET-PAY
END-READ
END-PERFORM

CLOSE EMPLOYEE-FILE
STOP RUN.
Loading