Ejemplo n.º 1
0
        public bool CheckIfNameExist(string name, out SymbolObject obj)
        {
            var scope = _currentScope;
            obj = null;
            bool result = false;
            bool cont = true;
            do
            {
                var objs = from x in _symbolTable.Values
                           where x.Scope == scope && x.Value == name
                           select x;
                if (objs.Any())
                {
                    obj = objs.First();
                    result = true;
                    cont = false;
                }
                if (scope != "g")
                {
                    scope = scope.Substring(0, scope.LastIndexOf('.'));
                }
                else
                {
                    cont = false;
                }

            } while (cont);
            return result;
        }
Ejemplo n.º 2
0
 public string AddItem(SymbolObject item)
 {
     string symid = "";
     item.Scope = _currentScope;
     if (item.Kind == KindType.Class)
     {
         symid = string.Format("C{0}", _idCounter++);
         //_currentScope += string.Format(".{0}", item.Value);
     }
     else if (item.Kind == KindType.Method)
     {
         symid = string.Format("M{0}", _idCounter++);
         //_currentScope += string.Format(".{0}", item.Value);
     }
     else if (item.Kind == KindType.Ivar)
     {
         symid = string.Format("V{0}", _idCounter++);
     }
     else if (item.Kind == KindType.Param)
     {
         symid = string.Format("P{0}", _idCounter++);
     }
     else
     {
         symid = string.Format("L{0}", _idCounter++);
     }
     item.SymId = symid;
     _symbolTable.Add(symid, item);
     return symid;
 }
Ejemplo n.º 3
0
 public void AddOperator()
 {
     List<Quad> code = _isStatic ? _staticInitICode : Icode;
     var sar1 = _sas.Pop();
     var sar2 = _sas.Pop();
     if (sar1.Type == "int" && sar2.Type == "int")
     {
         Sar temp = new IdentifierSar();
         temp.Value = SymbolTb.CreateTempId();
         temp.Type = "int";
         SymbolObject obj = new SymbolObject { Value = temp.Value, SymId = temp.Value, MemLocation = MemoryLocation.Stack };
         temp.SymObj = obj;
         temp.SymObj.Data.Type = "int";
         _sas.Push(temp);
         SymbolTb.AddTemp(obj);
         string label = "";
         if (_labelStack.Any())
         {
             if (_labelStack.Count > 1)
             {
                 BackPatching();
             }
             label = _labelStack.Pop();
         }
         code.Add(new Quad { Label = label, Instruction = "ADD", OpOne = sar2.SymObj.SymId, OpTwo = sar1.SymObj.SymId, OpThree = temp.SymObj.SymId, Comment = string.Format("{0} + {1}", sar2.Value, sar1.Value) });
     }
     else
     {
         SemanticAssignType(sar1, sar2, _lex.LineCounter());
     }
 }
Ejemplo n.º 4
0
        public SymbolObject LiteralPush(string value, LexemeType type)
        {
            SymbolObject obj = null;
            if (!Getliteral(value, out obj, type))
            {
                string datatype = "";
                switch(type)
                {
                    case LexemeType.Null:
                        datatype = "null";
                        break;
                    case LexemeType.True:
                    case LexemeType.False:
                        datatype = "bool";
                        break;
                    case LexemeType.Character:
                        datatype = "char";
                        break;
                    case LexemeType.Number:
                        datatype = "int";
                        break;

                }
                obj = new SymbolObject { Kind = KindType.Temp, Scope = "g", Value = value, SymId = "Ct" + _idCounter++, Data = new Data { AccessMode = "public", Type = datatype }, MemLocation = MemoryLocation.Global};
                _symbolTable.Add(obj.SymId, obj);
            }
            return obj;
        }
