hasMoreLines() and advance() Methods: How to properly implement them?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

hasMoreLines() and advance() Methods: How to properly implement them?

Rodolfo
Hello, everybody!

I am confused about implementing hasMoreLines() and advance() methods of the Parser class.

I found this thread that considers the matter: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/How-to-handle-comments-using-API-td4032033.html.

However, the conclusion from that discussion is that the Parser class can be improved by combining hasMoreLines() with the advance() method (making them one). Therefore, the first one is useless in some sense.

Is this true? If not, how should the hasMoreLines() method check if there are more lines for the advance() method?

I am implementing the Assembler using C++. The current solution I have in mind requires the hasMoreLines() method to read the file until it finds a valid instruction, then return to its original position and return true. When it is true, the advance() method will do the same steps to find the next valid instruction. The only difference from the hasMoreLines() subroutine is that it will update the current instruction and keep the current reading position. As you may have observed, this is redundant.

If this were the case, then it is better to combine these two methods. It will be more efficient. Therefore, is this true? Or am I misunderstanding how they should behave and what they should do?

Thank you for your time and feedback.

Best,
Rodolfo
Reply | Threaded
Open this post in threaded view
|

Re: hasMoreLines() and advance() Methods: How to properly implement them?

WBahn
Administrator
This post was updated on .
It can be argued either way and really comes down to what makes the most sense to you as you partition the tasks. One of the common goals of good software design is that functions should generally do one thing and do it well. By breaking the API up into these separate tasks, I think the idea was to let the implementer focus on a smaller task for each one. But if the combined task makes more sense to someone, then they are free to do so. Remember that implementing the API is not required, but rather is the recommended approach.

It's been quite some time since I implemented the assembler, but if I recall correctly I used two string buffers, one to store the current command and one to store the next command. I read the file in one pass, one character (not one line) at a time, in order. This was to help demonstrate that it could be implemented as a finite automaton. The hasMoreLines() function populated the nextLine buffer and returned TRUE upon successfully identifying a command that needs to be processed. The advance() function then merely swapped the two buffers.
Reply | Threaded
Open this post in threaded view
|

Re: hasMoreLines() and advance() Methods: How to properly implement them?

Rodolfo
Thank you for taking the time to respond my question.
I think I will make a single method then.

Best,
Rodolfo Andrés Rivas Matta