|
| 1 | +# COBOL (Wasm) |
| 2 | + |
| 3 | +COBOL is a compiled, imperative programming language designed for business data processing. |
| 4 | + |
| 5 | +COBOL in LiveCodes is compiled to WebAssembly by the real [GnuCOBOL](https://gnucobol.sourceforge.io/) |
| 6 | +compiler and run in the browser, with no server. The toolchain translates COBOL to C, compiles that |
| 7 | +with Clang, links `libcob`, and runs the resulting WebAssembly module. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +Demo: |
| 12 | + |
| 13 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 14 | + |
| 15 | +export const cobolConfig = { |
| 16 | + activeEditor: 'script', |
| 17 | + script: { |
| 18 | + language: 'cobol-wasm', |
| 19 | + content: `IDENTIFICATION DIVISION. |
| 20 | +PROGRAM-ID. HELLO-WORLD. |
| 21 | +PROCEDURE DIVISION. |
| 22 | + DISPLAY "Hello, World!". |
| 23 | + STOP RUN.`, |
| 24 | + }, |
| 25 | + mode: 'simple', |
| 26 | + editor: 'auto', |
| 27 | + tools: { |
| 28 | + status: 'full', |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +<LiveCodes config={cobolConfig}></LiveCodes> |
| 33 | + |
| 34 | +### Communication with JavaScript |
| 35 | + |
| 36 | +The program runs in the context of the result page. A few helpers are available in the browser global |
| 37 | +`livecodes.cobol` object: |
| 38 | + |
| 39 | +- `livecodes.cobol.input`: the standard input passed to the program. |
| 40 | +- `livecodes.cobol.loaded`: a promise that resolves when the compiler has downloaded and the program |
| 41 | + has run, and rejects if the toolchain fails to load. Other helpers should be used after it resolves. |
| 42 | +- `livecodes.cobol.output`: the program's output (stdout and stderr combined). |
| 43 | +- `livecodes.cobol.error`: the compiler diagnostics, if the code failed to compile. |
| 44 | +- `livecodes.cobol.exitCode`: the exit code of the program. |
| 45 | +- `livecodes.cobol.run`: compiles and runs the code again with new input, returning a promise that |
| 46 | + resolves with `output`, `error` and `exitCode`. |
| 47 | + |
| 48 | +Example: |
| 49 | + |
| 50 | +<LiveCodes template="cobol-wasm" params={{ activeEditor: 'markup' }} height="80vh"></LiveCodes> |
| 51 | + |
| 52 | +```cobol |
| 53 | +IDENTIFICATION DIVISION. |
| 54 | +PROGRAM-ID. HELLO-WORLD. |
| 55 | +PROCEDURE DIVISION. |
| 56 | + DISPLAY "Hello, World!". |
| 57 | + STOP RUN. |
| 58 | +``` |
| 59 | + |
| 60 | +`ACCEPT` reads from the **stdin** pane, in the same way as the other console languages: |
| 61 | + |
| 62 | +```cobol |
| 63 | +IDENTIFICATION DIVISION. |
| 64 | +PROGRAM-ID. GREET. |
| 65 | +DATA DIVISION. |
| 66 | +WORKING-STORAGE SECTION. |
| 67 | +01 WS-NAME PIC X(20). |
| 68 | +PROCEDURE DIVISION. |
| 69 | + ACCEPT WS-NAME |
| 70 | + DISPLAY "Hello, " WS-NAME |
| 71 | + STOP RUN. |
| 72 | +``` |
| 73 | + |
| 74 | +## Language Info |
| 75 | + |
| 76 | +### Name |
| 77 | + |
| 78 | +`cobol-wasm` |
| 79 | + |
| 80 | +### Aliases / Extensions |
| 81 | + |
| 82 | +`cobol`, `cob`, `cbl`, `cpy`, `gnucobol`, `wasm.cobol` |
| 83 | + |
| 84 | +### Editor |
| 85 | + |
| 86 | +`script` |
| 87 | + |
| 88 | +## Compiler |
| 89 | + |
| 90 | +[GnuCOBOL](https://gnucobol.sourceforge.io/) 3.2 compiled to WebAssembly, with Clang and LLD for the |
| 91 | +generated C. |
| 92 | + |
| 93 | +### Custom Settings |
| 94 | + |
| 95 | +The GnuCOBOL compiler can be configured through the `customSettings` object, under the `cobol-wasm` |
| 96 | +key. The following options are supported: |
| 97 | + |
| 98 | +- `sourceFormat`: the COBOL source format, `free` (the default) or `fixed`. |
| 99 | +- `compileArgs`: extra GnuCOBOL (`cobc`) flags. |
| 100 | +- `cCompileArgs`: extra Clang flags for the generated C. |
| 101 | +- `args`: the program's command-line arguments. |
| 102 | +- `fileName`: the name the source is compiled under (`main.cob` by default). |
| 103 | + |
| 104 | +Example: |
| 105 | + |
| 106 | +```js |
| 107 | +const config = { |
| 108 | + activeEditor: 'script', |
| 109 | + script: { |
| 110 | + language: 'cobol-wasm', |
| 111 | + content: '...', |
| 112 | + }, |
| 113 | + customSettings: { |
| 114 | + 'cobol-wasm': { |
| 115 | + sourceFormat: 'fixed', |
| 116 | + }, |
| 117 | + }, |
| 118 | +}; |
| 119 | +``` |
| 120 | + |
| 121 | +## Code Formatting |
| 122 | + |
| 123 | +Not currently supported. |
| 124 | + |
| 125 | +## Live Reload |
| 126 | + |
| 127 | +By default, new code changes are sent to the result page for re-evaluation without a full page reload. This behavior can be disabled by adding the code comment `*> __livecodes_reload__` to the COBOL code, which forces a full page reload. |
| 128 | + |
| 129 | +This comment can be added in the [`hiddenContent` property of the editor](../configuration/configuration-object.mdx#markup) for embedded playgrounds. |
| 130 | + |
| 131 | +## Example Usage |
| 132 | + |
| 133 | +```cobol |
| 134 | +IDENTIFICATION DIVISION. |
| 135 | +PROGRAM-ID. INVOICE. |
| 136 | +DATA DIVISION. |
| 137 | +WORKING-STORAGE SECTION. |
| 138 | +01 WS-RATE PIC 9(3)V99 VALUE 25.50. |
| 139 | +01 WS-HOURS PIC 9(3) VALUE 40. |
| 140 | +01 WS-PAY PIC 9(5)V99. |
| 141 | +PROCEDURE DIVISION. |
| 142 | + COMPUTE WS-PAY = WS-RATE * WS-HOURS |
| 143 | + DISPLAY "Rate: " WS-RATE |
| 144 | + DISPLAY "Pay: " WS-PAY |
| 145 | + STOP RUN. |
| 146 | +``` |
| 147 | + |
| 148 | +## Starter Template |
| 149 | + |
| 150 | +https://livecodes.io/?template=cobol-wasm |
| 151 | + |
| 152 | +## Links |
| 153 | + |
| 154 | +- [GnuCOBOL](https://gnucobol.sourceforge.io/) |
| 155 | +- [Learn X in Y minutes, where X=COBOL](https://learnxinyminutes.com/docs/cobol/) |
| 156 | +- [@live-codes/cobol-wasm](https://www.npmjs.com/package/@live-codes/cobol-wasm) |
0 commit comments