It was not possible yesterday, but it is today, and I'll get the changes into the source tree as soon as the lead approves the change.
Two small changes to the compiler source and - can be used to indicate stdin.
prompt$ echo 'program-id. test. display "hello".' | cobc -free -frelax -x -o thing -
-: 1: Warning: PROCEDURE DIVISION header missing - assumed
prompt$ ls -l
-rwxrwxr-x. 1 btiffin btiffin 13192 Jul 31 17:27 thing
prompt$ ./thing
hello
This will then work for any operating system, and not just those with /dev/stdin support (which, for now, as paxdiablo wrote, is a very valid option with GNU/Linux and the like).
And as an aside; cobc can already be used as a POSIX interpreter.
#!/usr/local/bin/cobc -xvg
identification division.
program-id. SAMPLE.
procedure division.
display "scripted" end-display
goback.
end program SAMPLE.
with
prompt$ chmod +x interp.cob
prompt$ ./interp.cob
giving
Command line: /usr/local/bin/cobc -xvg ./interp.cob
Preprocessing: ./interp.cob -> interp.i
Return status: 0
Parsing: interp.i (./interp.cob)
Return status: 0
Translating: interp.i -> interp.c (./interp.cob)
Executing: gcc -std=gnu99 -c -I/usr/local/include -pipe -Wno-unused
-fsigned-char -Wno-pointer-sign -g -o "/tmp/cob25113_0.o"
"interp.c"
Return status: 0
Executing: gcc -std=gnu99 -Wl,--export-dynamic -o "interp"
"/tmp/cob25113_0.o" -L/usr/local/lib -lcob -lm -lgmp
-lncursesw -ldb -ldl
Return status: 0
Sadly, that is just for the build. Execution requires an extra step.
prompt$ ./interp
scripted
There is no compiler option for "run the code now", so a -J (fixed form) and -j (free form) option will be added. Run job, becomes a new form of -x (create executable) but will also execute the binary at end of processing. Then we'll have access to cobc "scripts" in POSIX. Currently all that is allowed is build, and the run has to be a separate command.
And the inherent limitations that POSIX hashbang lines are limited to a single argument, so -j -free wouldn't work, nor would -j -Xref etc, but within limits, it'll allow for cobc scripts.
That's another one line change to the compiler, and 400 lines of option parsing and documentation. ;-)
Should be ready for testing in a few hours. Again, these changes will require approval from all the other compiler maintainers, but I don't see any reason for not allowing the improvements. Two commits, one for - as stdin, and -J -j for run job at end of compile.
cobc
would support piping the code snippet instdin
or even as cli option, and generate a binary file or even showing the result directly (not sure if that's a good or bad idea).node
, for example, has the-p
option:node -p 'console.log(1)'
-- this will output1\nundefined
. g++ has this feature – Jewfish