Ejemplo n.º 5
0
 public bool Getliteral(string value, out SymbolObject obj, LexemeType type)
 {
     string datatype = "";
     switch (type)
     {
         case LexemeType.Null:
             datatype = "null";
             break;
         case LexemeType.True:
         case LexemeType.False:
             datatype = "bool";
             break;
         case LexemeType.Character:
             datatype = "char";
             break;
         case LexemeType.Number:
             datatype = "int";
             break;
     }
     var result = _symbolTable.Values.FirstOrDefault(o => o.Value == value && o.Scope == "g" && o.Data.Type == datatype);
     if (result == null)
     {
         obj = null;
         return false;
     }
     obj = result;
     return true;
 }
Ejemplo n.º 6
0
        public void Field_Declaration(SymbolObject obj)
        {
            switch (_lex.GetToken().Type)
            {
                case LexemeType.ArrayBegin:
                case LexemeType.AssignOp:
                case LexemeType.SemiColon:
                    _isStatic = true;
                    obj.Kind = KindType.Ivar;
                    if (_lex.GetToken().Type == LexemeType.ArrayBegin)
                    {
                        _lex.NextToken();
                        obj.Data.Type = string.Format("@:{0}", obj.Data.Type);
                        if (_lex.GetToken().Type != LexemeType.ArrayEnd)
                        {
                            GenError(_lex.GetToken().Type, LexemeType.ArrayEnd, _lex.LineCounter());
                        }
                        _lex.NextToken();
                    }
                    if (!_pass2)
                    {
                        obj.MemLocation = MemoryLocation.Heap;
                        SymbolTb.AddItem(obj);
                    }

                    if (_pass2)
                    {
                        Vpush(obj.Value);
                    }
                    if (!_pass2)
                    {
                        var classSym = SymbolTb.GetClassObject();
                        var objSym = SymbolTb.GetSymbolByName(obj.Value);
                        objSym.OffSet = classSym.SizeOfObject;
                        classSym.SizeOfObject += 4;

                    }
                    if (_lex.GetToken().Type == LexemeType.AssignOp)
                    {
                        if (_pass2)
                        {
                            OPush(_lex.GetToken().Lexeme);
                        }
                        _lex.NextToken();
                        Assignment_Expression();
                    }
                    if (_lex.GetToken().Type != LexemeType.SemiColon)
                    {
                        GenError(_lex.GetToken().Type, LexemeType.SemiColon, _lex.LineCounter());
                    }
                    if (_pass2)
                    {
                        EOE();
                    }
                    _lex.NextToken();
                    _isStatic = false;
                    break;

                case LexemeType.ParentOpen:
                case LexemeType.ParentClose:
                    if (_lex.GetToken().Type != LexemeType.ParentOpen)
                    {
                        GenError(_lex.GetToken().Type, LexemeType.ParentOpen, _lex.LineCounter());
                    }
                    _lex.NextToken();
                    obj.Kind = KindType.Method;
                    obj.Data.ReturnType = obj.Data.Type;
                    if (!_pass2)
                    {
                        SymbolTb.AddItem(obj);
                    }

                    SymbolTb.SetScope(obj.Value);

                    if (_lex.GetToken().Type == LexemeType.Int || _lex.GetToken().Type == LexemeType.Char ||
               _lex.GetToken().Type == LexemeType.Bool || _lex.GetToken().Type == LexemeType.Void ||
               _lex.GetToken().Type == LexemeType.Identifier)
                    {
                        obj.Data.Params = Parameter_List();
                    }
                    if (_lex.GetToken().Type != LexemeType.ParentClose)
                    {
                        GenError(_lex.GetToken().Type, LexemeType.ParentClose, _lex.LineCounter());
                    }
                    _lex.NextToken();
                    if (_pass2)
                    {
                        _labelStack.Push(SymbolTb.GetCurrentMethod().SymId);
                    }
                    Method_body();
                    break;
                default:
                    GenError(_lex.GetToken().Type, LexemeType.SemiColon, _lex.LineCounter());
                    break;
            }
        }
