Example #1
0
        public static void return_(Token t, string scope)
        {
            SAR expression = null;

            ShuntYardAll();
            if (SAS.Count > 0)
            {
                expression = SAS.Pop();
                if (expression.value == "true" || expression.value == "false")
                {
                    expression.dataType = "bool";
                }
            }
            if (!SymbolTable.return_(expression, scope))
            {
                SemanticReturnError(expression, scope);
            }
            if (expression == null)
            {
                ICode.RTN();
            }
            else
            {
                ICode.RETURN(expression.symid);
            }
        }
Example #2
0
        private static bool Start()
        {
            globalOffset = 12;//first line in assembly is a jmp
            if (!semanticPass)
            {
                address         = 0;
                offset          = 0;
                memberVariables = new List <string>();
            }
            while (ClassDeclaration())
            {
                ;
            }
            if (!Tokens.GetToken().lexeme.Equals("void"))
            {
                SyntaxError(Tokens.GetToken(), "void return for main");
            }
            Tokens.NextToken();
            if (!Tokens.GetToken().lexeme.Equals("pxi"))
            {
                SyntaxError(Tokens.GetToken(), "pxi");
            }
            Tokens.NextToken();
            if (!Tokens.GetToken().lexeme.Equals("main"))
            {
                SyntaxError(Tokens.GetToken(), "main method");
            }
            string[] data = new string[2];
            data[0] = "returnType:void";
            data[1] = "accessMod:public";
            Symbol symbol = new Symbol("g", ("MAIN"), Tokens.GetToken().lexeme, "main", data);

            SymbolTable.Add(symbol);
            currentMethodName = "M";
            identifierToken   = Tokens.GetToken();
            currentIdentifier = "";
            scope            += ".main";
            if (semanticPass)
            {
                ICode.FUNC(symbol.symid);
            }
            Tokens.NextToken();
            if (Tokens.GetToken().lexeme != "(")
            {
                SyntaxError(Tokens.GetToken(), "(");
            }
            Tokens.NextToken();
            if (Tokens.GetToken().lexeme != ")")
            {
                SyntaxError(Tokens.GetToken(), ")");
            }
            Tokens.NextToken();
            if (!MethodBody())
            {
                SyntaxError(Tokens.GetToken(), "method body");
            }
            return(true);
        }
Example #3
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);
        }
Example #4
0
        public static void MathOperator()
        {
            bool valid = false;
            SAR  y     = SAS.Pop();
            SAR  x     = SAS.Pop();
            SAR  z     = new SAR(SAR.SARtype.Identifier, x.token, x.value, x.scope);

            z.dataType = x.dataType;
            string op = OS.Peek();

            if (x.dataType[0] == '@' && x.argList != null && x.argList.Count != 0)
            {
                if (x.dataType.Substring(1, x.dataType.Length - 1) == y.dataType)
                {
                    valid = true;
                }
            }
            if (y.dataType[0] == '@' && y.argList != null && y.argList.Count != 0)
            {
                if (y.dataType.Substring(1, y.dataType.Length - 1) == x.dataType)
                {
                    valid = true;
                }
            }
            if (((x.dataType == y.dataType) && (x.dataType == "int")) || valid)
            {
                z.symid  = "t" + uniqueCounter++;
                z.value += "_" + y.value;
                z.scope  = "t";
                string[] data = { "returnType:" + z.dataType, "accessMod:private" };
                Symbol   temp = new Symbol("t", z.symid, z.value, "tvar", data);
                SymbolTable.Add(temp);
                if (op == "+")
                {
                    ICode.ADD(x.symid, y.symid, z.symid);
                }
                else if (op == "-")
                {
                    ICode.SUB(x.symid, y.symid, z.symid);
                }
                else if (op == "*")
                {
                    ICode.MUL(x.symid, y.symid, z.symid);
                }
                else if (op == "/")
                {
                    ICode.DIV(y.symid, x.symid, z.symid);
                }
                SAS.Push(z);
                OS.Pop();
                OSprecidence.Pop();
                return;
            }
            SemanticOperationError(x, y, OS.Peek());
        }
