示例#1
0
        private static bool ClassMemberDeclaration()
        {
            if (Tokens.GetToken().lexeme != "public" &&
                Tokens.GetToken().lexeme != "private")
            {
                parameters = "";
                methodSize = 0;
                return(ConstructorDeclaration());
            }
            accessMod = Tokens.GetToken().lexeme;
            Tokens.NextToken();
            if (!type())
            {
                SyntaxError(Tokens.GetToken(), "a type");
                return(false);
            }
            if (semanticPass)
            {
                SemanticActions.tExist();
            }
            Tokens.NextToken();
            if (Tokens.GetToken().type != Token.Type.Identifier)
            {
                SyntaxError(Tokens.GetToken(), "identifer");
            }
            currentIdentifier = Tokens.GetToken().lexeme;
            identifierToken   = Tokens.GetToken();
            if (semanticPass)
            {
                SemanticActions.dup(identifierToken, scope);
            }
            Tokens.NextToken();
            Symbol symbol = null;

            if (Tokens.GetToken().lexeme == "[")
            {
                currentType = "@" + currentType;
            }
            if (Tokens.GetToken().lexeme == "[" || Tokens.GetToken().lexeme == "=" || Tokens.GetToken().lexeme == ";")
            {
                string[] data = new string[2];
                data[0] = "returnType:" + currentType;
                data[1] = "accessMod:" + accessMod;
                symbol  = new Symbol(scope, ("V" + uniqueCounter++), currentIdentifier, "ivar", data);
                SymbolTable.Add(symbol);
                if (!semanticPass)
                {
                    memberVariables.Add(symbol.symid);
                }
                classSize += symbol.size;
            }
            identifierSymbol = symbol;
            if (!FieldDeclaration())
            {
                SyntaxError(Tokens.GetToken(), "field declaration");
            }
            accessMod = "public";
            return(true);
        }
示例#2
0
        private static bool ClassDeclaration()
        {
            if (Tokens.GetToken().lexeme != "class")
            {
                return(false);
            }
            Tokens.NextToken();
            scope = "g" + "." + Tokens.GetToken().lexeme;
            if (Tokens.GetToken().type != Token.Type.Identifier)
            {
                SyntaxError(Tokens.GetToken(), "an identifer");
            }
            Symbol symbol = new Symbol("g", ("C" + uniqueCounter++), Tokens.GetToken().lexeme, "Class", null);

            identifierToken = Tokens.GetToken();
            if (semanticPass)
            {
                SemanticActions.dup(identifierToken, "g");
                ICode.StaticInit();
            }
            Tokens.NextToken();
            if (Tokens.GetToken().lexeme != "{")
            {
                SyntaxError(Tokens.GetToken(), "{");
            }
            Tokens.NextToken();
            classSize             = 0;
            classMemberOffset     = 0;
            classTempMemberOffset = 0;
            while (ClassMemberDeclaration())
            {
            }
            if (semanticPass)
            {
                ICode.StaticInitInsertVars();
            }
            if (Tokens.GetToken().lexeme != "}")
            {
                SyntaxError(Tokens.GetToken(), "modifier or constructor or a closing brace");
            }
            else
            {
                memberVariables.Add(currentClassConstructorSymid);
            }
            Tokens.NextToken();
            symbol.size = classMemberOffset;
            SymbolTable.Add(symbol);
            scope = "g";
            return(true);
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }
示例#5
0
        private static bool ConstructorDeclaration()
        {
            if (Tokens.GetToken().type != Token.Type.Identifier)
            {
                return(false);
            }
            currentIdentifier = Tokens.GetToken().lexeme;
            identifierToken   = Tokens.GetToken();
            if (semanticPass)
            {
                SemanticActions.dup(identifierToken, scope);
                SemanticActions.CD(identifierToken, scope);
            }
            Tokens.NextToken();
            if (Tokens.GetToken().lexeme != "(")
            {
                SyntaxError(Tokens.GetToken(), "(");
            }
            Tokens.NextToken();
            offset         = 0;
            sizeParameters = 0;
            ParameterList();
            string[] data = new string[2];
            data[0] = "returnType:" + currentIdentifier;
            data[1] = "accessMod:" + accessMod;
            Symbol symbol = new Symbol(scope, ("X" + uniqueCounter++), currentIdentifier, "constructor", data);

            currentMethodName = symbol.symid;
            if (!semanticPass)
            {
                currentClassConstructorSymid = symbol.symid;
            }
            data[0] = "returnType:" + currentIdentifier;
            data[1] = "accessMod:" + accessMod;
            Symbol symbol2 = new Symbol(scope, ("Y" + symbol.symid.Substring(1)), currentIdentifier + "StaticInit", "Init", data);

            if (semanticPass)
            {
                ICode.RETURN("this");
                ICode.FUNC(symbol.symid);
                ICode.FRAME(symbol2.symid, "this");
                ICode.CALL(symbol2.symid);
            }
            symbol.parameters = parameters;
            if (Tokens.GetToken().lexeme != ")")
            {
                SyntaxError(Tokens.GetToken(), ")");
            }
            Tokens.NextToken();
            if (!MethodBody())
            {
                SyntaxError(Tokens.GetToken(), "method body");
            }
            symbol.size  = methodSize;
            symbol2.size = 0;
            SymbolTable.Add(symbol);
            SymbolTable.Add(symbol2);
            offset     = 0;
            methodSize = 0;
            return(true);
        }