/// <summary> /// 得到标识符的类型 /// </summary> /// <param name="s">标识符变量名</param> /// <returns>返回变量类型</returns> private string get_BStype(string s) { int r = IDlist.IndexOf(s); IDtable b = (IDtable)IDtablelist[r]; return(b.Type); }
/// <summary> /// 赋值语句中,检查被赋值变量是否声明(声明过为true,并将其放入临时变量表中) /// </summary> /// <param name="next">要判断的变量</param> /// <param name="n">变量所在位置</param> /// <returns>返回判断结果</returns> private bool toLS(string next, int n) { if (!IDlist.Contains(next) && !SZlist.Contains(next)) { return(false); } else { if (IDlist.Contains(next)) { int indexID = IDlist.IndexOf(next); IDtable BStmp = (IDtable)IDtablelist[indexID]; TempIDlist.Add(BStmp.Name); TempIDtablelist.Add(new TempIDtable(BStmp.Name, BStmp.Type, BStmp.Level, BStmp.Valued)); return(true); } else { int indexSZ = SZlist.IndexOf(next); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; LZlist.Add(SZtmp.Name); n--; LZtablelist.Add(new TempSZtable(SZtmp.Name, SZtmp.Type, SZtmp.Length, SZtmp.Level)); return(true); } } }
/// <summary> /// 数组下标所对应的元素是否已经赋值 /// </summary> /// <param name="n">要判断的位置</param> /// <returns>返回判读结果</returns> private bool isSZvalue(int n) { tmp = n + 2; string next = getToken(ref n); try { int index = Convert.ToInt32(getToken(ref tmp)); int SZindex = SZlist.IndexOf(next); SZtable SZtmp = (SZtable)SZtablelist[SZindex]; return(SZtmp.Value.Contains(index)); } catch { tmp = tmp - 1; string index = getToken(ref tmp); if (IDlist.Contains(index)) { int index1 = IDlist.IndexOf(index); IDtable BStmp = (IDtable)IDtablelist[index1]; if (BStmp.Valued == true) { return(true); } else { return(false); } } return(false); } }
/// <summary> /// 给数组某个下标变量赋值的时候,将其下标添加进在数组表中对应的valued /// </summary> /// <param name="next">要判断的变量</param> /// <param name="n">变量所在位置</param> /// <returns>返回判断结果</returns> private bool toSZvalue(int n) { tmp = n + 2; string next = getToken(ref n); int index = SZlist.IndexOf(next); SZtable tmpSZ = (SZtable)SZtablelist[index]; string next1 = getToken(ref tmp); try { tmpSZ.Value.Add(Convert.ToInt32(next1)); return(true); } catch { int index1 = IDlist.IndexOf(next1); IDtable BStmp = (IDtable)IDtablelist[index1]; if (BStmp.Type == "real") { errAdd("错误代码: 数组下标不为整型", getLine(n - 1)); return(false); } if (BStmp.Valued == false) { errAdd("错误代码: 数组下标未赋值", getLine(n - 1)); return(false); } return(true); } }
/// <summary> /// 退出嵌套时,删除嵌套内声明的变量和数组 /// </summary> /// <param name="level">当前所在层</param> private void clsLevel(int level) { int b = IDtablelist.Count; int z = SZtablelist.Count; for (int index = 0; index < b; index++) { IDtable tmpBS = (IDtable)IDtablelist[index]; if (tmpBS.Level == level) { IDtablelist.RemoveAt(index); IDlist.RemoveAt(index); b--; } } for (int index = 0; index < z; index++) { SZtable tmpSZ = (SZtable)SZtablelist[index]; if (tmpSZ.Level == level) { SZtablelist.RemoveAt(index); SZlist.RemoveAt(index); z--; } } }
/// <summary> /// 给变量赋值的时候,将其在变量表中的valued改为true /// </summary> private bool toValueSM(int n) { string next = getToken(ref n); try { int index = IDlist.IndexOf(next); IDtable tmpBS = (IDtable)IDtablelist[index]; tmpBS.Valued = true; return(true); } catch { try { int index = TempIDlist.IndexOf(next); TempIDtable tmpLS = (TempIDtable)TempIDtablelist[index]; tmpLS.Valued = true; return(true); } catch { return(false); } } }
/// <summary> /// 此方法修改result对应的所在表的value值,为change /// </summary> /// <param name="result">需要重新赋值的对象</param> /// <param name="change">所赋的值</param> public void retSetValue(string result, string change) { string a = getValue(result); switch (ARG) { case "VARG": int indexID = IDlist.IndexOf(IN_1); IDtable BStmp = (IDtable)IDtablelist[indexID]; BStmp.Value = change; break; case "TARG": int indexTempID = TempIDlist.IndexOf(IN_1); TempIDtable LStmp = (TempIDtable)TempIDtablelist[indexTempID]; LStmp.Value = change; break; case "SARG": if (!Regex.IsMatch(IN_2, pattern)) //如果数组下标为常数 { int indexBS1 = IDlist.IndexOf(IN_2); IDtable BStmp1 = (IDtable)IDtablelist[indexBS1]; IN_2 = BStmp1.Value; } int indexSZ = SZlist.IndexOf(IN_1); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; SZtmp.element[Convert.ToInt32(IN_2)] = change; break; default: break; } }
/// <summary> /// 此方法返回result对应的所在表的value值 /// </summary> /// <param name="sth">需要返回值的对象</param> /// <returns></returns> public string getValue(string result) { IN_1 = find_Table_Kind(result); switch (ARG) { case "VARG": //标识符 int indexID = IDlist.IndexOf(IN_1); IDtable BStmp = (IDtable)IDtablelist[indexID]; return(BStmp.Value); case "TARG": //临时变量 int indexTempID = TempIDlist.IndexOf(IN_1); TempIDtable LStmp = (TempIDtable)TempIDtablelist[indexTempID]; return(LStmp.Value); case "SARG": //数组 try //如果数组出界,返回一个空值null { if (!Regex.IsMatch(IN_2, pattern)) //如果数组下标为常数 { int indexBS1 = IDlist.IndexOf(IN_2); IDtable BStmp1 = (IDtable)IDtablelist[indexBS1]; IN_2 = BStmp1.Value; //得到数组下标的值 } int indexSZ = SZlist.IndexOf(IN_1); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; if (Convert.ToInt32(IN_2) > Convert.ToInt32(SZtmp.Length) - 1) { return("OutSide"); //数组越界 } return(Convert.ToString(SZtmp.element[Convert.ToInt32(IN_2)])); } catch (ArgumentNullException) { return(null); //如果返回的为空值 } default: return(ARG); //如果为常数,得到常数的值 } }
/// <summary> /// read时,将read的变量的valued改为true /// </summary> /// <param name="n">变量所在位置</param> private void toValueRead(int n) { string next = getToken(ref n); try { if (IDlist.Contains(next)) { int indexID = IDlist.IndexOf(next); IDtable BStmp = (IDtable)IDtablelist[indexID]; BStmp.Valued = true; } else { int indexSZ = SZlist.IndexOf(next); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; n++; SZtmp.Value.Add(Convert.ToInt32(getToken(ref n))); } } catch { n = n - 4; next = getToken(ref n); int indexSZ = SZlist.IndexOf(next); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; n++; try { int next1 = Convert.ToInt32(getToken(ref n)); if (!SZtablelist.Contains(next1)) { SZtmp.Value.Add(next1); } } catch { } } }
/// <summary> /// 此方法返回result在对应的符号表中的类型 /// </summary> /// <param name="result">需要返回类型的对象</param> /// <returns></returns> public string getType(string result) { IN_1 = find_Table_Kind(result); switch (ARG) { case "VARG": //标识符 int indexID = IDlist.IndexOf(IN_1); IDtable BStmp = (IDtable)IDtablelist[indexID]; return(BStmp.Type); case "TARG": //临时变量 int indexTempID = TempIDlist.IndexOf(IN_1); TempIDtable LStmp = (TempIDtable)TempIDtablelist[indexTempID]; return(LStmp.Type); case "SARG": //数组 int indexSZ = SZlist.IndexOf(IN_1); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; return(SZtmp.Type); default: return("real"); //理论上是不会运行到这里的,因为只在read的方法中才会使用,而read后面不会连接常数 } }
/// <summary> /// 等号两边的变量类型是否相同(相同为true)(等号左边为real时,肯定为true) /// </summary> /// <param name="next">要判断的变量</param> /// <param name="n">变量所在位置</param> /// <returns>返回判断结果</returns> private bool isType(int n) { if (TempIDlist.Count == 0 && LZlist.Count == 0) { return(true); } try //变量——变量 { TempIDtable LStmp = (TempIDtable)TempIDtablelist[0]; int indexID = IDlist.IndexOf(getToken(ref n)); IDtable BStmp = (IDtable)IDtablelist[indexID]; if (LStmp.Type == BStmp.Type) { return(true); } else if (LStmp.Type == "real") { return(true); } } catch { try //数组——数组 { n--; TempSZtable LZtmp = (TempSZtable)LZtablelist[0]; int indexSZ = SZlist.IndexOf(getToken(ref n)); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; if (LZtmp.Type == SZtmp.Type) { return(true); } else if (LZtmp.Type == "real") { return(true); } } catch { try //变量——数组 { n--; TempIDtable LStmp = (TempIDtable)TempIDtablelist[0]; int indexSZ = SZlist.IndexOf(getToken(ref n)); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; if (LStmp.Type == SZtmp.Type) { return(true); } else if (LStmp.Type == "real") { return(true); } } catch { try //数组——变量 { n++; TempSZtable LZtmp = (TempSZtable)LZtablelist[0]; int indexID = IDlist.IndexOf(getToken(ref n)); IDtable BStmp = (IDtable)IDtablelist[indexID]; if (LZtmp.Type == BStmp.Type) { return(true); } else if (LZtmp.Type == "real") { return(true); } } catch { } } } } return(false); }
/// <summary> /// 变量是否声明过(声明过为false) /// </summary> /// <param name="next">要判断的变量</param> /// <param name="n">变量所在位置</param> /// <returns>返回判断结果</returns> private bool SM(string next, int n) { if (IDlist.Contains(next) || SZlist.Contains(next)) { if (IDlist.Contains(next)) { int indexID = IDlist.IndexOf(next); IDtable BStmp = (IDtable)IDtablelist[indexID]; if (n == -1) { return(false); } if (BStmp.Valued == false) { return(true); } return(false); } else { int indexSZ = SZlist.IndexOf(next); SZtable SZtmp = (SZtable)SZtablelist[indexSZ]; if (n == -1) { return(false); } return(false); } } else { if (n == -1) { return(true); } n = n + 2; if (getToken(ref n) == "[") { LZlist.Add(next); tmp = n - 3; try { LZtablelist.Add(new TempSZtable(next, getToken(ref tmp), Convert.ToInt32(getToken(ref n)), level)); } catch { return(false); } return(true); } else { TempIDlist.Add(next); n = n - 3; TempIDtablelist.Add(new TempIDtable(next, getToken(ref n), level)); return(true); } } }