1

Is there a way to write a front-end for my own, invented language?

Let's say I know regular expressions, yacc, lex and stuff, I want only to know how to bind it to existing GCC. I want something like:

%% This is a comment
%% This is source of XD programming language
troll x = 1; %% 'troll' is an automatically guessed type
x+laugh[]; %% Will print some laughs and format the hard drive

Here, the + represents C/C++ structure/class member operator, the square brackets are function call arguments. Semicolon means the same as in C.

How could I bind it so that if i entered

gcc -o app app.troll

it would compile my code, possibly with a similar C intermediate (anyways, the comments would probably be absent):

// The type has been guessed... It's int!!!
int x = 1;
// We can't call a method on an `int`, so we make it an external one
__trollvm_laugh_int (x);

And finally, it would be converted to GENERIC/GIMPLE, optimized and dumped to assembly.

I have seen two approaches:

  • G++ (GNU C++ Compiler, part of GCC). It compiles directly to GENERIC/GIMPLE
  • GPC (GNU Pascal Compiler) - translates code to C, then eventually calls GCC

If anyone knows anything, please let me know. Any help appreciated.

Top Sekret
  • 748
  • 5
  • 21
  • I suppose you googled? "gcc frontend" gives plenty of tutorials to get you started. [Gcc-tiny](https://github.com/rofirrim/gcc-tiny/tree/master/gcc/tiny) has example code. – SáT Dec 21 '16 at 01:06
  • An alternative to GCC: http://stackoverflow.com/questions/3455456/what-kinds-of-patterns-could-i-enforce-on-the-code-to-make-it-easier-to-translat/3460977#3460977 – Ira Baxter Dec 21 '16 at 04:31
  • 2
    GCC is complicated, for a new project it is almost always better to target LLVM (unless you want a backend that is only available for gcc). – SK-logic Dec 23 '16 at 07:59

0 Answers0