1

I'm prototyping a web app whose function will be to manually build a JS AST for JavaScript code gen (for which I'll use esprima and escodegen), and a C AST for C code generation.

Clang would seem to be the logical place for me to begin, for the latter. What are my options for accessing Clang from JS? So far my only sane thought is using Node to access Clang locally.

Statement of intent

The visual development environment (VDE) I'm working on needs to be able to turn "outer" structures i.e. modules, functions that are represented graphically, into source code. Function bodies could(?) then be filled in by the user, avoiding overt complexity in the VDE, which needn't care about such things (functions as in-outs are all that matters from its perspective), at least not presently. Furthermore, if I modify a structure in the VDE - e.g. changing arg types, or creating a new global in a module - then any existing body copy must not be erased during that process.

I'd like to do this the "right" way, to keep VDE code as maintainable as possible and avoid corner cases it can't handle. Ideally this editor will grow in code-processing functionality as time passes.

Engineer
  • 8,529
  • 7
  • 65
  • 105
  • NodeJS or Browser? –  Apr 10 '16 at 21:18
  • NodeJS is an option - read last sentence. Of course I'd prefer to go browser only = one less level of indirection. – Engineer Apr 10 '16 at 21:19
  • Browser: https://github.com/kripken/emscripten-fastcomp-clang –  Apr 10 '16 at 21:21
  • Node will probably be easier –  Apr 10 '16 at 21:21
  • 1
    Node: https://www.npmjs.com/package/libclang –  Apr 10 '16 at 21:24
  • @user104317 That browser version has basically no instructions at all. Pity. – Engineer Apr 10 '16 at 21:25
  • I would use libclang on nodejs, doing it in the browser would be difficult –  Apr 10 '16 at 21:27
  • How are you going to generate C code with ASTs? You might put the ASTs together (that will be hard enough), but what will turn them back into text you can compile? – Ira Baxter Apr 12 '16 at 09:57
  • @IraBaxter, Hi and thanks for bringing your experience here. My understanding was that the Clang toolchain would be able to do that? An issue I'm dwelling on at the moment however is WRT macros and reassembly thereof. I've read some of your answers on this per DMS (which I can't afford). I only need to *create* (modules,) functions and members. Function logic is left entirely up to the user to write, so I assumed I could get back to the source code of the function and copy it back in once the outer module/function structures have been updated. As for macros in module scope, not sure there. – Engineer Apr 12 '16 at 11:36
  • Your answer was more surprising than I expected. You seem to want to work with ASTs, and somehow assemble them (you didn't address how this will work), but weirder from my perspective is the notion that you are generating just the shells of functions and users will write the content. So the shells are represented as ASTs, and the function body is represented as text? Now I'm really puzzled as to how you are going to assemble them; different representations are really hard to combine. Regarding macros you might find this Q/A interesting: : http://stackoverflow.com/q/36494637/120163 – Ira Baxter Apr 12 '16 at 14:50
  • Regarding generating text from ASTs: I think Clang can do that for an AST that it is holding. So you're going to turn Clang into some kind of server that parses text to ASTs on demand, and unparses ASTs (that you give it?) to text on demand? – Ira Baxter Apr 12 '16 at 14:52
  • @IraBaxter, See edit. Not certain about the best way to do this; the proposal not to touch module/function bodies, treat them only as text, may be silly and was simply inspired by not wanting to mess with existing macros when writing the text back out into the source file after modification. Yes, it's an on-demand setup, since these changes to source need to occur every time the VDE dictates it, i.e. read file -> transform AST -> write file. This ensures that source operated on by the VDE is constantly in sync with changes made externally by user's text editor of choice. Complicated, I know. – Engineer Apr 12 '16 at 16:23
  • No issues with JS, because the AST generator & code gen are native to the VDE dev env (JS!), and _no macros_. – Engineer Apr 12 '16 at 16:25
  • It is looking increasingly like ANTLR is the way to go. There is even a JS version. – Engineer Apr 12 '16 at 18:47
  • @IraBaxter Your [other comments](http://stackoverflow.com/questions/36673895/antlr4-generate-code-from-non-file-inputs?noredirect=1#comment60941881_36673895) led me back here. Clang won't do for me as I understand it works only on C/C++/Obj-C (?) where I need JS + C; and ANTLR as a parser generator does only part of the job... a fact to which you have since enlightened me. So, what other (free) options are there? I can either forge ahead with ANTLR and make up for its shortcomings manually, or try another route. – Engineer Apr 17 '16 at 16:39
  • 1
    I'm following your discussion because you want to do general program manipulation (which is very hard to do), which is what I've been working on since the 1980s (check my bio). In general what you want is a program transformation system: http://stackoverflow.com/a/26063536/120163 I've been watching the technology for 30+ years; in terms of *practical* versions of these, there are two free ones (Stratego/XT) and TXL. There is AFAIK only one commercial one (mine); we justify its value in terms of solving these kinds of problems more effectively than the free versions; YMMV. – Ira Baxter Apr 17 '16 at 16:48

0 Answers0