Ejemplo n.º 7
0
        public void EOE()
        {
            List<Quad> code = _isStatic ? _staticInitICode : Icode;
            while (_op.Count > 0)
            {
                var oper = _op.Peek();
                if (oper.Operator == "=")
                {
                    _op.Pop();
                    var sar1 = _sas.Pop();
                    var sar2 = _sas.Pop();

                    if (sar1.Value == "null" && sar2.SymObj.Data.Type != "int" && sar2.SymObj.Data.Type != "char" && sar2.SymObj.Data.Type != "bool")
                    {
                        sar1.Type = sar2.Type;
                    }

                    if (sar1.Type != sar2.Type)
                    {
                        SemanticAssignType(sar2, sar1, _lex.LineCounter());
                    }
                    else if (!(sar2 is IdentifierSar || (sar2 is MemeberReferenceSar && (sar2 as MemeberReferenceSar).MemberType == MemberType.Variable)))
                    {
                        SemanticCantAssign(sar2, _lex.LineCounter());
                    }
                    string label = "";
                    if (_labelStack.Any())
                    {
                        if (_labelStack.Count > 1)
                        {
                            BackPatching();
                        }
                        label = _labelStack.Pop();
                    }
                    if (sar1 is NewSar)
                    {
                        string tempId = SymbolTb.CreateTempId();
                        if (sar1.Type.Contains("@:"))
                        {
                            string tempid = SymbolTb.CreateTempId();
                            SymbolObject tempSymbObj = new SymbolObject { SymId = tempid, Value = tempid, Kind = KindType.Temp, Data = new Data { Type = "int" }, MemLocation = MemoryLocation.Stack };
                            SymbolTb.AddTemp(tempSymbObj);
                            IdentifierSar tempSar = new IdentifierSar { Value = tempid, Type = "int", SymObj = tempSymbObj };
                            string getSize = Size(sar1.Type);
                            var size = SymbolTb.LiteralPush(getSize, LexemeType.Number);
                            code.Add(new Quad { Label = label, Instruction = "MUL", OpOne = size.SymId, OpTwo = (sar1 as NewSar).Index.SymObj.SymId, OpThree = tempSar.SymObj.SymId });
                            label = "";
                            tempid = SymbolTb.CreateTempId();
                            tempSymbObj = new SymbolObject { SymId = tempid, Value = tempid, Kind = KindType.Temp, Data = new Data { Type = sar1.Type }, MemLocation = MemoryLocation.Heap };
                            SymbolTb.AddTemp(tempSymbObj);
                            sar1 = new IdentifierSar { SymObj = tempSymbObj, Type = tempSymbObj.Data.Type, Value = tempid };
                            code.Add(new Quad { Instruction = "NEW", OpOne = tempSar.SymObj.SymId, OpTwo = sar1.SymObj.SymId });
                        }
                        else
                        {
                            SymbolObject tempObj = new SymbolObject { Kind = KindType.Temp, Data = sar1.SymObj.Data, Value = tempId, SymId = tempId, MemLocation = MemoryLocation.Stack };
                            SymbolTb.AddTemp(tempObj);
                            SymbolObject classObj = SymbolTb.GetClassObject(sar2.Type);
                            var tempSar = new IdentifierSar { Value = tempObj.SymId, Type = tempObj.Data.Type, SymObj = tempObj };
                            code.Add(new Quad { Label = label, Instruction = "NEWI", OpOne = classObj.SizeOfObject.ToString(), OpTwo = tempSar.SymObj.SymId });
                            label = "";
                            code.Add(new Quad { Instruction = "FRAME", OpOne = sar1.SymObj.SymId, OpTwo = tempSar.Value });
                            foreach (Sar psar in (sar1 as NewSar).ArgList.ArgList)
                            {
                                code.Add(new Quad { Instruction = "PUSH", OpOne = psar.SymObj.SymId, Comment = "Param: " + psar.Value });
                            }
                            code.Add(new Quad { Instruction = "CALL", OpOne = sar1.SymObj.SymId });
                            code.Add(new Quad { Instruction = "PEEK", OpOne = tempSar.SymObj.SymId, Comment = string.Format("Retun Value: {0}", tempSar.Type) });
                            sar1 = tempSar;
                        }
                    }

                    code.Add(new Quad { Label = label, Instruction = "MOV", OpOne = sar1.SymObj.SymId, OpTwo = sar2.SymObj.SymId, Comment = string.Format("{0} = {1}", sar2.Value, sar1.Value) });
                }
                else
                {
                    Shuntyard();
                }

            }
        }
Ejemplo n.º 8
0
 public string Parameter()
 {
     SymbolObject param = new SymbolObject();
     if (_lex.GetToken().Type != LexemeType.Int && _lex.GetToken().Type != LexemeType.Char &&
         _lex.GetToken().Type != LexemeType.Bool && _lex.GetToken().Type != LexemeType.Void &&
         _lex.GetToken().Type != LexemeType.Identifier)
     {
         GenError(_lex.GetToken().Type, LexemeType.Type, _lex.LineCounter());
     }
     param.Kind = KindType.Param;
     param.Data.Type = _lex.GetToken().Lexeme;
     param.Data.AccessMode = Private;
     param.MemLocation = MemoryLocation.Stack;
     _lex.NextToken();
     if (_lex.GetToken().Type != LexemeType.Identifier)
     {
         GenError(_lex.GetToken().Type, LexemeType.Identifier, _lex.LineCounter());
     }
     param.Value = _lex.GetToken().Lexeme;
     _lex.NextToken();
     if (_lex.GetToken().Type == LexemeType.ArrayBegin)
     {
         _lex.NextToken();
         if (_lex.GetToken().Type != LexemeType.ArrayEnd)
         {
             GenError(_lex.GetToken().Type, LexemeType.ArrayEnd, _lex.LineCounter());
         }
         param.Data.Type = string.Format("@:{0}", param.Data.Type);
     }
     string value = "";
     if (!_pass2)
     {
         value = SymbolTb.AddItem(param);
     }
     return value;
 }
Ejemplo n.º 9
0
        public void Constructor_declaration()
        {
            SymbolObject constructor = new SymbolObject();
            if (_lex.GetToken().Type != LexemeType.Identifier)
            {
                GenError(_lex.GetToken().Type, LexemeType.Identifier, _lex.LineCounter());
            }

            constructor.Value = _lex.GetToken().Lexeme;
            constructor.Kind = KindType.Method;
            constructor.Data.ReturnType = _lex.GetToken().Lexeme;
            constructor.Data.AccessMode = Public;
            if (_pass2)
            {
                CD();
            }

            _lex.NextToken();
            if (!_pass2)
            {
                SymbolTb.AddItem(constructor);
            }

            SymbolTb.SetScope(constructor.Value);
            if (_lex.GetToken().Type != LexemeType.ParentOpen)
            {
                GenError(_lex.GetToken().Type, LexemeType.ParentOpen, _lex.LineCounter());
            }
            _lex.NextToken();

            if (_lex.GetToken().Type == LexemeType.Int || _lex.GetToken().Type == LexemeType.Char ||
                _lex.GetToken().Type == LexemeType.Bool || _lex.GetToken().Type == LexemeType.Void ||
                _lex.GetToken().Type == LexemeType.Identifier)
            {
                constructor.Data.Params = Parameter_List();
            }
            if (_lex.GetToken().Type != LexemeType.ParentClose)
            {
                GenError(_lex.GetToken().Type, LexemeType.ParentClose, _lex.LineCounter());
            }
            _lex.NextToken();
            if (_pass2)
            {
                _labelStack.Push(SymbolTb.GetCurrentMethod().SymId);
            }
            Method_body(true);
            if (_pass2)
            {
                Icode.Remove(Icode.Last());
                Icode.Add(new Quad { Instruction = "RETURN", OpOne = "this" });
            }
        }
Ejemplo n.º 10
0
        public bool Compiliation_Unit()
        {
            bool result = true;
            try
            {
                if (_pass2)
                {
                    SymbolObject mainSym = SymbolTb.GetMainObject();
                    Icode.Add(new Quad { Instruction = "FRAME", OpOne = mainSym.SymId, OpTwo = "this" });
                    Icode.Add(new Quad { Instruction = "CALL", OpOne = mainSym.SymId });
                    Icode.Add(new Quad { Label = "END1", Instruction = "END" });
                }
                while (_lex.GetToken().Type == LexemeType.Class)
                {
                    Class_Declaration();
                }
                SymbolObject main = new SymbolObject();
                if (_lex.GetToken().Type != LexemeType.Void)
                {
                    GenError(_lex.GetToken().Type, LexemeType.Void, _lex.LineCounter());
                }
                _lex.NextToken();
                main.Data.ReturnType = "void";
                main.Data.AccessMode = Public;
                main.Kind = KindType.Method;
                main.Value = "main";
                if (!_pass2)
                {
                    SymbolTb.AddItem(main);
                }

                SymbolTb.SetScope("main");

                if (_lex.GetToken().Type != LexemeType.Main)
                {
                    GenError(_lex.GetToken().Type, LexemeType.Main, _lex.LineCounter());
                }
                _lex.NextToken();

                if (_lex.GetToken().Type != LexemeType.ParentOpen)
                {
                    GenError(_lex.GetToken().Type, LexemeType.ParentOpen, _lex.LineCounter());
                }
                _lex.NextToken();
                if (_lex.GetToken().Type != LexemeType.ParentClose)
                {
                    GenError(_lex.GetToken().Type, LexemeType.ParentClose, _lex.LineCounter());
                }
                _lex.NextToken();
                if (_pass2)
                {
                    _labelStack.Push(SymbolTb.GetMainObject().SymId);
                }
                Method_body();
                if (_lex.GetToken().Type != LexemeType.EOF)
                {
                    GenError(_lex.GetToken().Type, LexemeType.EOF, _lex.LineCounter());
                }
            }
            catch (Exception ex)
            {

                result = false;
                Console.WriteLine(ex.Message);
            }
            return result;
        }
Ejemplo n.º 11
0
 public void Class_Member_Declaration()
 {
     switch (_lex.GetToken().Type)
     {
         case LexemeType.Private:
         case LexemeType.Public:
             SymbolObject sym = new SymbolObject();
             sym.Data.AccessMode = _lex.GetToken().Lexeme;
             _lex.NextToken();
             if (_lex.GetToken().Type != LexemeType.Int && _lex.GetToken().Type != LexemeType.Char &&
                 _lex.GetToken().Type != LexemeType.Bool && _lex.GetToken().Type != LexemeType.Void &&
                 _lex.GetToken().Type != LexemeType.Identifier)
             {
                 GenError(_lex.GetToken().Type, LexemeType.Type, _lex.LineCounter());
             }
             if (_pass2)
             {
                 Tpush();
                 if (!Texist())
                 {
                     SemanticDoesntExist(_lex.GetToken().Lexeme, _lex.LineCounter());
                 }
             }
             sym.Data.Type = _lex.GetToken().Lexeme;
             _lex.NextToken();
             if (_lex.GetToken().Type != LexemeType.Identifier)
             {
                 GenError(_lex.GetToken().Type, LexemeType.Identifier, _lex.LineCounter());
             }
             sym.Value = _lex.GetToken().Lexeme;
             _lex.NextToken();
             Field_Declaration(sym);
             break;
         case LexemeType.Identifier:
             Constructor_declaration();
             break;
     }
 }
Ejemplo n.º 12
0
        public void Class_Declaration()
        {
            SymbolObject sClass = new SymbolObject();
            if (_lex.GetToken().Type != LexemeType.Class)
            {
                GenError(_lex.GetToken().Type, LexemeType.Class, _lex.LineCounter());
            }
            _lex.NextToken();
            sClass.Kind = KindType.Class;
            if (_lex.GetToken().Type != LexemeType.Identifier)
            {
                GenError(_lex.GetToken().Type, LexemeType.Identifier, _lex.LineCounter());
            }
            sClass.Value = _lex.GetToken().Lexeme;
            _lex.NextToken();
            if (!_pass2)
            {
                SymbolTb.AddItem(sClass);
            }
            SymbolTb.SetScope(sClass.Value);
            if (_lex.GetToken().Type != LexemeType.BlockBegin)
            {
                GenError(_lex.GetToken().Type, LexemeType.BlockBegin, _lex.LineCounter());
            }
            _lex.NextToken();
            SymbolObject staticInt = null;
            if (_pass2)
            {
                staticInt = new SymbolObject { Kind = KindType.Method, Value = sClass.Value + "StaticInit", Data = new Data { AccessMode = Private, ReturnType = "void", Type = "void" } };
                SymbolTb.AddItem(staticInt);
            }
            while (_lex.GetToken().Type == LexemeType.Public || _lex.GetToken().Type == LexemeType.Private || _lex.GetToken().Type == LexemeType.Identifier)
            {
                Class_Member_Declaration();
            }
            if (_lex.GetToken().Type != LexemeType.BlockEnd)
            {
                GenError(_lex.GetToken().Type, LexemeType.BlockEnd, _lex.LineCounter());
            }

            if (_pass2)
            {
                staticInt.FrameSize = 8;
                staticInt.SizeOfObject = staticInt.FrameSize;
                var variables = SymbolTb.GetAllVariable();
                foreach (SymbolObject sym in variables)
                {
                    if (sym.Kind == KindType.Temp)
                    {
                        staticInt.SizeOfObject += 4;
                        sym.OffSet = -1 * staticInt.SizeOfObject;
                    }
                }
                _staticInitICode.Insert(0, new Quad { Label = staticInt.SymId, Instruction = "FUNC", OpOne = staticInt.SymId });
                _staticInitICode.Add(new Quad { Instruction = "RTN" });
                Icode.AddRange(_staticInitICode);
                _staticInitICode.Clear();
            }
            _lex.NextToken();
            SymbolTb.LeaveScope();
        }
Ejemplo n.º 13
0
        public void Variable_Declaration()
        {
            SymbolObject dec = new SymbolObject();
            if (_lex.GetToken().Type != LexemeType.Int && _lex.GetToken().Type != LexemeType.Char &&
                _lex.GetToken().Type != LexemeType.Bool && _lex.GetToken().Type != LexemeType.Void &&
                _lex.GetToken().Type != LexemeType.Identifier)
            {
                GenError(_lex.GetToken().Type, LexemeType.Type, _lex.LineCounter());
            }
            if (_pass2)
            {
                Tpush();
                if (!Texist())
                {
                    SemanticDoesntExist(_lex.GetToken().Lexeme, _lex.LineCounter());
                }
            }
            dec.MemLocation = MemoryLocation.Stack;
            dec.Kind = KindType.Lvar;
            dec.Data.AccessMode = Private;
            dec.Data.Type = _lex.GetToken().Lexeme;
            _lex.NextToken();
            if (_lex.GetToken().Type != LexemeType.Identifier)
            {
                GenError(_lex.GetToken().Type, LexemeType.Identifier, _lex.LineCounter());
            }
            dec.Value = _lex.GetToken().Lexeme;
            _lex.NextToken();
            if (_lex.GetToken().Type == LexemeType.ArrayBegin)
            {
                _lex.NextToken();
                if (_lex.GetToken().Type != LexemeType.ArrayEnd)
                {
                    GenError(_lex.GetToken().Type, LexemeType.ArrayEnd, _lex.LineCounter());
                }
                _lex.NextToken();
                dec.Data.Type = string.Format("@:{0}", dec.Data.Type);
            }
            if (_pass2)
            {
                Vpush(dec.Value);
            }

            if (_lex.GetToken().Type == LexemeType.AssignOp)
            {
                if (_pass2)
                {
                    OPush(_lex.GetToken().Lexeme);
                }
                _lex.NextToken();
                Assignment_Expression();
            }
            if (_lex.GetToken().Type != LexemeType.SemiColon)
            {
                GenError(_lex.GetToken().Type, LexemeType.SemiColon, _lex.LineCounter());
            }
            _lex.NextToken();

            if (_pass2)
            {
                EOE();
            }

            if (!_pass2)
            {
                SymbolTb.AddItem(dec);
            }
        }
