by _haoa · 5y ago · 0 comments
In C++, you can make a very simple REPL that is superior to the one in the OP in almost every way.
#include <iostream>
#include <string>
int main() {
std::string input;
while (true) {
std::cout << "> ";
std::getline(std::cin, input);
if(input == "exit") {
return EXIT_SUCCESS;
} else {
std::cout << "Unrecognized command: " << input << '\n';
}
}
}
Code quoted from “Setting Up A Better REPL” (https://mikeloomisgg.github.io/2019-04-10-set-up-repl/). It offers good insight into why C is bad in light of the alternatives like Rust and C++.And in Scheme, you can make a very simple REPL that is vastly superior to the one of yours in every way. But I suspect that neither fulfills the goal of learning how things are done in C, which seemed to be the goal of the author of the article.
Using bit-shift operators for input/output is a heinous crime against human dignity, basic decency and social order. Every use of it should be punished with 10 lashings, and the promoters of this atrocity should be tarred and feathered.
I'm agnostic on C++'s std stream stuff but I am pretty anti-"yes bitwise logical operators are so important that they get priority over all these symobls".
- wrong arguments types (mostly solved by libraries such as fmt tho): printf("%d", 0.5);
- too much / not enough arguments passed: printf("%d", 0, 1);
- passing unsanitized strings as template argument: printf(some_string_i_got_from_the_network_lol, arg_1, arg_2);
the << method makes these three errors impossible.
Also << does not allow you to have something like printf's %n, which writes to one of its arguments.
fmt is able to give compile-time validation of string literals, but at a hefty compile-time cost. In my opinion the best solution would be string interpolation, but we're definitely not there yet...
C has free, which deallocates a block of memory pointed to by a pointer. If it's a struct, the struct is deallocated. If the struct contains pointers, that memory is still there. If you've lose all the pointers to that chunk of memory, you now have a memory leak.
It is not uncommon to make your own allocators and deallocaters for complex structs.