
نمایش گرافیکی Call Stack (پشته فراخوانی) با #C
در این پست برنامه ای را قرار می دهم که می خواهد یک فایل زبان سی یا سی پلاس پلاس را با فرمت c. یا cpp. بگیرد و پشته ی فراخوانی (CallStack) آن را شبیه سازی کند و آن را به صورت گرافیکی و غیر گرافیکی نمایش دهد.
قبل از توضیح کد و قرار دادن آن بگذارید مفهموم کال استک یا پشته ی فراخوانی یا Call Stack را بگوییم.
اکثر کامپایلرها برای فراخوانی و برگشت زیربرنامه فراخوانی پشته (call stack) را پیادهسازی میکنند. Call stack یا run-time stack یک پشته است که اطلاعاتی درباره زیربرنامه فعال یک برنامه را نگهداری میکند. زیربرنامه فعال زیربرنامهای است که فراخوانی شدهاست اما هنوز اجرایش تمام نشدهاست.
وقتی زیربرنامهای فراخوانی میشود، قبل از اینکه کنترل اجرای برنامه به آدرس زیربرنامه پرش کند آدرس دستورالعمل بعدی (دستورالعملی که درحافظه بعد از دستور فراخوانی قرار دارد) درجایی باید ذخیره شود که هنگام برگشت از زیربرنامه از آن استفاده میشود. این آدرس را آدرس برگشتی (return addresses) مینامند.
معماری که بر اساس پشته است آدرس برگشتی را به عنوان نقطه برگشت در پشته اضافه میشود. هر بار که زیربرنامهای فراخوانی میشود آدرس برگشتی در پشته push میشود. هنگام برگشت از زیربرنامه آدرس برگشتی از پشته pop شده و کنترل برنامه به آن آدرس پرش میکند و اجرای برنامه از بعد از دستور فراخوانی ادامه پیدا میکند.
به دلیل استفاده از پشته یک زیربرنامه میتواند خودش یا زیربرنامههای دیگر را صدا بزند.
یکی از معروف ترین شکل های Call Stack به صورت تصویر زیر است که ابتدا آرگومان های تابع وارد استک می شود بعد از آن آدرس جایی که تابع از آن فراخوانی شده و بعد از آن متغییر های محلی در استک push می شود.
فرض کنید Fun1 , Fun2 دو تابع اند و کدشان شبیه این کد است:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> using namespace std; void Fun1() { Fun2(); Do something } void Fun2() { Do something } int main() { Fun1(); return 0; } |
شکل call stack (پشته فراخوانی) این می شود :
برنامه ای که من نوشتم با زبان سی شارپ (#C) از شش Class تشکیل شده است که به ترتیب همه را با جزییات در زیر توضیح می دهم :
کلاس datatype.cs :
ما برای آن که توابع و متغییر های محلی را تشخیص دهیم باید همه ی گونه های داده ها (Data type) را در برنامه شناسایی کنیم .
ما Data Type هایی رو که شناسایی می کنیم در لیست DataTypeOfCppFile
میریزیم که در قطعه کد datatype.cs که در پایین اومده مشاهده می کنید.
یکی از متد های این کلاس تابع سازنده ی datatype است که در آن Data type های سی پلاس پلاس و سی به لیست اضافه می شود .
متد دیگر در کلاس datatype.cs متد find_datatype است که وظیفه ی پیدا کردن سایر Data Type ها رو داره .این تابع به دنبال کلاس هایی که برنامه نویس در فایل برنامه تعریف کرده است می گردد و این گونه نوع های جدید را پیدا می کند، در آخر این متد دیتا تایپ های اشاره گر و رفرنس نیز ساخته می شود (به دیتا تایپ ها کشف شده و موجود “*” “* ” “&” “& ” اضافه می شود و به لیست اضافه می شود ، البته از برهان خلف نیز می توان یک جورایی به متغییر های ناشناخته که کلاسشان درون فایل نیست دست یافت).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace call_stack { //this class was created for maintance datatype that use in program public class datatype { //there are all data type of program in this list public List<string> DataTypeOfCppFile; //Data type of c++ public datatype() { DataTypeOfCppFile = new List<string>(); DataTypeOfCppFile.Add("signed"); DataTypeOfCppFile.Add("int"); DataTypeOfCppFile.Add("char"); DataTypeOfCppFile.Add("short "); DataTypeOfCppFile.Add("bool"); DataTypeOfCppFile.Add("long"); DataTypeOfCppFile.Add("enum"); DataTypeOfCppFile.Add("float"); DataTypeOfCppFile.Add("double"); DataTypeOfCppFile.Add("string"); DataTypeOfCppFile.Add("void"); } //this method find new type that make by programmer. public void find_datatype() { int position_of_str; string temp; string temp1 = " "; var form = Form.ActiveForm as Form1; StreamReader fp = new StreamReader(form.textBox2.Text); if (fp != null) { while ((temp = fp.ReadLine()) != null) { temp1 = ""; position_of_str = temp.IndexOf("class "); if (position_of_str != -1) { for (int i = position_of_str + 6; i < temp.Length && temp[i] != ' ' && temp[i] != '{' && temp[i] != ';'; i++) { temp1 += temp[i]; } for (int i = 0; i < DataTypeOfCppFile.Count; i++) { if (DataTypeOfCppFile[i] == temp1) { break; } if (i == DataTypeOfCppFile.Count - 1) { DataTypeOfCppFile.Add(temp1); } } } } } else { form.textBox1.Text += "Error rn Can't open File!"; } //add pointer datatype to program. int k = DataTypeOfCppFile.Count; for (int i = 0; i < k; i++) { DataTypeOfCppFile.Add(DataTypeOfCppFile[i] + '*'); DataTypeOfCppFile.Add(DataTypeOfCppFile[i] +' '+'*'); DataTypeOfCppFile.Add(DataTypeOfCppFile[i] + '&'); DataTypeOfCppFile.Add(DataTypeOfCppFile[i] +' ' +'&'); } } } } |
کلاس function.cs :
این کلاس دارای ویژگی های arquments ، local_variable ،Kind_local_variable,start_position،number_of_line است که start_position شماره خطی است که تعریف تابع از آن شروع شده است.
number_of_line تعداد خط های تابع است.
arquments لیستی از string است که آرگومان های تابع در آن قرار می گیرد.
local_variable لیستی از string است که اسم متغییر های محلی تابع در آن قرار می گیرد.
Kind_local_variable لیستی از string است که نوع متغییرهای محلی کشف شده در آن قرار می گیرد.
این کلاس دارای متد های function , find_function است .
در این کلاس دو تا متد سازنده مختلف به نام function داریم .
find_function مهم ترین تابع این برنامه است که وظیفه شناسایی تابع ، نام تابع ، آرگومان های تابع ،بدنه تابع ، تعداد خط های تابع و متغییر های محلی تابع را دارد .که تعداد زیادی حالت تابع بودن و یا نبود و شناسایی سر و ته تابع با استک و شناسایی متغییر ها و آرگومان ها با حالت های مختلف و روش های مختلف را انجام می دهد.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Drawing; namespace call_stack { public class function { public string name; public List<string> arquments; public List<string> local_variable; public List<string> Kind_local_variable; public int start_position; public int number_of_line; public function(function temp) { name = ""; arquments = new List<string>(); local_variable = new List<string>(); Kind_local_variable = new List<string>(); start_position = 0; number_of_line = 0; name = temp.name; start_position = temp.start_position; number_of_line = temp.number_of_line; for (int i = 0; i < temp.arquments.Count; i++) { arquments.Add(temp.arquments[i]); } for (int i = 0; i < temp.local_variable.Count; i++) { local_variable.Add(temp.local_variable[i]); } for (int i = 0; i < temp.Kind_local_variable.Count; i++) { Kind_local_variable.Add(temp.Kind_local_variable[i]); } } public function() { name = ""; arquments = new List<string>(); local_variable = new List<string>(); Kind_local_variable = new List<string>(); start_position = 0; number_of_line = 0; } //this function find function with local variable and argument that programmer build. public int find_function(int LineOfFile) { int p = -1; int q; int l1; int l2; int l3 = 0; int l4; int index = -1; int indextemp = -1; int indextemp1 = -1; bool acsesstemp = false; bool stack_check = false; string temp;//string was readed from file from special line. string temp1 = ""; string temp2 = ""; Point brace_pointer = new Point(); Stack<Point> Stack = new Stack<Point>(); name = ""; for (int i = 0; i < arquments.Count; i++) { arquments.RemoveAt(i); } for (int i = 0; i < local_variable.Count; i++) { local_variable.RemoveAt(i); } for (int i = 0; i < Kind_local_variable.Count; i++) { Kind_local_variable.RemoveAt(i); } var form = Form.ActiveForm as Form1; temp = File.ReadLines(form.textBox2.Text).Skip(LineOfFile).Take(1).First(); if (temp.Length == 0) { return 0; } if (temp.Last() == ';') { return 0; } indextemp = temp.IndexOf("//"); indextemp1 = temp.IndexOf(";"); acsesstemp = false; if (indextemp != -1 && indextemp1 != -1 && indextemp1 < indextemp) { if (indextemp - indextemp1 == 1 ) { return 0; } for (int i = indextemp1+1; i < indextemp; i++) { if (temp[i] != ' ') { acsesstemp = true; } } if (acsesstemp == false) { return 0; } } acsesstemp = false; if (indextemp1 != -1 && indextemp1 < temp.Length) { for (int i = indextemp1+1; i < temp.Length; i++) { if (temp[i] != ' ' ) { acsesstemp = true; } } if (acsesstemp == false) { return 0; } } index = temp.IndexOf("class"); if (index != -1) { if (index+5 <temp.Length && temp[index+5]==' ') { return 0; } } //this part of program find name of function p = -1; index = -1; indextemp = -1; indextemp1 = -1; for (int j = form.data_type.DataTypeOfCppFile.Count-1; j >= 0; j--) { index = temp.IndexOf(form.data_type.DataTypeOfCppFile[j]); indextemp = temp.IndexOf("("); indextemp1 = temp.IndexOf("<"); if (index == -1) { continue; } if (indextemp != -1 && (indextemp == index + form.data_type.DataTypeOfCppFile[j].Length || indextemp - 1 == index + form.data_type.DataTypeOfCppFile[j].Length)) { continue; } if (indextemp1 != -1 && ((indextemp1 + 1 == index) || (indextemp1 + 2 == index))) { continue; } if (index == 0) { p = j; break; } if(index > 0) { l3 = 0; for (int z = 0; z < index; z++) { if (temp[z] != ' ') { l3 = 1; } } } if (l3 == 0) { p = j; break; } } if (p == -1) { return 0; } // if (form.data_type.DataTypeOfCppFile[p].Length + index + 1 < temp.Length && temp[form.data_type.DataTypeOfCppFile[p].Length + index] == ' ') { index++; } for (q = form.data_type.DataTypeOfCppFile[p].Length + index; q < temp.Length && temp[q] != '('; q++) { temp1 += temp[q]; } // if (q+1<temp.Length && temp[q] == ' ') { q++; } if (q<temp.Length && temp[q] != '(') { return 0; } temp2 = ""; index = temp1.IndexOf("::"); if (index != -1) { for (int i = index+2; i < temp1.Length; i++) { temp2 += temp1[i]; } temp1 = temp2; } name = temp1;//set name of function. start_position = LineOfFile;//determine position that file start from it. temp1 = ""; for (int i = q+1; i < temp.Length && temp[i] != ')'; i++) { if (temp[i] != ',') { temp1 += temp[i]; } else { arquments.Add(temp1);//set arqument of function temp1 = ""; } } arquments.Add(temp1);//set arqument of function temp1 = ""; l2 = LineOfFile-1; stack_check = false; for (int i = LineOfFile; i < File.ReadLines(form.textBox2.Text).Count(); i++) { temp = File.ReadLines(form.textBox2.Text).Skip(i).Take(1).First(); l2++; if (temp.Length == 0) { number_of_line = l2 - start_position;//in this part of program number of file line set. continue; } if(stack_check == true && Stack.Count == 0) { number_of_line = l2 - start_position;//in this part of program number of file line set. break; } for (int j = form.data_type.DataTypeOfCppFile.Count-1; j >= 0; j--) { p = temp.IndexOf(form.data_type.DataTypeOfCppFile[j]); // if (p == -1) { continue; } l1 = temp.IndexOf("//"); if (l1 < p && l1 != -1) { continue; } l1 = temp.IndexOf("""); if (l1 < p && l1 != -1) { continue; } l1 = temp.IndexOf("("); if (l1 != -1 && (p-1==l1 || p-2==l1)) { continue; } l1 = temp.IndexOf(","); if (l1 != -1 && (p - 1 == l1 || p - 2 == l1 || p - 3 == l1)) { continue; } temp1 = ""; if (p + form.data_type.DataTypeOfCppFile[j].Length < temp.Length && temp[p + form.data_type.DataTypeOfCppFile[j].Length] == ':') { break; } if (p + form.data_type.DataTypeOfCppFile[j].Length+1 < temp.Length && temp[p + form.data_type.DataTypeOfCppFile[j].Length+1] == ':') { break; } if (p + form.data_type.DataTypeOfCppFile[j].Length < temp.Length && (temp[p + form.data_type.DataTypeOfCppFile[j].Length] == '*' || temp[p + form.data_type.DataTypeOfCppFile[j].Length] == '&')) { continue; } if (p + form.data_type.DataTypeOfCppFile[j].Length+1 < temp.Length && (temp[p + form.data_type.DataTypeOfCppFile[j].Length+1] == '*' || temp[p + form.data_type.DataTypeOfCppFile[j].Length+1] == '&')) { continue; } l4 = temp.IndexOf("//"); for (int k = p+form.data_type.DataTypeOfCppFile[j].Length; k < temp.Length && temp[k] != ';' && k != l4; k++) { if (temp[k] == '(' || temp[k] == ')') { temp1 = ""; break; } if (temp[k] == ' ') { continue; } if (temp[k] == '=') { while (k < temp.Length && temp[k] != ';' && temp[k] != ',' && temp[k] != '{') { k++; } if (temp[k] == '{' ) { for (int s = 0; s < local_variable.Count; s++) { if (local_variable[s] == temp1) { temp1 = ""; } } if (temp1 != "") { local_variable.Add(temp1);//in this part we add local variable Kind_local_variable.Add(form.data_type.DataTypeOfCppFile[j]);//in this part we add kind of local variable. } temp1 = ""; break; } k--; continue; } if (temp[k] != ',') { temp1 += temp[k]; } else { for (int s = 0; s < local_variable.Count; s++) { if (local_variable[s] == temp1) { temp1 = ""; } } if (temp1 != "") { local_variable.Add(temp1);//in this part we add local variable Kind_local_variable.Add(form.data_type.DataTypeOfCppFile[j]);//in this part we add kind of local variable. } temp1 = ""; } } for (int s = 0; s < local_variable.Count; s++) { if (local_variable[s] == temp1) { temp1 = ""; } } if (temp1.Length != 0) { local_variable.Add(temp1);//in this part we add local variable Kind_local_variable.Add(form.data_type.DataTypeOfCppFile[j]);//in this part we add kind of local variable. } temp1 = ""; } // for (int j = 0; j < temp.Length; j++) { if (temp[j] == '{') { stack_check = true; brace_pointer.X = 0; brace_pointer.Y = i; Stack.Push(brace_pointer); } if (temp[j] == '}') { stack_check = true; brace_pointer.X = 1; brace_pointer.Y = i; ///////////////////////// //if(Stack.Count != 0) //{ if (Stack.Peek().X == 0) { Stack.Pop(); } else { Stack.Push(brace_pointer); } //} } } // } number_of_line = l2 - start_position; return 1; } } } |
کلاس call_stack:
در این کلاس کلاس کمکی showcallstack قرار دارد که ما با استفاده از آن دیتا های مورد نظر را برای نمایش کال استک در لیست و صف می ریزیم.
در این کلاس ویژگی های fun,line,mood, numberofstack قرار دارد.
line خطی از برنامه است که تابع فراخوانی شده است .
mood اگر true یعنی تابع در حال push شدن در استک است اگر false بود یعنی تابع در حال pop شدن است.
numberofstack تعداد توابعی است که در حال حاضر در استک موجود است.
fun شی از کلاس function است که قرار است در استک پاپ یا پوش شود.
همین طور دارای دو سازنده است.
خود کلاس call_stack دارای ویژگی های NumberOfCallstack ,qee,lis است.
NumberOfCallstack تعداد قطعات موجود در استک را نشان می دهد.
qee صفی از جنس showcallstack است که بعدا از آن برای نمایش گرافیکی کال استک استفاده می کنیم.
lis لیستی از جنس showcallstack است که از آن برای نمایش غیر گرافیکی call stack در textbox استفاده می کنیم.
کلاس call_stack دارای متد های do_callstack , Draw_push_callsatack , Draw_pop_callsatack,Draw,
Display_call_stack_in_textbox است.
متد do_callstack به صورت بازگشتی کال استک را پیدا می کند و ترتیب مراحل را در qee , lis می ریزد تا بعدا استفاده کنیم.
Draw_pop_callsatack,Draw برای نمایش پاپ کردن است.
Draw_push_callsatack برای کشیدن push کردن است.
Draw تاین تابه با استفاده از دو تابع قبلی استک را گرافیکی می کشد ولی برای این این تاب را درست کردیم که از آن به صورت Thread استفاده کنیم و دچار سندروم انجماد نشیم.
Display_call_stack_in_textbox این تابع اطلاعات function ها و کال استک را در Textbox1 چاپ می کند.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Threading; using System.Drawing; namespace call_stack { public class showcallstack { public function fun; public string line; public bool mood; public int numberofstack; public showcallstack(function Fun,string Line,bool Mood,int Numberofstack) { fun = new function(); numberofstack = Numberofstack; line = Line; mood = Mood; fun = Fun; } public showcallstack() { numberofstack = 0; fun = new function(); line = "0"; mood = true; } } public class Call_stack { public int NumberOfStack; public Queue<showcallstack> qee; public List<showcallstack> lis ; public Call_stack() { NumberOfStack = 0; qee = new Queue<showcallstack>(); lis = new List<showcallstack>(); } public int do_callstack(string name,int line) { var form = Form.ActiveForm as Form1; int p = 0; int index = 0; int index1 = 0; showcallstack myshowcallstack = new showcallstack(); string temp; if (form.Function.Count == 0) { return 0; } for (int i = 0; i < form.Function.Count; i++) { if (name == form.Function[i].name) { p = i; break; } } qee.Enqueue(new showcallstack(form.Function[p],Convert.ToString(line),true,NumberOfStack)); lis.Add(new showcallstack(form.Function[p], Convert.ToString(line), true, NumberOfStack)); NumberOfStack++; for (int i = form.Function[p].start_position; i < form.Function[p].number_of_line + form.Function[p].start_position; i++) { temp = File.ReadLines(form.textBox2.Text).Skip(i).Take(1).First(); if (temp.Length == 0) { continue; } for (int j = 0; j < form.Function.Count; j++) { index = temp.IndexOf(form.Function[j].name); if (index != -1 && form.Function[j].name != form.Function[p].name) { for (int k = 0; k < form.data_type.DataTypeOfCppFile.Count; k++) { index1 = temp.IndexOf(form.data_type.DataTypeOfCppFile[k]); } if (index1 == -1) { this.do_callstack(form.Function[j].name, i); } } } form.read_quee = false; } myshowcallstack.mood = false; myshowcallstack.numberofstack = NumberOfStack; qee.Enqueue(new showcallstack(form.Function[p], Convert.ToString(line), false, NumberOfStack)); lis.Add(new showcallstack(form.Function[p], Convert.ToString(line), false, NumberOfStack)); NumberOfStack--; return 1; } public void Draw_push_callsatack(function fun, string line, int NumberOfStack) { var form = Form.ActiveForm as Form1; SolidBrush mybrush = new SolidBrush(Color.Red); Font myfont = new Font("Arial", 12); Point mypoint = new Point(); string temp = ""; for (int i = 0; i < fun.local_variable.Count; i++) { temp += fun.Kind_local_variable[i] + " " + fun.local_variable[i] + ","; } temp += "n"; temp += line; temp += "n"; temp += fun.name + " :"; for (int i = 0; i < fun.arquments.Count; i++) { temp += fun.arquments[i] + ","; } mypoint.X = 0; mypoint.Y = 600 - (NumberOfStack + 1) * 60; form.grastack.FillRectangle(mybrush, mypoint.X, mypoint.Y, 600, 60); form.grastack.DrawString(temp, myfont, Brushes.White, mypoint.X + 5, mypoint.Y + 5); form.pictureBox1.Image = form.img; //Application.DoEvents(); } public void Draw_pop_callsatack(int NumberOfStack) { var form = Form.ActiveForm as Form1; SolidBrush mybrush = new SolidBrush(Color.White); Font myfont = new Font("Arial", 12); Point mypoint = new Point(); mypoint.X = 0; mypoint.Y = 600 - (NumberOfStack) * 60; form.grastack.FillRectangle(mybrush, mypoint.X, mypoint.Y, 600, 60); form.pictureBox1.Image = form.img; //Application.DoEvents(); } public void Draw() { var form = Form.ActiveForm as Form1; while (form.read_quee == true) { } while (qee.Count != 0) { Thread.Sleep(3000); if (qee.Peek().mood == true) { Draw_push_callsatack(qee.Peek().fun,qee.Peek().line,qee.Peek().numberofstack); } else { Draw_pop_callsatack(qee.Peek().numberofstack); } qee.Dequeue(); } } public void Display_call_stack_in_textbox() { var form = Form.ActiveForm as Form1; showcallstack myshowcallstack = new showcallstack(); form.textBox1.Text += "rnttt--------------------------------------------------------------------------------------------rn"; for (int i = 0; i < form.Function.Count; i++) { form.textBox1.Text += "Name : rn"; form.textBox1.Text += form.Function[i].name + "rn"; form.textBox1.Text += "Argument : rn"; for (int j = 0; j < form.Function[i].arquments.Count; j++) { form.textBox1.Text += form.Function[i].arquments[j] + "rn"; } form.textBox1.Text += "Local variable : rn"; for (int j = 0; j < form.Function[i].local_variable.Count; j++) { form.textBox1.Text += form.Function[i].Kind_local_variable[j] + " " + form.Function[i].local_variable[j] + "rn"; } form.textBox1.Text += "rn"; } form.textBox1.Text += "rnttt------------------------------------------CallStack-------------------------------------rn"; if (qee.Count != 0) { for (int i = 0; i < qee.Count; i++) { // //Thread.Sleep(2000);// Not work !! // if (lis[i].mood == true) { form.textBox1.Text += "PUSH :: rn"; form.textBox1.Text += lis[i].fun.name; for (int p = 0; p < lis[i].fun.arquments.Count; p++) { form.textBox1.Text += " : " + lis[i].fun.arquments[p] + ","; } form.textBox1.Text += "rn"; form.textBox1.Text += "Pointer to line : " + lis[i].line + "rn"; for (int p = 0; p < lis[i].fun.local_variable.Count; p++) { form.textBox1.Text += " : " + lis[i].fun.Kind_local_variable[p] + " " + lis[i].fun.local_variable[p] + ","; } form.textBox1.Text += "rnrn"; } else { form.textBox1.Text += "POP :: rn"; form.textBox1.Text += lis[i].fun.name; for (int p = 0; p < lis[i].fun.arquments.Count; p++) { form.textBox1.Text += " : " + lis[i].fun.arquments[p] + ","; } form.textBox1.Text += "rn"; form.textBox1.Text += "Pointer to line : " + lis[i].line + "rn"; for (int p = 0; p < lis[i].fun.local_variable.Count; p++) { form.textBox1.Text += " : " + lis[i].fun.Kind_local_variable[p] + " " + lis[i].fun.local_variable[p] + ","; } form.textBox1.Text += "rnrn"; } } } } } } |
کلاس Form1.cs:
این کلاس ،کلاس فرم برنامه است که شامل کد های قسمت های مختلف فرم است .
در زیر عکسی از برنامه در حال اجرا را مشاهده می کنید (برای بزرگنمایی روی عکس کلیک کنید):