public bool getEndFor() { if (treeLevel > programTree.Count() - 1) { return(false); } List <programTreeElement> level = programTree[treeLevel]; programTreeElement elem = level[0]; if (elem.type != (int)LEX_TYPES.KEYWORD) { return(true); } if (elem.id != (int)LEXEM_CODES.END_FOR) { return(true); } for_struct fs = loops.Pop(); int var_val; if ((var_val = getVarVal(fs.var_id)) < fs.check_val) { treeLevel = fs.body_level; setVarVal(fs.var_id, ++var_val); loops.Push(fs); return(true); } treeLevel++; return(true); }
public bool showWrite() { if (treeLevel > programTree.Count() - 1) { return(false); } List <programTreeElement> level = programTree[treeLevel]; programTreeElement elem = level[0]; if (elem.type != (int)LEX_TYPES.OPERATOR && elem.id != (int)LEXEM_CODES.W) { return(true); } String msg = "Result: "; for (int i = 1; i <= level.Count() - 1; i++) { elem = level[i]; msg += getVarName(elem.id) + "= " + getVarVal(elem.id) + " "; } MessageBox.Show(msg, "Write vars", MessageBoxButtons.OK, MessageBoxIcon.Information); treeLevel++; return(true); }
public bool getFor() { if (treeLevel > programTree.Count() - 1) { return(false); } List <programTreeElement> level = programTree[treeLevel]; programTreeElement elem = level[0]; if (elem.type != (int)LEX_TYPES.KEYWORD) { return(true); } if (elem.id != (int)LEXEM_CODES.FOR) { return(true); } for_struct fs; treeLevel++; level = programTree[treeLevel]; elem = level[0]; fs.var_id = elem.id; setVarVal(elem.id, getExprVal()); treeLevel++; level = programTree[treeLevel]; elem = level[0]; fs.check_val = getExprVal(); treeLevel++; fs.body_level = treeLevel; loops.Push(fs); return(true); }
private static int getOpPriority(programTreeElement elem) { if (elem.id == (int)LEXEM_CODES.MUL && elem.type == (int)LEX_TYPES.OPERATOR) { return(3); } if ((elem.id == (int)LEXEM_CODES.MIN || elem.id == (int)LEXEM_CODES.PLUS) && elem.type == (int)LEX_TYPES.OPERATOR) { return(2); } //if(elem.type == LexAnalysis.LEX_TYPES.DELIM) return(1); }
public bool showRead() { if (treeLevel > programTree.Count() - 1) { return(false); } List <programTreeElement> level = programTree[treeLevel]; programTreeElement elem = level[0]; if (elem.type != (int)LEX_TYPES.OPERATOR && elem.id != (int)LEXEM_CODES.R) { return(true); } String msg = ""; for (int i = 1; i <= level.Count() - 1; i++) { elem = level[i]; msg += getVarName(elem.id) + " "; } String res = ""; if (DialogResult.OK == InputBox("Input vars values", msg, ref res)) { String[] vals = res.Split(' '); for (int i = 1; i <= level.Count() - 1; i++) { elem = level[i]; int val; if (!int.TryParse(vals[i - 1], out val)) { MessageBox.Show ( "Non integer var value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); return(false); } setVarVal(elem.id, val); } treeLevel++; return(true); } return(true); }
public bool varAssign() { if (treeLevel > programTree.Count() - 1) { return(false); } List <programTreeElement> level = programTree[treeLevel]; programTreeElement elem = level[0]; if (elem.type != (int)LEX_TYPES.VAR) { return(true); } setVarVal(elem.id, getExprVal()); treeLevel++; return(true); }
public static List <programTreeElement> infixToPostfix(List <programTreeElement> expression) { operatorsStack.Clear(); List <programTreeElement> result = new List <programTreeElement>(); programTreeElement elem = expression[0]; if (elem.type != (int)LEX_TYPES.VAR) { return(expression); } result.Add(elem); for (int i = 1; i <= expression.Count() - 1; i++) { elem = expression[i]; if (elem.id == (int)LEXEM_CODES.BC && elem.type == (int)LEX_TYPES.DELIM) // Если очеpедной символ - ')' { while (true) { programTreeElement pop = operatorsStack.Pop(); if (pop.id == (int)LEXEM_CODES.BO && pop.type == (int)LEX_TYPES.DELIM) { break; } result.Add(pop); } continue; } if (elem.type == (int)LEX_TYPES.VAR || elem.type == (int)LEX_TYPES.CONSTANT) { result.Add(elem); continue; } if (elem.id == (int)LEXEM_CODES.BO && elem.type == (int)LEX_TYPES.DELIM) { operatorsStack.Push(elem); continue; } if (elem.type == (int)LEX_TYPES.OPERATOR) { if (operatorsStack.Count == 0) { operatorsStack.Push(elem); } else if (getOpPriority(elem) > getOpPriority(operatorsStack.Peek())) { operatorsStack.Push(elem); } else { while (true) { if (operatorsStack.Count == 0) { break; } if (getOpPriority(elem) > getOpPriority(operatorsStack.Peek())) { break; } result.Add(operatorsStack.Pop()); } operatorsStack.Push(elem); } } } while (operatorsStack.Count != 0) { result.Add(operatorsStack.Pop()); } return(result); }