コード例 #1
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        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
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        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);
        }
コード例 #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);
        }
コード例 #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());
        }
コード例 #5
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        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);
        }
コード例 #6
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
 private static bool character_literal()
 {
     if (Tokens.GetToken().type == Token.Type.Character)
     {
         string   value = Tokens.GetToken().lexeme.Replace("\'", "");
         string[] data  = new string[2];
         data[0] = "returnType:char";
         data[1] = "accessMod:public";
         Symbol symbol = new Symbol("g", ("H" + value.Trim()), "CHAR_" + value, "c**t", data);
         SymbolTable.Add(symbol);
         if (semanticPass)
         {
             SemanticActions.lPush(symbol, Tokens.GetToken(), scope);
         }
         return(true);
     }
     return(false);
 }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
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);
 }
コード例 #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);
        }
コード例 #11
0
        public static void Start(ref List <string> iCode, string filename)
        {
            int    labelOffset = 0;
            string operand1    = "";
            string operand2    = "";
            string operand3    = "";
            string instruction;

            string[] line;
            string[] comments;
            string[] data = new string[2];
            data[0] = "returnType:int";
            data[1] = "accessMod:public";
            string[] dataChar = new string[2];
            dataChar[0] = "returnType:char";
            dataChar[1] = "accessMod:public";
            SymbolTable.Add(new Symbol("g", TRUE, "1", "ilit", data));
            SymbolTable.Add(new Symbol("g", FALSE, "0", "ilit", data));
            SymbolTable.Add(new Symbol("g", "N4", "4", "ilit", data));
            SymbolTable.Add(new Symbol("g", "N-1", "-1", "ilit", data));
            SymbolTable.Add(new Symbol("g", "H!", "!", "c**t", dataChar));
            SymbolTable.Add(new Symbol("g", "FREE", "0", "ilit", data));
            filename = filename.Split('.')[0] + ".asm";
            sw       = new StreamWriter(filename, false);
            List <Symbol> globalVars = SymbolTable.GetSymbolsInScope("g");
            Symbol        var;

            pc = 0;
            for (int i = 0; i < globalVars.Count; ++i)//print all global variables at top of assembly file
            {
                var = globalVars[i];
                if (var.symid[0] == 'H')
                {
                    if (var.symid.Length == 1)
                    {
                        sw.WriteLine(var.symid + " .BYT  ");
                    }
                    else
                    {
                        sw.WriteLine(var.symid + " .BYT " + var.symid.Substring(1, (var.symid.Length - 1)));
                    }
                    ++pc;
                }
                else if (var.symid[0] == 'N' || var.symid[0] == 'B' || var.symid == "null")
                {
                    sw.WriteLine(var.symid + " .INT " + var.value);
                    pc += 4;
                }
            }
            sw.WriteLine(FREE + " .INT 0");//address of available heap
            pc += 4;
            sw.WriteLine(";------------------------END OF DATA SEGMENT-----------------------");
            LDR(registers[(int)Register.True], TRUE);
            LDR(registers[(int)Register.False], FALSE);
            LDA(registers[(int)Register.OP1], END);                            //load address of final instruction in code segment
            ADI(registers[(int)Register.OP1], (3 * INSTRUCT_SIZE).ToString()); //get address of first free heap location
            STR(registers[(int)Register.OP1], FREE);                           //update free heap location
            MOV(registers[(int)Register.OP1], registers[(int)Register.FP]);
            ADI(registers[(int)Register.OP1], (-1 * VAR_SIZE).ToString());
            STR(registers[(int)Register.FP], registers[(int)Register.OP1]);//set fp to pfp (bottom of stack)

            _iCode = iCode;
            foreach (string s in _iCode)
            {
                bufferedLine = "";
                labelOffset  = 0;
                operand1     = "";
                operand2     = "";
                operand3     = "";
                comments     = s.Split(';');
                line         = comments[0].Trim().Split(' ');
                comment      = COMMENT_FORMAT + comments[0];
                for (int i = 1; i < comments.Length; ++i)
                {
                    comment += comments[i];
                }

                if (!_instructions.Contains <string>(line[0]))
                {
                    //label
                    labelOffset  = 1;
                    instruction  = line[1];
                    bufferedLine = line[0] + " ";
                }
                else
                {
                    labelOffset = 0;
                    instruction = line[0];
                }
                if (line.Length > labelOffset + 1)//contains 1 or more operands
                {
                    operand1 = line[labelOffset + 1];
                }
                if (instruction == "FUNC")
                {
                    operand2 = operand1;
                }
                else
                {
                    if (line.Length > labelOffset + 2)//contains 2 or more operands
                    {
                        operand2 = line[labelOffset + 2];
                    }
                    if (line.Length > labelOffset + 3)//contains 2 or more operands
                    {
                        operand3 = line[labelOffset + 3];
                    }
                }
                InstructionCall(instruction, operand1, operand2, operand3);
            }
            sw.WriteLine(OVERFLOW + " LDB R3 H!");
            pc += 12;
            sw.WriteLine("TRP 3");
            pc += 12;
            sw.WriteLine(UNDERFLOW + " LDB R3 H!");
            pc += 12;
            sw.WriteLine("TRP 3");
            pc += 12;
            sw.Write(END + " TRP 0");
            sw.Flush();
            sw.Close();
        }