Example #5
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);
        }
Example #6
0
        public static void cin()
        {
            ShuntYardAll();
            SAR    exp  = SAS.Pop();
            string type = exp.dataType;

            if ((type[0] == '@') && (exp.argList.Count > 0))
            {
                type = type.Substring(1, type.Length - 1);
            }
            if (type != "char" && type != "int")
            {
                SemanticCoutError(exp);
            }
            ICode.READ(exp.symid);
        }
Example #7
0
        public static void while_(Token t)
        {
            if (SAS.Count == 0)
            {
                SemanticBoolError(t);
            }
            SAR expression = SAS.Pop();

            if (expression.dataType != "bool")
            {
                SemanticBoolError(t);
            }
            string ENDWHILE = (ICode.ENDWHILE + ICode.labelCounter++) + " ";

            ICode.BF(expression.symid, ENDWHILE);
            ICode.StackEndWhile(ENDWHILE);
        }
Example #8
0
        public static void if_(Token t)
        {
            if (SAS.Count == 0)
            {
                SemanticBoolError(t);
            }
            SAR expression = SAS.Pop();

            if (expression.dataType != "bool")
            {
                SemanticBoolError(t);
            }
            string SKIPIF = (ICode.SKIPIF + ICode.labelCounter++) + " ";

            ICode.BF(expression.symid, SKIPIF);
            ICode.StackIf(SKIPIF);
        }
Example #9
0
        public static void itoa()
        {
            SAR exp = SAS.Pop();

            if (exp.dataType != "int")
            {
                SemanticAtoiError(exp);
            }
            string[] data = { "returnType:char", "accessMod:private" };
            Symbol   temp = new Symbol("t", "t" + uniqueCounter++, "t" + exp.value, "tvar", data);

            SymbolTable.Add(temp);
            ICode.MOV(temp.symid, exp.symid);
            ICode.CONVERT(temp.symid);
            exp.dataType = "char";
            exp.symid    = temp.symid;
            SAS.Push(exp);
        }
Example #10
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);
        }
Example #11
0
 private static bool MethodBody()
 {
     if (Tokens.GetToken().lexeme != "{")
     {
         return(false);
     }
     if (currentIdentifier != "")
     {
         scope += "." + currentIdentifier;
     }
     Tokens.NextToken();
     while (VariableDeclaration())
     {
         ;
     }
     while (Statement())
     {
         ;
     }
     if (semanticPass)
     {
         if (currentMethodName[0] == 'X')
         {
             ICode.RETURN("this");
         }
         else
         {
             ICode.RTN();
         }
     }
     if (Tokens.GetToken().lexeme != "}")
     {
         SyntaxError(Tokens.GetToken(), "}  \n~Variable declarations after a statement not allowed~");
     }
     Tokens.NextToken();
     scope = leaveScope(scope);
     return(true);
 }
Example #12
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);
        }
