📄 interpretor.cs
字号:
{
string varId = st.Els[0]; //标识符
string varArrIndex = st.Els[1]; //数组索引
string varValue = st.Els[2]; //值
bool isDclareID = false; //检查该ID是否存在
for (int i = 0; i < IDLst.Count; i++)
{
Variable var = (Variable)IDLst[i];
if (var.VID.Equals(varId))
{
isDclareID = true;
int valueIndex = var.VIndex;
if (!varArrIndex.Equals(NOTARR)) //数组变量的索引为:变量索引+下标
{
ArthmExp arrIndexExp = new ArthmExp(varArrIndex);
int arrIndex = 0;
if (arrIndexExp.GetValue(IDLst, ValueLst, ref arrIndex))
{
if (arrIndex >= 0)
{
valueIndex = valueIndex + arrIndex;
}
else
{
ExecuteError(st,
"非法的数组下标");
return;
}
}
else
{
ExecuteError(st,
"非法的数组下标");
return;
}
}
ArthmExp valueExp = new ArthmExp(varValue);
int varType = var.VType;
if (varType == INTG)
{
int assValue = 0;
if (valueExp.GetValue(IDLst, ValueLst, ref assValue))
{
try
{
ValueLst[valueIndex] = assValue;
return;
}
catch (Exception excp)
{
ExecuteError(st,"非法的数组下标");
return;
}
}
else
{
ExecuteError(st,
"表达式类型与变量类型不一致或者标识符尚未声明");
return;
}
}
else if (varType == REAL)
{
double assValue = 0.0;
if (valueExp.GetValue(IDLst, ValueLst, ref assValue))
{
try
{
ValueLst[valueIndex] = assValue;
return;
}
catch (Exception excp)
{
ExecuteError(st,
"数组索引越界");
return;
}
}
else
{
int assIntValue = 0;
if (valueExp.GetValue(IDLst, ValueLst, ref assIntValue))
{
//支持将整型到实型的自动强制转化
ResultBox.Text += "(提示) > 执行 '" + st.Con
+ "' 时,已将右侧整型赋值强制转化为实型\r\n";
try
{
ValueLst[valueIndex] = (double)assIntValue;
return;
}
catch (Exception excp)
{
ExecuteError(st,
"数组索引越界");
return;
}
}
else
{
ExecuteError(st,
"表达式类型与变量类型不一致或者标识符尚未声明");
return;
}
}
}
}
}
if (!isDclareID)
ExecuteError(st, "该变量尚未声明");
}
//执行输出语句
public void ExecuteOutput(CMMStatement st)
{
ArthmExp outputExp = new ArthmExp(st.Els[0]);
int expType = outputExp.GetExpType(IDLst);
if (expType == ERROR)
{
ExecuteError(st, "不合法的算术表达式");
return;
}
else if (expType == INTG)
{
int expValue = 0;
if (outputExp.GetValue(IDLst, ValueLst, ref expValue))
{
ResultBox.Text += "(输出) > " + Convert.ToString(expValue) + "\r\n";
}
else
ExecuteError(st, "变量未声明或未赋值,或者数组索引越界");
}
else if (expType == REAL)
{
double expValue = 0.0;
if (outputExp.GetValue(IDLst, ValueLst, ref expValue))
{
ResultBox.Text += "(输出) > " + Convert.ToString(expValue) + "\r\n";
}
else
ExecuteError(st, "变量未声明或未赋值,或者数组索引越界");
}
}
//执行输入语句
public void ExecuteInput(CMMStatement st)
{
ResultBox.Text += "(输入) > ";
LastInputID = st.Els[0];
if (!st.Els[1].Equals(NOTARR))
LastInputID += "[" + st.Els[1] + "]";
isTmpStopped = true;
}
//执行选择语句
public void ExecuteSelect(CMMStatement st)
{
bool conditionValue = false; //表示选择条件的值
CMMStatement condition = new CMMStatement(st.Els[0]);
CMMStatement thenBranch = new CMMStatement(st.Els[1]);
CMMStatement elseBranch = new CMMStatement(st.Els[2]);
if (!GetRelatExpValue(condition, ref conditionValue))
{
ExecuteError(st, "不合法的选择条件");
return;
}
CurLevel++;
if (conditionValue)
ExecuteSt(thenBranch);
else
ExecuteSt(elseBranch);
CurLevel--;
DelectOutdateID(); //删除过时的变量
}
//执行循环语句
public void ExecuteLoop(CMMStatement st)
{
bool conditionValue = false; //表示选择条件的值
CMMStatement condition = new CMMStatement(st.Els[0]);
CMMStatement loopSt = new CMMStatement(st.Els[1]);
if (!GetRelatExpValue(condition, ref conditionValue))
{
ExecuteError(st, "不合法的循环条件");
return;
}
CurLevel++;
while (conditionValue)
{
ExecuteSt(loopSt);
GetRelatExpValue(condition, ref conditionValue);
}
CurLevel--;
DelectOutdateID(); //删除过时的变量
}
//执行错误语句
public void ExecuteError(CMMStatement st)
{
ExecuteError(st, "详细原因请点击\"语法分析\"按钮");
}
//执行错误语句
public void ExecuteError(CMMStatement st, string cause)
{
DialogResult response =
MessageBox.Show("代码'" + st.Con + "'有语法错误。"
+ "是否立刻结束该程序?点击\"否\"则跳过该条语句。",
"请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
ResultBox.Text += "(错误) > " + st.Con + " (" + cause + ")\r\n";
if (response == DialogResult.Yes)
hasFinished = true;
}
//列出四元式
private void printMidCode(string op, string adr1, string adr2, string adr3)
{
ResultBox.Text += CodeNum + ". " + op + "," + adr1 + "," + adr2 + "," + adr3 + "\r\n";
CodeNum++;
}
//从标识符列表删除过时的ID
private void DelectOutdateID()
{
//从尾部开始查找,提高程序效率
for (int i = IDLst.Count - 1; i >= 0; i--)
{
Variable var = (Variable)IDLst[i];
//如果变量层次大于当前层次,则变量已过时
if (var.VLevel > CurLevel)
{
IDLst.RemoveAt(i);
}
else //如果小于或等于,则前面的必然小于或等于,此过程结束
break;
}
}
//判断关系表达式的值,返回值表示操作是否成功,参数relatValue表示表达式结果
private bool GetRelatExpValue(CMMStatement exp, ref bool relatValue)
{
grammerAnalysis.AnalyzeRelatExp(ref exp);
if (exp.Type == ERROR
|| exp.Els[1] == null || !ArthmExp.IsArthmExp(exp.Els[1])
|| exp.Els[2] == null || !ArthmExp.IsArthmExp(exp.Els[2]))
{
return false; //不合法的关系表达式
}
else
{
ArthmExp leftExp = new ArthmExp(exp.Els[1]);
ArthmExp rightExp = new ArthmExp(exp.Els[2]);
//左边的表达式
double leftValue = 0.0;
int leftIntValue = 0;
if (!leftExp.GetValue(IDLst, ValueLst, ref leftValue))
{
if (leftExp.GetValue(IDLst, ValueLst, ref leftIntValue))
leftValue = (double)leftIntValue; //统一为double方便比较
else
return false;
}
//右边的表达式
double rightValue = 0.0;
int rightIntValue = 0;
if (!rightExp.GetValue(IDLst, ValueLst, ref rightValue))
{
if (rightExp.GetValue(IDLst, ValueLst, ref rightIntValue))
rightValue = (double)rightIntValue; //统一为double方便比较
else
return false;
}
switch (exp.Els[0])
{
case "<":
if (leftValue < rightValue)
relatValue = true;
else
relatValue = false;
return true;
case "==":
if (leftValue == rightValue)
relatValue = true;
else
relatValue = false;
return true;
case "<>":
if (leftValue != rightValue)
relatValue = true;
else
relatValue = false;
return true;
default:
return false;
}
}
}
//全局变量
//四元式序号
private int CodeNum;
//输出文本区
private TextBox ResultBox;
//最后一条输入语句的标识符
private string LastInputID;
//指示该程序是否完成
private bool hasFinished;
public bool HasFinished
{
get { return hasFinished; }
}
//指示是否暂时中断程序
private bool isTmpStopped;
public bool IsTmpStopped
{
get { return isTmpStopped; }
set { isTmpStopped = value; }
}
//当前层次
private int CurLevel;
//标识符列表
private ArrayList IDLst;
//值列表
private ArrayList ValueLst;
//值列表索引
private int ValueLstIndex;
WordAnalysis wordAnalysis = new WordAnalysis();
GrammerAnalysis grammerAnalysis = new GrammerAnalysis();
//词句类型常量
const int ERROR = -1;
const int RSVWORD = -11;
const int ID = -12;
const int ARR_ID = -13;
const int INTG = -14;
const int REAL = -15;
const int OPRT = -16;
const int COMPAR = -17;
const int NULLST = -31;
const int EMPTY = -32;
const int COMPLEX = -33;
const int DECLARE = -34;
const int ASSIGN = -37;
const int INPUT = -39;
const int OUTPUT = -41;
const int SELECT = -42;
const int ELSE_BR = -44;
const int LOOP = -45;
const int ARTHM_EXP = -46;
const int RELAT_EXP = -47;
const string NOTARR = "NotArr";
const string NOTASS = "NotAss";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -