How to get a function name?

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

How to get a function name?

peterxu422
I'm trying to implement the label, if, and if-goto parts of Project8. It seems that when we implement 'label', we must use the format functionName$label. I am unsure as to how we would acquire the functionName in our implementation. How can we tell which function we are in when we are parsing through the VM code? Any insights on how to go about this?
Reply | Threaded
Open this post in threaded view
|

Re: How to get a function name?

cadet1620
Administrator
peterxu422 wrote
I'm trying to implement the label, if, and if-goto parts of Project8. It seems that when we implement 'label', we must use the format functionName$label. I am unsure as to how we would acquire the functionName in our implementation. How can we tell which function we are in when we are parsing through the VM code? Any insights on how to go about this?
Save the function name when you encounter a function VM command. Although it should not be needed before the first function, I initialize my functionName variable to the filename so that it's obvious that something's FUBAR if I see a generated label like MyClass.vm$label.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: How to get a function name?

ybakos
In reply to this post by peterxu422
Imagine yourself as the parser. You step through each line of Jack code, one line at a time.

Can an if/else statement exist outside of a function? (No.)

Would you be able to write a parser that keeps track of the name of the last function that you entered?

Reply | Threaded
Open this post in threaded view
|

Re: How to get a function name?

peterxu422
Two more questions:

1) When functions start calling other functions, is it recommended to use a Stack in the implementation to keep track of all the function names called previously that have not yet terminated?

2) When you create functionName$label, should functionName include the name of the vm class that precedes it? (E.g. Main.mult$label or just mult$label)
Reply | Threaded
Open this post in threaded view
|

Re: How to get a function name?

ybakos
1) I think you may be confusing what will happen at runtime vs. during parsing/codegen. Just because you parse a function call, does not mean that your parser should jump to that function and start parsing there.

2) Hmmm... think about the nature of labels in assembly. Each one should be unique, right? So that when you jump to (X) there is one and only one (X). Do you think the ClassName.functionName$someLabel format will be good enough? (I do... but there's oooone more part of that label you'll need to generate to make sure its unique. What if there are multiple labels within one function body?)