OO COBOL: what 3.2 does not build

The two lessons before this one showed what OO COBOL is and how a program lives without it. This one writes the boundary down: every object-oriented word of the language, the build that uses it, and the line the compiler answers with. Nothing on this page is a fragment of another example; each program is as small as the word it tests.

METHOD-ID

A method is a paragraph with divisions of its own, and the word that starts it is not implemented:

IDENTIFICATION DIVISION. PROGRAM-ID. B1. DATA DIVISION. PROCEDURE DIVISION. STOP RUN. METHOD-ID. DO-IT. PROCEDURE DIVISION. DISPLAY "it". END METHOD DO-IT.

cobc -x -free -Wall -o b1 b1.cob

b1.cob:6: error: 'METHOD-ID' is a reserved word, but isn't supported
4 | PROCEDURE DIVISION.
5 | STOP RUN.
6 > METHOD-ID. DO-IT.
7 | PROCEDURE DIVISION.
8 | DISPLAY "it".
b1.cob: in paragraph 'DO-IT':
b1.cob:7: error: syntax error, unexpected PROCEDURE
5 | STOP RUN.
6 | METHOD-ID. DO-IT.
7 > PROCEDURE DIVISION.
8 | DISPLAY "it".
9 | END METHOD DO-IT.
b1.cob:9: error: syntax error, unexpected END

Four errors for one construct, and the first of them is the only one that says anything true: the word is in the reserved list and has no implementation. The others are the parser losing its place, which is what a program sees when it uses a syntax from a standard the compiler does not implement.

INVOKE

INVOKE is the call of OO COBOL: an object, a method name and a parameter list:

IDENTIFICATION DIVISION. PROGRAM-ID. B2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-AMOUNT PIC 9(7) VALUE 100. PROCEDURE DIVISION. INVOKE WS-AMOUNT "DEPOSIT" USING WS-AMOUNT. STOP RUN.

cobc -x -free -Wall -o b2 b2.cob

b2.cob:7: error: 'INVOKE' is a reserved word, but isn't supported
5 | 01 WS-AMOUNT PIC 9(7) VALUE 100.
6 | PROCEDURE DIVISION.
7 > INVOKE WS-AMOUNT "DEPOSIT" USING WS-AMOUNT.
8 | STOP RUN.
9 | <EOF>
b2.cob:7: error: syntax error, unexpected Identifier, expecting SECTION or .
b2.cob:7: note: 'INVOKE' is not a statement

The note is the clearest line of the three. The compiler knows the word, so the lexer of a program that uses OO COBOL gets a sensible message, and it is the message of a statement that will never exist in this build.

OBJECT REFERENCE, which crashes the compiler

The type of an object variable is USAGE OBJECT REFERENCE, and it is worse than unsupported: it is incomplete enough to take the compiler down:

IDENTIFICATION DIVISION. PROGRAM-ID. B3. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-OBJ USAGE OBJECT REFERENCE. PROCEDURE DIVISION. DISPLAY "declared". STOP RUN.

cobc -x -free -Wall -o b3 b3.cob

b3.cob:5: warning: OBJECTS is not implemented [-Wpending]
3 | DATA DIVISION.
4 | WORKING-STORAGE SECTION.
5 > 01 WS-OBJ USAGE OBJECT REFERENCE.
6 | PROCEDURE DIVISION.
7 | DISPLAY "declared".
cobc: unexpected category: 0
cobc: tree.c:1606: internal compiler error

cobc: aborting codegen for b3.cob, last statement at line 8 (PROGRAM-ID: B3)

cobc: Please report this!
Aborted (core dumped)

The exit status is 134 and the message asks for a bug report, which is what it should: a compiler that warns about a feature and then aborts in its code generator is an unfinished feature, not a language limit. A program that stumbles on this has found a defect of the compiler and a complete answer is the word out of the source.

Every OO word and its message

The same treatment for the rest of the vocabulary, each one compiled on its own:

