public string GetATempVarName(Record r, string scope) { if (r == null || r.LinkedSymbol == null || r.LinkedSymbol.Scope == null || r.LinkedSymbol.Scope.Length == 0) { throw new Exception("Intercode Error: Trying to make a temp variable, but there's no scope to add it to"); } var name = "_atmp" + _tempVarNames.Count; _tempVarNames.Push(name); symbolTable.Add(name, new Symbol { Scope = scope, Data = r.LinkedSymbol.Data, Kind = "atemp", SymId = name, Value = name }); return(name); }
public void WriteNewObj(Record r1, Record r2, string scope) { var newTemp = GetTempVarName(r1, scope); WriteQuad("", "NEWI", "" + r2.LinkedSymbol.SymId, newTemp, "", "newobj"); WriteQuad("", "FRAME", r1.LinkedSymbol.SymId, newTemp, "", "newobj"); if (r1.ArgumentList != null && r1.ArgumentList.Count > 0) { foreach (var a in r1.ArgumentList.Reverse()) { WriteQuad("", "PUSH", ToOperand(a), "", "", "function"); } } WriteQuad("", "CALL", r1.LinkedSymbol.SymId, "", "", "newobj"); WriteQuad("", "PEEK", r1.TempVariable.ToString(), "", "", "function"); }
public void WriteReturn(string type, Record r, bool returnThis) { if (returnThis) { WriteQuad("", "RETURN", "this", "", "", "return"); } else { if (type.Equals("void") || r == null) { WriteQuad("", "RTN", "", "", "", "return"); } else { WriteQuad("", "RETURN", ToOperand(r), "", "", "return"); } } }
public void WriteOperation(Operator nextOp, Record r1, Record r2, Record r3 = null) { var operation = OpMap[nextOp]; var operand1 = ToOperand(r1); if (r2.Type == Semanter.RecordType.IVar) { Console.WriteLine("Not yet implemented"); return; //this is to weed out static initializers } var operand2 = ToOperand(r2); var operand3 = ToOperand(r3); if (nextOp == Operator.Assignment) { WriteQuad("", operation, operand1, operand2, null, "operation"); } else { WriteQuad("", operation, operand1, operand2, operand3, "operation"); } }
public string GetTempVarName(Record r, string scope = "") { if (r == null || r.LinkedSymbol == null || r.LinkedSymbol.Scope == null || r.LinkedSymbol.Scope.Length == 0) { throw new Exception("Intercode Error: Trying to make a temp variable, but there's no scope to add it to"); } var name = "_tmp" + _tempVarNames.Count; _tempVarNames.Push(name); if (name.Equals("_tmp15")) { var j = 3; } var newSymbol = new Symbol { Scope = r.LinkedSymbol.Scope, Data = r.LinkedSymbol.Data, Kind = "temp", SymId = name, Value = name }; symbolTable.Add(name, newSymbol); if (scope.Length != 0 && r.Type == Semanter.RecordType.Func && r.LinkedSymbol != null && r.LinkedSymbol.Kind.Equals("method") && !newSymbol.Scope.EndsWith(r.LinkedSymbol.Value)) { newSymbol.Scope = scope; } if (scope.Length != 0 && r.Type == Semanter.RecordType.New && r.LinkedSymbol != null && r.LinkedSymbol.Kind.Equals("Constructor")) { newSymbol.Scope = scope; } if (scope.Length != 0 && r.Type == Semanter.RecordType.Reference && r.LinkedSymbol != null && r.LinkedSymbol.Kind.Equals("ivar")) { newSymbol.Scope = scope; } return(name); }
public void WriteArray(Record r1, Record r2, Record r3) { WriteQuad("", "AEF", ToOperand(r2), ToOperand(r1), ToOperand(r3, true), "array"); }
public void WriteCin(Record r) { WriteQuad("", "READ", r.LinkedSymbol.Data.Type.Equals("int") ? "1" : "2", ToOperand(r), "", "return"); }
public void WriteMiddleWhile(Record expressionSar) { var op1 = GetExpressionOp(expressionSar); WriteQuad("", "BF", op1, GetLabelName("EndWhile"), "", "while"); }
public void WriteIf(Record expressionSar) { var op1 = GetExpressionOp(expressionSar); WriteQuad("", "BF", op1, GetLabelName("SkipIf"), "", "if"); }
private string ToOperand(Record r, bool atempIsPointer = false) { if (r == null) { return(""); } if (r.LinkedSymbol != null) { if (r.LinkedSymbol.Kind != null) { if (r.LinkedSymbol.Kind.Equals("literal")) { return(r.LinkedSymbol.SymId); } switch (r.Type) { case Semanter.RecordType.New: case Semanter.RecordType.NewArray: return(r.TempVariable.ToString()); break; case Semanter.RecordType.ArrayElement: return(atempIsPointer ? r.TempVariable.ToString() : r.TempVariable.ToString() + "_value"); break; case Semanter.RecordType.LVar: case Semanter.RecordType.Identifier: case Semanter.RecordType.Reference: if (r.LinkedSymbol.Kind.ToLower().Equals("method") && r.TempVariable != null && r.TempVariable.ToString().Length != 0) { return(r.TempVariable.ToString()); } if (r.LinkedSymbol.Kind.ToLower().Equals("ivar") && r.TempVariable != null && r.TempVariable.ToString().Length != 0) { if (!r.TempVariable.ToString().StartsWith("_atmp")) { return(r.TempVariable.ToString()); } else { return(atempIsPointer ? r.TempVariable.ToString() : r.TempVariable.ToString() + "_value"); } } return(r.LinkedSymbol.SymId); break; default: throw new Exception("In ToOperand(), trying to convert a non-supported recordtype"); } } } if (r.Type == Semanter.RecordType.NewArray) { return(r.TempVariable.ToString()); } if (r.Type == Semanter.RecordType.ArrayElement) { return(atempIsPointer ? r.TempVariable.ToString() : r.TempVariable.ToString() + "_value"); } return(r.Value); }