コード例 #1
0
        //
        // subroutineBody: '{' varDec* statements '}'
        public void CompileSubroutineBody(int depth, string subType, string subName, string className)
        {
            // compile: '{'
            var leftBraceToken = Eat("{");

            // compile: varDec*
            while (_tokenizer.CurrentToken.Value == "var")
            {
                CompileVarDec(depth + 1);
            }
            _vmWriter.WriteFunction(string.Format("{0}.{1}", className, subName), SymbolTableManager.GetLocalsNum());
            // if constructor -> allocate
            if (subType == "constructor")
            {
                int wordsToAllocate = SymbolTableManager.GetFieldsNum();
                _vmWriter.WritePush("constant", wordsToAllocate);
                _vmWriter.WriteCall("Memory.alloc", 1);
                _vmWriter.WritePop("pointer", 0);
            }
            else if (subType == "method")
            {
                _vmWriter.WritePush("argument", 0);
                _vmWriter.WritePop("pointer", 0);
            }

            // compile: statements
            CompileStatements(depth + 1);

            // compile: '}'
            var rightBraceToken = Eat("}");
        }