Beispiel #1
0
        public static void iExist()
        {
            SAR top_sar = SAS.Pop();

            if (top_sar.value == "this")
            {
                SAS.Push(top_sar);
                return;
            }
            string args = "";

            if (top_sar.argList.Count != 0)
            {
                args = top_sar.argList[0];
                for (int i = 1; i < top_sar.argList.Count; ++i)
                {
                    args += "," + top_sar.argList[i];
                }
            }
            top_sar.paramType = args;
            string symid = SymbolTable.iExists(top_sar);

            if (symid == null || !SymbolTable.ContainsSymid(symid))
            {
                SemanticError(top_sar);
            }
            Symbol symbol = SymbolTable.GetValue(symid);

            if (symbol.data != null)
            {
                top_sar.dataType = symbol.data[0].Split(':')[1];
                top_sar.dataType.Trim();
            }
            top_sar.paramType = symbol.parameters;
            top_sar.symid     = symbol.symid;
            if (symbol.kind == "method")
            {
                ICode.FRAME(symbol.symid, "this");
                if (symbol.parameters != null && symbol.parameters != "")
                {
                    for (int i = 0; i < top_sar.argList.Count; ++i)
                    {
                        ICode.PUSH(top_sar.argList[i]);
                    }
                }
                ICode.CALL(symbol.symid);
                Symbol temp = new Symbol("t", "t" + uniqueCounter++, "t_" + symbol.value + "_ReturnValue", "tvar", symbol.data);
                SymbolTable.Add(temp);
                top_sar.symid = temp.symid;
                if (symbol.data[0].Split(':')[1] != "void" && symbol.data[0].Split(':')[1] != "null")
                {
                    ICode.PEEK(temp.symid);
                }
            }
            SAS.Push(top_sar);
        }
Beispiel #2
0
        public static void NewObj()
        {
            SAR al   = SAS.Pop();
            SAR type = SAS.Pop();

            if (!SymbolTable.NewObj(type, al.argList.ToArray()))
            {
                SemanticError(type);
            }
            string functionSymid = type.symid;

            type.sarType = SAR.SARtype.Ref;
            type.symid   = "t" + uniqueCounter++;
            string[] data     = { "returnType:" + type.dataType, "accessMod:private" };
            Symbol   instance = new Symbol("t", type.symid, "t" + type.value, "tvar", data);

            SymbolTable.Add(instance);
            string[] data2    = { "returnType:" + type.dataType, "accessMod:private" };
            Symbol   peekTemp = new Symbol("t", "t" + uniqueCounter++, "t" + type.value + "ReturnValue", "tvar", data2);

            SymbolTable.Add(peekTemp);
            Symbol staticInit = SymbolTable.GetValue(("Y" + functionSymid.Substring(1)));

            ICode.NEWI(type.size.ToString(), type.symid);
            //call constructor
            ICode.FRAME(functionSymid, type.symid);
            if (al.argList != null && al.argList.Count > 0 && al.argList[0] != "")
            {
                for (int i = 0; i < al.argList.Count; ++i)
                {
                    ICode.PUSH(al.argList[i]);
                }
            }
            ICode.CALL(functionSymid);
            ICode.PEEK(peekTemp.symid);
            type.symid = peekTemp.symid;
            SAS.Push(type);
        }
Beispiel #3
0
        public static void rExist()
        {
            SAR    top_sar = SAS.Pop();
            SAR    LHS     = SAS.Peek();
            bool   this_   = false;
            string scope   = top_sar.scope;

            if (SAS.Count > 0)
            {
                if (SAS.Peek().value != "this")
                {
                    top_sar.classType = SAS.Pop().dataType;
                }
                else
                {
                    this_ = true;
                    string[] temp = top_sar.scope.Split('.');
                    top_sar.classType = temp[temp.Length - 1];
                    SAS.Pop();
                }
            }
            else
            {
                SemanticError(top_sar);
            }
            Symbol symbol;

            if ((symbol = SymbolTable.rExists(top_sar)) == null)
            {
                SemanticError(top_sar);
            }
            if (symbol.data != null)                                                      //not a class
            {
                if ((symbol.data[symbol.data.Length - 1] == "accessMod:public") || this_) //member is public
                {
                    top_sar.dataType = symbol.data[0].Split(':')[1];
                    top_sar.dataType.Trim();
                    top_sar.sarType = SAR.SARtype.Ref;
                    top_sar.symid   = "t" + uniqueCounter++;
                    top_sar.scope   = "t";
                    string[] data = { "returnType:" + top_sar.dataType, "accessMod:private" };
                    if (symbol.kind == "method")
                    {
                        ICode.FRAME(symbol.symid, LHS.symid);
                        if (symbol.parameters != null && symbol.parameters != "")
                        {
                            for (int i = 0; i < top_sar.argList.Count; ++i)
                            {
                                ICode.PUSH(top_sar.argList[i]);
                            }
                        }
                        ICode.CALL(symbol.symid);
                        ICode.PEEK(top_sar.symid);
                    }
                    else
                    {
                        top_sar.symid += "r";
                        if (LHS.value == "this")
                        {
                            LHS.symid = "this";
                        }
                        ICode.REF(LHS.symid, symbol.symid, top_sar.symid);
                    }
                    Symbol temp = new Symbol(top_sar.scope, top_sar.symid, "t" + top_sar.value, "tvar", data);
                    SymbolTable.Add(temp);
                    SAS.Push(top_sar);
                    return;
                }
                else
                {
                    SemanticPrivateError(top_sar);
                }
            }

            /*if (SymbolTable.ContainsValue(scope + "." + top_sar.value))
             * {
             *  Symbol symbol = SymbolTable.GetValue(SymbolTable.GetSymid(scope + "." + top_sar.value));
             *  if (symbol.data != null) //not a class
             *  {
             *      if (symbol.data[symbol.data.Length - 1]=="accessMod:public") //member is public
             *      {
             *          top_sar.dataType = symbol.data[0].Split(':')[1];
             *          top_sar.dataType.Trim();
             *          if (symbol.data.Length == 3)
             *          {
             *              top_sar.paramType = symbol.data[1];
             *          }
             *          top_sar.sarType = SAR.SARtype.Ref;
             *          SAS.Push(top_sar);
             *          return;
             *      }
             *  }
             * }*/
            SemanticError(top_sar);
        }
Beispiel #4
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);
        }