Ejemplo n.º 14
0
        public bool RExist()
        {
            List<Quad> code = _isStatic ? _staticInitICode : Icode;
            var member = _sas.Pop();
            var holder = _sas.Pop();
            bool result = false;
            if (SymbolTb.CheckIfNameIsPartOfMember(member, holder.SymObj.Data.Type))
            {
                result = true;
                string symId = SymbolTb.CreateTempId();
                var symObj = new SymbolObject { Scope = member.SymObj.Scope, Data = member.SymObj.Data, Kind = member.SymObj.Kind, SymId = symId, Value = symId, MemLocation = MemoryLocation.Heap };
                SymbolTb.AddTemp(symObj);
                MemeberReferenceSar mrSar = new MemeberReferenceSar { Value = symId, SymObj = symObj, Type = symObj.Data.Type };
                string label = "";
                if (_labelStack.Any())
                {
                    if (_labelStack.Count > 1)
                    {
                        BackPatching();
                    }
                    label = _labelStack.Pop();
                }
                if (mrSar.SymObj.Kind == KindType.Method)
                {
                    CheckFunction((FuncSar)member);
                    mrSar.FunctionParams = (FuncSar)member;
                    mrSar.MemberType = MemberType.Function;
                    code.Add(new Quad { Label = label, Instruction = "FRAME", OpOne = member.SymObj.SymId, OpTwo = holder.SymObj.SymId, Comment = string.Format("{0}.{1}", holder.Value, member.Value) });
                    foreach (Sar sar in mrSar.FunctionParams.ArgList.ArgList)
                    {
                        code.Add(new Quad { Instruction = "PUSH", OpOne = sar.SymObj.SymId, Comment = "Param: " + sar.Value });
                    }
                    code.Add(new Quad { Instruction = "CALL", OpOne = member.SymObj.SymId, Comment = string.Format("{0}.{1}", holder.Value, member.Value) });
                    code.Add(new Quad { Instruction = "PEEK", OpOne = mrSar.SymObj.SymId, Comment = string.Format("Retun Value: {0}", mrSar.Type) });
                }
                else if (member is ArrSar)
                {
                    //mrSar.Type = mrSar.Type.Replace("@:", "");
                    string tempId = SymbolTb.CreateTempId();
                    SymbolObject tempObj = new SymbolObject { Kind = KindType.Temp, Data = mrSar.SymObj.Data.Copy(), Value = tempId, SymId = tempId, MemLocation = MemoryLocation.Heap };
                    tempObj.Data.Type = tempObj.Data.Type.Replace("@:", "");
                    SymbolTb.AddTemp(tempObj);
                    code.Add(new Quad { Label = label, Instruction = "REF", OpOne = holder.SymObj.SymId, OpTwo = member.SymObj.SymId, OpThree = mrSar.SymObj.SymId, Comment = string.Format("{0}.{1}", holder.Value, member.Value) });
                    var temp = new MemeberReferenceSar { Value = tempId, Type = tempObj.Data.Type, SymObj = tempObj, TempVar = mrSar };
                    code.Add(new Quad { Instruction = "AEF", OpOne = mrSar.SymObj.SymId, OpTwo = ((ArrSar)member).Index.SymObj.SymId, OpThree = temp.SymObj.SymId, Comment = string.Format("{0}[{1}]", mrSar.Value, ((ArrSar)member).Index.Value) });
                    mrSar = temp;
                }
                else
                {
                    mrSar.SymObj.MemLocation = MemoryLocation.Heap;
                    code.Add(new Quad { Label = label, Instruction = "REF", OpOne = holder.SymObj.SymId, OpTwo = member.SymObj.SymId, OpThree = mrSar.SymObj.SymId, Comment = string.Format("{0}.{1}", holder.Value, member.Value) });
                }

                _sas.Push(mrSar);
            }
            else
            {
                SemanticNotaMember(member, holder);
            }
            return result;
        }
Ejemplo n.º 15
0
 public void AddTemp(SymbolObject obj)
 {
     obj.Scope = _currentScope;
     _symbolTable.Add(obj.SymId, obj);
 }