Example #13
0
        private static bool Statement()
        {
            if (Tokens.GetToken().lexeme == "{")
            {
                Tokens.NextToken();
                while (Statement())
                {
                    ;
                }
                if (Tokens.GetToken().lexeme != "}")
                {
                    SyntaxError(Tokens.GetToken(), "}");
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "if")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != "(")
                {
                    SyntaxError(Tokens.GetToken(), "(");
                }
                if (semanticPass)
                {
                    SemanticActions.oPush(Tokens.GetToken());
                }
                Tokens.NextToken();
                if (!Expression())
                {
                    SyntaxError(Tokens.GetToken(), "an expression");
                }
                if (Tokens.GetToken().lexeme != ")")
                {
                    SyntaxError(Tokens.GetToken(), ")");
                }
                if (semanticPass)
                {
                    SemanticActions.ShuntYardAll();
                    SemanticActions.if_(Tokens.GetToken());
                }
                Tokens.NextToken();
                if (!Statement())
                {
                    SyntaxError(Tokens.GetToken(), "a statement");
                }
                if (Tokens.GetToken().lexeme == "else")
                {
                    Tokens.NextToken();
                    if (semanticPass)
                    {
                        string SKIPELSE = ICode.SKIPELSE + ICode.labelCounter++ + " ";
                        ICode.JMP(SKIPELSE);
                        ICode.StackElse(SKIPELSE);
                        ICode.Print(ICode.StackIf());
                    }
                    if (!Statement())
                    {
                        SyntaxError(Tokens.GetToken(), "a statement");
                    }
                    if (semanticPass)
                    {
                        ICode.Print(ICode.StackElse());
                    }
                }
                else if (semanticPass)
                {
                    ICode.Print(ICode.StackIf());
                }
                return(true);
            }

            if (Tokens.GetToken().lexeme == "while")
            {
                if (semanticPass)
                {
                    string BEGIN = ICode.BEGIN + ICode.labelCounter++ + " ";
                    ICode.Print(BEGIN);
                    ICode.StackWhile(BEGIN);
                }
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != "(")
                {
                    SyntaxError(Tokens.GetToken(), "(");
                }
                if (semanticPass)
                {
                    SemanticActions.oPush(Tokens.GetToken());
                }
                Tokens.NextToken();
                if (!Expression())
                {
                    SyntaxError(Tokens.GetToken(), "an expression");
                }
                if (Tokens.GetToken().lexeme != ")")
                {
                    SyntaxError(Tokens.GetToken(), ")");
                }
                if (semanticPass)
                {
                    SemanticActions.ShuntYardAll();
                    SemanticActions.while_(Tokens.GetToken());
                }
                Tokens.NextToken();
                if (!Statement())
                {
                    SyntaxError(Tokens.GetToken(), "a statement");
                }
                if (semanticPass)
                {
                    string BEGIN = ICode.StackWhile();
                    ICode.JMP(BEGIN);
                    string ENDWHILE = ICode.StackEndWhile();
                    ICode.Print(ENDWHILE);
                }
                return(true);
            }

            if (Tokens.GetToken().lexeme == "return")
            {
                Tokens.NextToken();
                Expression();
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.return_(Tokens.GetToken(), scope);
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "cout")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != "<<")
                {
                    SyntaxError(Tokens.GetToken(), "<<");
                }
                Tokens.NextToken();
                if (!Expression())
                {
                    SyntaxError(Tokens.GetToken(), "an expression");
                }
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.cout();
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "cin")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != ">>")
                {
                    SyntaxError(Tokens.GetToken(), ">>");
                }
                Tokens.NextToken();
                if (!Expression())
                {
                    SyntaxError(Tokens.GetToken(), "an expression");
                }
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.cin();
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "spawn")
            {
                Tokens.NextToken();
                if (!Expression())
                {
                    SyntaxError(Tokens.GetToken(), "an expression");
                }
                if (Tokens.GetToken().lexeme != "set")
                {
                    SyntaxError(Tokens.GetToken(), "set");
                }
                Tokens.NextToken();
                if (Tokens.GetToken().type != Token.Type.Identifier)
                {
                    SyntaxError(Tokens.GetToken(), "an identifier");
                }
                if (semanticPass)
                {
                    SemanticActions.iPush(Tokens.GetToken(), scope);
                }
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.spawn();
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "block")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.block();
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "lock")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().type != Token.Type.Identifier)
                {
                    SyntaxError(Tokens.GetToken(), "an identifier");
                }
                if (semanticPass)
                {
                    SemanticActions.iPush(Tokens.GetToken(), scope);
                }
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.lock_();
                }
                Tokens.NextToken();
                return(true);
            }

            if (Tokens.GetToken().lexeme == "release")
            {
                Tokens.NextToken();
                if (Tokens.GetToken().type != Token.Type.Identifier)
                {
                    SyntaxError(Tokens.GetToken(), "an identifier");
                }
                if (semanticPass)
                {
                    SemanticActions.iPush(Tokens.GetToken(), scope);
                }
                Tokens.NextToken();
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.release();
                }
                Tokens.NextToken();
                return(true);
            }

            if (Expression())
            {
                if (Tokens.GetToken().lexeme != ";")
                {
                    SyntaxError(Tokens.GetToken(), ";");
                }
                if (semanticPass)
                {
                    SemanticActions.EOE();
                }
                Tokens.NextToken();
                return(true);
            }
            return(false);
        }