WordWhat the build says
CLASS-ID 'CLASS-ID' is a reserved word, but isn't supported
METHOD-ID 'METHOD-ID' is a reserved word, but isn't supported
END METHOD 'METHOD' is a reserved word, but isn't supported
INVOKE 'INVOKE' is a reserved word, but isn't supported, then 'INVOKE' is not a statement
SELF 'SELF' is a reserved word, but isn't supported, then 'CLASS-NAME IN SELF' is not defined
USAGE OBJECT REFERENCE object reference is not implemented as a warning, then cobc: tree.c:1606: internal compiler error
INHERITS FROM never read: the file stops at CLASS-ID
IMPLEMENTS never read, for the same reason

The pattern is a grammar that knows the words and a code generator that has none of them behind it. That is worth knowing before a port: a program written in OO COBOL does not lose a feature here, it does not compile, and the work of the port is the rethinking of the classes as subprograms with an operation code.

What to use instead

The three lessons of this part stand on three pieces of the language that do work, and all three are ordinary COBOL of the 1985 standard:

  1. An object is a subprogram whose working storage holds the state, as in the account of the first lesson; the operations are an operation code in the parameter list and an EVALUATE.
  2. An interface is a copybook with the request and the answer, plus the codes the callee may return, which is the contract lesson.
  3. A dispatch is either a type field with EVALUATE or a table of program names in a CALL with a variable.

The next part of the course turns from what the language has to what the program does when something goes wrong: the declaratives, the status codes and the message a user can act on.

Exercises

  1. Compile the method program with -fsyntax-only and compare the messages with the ones on this page.
  2. Put SELF in a DISPLAY of a normal program and read the two lines.
  3. Compile the object-reference program without -Wall. Which of its lines disappear, and does the compiler still abort?
  4. Rewrite the invoke program as a subprogram call with an operation code, as the previous lesson does.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the compiler say about METHOD-ID?

    Answer

    That it is a reserved word which is not supported

    That is the first error line of the method program.

  2. Which note does INVOKE get in addition to its first error?

    Answer

    INVOKE is not a statement

    The third line of the INVOKE build says exactly that.

  3. What happens with USAGE OBJECT REFERENCE?

    Answer

    The compiler warns, then aborts with an internal compiler error

    The build printed tree.c:1606 internal compiler error and the shell saw 134.

  4. What replaces a class in this compiler?

    Answer

    A subprogram whose working storage holds the state, with an operation code

    The account subprogram of the previous lesson is that shape.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(7) VALUE 1000. 01 W-R PIC 9(7). PROCEDURE DIVISION. COMPUTE W-R = W-A * 5 / 1000 DISPLAY W-R. STOP RUN.

    Answer

    0000005

    Five per thousand of a thousand is five.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-O PIC X(6) VALUE "DOUBLE". 01 W-R PIC 9(5) VALUE 0. PROCEDURE DIVISION. PERFORM ONE-STEP DISPLAY W-R. STOP RUN. ONE-STEP. MOVE 42 TO W-R.

    Answer

    00042

    The paragraph that stands in for a method sets the result before the display.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-N PIC 9 VALUE 0. 01 W-I PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING W-I FROM 1 BY 1 UNTIL W-I IS GREATER THAN 3 ADD W-I TO W-N END-PERFORM. DISPLAY W-N. STOP RUN.

    Answer

    6

    The three passes add one, two and three.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-T PIC X(4) VALUE "SAVE". 01 W-RATE PIC 9V999 VALUE 0. PROCEDURE DIVISION. EVALUATE W-T WHEN "SAVE" MOVE 0.020 TO W-RATE WHEN OTHER MOVE 0 TO W-RATE END-EVALUATE. DISPLAY W-RATE. STOP RUN.

    Answer

    0.020

    The field has one digit before the point and three after it.

  9. Why is the internal compiler error worse than a plain unsupported word?

    Answer

    A warning followed by an abort is a defect of the compiler, not a limit of the language

    The build says Please report this, which a language limit never does.

  10. What is a fair summary of OO COBOL in this build?

    Answer

    The words are reserved, the classes do not compile, and the same design is written with subprograms

    Every construct of the three lessons failed, starting with CLASS-ID.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev