Question on codewriter

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

Question on codewriter

pawdwanlearner
It's true building this compiler does challenge you and make you a better high level programmer. Question for Mark, Did you implement the CodWriter() with the way my code is i ended up not using it. If you did are we suppose to use it in conjunction with the VmWriter module.
Reply | Threaded
Open this post in threaded view
|

Re: Question on codewriter

cadet1620
Administrator
Are you asking about CodeWriter class from the VM Translator?

The intent is that the Compiler writes a SomeClass.vm file for every SomeClass.jack source file.  Those VM files are what VmWriter generates.  VmTranslator is then run independently to generate the ASM file for the compiled Jack program.

Since the JackCompiler does not write ASM files, it doesn't need any of the CodeWriter services.

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

Re: Question on codewriter

pawdwanlearner
No, What i am referring to is in chapter 11 on evaluating expressions right under figure 11.4 where they try to explain that the recursive nature of the codeWrite(exp)  evaluates the expression in the needed postfix structure needed for evaluation on the stack. I seem to have incorporated this in my code when evaluating a term. My question is that a good idea or should i be using this algorithm, and could you give a clue on how you used it if you incorporated it into your compiler
Reply | Threaded
Open this post in threaded view
|

Re: Question on codewriter

cadet1620
Administrator
Got it.

codeWrite(exp) in that example is analogous to CompilationEngine.CompileExpression() and CompileTerm() and whatever other CompileXxx() they may call.  All of my calls to VmWriter are within the CompilationEngine functions that parse the expressions syntax.

For example when parsing "a+2",
CompileExpression() calls CopmpileTerm()

    CompileTerm() parses the variable name and calls VmWriter.WritePush()
    to write the "push a" VM code. 

CompileExpression() advances and parses the '+' operator and remembers it for later.

CompileExpression() advances and calls CompileTerm()

    CompileTerm() parses the constant and calls VmWriter.WritePush()
    to write the "push constant 2" VM code. 

CompileExpression() calls VmWriter.WriteArithmetic() to write the "add" VM code.

Finally, CompileExpression() returns to it's caller.
It is important that "remember it for later" uses a local variable.  (If it used an object variable, it would be overwritten during a recursive call to CompileExpression().)

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

Re: Question on codewriter

pawdwanlearner
Ok, i see. I kind of figured as much when the recursive nature of term and compileExpression() seemed to be doing the same thing.

Appreciate the quick response.