node-cobol icon indicating copy to clipboard operation
node-cobol copied to clipboard

idea to run cobol as native javascript

Open seriousme opened this issue 8 years ago • 11 comments

Hi,

I read you were looking for ways to run cobol as native javascript in Node.js.

GNUcobol (http://sourceforge.net/projects/open-cobol/) is written in C and compiles via C to native. Now if you compile the C code resulting from the cobol compilation to Javascript using emscripten you can convert Cobol to Javascript.

If you compile the cobol compiler itself using emscripten you could even do this on the fly if you'd feel the need :-)

The alternative is to manually convert the gnucobol compiler to Javascript and have it produce javascript directly. Not a complex task imho, but will take loads of time to develop :-)

Cheers, Hans

seriousme avatar Aug 19 '15 13:08 seriousme

Now if you compile the C code resulting from the cobol compilation to Javascript using emscripten you can convert Cobol to Javascript.

That would be awesome! :sparkles: But I'm not sure what that means: how is the file system access handled, for example and things like that?

The alternative is to manually convert the gnucobol compiler to Javascript and have it produce javascript directly. Not a complex task imho, but will take loads of time to develop :-)

There is already a project by @ajlopez: https://github.com/ajlopez/CobolScript –not really sure what it offers, but I think I will look into it. :smile:

Anyway, if possible, converting the C code into JS sounds awesome. :cake:

IonicaBizau avatar Aug 19 '15 15:08 IonicaBizau

Have a look at http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html to see how emscripten works. It should be possible to get gnucobol to use emscripten as backend.

CobolScript seems to be using its own lexer and parser. If I would try to build a cobol interpreter myself I'd use something like JISON ( https://www.npmjs.com/package/jison) as this enables you to take a formal definition (e.g. BNF) of cobol and generate a parser out of it.

Good luck with your project ;-)

seriousme avatar Aug 19 '15 15:08 seriousme

@seriousme Thanks! I tried to compile emscripten but it failed... Are there any binaries in the wild, for Ubuntu?

If I'm not wrong, GnuCOBOL transforms COBOL into C. That means I could make a tool to transform COBOL into JavaScript. :smile: Am I right?

So, the following code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       PROCEDURE DIVISION.

       PROGRAM-BEGIN.
           DISPLAY "Hello world".

       PROGRAM-DONE.
           STOP RUN.

...will become:

console.log("Hello World");

:smile:

IonicaBizau avatar Aug 21 '15 09:08 IonicaBizau

Correct :-)

When building a compiler it is quite common to tools like Lexx and Yacc to simplify the process.

If you check out the cobc folder of GNU Cobol you will see *.l (lex) files and *.y (yacc) files.

if look at the yacc files (e.g. parser.y) you will see that these files specify how to generate the C code. Those yacc files are processed by GNU Bison to create parser.c and parser.h when the cobol compiler is being build.

Now I'm not an expert at Lex or Yacc nor at Emscripten, but maybe you can find some tutorials to get you going. A nice intro can be found here In your case you don't need LLVM as the Bison generated compiler will produce javascript directly :-). Flex is an alternative to Lex and Bison is an alternative to Yacc. Jison is the javascript version of the combination of Flex and Bison

For emscripten you can find some guidance on installation at: http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html#sdk-download-and-install

It will be quite a learning curve and a lot of work to get this working. But then again: you might like to take on a challenge ;-)

seriousme avatar Aug 21 '15 12:08 seriousme

I reread my reply and concluded I might have been a bit vague ;-)

There are a few options to get from Cobol to Javascript: a) build a new cobol compiler in Javascript on Lex and Yacc (as described in my reply) and have it produce Javascript.

or

b) Take the C code produced by GNU cobol and hand it to something like emscripten. I looked at cobc.c line 713 which suggests that if you pass the cobol compiler a -C flag it will only generate C code out of your cobol code. This C code could then be translated to Javascript.

BTW: the gnucobol compiler also comes with a runtime library to handle stuff like ISAM tables etc. That would need to be redone in javascript as well (in both cases ;-))

Now the cleanest solution would of course be A, but the more easier route might be B ;-)

seriousme avatar Aug 21 '15 14:08 seriousme

For the memory management the translation process to javascript can be tweeked to do something like ASM.js that reimplements a heap using typed arrays, this can be a way to go.

The emscripten rout is indeed fast, but if COBOL (i don't have much knowlege of the language itself) is well defined and have few rules (eg. generates a small bnf) it's fairly simple to implement a interpreter, and then compile the interpreter itself or the code it generates using the VM module of node.

madcampos avatar Aug 26 '15 03:08 madcampos

I tried last week Emscriptem, but I am not so happy with it–for example, for a simple Hello World program written in C, it generates a 13k lines js file.

Is it possible to create binary files with the VM module?

IonicaBizau avatar Aug 26 '15 05:08 IonicaBizau

AFAIK, it generates a bitecode or full binary file that V8 can execute. I don't know if it can be shared or saved, but i think it would be platform specific. node-webkit have a similar method, but there is gotchas.

I think that in a context of a server, running the module once, and it compiling the cobol compiler in runtime and then reusing it for the lifetime of the application souldn't be a problem. In the case of compiling the cobol code it should be easier to do as it is now, send the code to a external compiler.

madcampos avatar Aug 26 '15 08:08 madcampos

I think that in a context of a server, running the module once, and it compiling the cobol compiler in runtime and then reusing it for the lifetime of the application souldn't be a problem.

Exactly! :sparkles: A nice caching method would be to create some md5 hashes and map them to executable paths.

In the case of compiling the cobol code it should be easier to do as it is now, send the code to a external compiler.

I agree. :+1: In case if cobc is not available it will fallback to cobolscript.

IonicaBizau avatar Aug 26 '15 08:08 IonicaBizau

Thanks for mentioning cobolscript project!

Interesting idea, node-cobol, using comments as source, as in edge.js ... nice!

Yes, cobolscript can compile a free subset of COBOL to JavaScript. I just added

https://github.com/ajlopez/CobolScript/tree/master/samples/hello

a compile.js to see the output.

It can manage async calls, see https://github.com/ajlopez/CobolScript/tree/master/samples/helloasync

But it doesn't support yet a lot of features, notably, pictures, and access to libraries written in CobolScript. But I could added it if necessary. It was a TDD exercise: all the parser was written using TDD (Test-Driven Development), and I think it was a good exercise.

It support web pages, and web pages with COBOL templates: https://github.com/ajlopez/CobolScript/blob/master/samples/templateweb/factorial.cobp

It leverage all the NodeJS ecosystem, using requires, as in https://github.com/ajlopez/CobolScript/blob/master/samples/webserver/webserver.cob

With a bit of JavaScript help, there is a web server using MySQL https://github.com/ajlopez/CobolScript/tree/master/samples/website

ajlopez avatar Aug 26 '15 10:08 ajlopez

@ajlopez You really did a great job there! The module is :sparkles:!

Interesting idea, node-cobol, using comments as source, as in edge.js ... nice!

Yeah, exactly! I strongly inspired from edge.js. :smile:

IonicaBizau avatar Aug 26 '15 18:08 IonicaBizau