Example #14
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);
        }
Example #15
0
 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);
 }
Example #16
0
        public static void BoolOperator()
        {
            SAR    y  = SAS.Pop();
            SAR    x  = SAS.Pop();
            SAR    z  = new SAR(SAR.SARtype.Identifier, x.token, x.value, x.scope);
            string op = OS.Peek();

            if (op == "==" || op == "!=")
            {
                if ((x.dataType != y.dataType) && (y.dataType != "null"))
                {
                    SemanticOperationError(x, y, OS.Pop());
                }
            }
            else if ((x.dataType != y.dataType))
            {
                /*if(x.dataType == "int" || x.dataType == "char")
                 * {
                 *
                 * }
                 * else
                 * {*/
                SemanticOperationError(x, y, OS.Pop());
                // }
            }

            z.symid    = "t" + uniqueCounter++;
            z.value   += "_" + y.value;
            z.scope    = "t";
            z.dataType = "bool";
            string[] data = { "returnType:" + z.dataType, "accessMod:private" };
            Symbol   temp = new Symbol("t", z.symid, z.value, "tvar", data);

            SymbolTable.Add(temp);
            if (op == "<")
            {
                ICode.LT(x.symid, y.symid, z.symid);
            }
            else if (op == ">")
            {
                ICode.GT(x.symid, y.symid, z.symid);
            }
            else if (op == "!=")
            {
                ICode.NE(x.symid, y.symid, z.symid);
            }
            else if (op == "==")
            {
                ICode.EQ(x.symid, y.symid, z.symid);
            }
            else if (op == "<=")
            {
                ICode.LE(x.symid, y.symid, z.symid);
            }
            else if (op == ">=")
            {
                ICode.GE(x.symid, y.symid, z.symid);
            }
            SAS.Push(z);
            OS.Pop();
            OSprecidence.Pop();
            return;
        }
