r/transprogrammer • • Sep 05 '22
I'm Making a Thing. Roast my Code?
I saw an Atomic Shrimp video about a singe board computer that just boots into a BASIC interpreter, and wanted to write an interpreter of my own. But I've got no clue what I'm really doing, so we get this
58
Upvotes
8
u/anarchy_witch Sep 05 '22
I've written a lot of comments, and I'm worried that it might be overwhelming.
I like the project and the idea, it looks cool, I like it that you implemented your own stack - keep going <3
5
u/retrosupersayan JSON.parse("{}").gender Sep 06 '22
A stylistic detail:
argandcmdbe declared inside the maindo...whileloop? they're not used or needed outside of it. (Probably irrelevant if you split things up as anarchy_witch recommends.)A bug/limitation:
jumpandifeqonly support forward jumps, meaning that loops are impossible with this implementation of toylang. A simple backwards-jump implementation could just userewind, or you could get more clever and try to keep track of what previous file positions correspond to previousins_ptrvalues withftelland usefseekto go back.5
u/MondayToFriday Sep 06 '22
Your inconsistent indentation and brace style make my head hurt.
4
u/TDplay Sep 06 '22
Your stack appears to be implemented as a linked list. Linked lists are almost always the wrong choice. They lure you in with O(1) insert and remove, but it's not worth it - the poor cache locality will almost always result in worse performance. Use an array instead.
You have a function declared as
This is a pre-standard function declaration, and declares a function that can accept any number of parameters. You probably want this instead:
The main function is far too long and complex. Break it down into smaller functions.
You use the
%spattern infscanf. This is inherently dangerous. Attempting to interpret the following input:will result in undefined behaviour, due to a buffer overflow reading the second line. To fix this, change
%sto%5s.After implementing this fix, your program will be unable to tell the difference between
printandprintt. To fix this, changechar cmd[6]tochar cmd[7]and%5sto%6s. The program will now print an error, as intended (although the name of the invalid command in the error may get truncated).30
u/anarchy_witch Sep 05 '22
SOME COOL THINGS TO LEARN IN THE FUTURE:
You might want to check out how to implement a stack using an array (you have an array, and double or half its size when needed) - it's a cool data structure, and believe me when I say that pushing and popping works in a constant time complexity.
(implementing the stack that way would be faster, as it requires fewer mallocs, which are quite slow)
9
u/anarchy_witch Sep 05 '22 edited Sep 05 '22
instead of:
do:
with each of these being a separate function
Learning how to break those functions, and write modular code comes with time, and requires practice
8
u/anarchy_witch Sep 05 '22
those kinds of system errors and messages are always displayed on the standard diagnostic output, instead of the standard output.
So just doing:
would be fine (or something like this. You might have to import a `"stderr.h" or something)
It's a bit hard to quickly explain how those two outputs differ, but generally speaking logs, info, warnings and errors should be printed to the diagnostic output, instead of the regular one.
5
u/anarchy_witch Sep 05 '22
Nitpicking:
while (ins_ptr < arg-1)-> by convention, the arithmetic operators should be separated by space:5
u/anarchy_witch Sep 05 '22
I would check if
stack != NULL- you might get an error otherwiseThe rest of freeing the stack is done quite well <3
5
u/anarchy_witch Sep 06 '22
and one last note: I would put parsing and executing the commands in different files: those are totally different things, and the code will be easier to maintain if we separate those
(I had to do a similar project for my programming course, and parsing was the hardest part lol)