Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my own C++ compiler understand templates, nested classes, etc. strong features of C++?

It is a university task in my group to write a compiler of C-like language. Of course I am going to implement a small part of our beloved C++.
The exact task is absolutely stupid, and the lecturer told us it need to be self-compilable (should be able to compile itself) - so, he meant not to use libraries such as Boost and STL.
He also does not want us to use templates because it is hard to implement.
The question is - is it real for me, as I`m going to write this project on my own, with the deadline at the end of May - the middle of June (this year), to implement not only templates, but also nested classes, namespaces, virtual functions tables at the level of syntax analysis?
PS I am not noobie in C++

like image 960
chester89 Avatar asked Nov 26 '22 20:11

chester89


2 Answers

Stick to doing a C compiler.

Believe me, it's hard enough work building a decent C compiler, especially if its expected to compile itself. Trying to support all the C++ features like nested classes and templates will drive you insane. Perhaps a group could do it, but on your own, I think a C compiler is more than enough to do.

If you are dead set on this, at least implement a C-like language first (so you have something to hand in). Then focus on showing off.

like image 158
rjh Avatar answered Feb 04 '23 10:02

rjh


"The exact task is absolutely stupid" - I don't think you're in a position to make that judgment fairly. Better to drop that view.

"I`m going to write this project on my own" - you said it's a group project. Are you saying that your group doesn't want to go along with your view that it should morph into C++, so you're taking off and working on your own? There's another bit I'd recommend changing.

It doesn't matter how knowledgable you are about C++. Your ability with grammars, parsers, lexers, ASTs, and code generation seems far more germane.

Without knowing more about you or the assignment, I'd say that you'd be doing well to have the original assignment done by the end of May. That's three months away. Stick to the assignment. It might surprise you with its difficulty.

If you finish early, and fulfill your obligation to your team, I'd say you should feel free to modify what's produced to add C++ features.

I'll bet it took Bjarne Stroustrup more than three months to add objects to C. Don't overestimate yourself or underestimate the original assignment.

like image 28
duffymo Avatar answered Feb 04 '23 10:02

duffymo