Example #17
0
        public static string iExists(SAR s)
        {
            Symbol symbol;
            Symbol check;
            Symbol defined;
            bool   flag           = false;
            string tempScope      = "";
            string scope          = s.scope;
            string parameterTypes = "";
            int    definedParamCount;

            while (scope != "")
            {
                if (ScopeTable.ContainsKey(scope))
                {
                    for (int i = 0; i < ScopeTable[scope].Count; ++i)
                    {
                        symbol = ScopeTable[scope][i];
                        if (symbol.value.Equals(s.value) && symbol.symid[1] != 'A')
                        {
                            tempScope = scope;
                            if (symbol.parameters.Split(',')[0] != "")
                            {
                                definedParamCount = symbol.parameters.Split(',').Length;
                            }
                            else
                            {
                                definedParamCount = 0;
                            }
                            if (s.dataType == "@")
                            {
                                string type = symbol.data[0].Split(':')[1];
                                if (type.Length < 1 || type[0] != '@')
                                {
                                    continue;
                                }
                                if (type[0] == '@')
                                {
                                    type = type.Substring(1, type.Length - 1);
                                }
                                if (s.value == "i")
                                {
                                    Console.Write(" ");
                                }
                                string   symid = "tArr" + SemanticActions.uniqueCounter++;
                                string[] data  = new string[2];
                                data[0] = "dataType:" + type;
                                data[1] = "accessMod:public";
                                Symbol temp = new Symbol("t", symid, "t" + s.value, "tvar", data);
                                Add(temp);
                                s.dataType = data[0];
                                s.symid    = symid;
                                s.scope    = "t";
                                ICode.AEF(symbol.symid, s.argList[0], temp.symid);
                                return(s.symid);
                            }
                            else if (s.argList.Count != definedParamCount)
                            {
                                for (int j = 0; j < s.argList.Count; ++j)
                                {
                                    parameterTypes += symbolTable[s.argList[s.argList.Count - j - 1]].data[0].Split(':')[1] + ",";
                                }
                                continue;
                            }
                            else
                            {
                                parameterTypes = "";
                                for (int j = 0; j < s.argList.Count; ++j)
                                {
                                    check   = symbolTable[s.argList[j]];//args are in reverse order
                                    defined = symbolTable[symbol.parameters.Split(',')[j]];
                                    if (check.data[0].Split(':')[1] != defined.data[0].Split(':')[1])
                                    {
                                        flag = true;
                                    }
                                    parameterTypes += check.data[0].Split(':')[1] + ",";
                                }
                                if (flag)
                                {
                                    continue;
                                }
                            }
                            s.symid = ScopeTable[scope][i].symid;
                            s.scope = scope;
                            return(s.symid);
                        }
                    }
                }
                if (!scope.Contains("."))
                {
                    if (parameterTypes != null && parameterTypes.Length > 0)
                    {
                        s.value += "(" + parameterTypes.Substring(0, parameterTypes.Length - 1) + ")";
                    }
                    if (tempScope != "")
                    {
                        s.scope = tempScope;
                    }
                    return(null);
                }
                string[] scopes = scope.Split('.');
                scope = scopes[0];
                for (int i = 1; i < scopes.Length - 1; ++i)
                {
                    scope += "." + scopes[i];
                }
            }
            s.value += "(" + parameterTypes.Substring(0, parameterTypes.Length - 1) + ")";
            s.scope  = tempScope;
            return(null);
        }
Example #18
0
        static int Main(string[] args)
        {
            bool   printFile    = false;
            bool   printConsole = false;
            string fileName;
            string outputFileName = null;

            if (args.Length == 0)
            {
                Console.Write("Enter filename with file extension pxi: ");
                return(0);
            }
            else
            {
                if (args.Length > 1)
                {
                    if (args.Length > 2)
                    {
                        outputFileName = args[2];
                    }
                    if (args[1].ToString().ToLower() == "f")
                    {
                        printFile = true;
                    }
                    if (args[1].ToString().ToLower() == "c")
                    {
                        printConsole = true;
                    }
                }
                if (args[0] == "help")
                {
                    Console.WriteLine("---- f for ouput tokens to file, c for display tokens on console ----");
                    return(0);
                }
                fileName = args[0];
            }
            if (!fileName.Contains(".pxi"))
            {
                if (fileName.Contains("."))
                {
                    Console.WriteLine("Requires file with .pxi extension");
                    return(0);
                }
                fileName = fileName + ".pxi";
            }
            if (!File.Exists(fileName))
            {
                Console.WriteLine("{0} not found.", fileName);
                return(0);
            }


            StreamReader sr = null;

            try
            {
                sr = new StreamReader(fileName);
            }
            catch (Exception)
            {
                Console.WriteLine("could not open file {0}", fileName);
                return(0);
            }

            Scanner.ScanFile(ref sr);
            if (printFile)
            {
                Scanner.PrintTokensFile(outputFileName);
            }
            else if (printConsole)
            {
                Scanner.PrintTokensConsole();
            }
            sr.DiscardBufferedData();
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            sr.BaseStream.Position = 0;
            Scanner.ScanFile(ref sr);
            Parser.ParseTokens();
            sr.DiscardBufferedData();
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            sr.BaseStream.Position = 0;
            ICode.SetFilename(fileName);
            Scanner.ScanFile(ref sr);   //semantic pass
            Parser.ParseTokens();
            ICode.Flush();
            //SymbolTable.printAll();
            return(0);
        }