tsoding / porth

It's like Forth but in Python
628 stars 50 forks source link

`compile_tokens_to_program` there's no need to reverse the tokens #58

Closed drocha87 closed 2 years ago

drocha87 commented 2 years ago

I just realized that there's no need to reverse the tokens since you can just tokens.pop(0) and tokens = macros[token.value].tokens + tokens when you need to append the list of tokens to tokens (in case of expanding a macro for example).

I don't know if this can bring any performance improvements but I think it simplifies the code a little bit.

If you want I can change the code and send a PR.

ap29600 commented 2 years ago

reversing was actually a choice made keeping in mind the algorithmic complexity of the program, since pop(0) is O(n) but pop() is O(1).

drocha87 commented 2 years ago

Ohh I didn't know about that. Holy molly good to know that pop(0) is O(n).