コード例 #12
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;
        }
コード例 #13
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);
 }
コード例 #14
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        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);
        }
コード例 #15
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);
        }
コード例 #16
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);
        }
コード例 #17
0
ファイル: Parser.cs プロジェクト: Metallicruz/CompVM
        private static bool Expression()
        {
            string lexeme = Tokens.GetToken().lexeme;

            if (lexeme == "(")
            {
                if (semanticPass)
                {
                    SemanticActions.oPush(Tokens.GetToken());
                }
                Tokens.NextToken();
                if (!Expression())
                {
                    SyntaxError(Tokens.GetToken(), "expression");
                }
                if (Tokens.GetToken().lexeme != ")")
                {
                    SyntaxError(Tokens.GetToken(), ")");
                }
                if (semanticPass)
                {
                    SemanticActions.ShuntYardAll();
                }
                Tokens.NextToken();
                Expressionz();
                return(true);
            }
            else if (lexeme == "+" || lexeme == "-" || lexeme == "true" || lexeme == "false" ||
                     lexeme == "null" || lexeme == "this" || Tokens.GetToken().type == Token.Type.Number ||
                     Tokens.GetToken().type == Token.Type.Character)
            {
                if (Tokens.GetToken().lexeme == "this")
                {
                    if (semanticPass)
                    {
                        SemanticActions.iPush(Tokens.GetToken(), scope);
                        SemanticActions.iExist();
                    }
                    Tokens.NextToken();
                    Member_Refz();
                    Expressionz();
                    return(true);
                }
                else if (lexeme == "true" || lexeme == "false" || lexeme == "null")
                {
                    string[] data = new string[2];
                    data[0] = "returnType:bool";
                    data[1] = "accessMod:public";
                    Symbol symbol;
                    if (lexeme == "true")
                    {
                        symbol = new Symbol("g", ("Btrue"), "1", "blit", data);
                    }
                    else if (lexeme == "false")
                    {
                        symbol = new Symbol("g", ("Bfalse"), "0", "blit", data);
                    }
                    else
                    {
                        data[0] = "returnType:null";
                        symbol  = new Symbol("g", "null", "2018", "null", data);
                    }
                    SymbolTable.Add(symbol);
                    if (semanticPass)
                    {
                        SemanticActions.lPush(symbol, Tokens.GetToken(), scope);
                    }
                    Tokens.NextToken();
                    Expressionz();
                    return(true);
                }
                else if (numeric_literal())
                {
                    Tokens.NextToken();
                    Expressionz();
                    return(true);
                }
                else if (character_literal())
                {
                    Tokens.NextToken();
                    Expressionz();
                    return(true);
                }
                return(false);
            }
            else if (Tokens.GetToken().type == Token.Type.Identifier)
            {
                currentIdentifier = Tokens.GetToken().lexeme;
                if (semanticPass)
                {
                    SemanticActions.iPush(Tokens.GetToken(), scope);
                }
                Tokens.NextToken();
                Fn_Arr_Member();
                if (semanticPass)
                {
                    SemanticActions.iExist();
                }
                Member_Refz();
                Expressionz();
                return(true);
            }
            return(false);
        }