esseks / monicelli

An esoteric programming language, come se fosse antani.
GNU General Public License v3.0
1.32k stars 52 forks source link

Added options for input and output files. #16

Closed massix closed 5 years ago

massix commented 9 years ago

Hello, in order to have mcc feel like a real compiler, some program options have to be parsed. I've added a facility to do it, in a simple and portable way (among UNIX systems, not sure about Windows though) and started testing using the "-i" and "-o" flags. Everything is working fine so far.

Both input and output provided

# massi at napi.home in ~/developer/monicelli on git:feature/options x [18:38:57]
$ ./mcc -i examples/float.mc -o float.cpp
META: Test all possible numerical forms (integer and float)

# massi at napi.home in ~/developer/monicelli on git:feature/options x [18:41:40]
$ cat float.cpp 
#include <iostream>
#include <cassert>
#include <cstdlib>

int main() {
    std::cout << (2) << std::endl;
    std::cout << (-2) << std::endl;
    std::cout << (2) << std::endl;
    std::cout << (0.232) << std::endl;
    std::cout << (-2) << std::endl;
    std::cout << (-0.2233) << std::endl;
    std::cout << (2) << std::endl;
    std::cout << (2.233e-23) << std::endl;
}

Only input (output on stdout by default)

# massi at napi.home in ~/developer/monicelli on git:feature/options x [18:41:43]
$ ./mcc -i examples/float.mc             
META: Test all possible numerical forms (integer and float)
#include <iostream>
#include <cassert>
#include <cstdlib>

int main() {
    std::cout << (2) << std::endl;
    std::cout << (-2) << std::endl;
    std::cout << (2) << std::endl;
    std::cout << (0.232) << std::endl;
    std::cout << (-2) << std::endl;
    std::cout << (-0.2233) << std::endl;
    std::cout << (2) << std::endl;
    std::cout << (2.233e-23) << std::endl;
}

Only output (input on stdin by default)

# massi at napi.home in ~/developer/monicelli on git:feature/options x [18:43:13]
$ cat examples/float.mc| ./mcc -o float.cpp
META: Test all possible numerical forms (integer and float)

# massi at napi.home in ~/developer/monicelli on git:feature/options x [18:43:15]
$ cat float.cpp 
#include <iostream>
#include <cassert>
#include <cstdlib>

int main() {
    std::cout << (2) << std::endl;
    std::cout << (-2) << std::endl;
    std::cout << (2) << std::endl;
    std::cout << (0.232) << std::endl;
    std::cout << (-2) << std::endl;
    std::cout << (-0.2233) << std::endl;
    std::cout << (2) << std::endl;
    std::cout << (2.233e-23) << std::endl;
}

Let me know if something feels weird (the parsing might be a little bit too basic?). Adding a --help option should be easy enough, just by tweaking the Option class in order to accept a boolean value and pretty printing the descriptions of all the options.

esseks commented 9 years ago

This answers #15, good! I was thinking about using boost::program_options for parsing command line, though.

massix commented 9 years ago

Despite the fact that I love boost libraries I kind of dislike adding it as a dependency for a project with a tiny C++ core like mcc.

The boost developers have a funny versioning system and you might often fall in non backward compatible changes between two sibling versions thus forcing you to freeze the current version in order to avoid serious migraines to other contributors and this might lead in a more sophisticated and complicated build system.

There's also a site that keeps track of all the non-backward compatible changes they're introducing, and that thing is scary. :8ball:

One could also argue that boost.program_options has just a dependency on boost.system and they will hardly screw that up since it's the very core of all the derived libraries, but who knows?

After all you are the maintainer and the final choice is yours, it depends on whether you want to keep mcc's core tiny and easily portable or you want to extend it enough to justify boost as a strong dependency.

In any case, thanks a lot for this project, it's hilarious and it's the best way I've found so far to teach some Italian to my French colleagues :-D

esseks commented 9 years ago

That's a good point, I never considered how Boost risks breaking compatibility twice a year. In all seriousness, I didn't think boost::program_options would be a large dependency, but I understand and totally second your concerns about portability. Another possibility I am evaluating is getopt/getopt++.

That said, your code looks good to me. Only, I'd rather avoid rewriting yet another command line parsing library.

massix commented 9 years ago

I see your point. I've never used getopt++ personally but we could give it a try. Another thing I'm thinking, somewhat related to #17 is that if we want MCC to feel and behave like a real compiler, the option -i is probably unnecessary: when you're using gcc the input files are all the `free' arguments.

esseks commented 5 years ago

v2.0 supports the usual -o option, among others, without using Boost or other external dependencies. This should fix the issue. Feel free to reopen otherwise.