コード例 #1
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        private static bool Parameter()
        {
            if (!type())
            {
                return(false);
            }
            if (semanticPass)
            {
                SemanticActions.tExist();
            }
            Tokens.NextToken();
            if (Tokens.GetToken().type != Token.Type.Identifier)
            {
                SyntaxError(Tokens.GetToken(), "identifer");
            }
            identifierToken = Tokens.GetToken();
            if (semanticPass)
            {
                SemanticActions.dup(identifierToken, (scope + "." + currentIdentifier));
            }
            if (Tokens.PeekToken().lexeme == "[")
            {
                currentType = "@" + currentType;
            }
            parameters += "P" + uniqueCounter;
            string[] data = new string[2];
            data[0] = "returnType:" + currentType;
            data[1] = "accessMod:" + accessMod;
            Symbol symbol = new Symbol((scope + "." + currentIdentifier), ("P" + uniqueCounter++), Tokens.GetToken().lexeme, "Param", data);

            Tokens.NextToken();
            if (Tokens.GetToken().lexeme == "[")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != "]")
                {
                    SyntaxError(Tokens.GetToken(), "]");
                }
                Tokens.NextToken();
            }
            SymbolTable.Add(symbol);
            return(true);
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
 private static bool numeric_literal()
 {
     if (Tokens.GetToken().lexeme == "+" || Tokens.GetToken().lexeme == "-")
     {
         if (Tokens.PeekToken().type != Token.Type.Number)
         {
             return(false);
         }
         string sign = Tokens.GetToken().lexeme;
         if (sign == "+")
         {
             sign = "";
         }
         Tokens.NextToken();
         string[] data = new string[2];
         data[0] = "returnType:int";
         data[1] = "accessMod:public";
         Symbol symbol = new Symbol("g", ("N" + sign + Tokens.GetToken().lexeme), (sign + Tokens.GetToken().lexeme), "ilit", data);
         SymbolTable.Add(symbol);
         if (semanticPass)
         {
             SemanticActions.lPush(symbol, Tokens.GetToken(), scope);
         }
     }
     else if (Tokens.GetToken().type == Token.Type.Number)
     {
         string[] data = new string[2];
         data[0] = "returnType:int";
         data[1] = "accessMod:public";
         Symbol symbol = new Symbol("g", ("N" + Tokens.GetToken().lexeme), Tokens.GetToken().lexeme, "ilit", data);
         SymbolTable.Add(symbol);
         if (semanticPass)
         {
             SemanticActions.lPush(symbol, Tokens.GetToken(), scope);
         }
     }
     else
     {
         return(false);
     }
     return(true);
 }
コード例 #3
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        private static bool VariableDeclaration()
        {
            if (Tokens.PeekToken().type != Token.Type.Identifier)
            {
                return(false);
            }
            if (!type())
            {
                return(false);
            }
            if (semanticPass)
            {
                SemanticActions.tExist();
            }
            Tokens.NextToken();
            currentIdentifier = Tokens.GetToken().lexeme;
            identifierToken   = Tokens.GetToken();
            Tokens.NextToken();
            if (Tokens.GetToken().lexeme == "[")
            {
                Tokens.NextToken();
                currentType = "@" + currentType;
                if (Tokens.GetToken().lexeme != "]")
                {
                    SyntaxError(Tokens.GetToken(), "]");
                }
                Tokens.NextToken();
            }
            string[] data = new string[2];
            data[0] = "returnType:" + currentType;
            data[1] = "accessMod:" + accessMod;
            Symbol symbol = new Symbol(scope, ("L" + uniqueCounter++), currentIdentifier, "lvar", data);

            identifierSymbol = symbol;
            if (semanticPass)
            {
                SemanticActions.dup(identifierToken, scope);
                SemanticActions.vPush(symbol, identifierToken, scope);
            }
            SymbolTable.Add(symbol);
            if (Tokens.GetToken().lexeme == "=")
            {
                if (semanticPass)
                {
                    SemanticActions.oPush(Tokens.GetToken());
                }
                Tokens.NextToken();
                if (!AssignmentExpression())
                {
                    SyntaxError(Tokens.GetToken(), "assignment expression");
                }
            }
            if (Tokens.GetToken().lexeme != ";")
            {
                SyntaxError(Tokens.GetToken(), ";");
            }
            if (semanticPass)
            {
                SemanticActions.EOE();
            }
            Tokens.NextToken();
            return(true);
        }
コード例 #4
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
 private static bool FieldDeclaration()
 {
     if (Tokens.GetToken().lexeme == "(")
     {
         Tokens.NextToken();
         parameters = "";
         string methodType = currentType;
         offset         = 0;
         sizeParameters = 0;
         ParameterList();
         methodSize = 0;
         string[] data = new string[2];
         data[0] = "returnType:" + methodType;
         data[1] = "accessMod:" + accessMod;
         Symbol symbol = new Symbol(scope, ("M" + uniqueCounter++), currentIdentifier, "method", data);
         symbol.parameters = parameters;
         currentMethodName = "M";
         if (semanticPass)
         {
             ICode.FUNC(symbol.symid);
         }
         if (Tokens.GetToken().lexeme != ")")
         {
             SyntaxError(Tokens.GetToken(), ")");
         }
         Tokens.NextToken();
         if (!MethodBody())
         {
             SyntaxError(Tokens.GetToken(), "method body");
         }
         symbol.size = methodSize;
         SymbolTable.Add(symbol);
         offset     = 0;
         methodSize = 0;
         return(true);
     }
     else
     {
         bool flag = false;
         if (Tokens.GetToken().lexeme == "[")
         {
             flag = true;
             if (Tokens.PeekToken().lexeme != "]")
             {
                 return(false);
             }
             Tokens.NextToken();
             Tokens.NextToken();
         }
         if (semanticPass)
         {
             SemanticActions.vPush(identifierSymbol, identifierToken, scope);
         }
         if (Tokens.GetToken().lexeme == "=")
         {
             flag = true;
             if (semanticPass)
             {
                 SemanticActions.oPush(Tokens.GetToken());
             }
             Tokens.NextToken();
             if (!AssignmentExpression())
             {
                 return(false);
             }
         }
         if ((Tokens.GetToken().lexeme == ";"))
         {
             if (semanticPass)
             {
                 SemanticActions.EOE();
             }
             Tokens.NextToken();
             return(true);
         }
         if (flag)
         {
             SyntaxError(Tokens.GetToken(), ";");
         }
     }
     return(false);
 }