Ejemplo n.º 1
0
 public static bool ExpectType(this JackTokenizer.ClassifiedJackToken token, bool acceptVoid = false, bool throwException = true)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.identifier)
     {
         return(true);
     }
     else if (
         token.TokenType() == JackTokenizer.JackTokenType.keyword &&
         (
             token.ParseKeyWord() == JackTokenizer.JackKeywordTypes.INT ||
             token.ParseKeyWord() == JackTokenizer.JackKeywordTypes.CHAR ||
             token.ParseKeyWord() == JackTokenizer.JackKeywordTypes.BOOLEAN ||
             (acceptVoid ? token.ParseKeyWord() == JackTokenizer.JackKeywordTypes.VOID : false)
         )
         )
     {
         return(true);
     }
     else
     {
         if (throwException)
         {
             throw new Exception("Expected a type keyword but found '" + token.value + "'.");
         }
         else
         {
             return(false);
         }
     }
 }
Ejemplo n.º 2
0
 public static string ExpectIdentifier(this JackTokenizer.ClassifiedJackToken token)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.identifier)
     {
         return(token.Identifier());
     }
     else
     {
         throw new Exception("Expected identifier but found '" + token.value + "'.");
     }
 }
Ejemplo n.º 3
0
 public static string getIdentifierCategory(this JackTokenizer.ClassifiedJackToken token, SymbolTable symbolTable = null)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.identifier && symbolTable != null)
     {
         var category = symbolTable.KindOf(token.Identifier());
         if (category != null)
         {
             return(" category=\"" + category.ToString().ToLower() + "\"");
         }
     }
     return("");
 }
Ejemplo n.º 4
0
 public static bool ExpectSymbol(this JackTokenizer.ClassifiedJackToken token, char[] symbols, bool throwException = true)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.symbol && symbols.Contains(token.ParseChar()))
     {
         return(true);
     }
     else
     {
         if (throwException)
         {
             throw new Exception("Expected on of theses symbols '" + symbols.ToString() + "' but found '" + token.value + "'.");
         }
         else
         {
             return(false);
         }
     }
 }
Ejemplo n.º 5
0
 public static bool ExpectKeyword(this JackTokenizer.ClassifiedJackToken token, JackTokenizer.JackKeywordTypes[] keywords, bool throwException = true)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.keyword &&
         keywords.Contains(token.ParseKeyWord()))
     {
         return(true);
     }
     else
     {
         if (throwException)
         {
             throw new Exception("Expected one of these keywords '" + keywords.Select(x => x.ToString()).ToString() + "' but found '" + token.value + "'.");
         }
         else
         {
             return(false);
         }
     }
 }
Ejemplo n.º 6
0
 public static bool ExpectKeyword(this JackTokenizer.ClassifiedJackToken token, JackTokenizer.JackKeywordTypes keyword, bool throwException = true)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.keyword &&
         token.ParseKeyWord() == keyword)
     {
         return(true);
     }
     else
     {
         if (throwException)
         {
             throw new Exception("Expected keyword 'class' but found '" + token.value + "'.");
         }
         else
         {
             return(false);
         }
     }
 }
Ejemplo n.º 7
0
        private void CompileOp(JackTokenizer.ClassifiedJackToken token)
        {
            var sym = token.ParseChar();

            switch (sym)
            {
            case '+':
                this.vm.WriteArithmetic(VMWriter.Command.ADD);
                break;

            case '-':
                this.vm.WriteArithmetic(VMWriter.Command.SUB);
                break;

            case '*':
                this.vm.WriteCall("Math.multiply", 2);
                break;

            case '/':
                this.vm.WriteCall("Math.divide", 2);
                break;

            case '>':
                this.vm.WriteArithmetic(VMWriter.Command.GT);
                break;

            case '<':
                this.vm.WriteArithmetic(VMWriter.Command.LT);
                break;

            case '=':
                this.vm.WriteArithmetic(VMWriter.Command.EQ);
                break;

            case '&':
                this.vm.WriteArithmetic(VMWriter.Command.AND);
                break;

            case '|':
                this.vm.WriteArithmetic(VMWriter.Command.OR);
                break;
            }
        }
Ejemplo n.º 8
0
 public static bool ExpectSymbol(this JackTokenizer.ClassifiedJackToken token, char symbol, bool throwException = true)
 {
     if (token.TokenType() == JackTokenizer.JackTokenType.symbol &&
         token.ParseChar() == symbol
         )
     {
         return(true);
     }
     else
     {
         if (throwException)
         {
             throw new Exception("Expected symbol'" + symbol + "' but found '" + token.value + "'.");
         }
         else
         {
             return(false);
         }
     }
 }
Ejemplo n.º 9
0
        public static string toXml(this JackTokenizer.ClassifiedJackToken token, SymbolTable symbolTable = null)
        {
            string xml = "";

            JackTokenizer.JackTokenType tokenClass = token.TokenType();
            xml += $"<{tokenClass.ToString()}{token.getIdentifierCategory(symbolTable)}>";
            switch (tokenClass)
            {
            case JackTokenizer.JackTokenType.identifier:
            case JackTokenizer.JackTokenType.intConst:
            case JackTokenizer.JackTokenType.keyword:
                xml += token.value;
                break;

            case JackTokenizer.JackTokenType.stringConst:
                xml += token.ParseStringVal();
                break;

            case JackTokenizer.JackTokenType.symbol:
                char sym = token.ParseChar();
                if (sym == '<')
                {
                    xml += "&lt;";
                }
                else if (sym == '>')
                {
                    xml += "&gt;";
                }
                else if (sym == '&')
                {
                    xml += "&amp;";
                }
                else
                {
                    xml += token.value;
                }
                break;
            }
            xml += "</" + tokenClass.ToString() + ">\n";
            return(xml);
        }