Example #1
0
        /// <summary>
        /// Constructor: creates a new compilation engine with the given input and output. The next routine called must be compileClass().
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        public CompilationEngine(string inputFile, string outputFile)
        {
            //1. call Tokenizer

            _xmlTokens = new StringBuilder();
            _tokenizer = new JackTokenizer(inputFile);

            //WriteXml("<tokens>"); //<tokens>

            //first check for class element

            _tokenizer.Advance();

            if (_tokenizer.TokenType == Enums.Enumerations.TokenType.KEYWORD && _tokenizer.KeyWord() == "class")
            {
                CompileClass();
            }
            else
            {
                //error: no class struct
                _tokenizer.RecordError("expecting class struct");
            }


            //WriteXml("</tokens>"); //</tokens>

            string xml = _xmlTokens.ToString();

            Xml = xml;
        }
        /// <summary>
        /// Constructor: creates a new compilation engine with the given input and output. The next routine called must be compileClass().
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        public CompilationEngine(string inputFile, string outputFile)
        {
            //1. call Tokenizer

            _xmlTokens = new StringBuilder();
            _tokenizer = new JackTokenizer(inputFile);

            //WriteXml("<tokens>"); //<tokens>

            //first check for class element

            _tokenizer.Advance();

            if (_tokenizer.TokenType == Enums.Enumerations.TokenType.KEYWORD && _tokenizer.KeyWord() == "class")
            {
                CompileClass();
            }
            else
            {
                //error: no class struct
                _tokenizer.RecordError("expecting class struct");
            }

            //WriteXml("</tokens>"); //</tokens>

            string xml = _xmlTokens.ToString();

            Xml = xml;
        }
Example #3
0
        /// <summary>
        /// Compiles a complete class.
        /// We'll use the jack programming grammar found on page 208/209 to check for compilation errors etc.
        /// </summary>
        public void CompileClass()
        {
            WriteXml("<class>"); //<class>
            WriteCurrentToken();

            //according to the grammar we should next have a className identifier
            _tokenizer.Advance();

            if (_tokenizer.TokenType == Enums.Enumerations.TokenType.IDENTIFIER)
            {
                WriteCurrentToken();

                _tokenizer.Advance();

                if (_tokenizer.TokenType == Enums.Enumerations.TokenType.SYMBOL)
                {
                    WriteCurrentToken();

                    _tokenizer.Advance();

                    //now we determine if we have class variable declarations or subroutine declaration
                    //looping through the variable declarations first, if there are any.
                    while (_tokenizer.TokenType == Enums.Enumerations.TokenType.KEYWORD && decTypes.Contains(_tokenizer.KeyWord()) && !_tokenizer.HasErrors)
                    {
                        CompileClassVarDec();
                    }

                    //looping through the subroutine declarations, if there are any.
                    while (_tokenizer.TokenType == Enums.Enumerations.TokenType.KEYWORD && subTypes.Contains(_tokenizer.KeyWord()) && !_tokenizer.HasErrors)
                    {
                        CompileSubroutine();
                    }
                }
                else
                {
                    //error: expecting '{'
                    _tokenizer.RecordError("expecting '{'");
                }
            }
            else
            {
                //error: expecting className
                _tokenizer.RecordError("expecting className");
            }


            if (_tokenizer.TokenType == Enums.Enumerations.TokenType.SYMBOL && _tokenizer.Symbol() == "}")
            {
                WriteCurrentToken();
            }
            else
            {
                _tokenizer.RecordError("expecting '}' end of class");
            }

            WriteXml("</class>"); //</class>
        }