Ejemplo n.º 16
0
 public void CreateTempForShunt(string type)
 {
     var sar1 = _sas.Pop();
     var sar2 = _sas.Pop();
     if (sar1.Type == "int" && sar2.Type == "int")
     {
         Sar temp = new IdentifierSar();
         temp.Value = SymbolTb.CreateTempId();
         temp.Type = type;
         SymbolObject obj = new SymbolObject { Value = temp.Value, SymId = temp.Value };
         temp.SymObj = obj;
         temp.SymObj.Data.Type = type;
         _sas.Push(temp);
         SymbolTb.AddTemp(obj);
     }
     else
     {
         SemanticAssignType(sar1, sar2, _lex.LineCounter());
     }
 }
Ejemplo n.º 17
0
        public bool IExist()
        {
            List<Quad> code = _isStatic ? _staticInitICode : Icode;
            bool result = false;
            var sar = _sas.Pop();
            SymbolObject obj = null;
            if (SymbolTb.CheckIfNameExist(sar.Value, out obj))
            {
                sar.Type = obj.Data.Type;
                sar.SymObj = obj;
                string label = "";

                if (obj.Kind == KindType.Method)
                {
                    if (_labelStack.Any())
                    {
                        if (_labelStack.Count > 1)
                        {
                            BackPatching();
                        }
                        label = _labelStack.Pop();
                    }
                    CheckFunction((FuncSar)sar);
                    string tempId = SymbolTb.CreateTempId();
                    SymbolObject tempObj = new SymbolObject { Kind = KindType.Temp, Data = sar.SymObj.Data, Value = tempId, SymId = tempId, MemLocation = MemoryLocation.Stack };
                    SymbolTb.AddTemp(tempObj);
                    var newsar = new IdentifierSar { FunctionParams = (FuncSar)sar, MemberType = MemberType.Function, SymObj = tempObj, Value = sar.Value, Type = sar.Type };
                    sar = newsar;
                    code.Add(new Quad { Label = label, Instruction = "FRAME", OpOne = newsar.FunctionParams.SymObj.SymId, OpTwo = "this", Comment = string.Format("{0}.{1}", "this", newsar.Value) });
                    foreach (Sar psar in newsar.FunctionParams.ArgList.ArgList)
                    {
                        code.Add(new Quad { Instruction = "PUSH", OpOne = psar.SymObj.SymId, Comment = "Param: " + sar.Value });
                    }
                    code.Add(new Quad { Instruction = "CALL", OpOne = newsar.FunctionParams.SymObj.SymId, Comment = string.Format("{0}.{1}", "this", newsar.Value) });
                    code.Add(new Quad { Instruction = "PEEK", OpOne = newsar.SymObj.SymId, Comment = string.Format("Retun Value: {0}", newsar.Type) });
                }
                else if (sar is ArrSar)
                {
                    if (_labelStack.Any())
                    {
                        if (_labelStack.Count > 1)
                        {
                            BackPatching();
                        }
                        label = _labelStack.Pop();
                    }
                    string tempId = SymbolTb.CreateTempId();
                    SymbolObject tempObj = new SymbolObject { Kind = KindType.Temp, Data = sar.SymObj.Data.Copy(), Value = tempId, SymId = tempId, MemLocation = MemoryLocation.Heap };
                    tempObj.Data.Type = tempObj.Data.Type.Replace("@:", "");
                    SymbolTb.AddTemp(tempObj);
                    var newsar = new IdentifierSar { Value = tempId, Type = tempObj.Data.Type, SymObj = tempObj, TempVar = sar };
                    code.Add(new Quad { Label = label, Instruction = "AEF", OpOne = sar.SymObj.SymId, OpTwo = ((ArrSar)sar).Index.SymObj.SymId, OpThree = newsar.SymObj.SymId, Comment = string.Format("{0}[{1}]", sar.Value, ((ArrSar)sar).Index.Value) });
                    sar = newsar;
                }

                result = true;
            }
            _sas.Push(sar);
            return result;
        }