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()); } }
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(); } } }
public void Vpush(string value) { IdentifierSar sar = new IdentifierSar { Value = value }; if (SymbolTb.CheckIfMultipleNameExist(sar.Value)) { SemanticMultipleSameName(sar); } SymbolTb.CheckIfNameExist(sar); sar.Type = sar.SymObj.Data.Type; _sas.Push(sar); }
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()); } }
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; }