📄 tree.cs
字号:
showD++;
}
else { break; throw new Exception("因子类型错误"); }
}
else { break; throw new Exception("只能出现二级运算符"); }
}
if (showD>0)
{
return basis * basis2;
}
else
{
return basis;
}
}
else if (the_base is Double)
{
double basis = (Double)the_base;
for (int i = 1; i < the_item.getChildCount(); i++)
{
string sbl = ((Token)the_item.getAChild(i)).getValue();
if (sbl == "*")
{
Object next = the_atom_action((TreeNode)the_item.getAChild(++i));
if (next is Int32)
{
basis *= (Int32)next;
}
else if (next is Double)
{
basis *= (Double)next;
}
else { break; throw new Exception("因子类型错误"); }
}
else if (sbl == "/")
{
Object next = the_atom_action((TreeNode)the_item.getAChild(++i));
if (next is Int32)
{
basis = basis / ((Int32)next);
}
else if (next is Double)
{
basis = basis / ((Double)next);
}
else { break; throw new Exception("因子类型错误"); }
}
else { break; throw new Exception("只能出现二级运算符"); }
}
return basis;
}
else
{
throw new Exception("表达式因子类型错误");
}
}
catch (Exception e)
{
errorCount++; MyInvoke mi = new MyInvoke(change);
theOutIn.BeginInvoke(mi, e.Message); return null;
}
}
//因子动作
Object the_atom_action(TreeNode the_atom)
{
try
{
Object theChild = the_atom.getAChild(0);
if (theChild is Token)
{
Token lookChild = (Token)theChild;
if (lookChild.isIdentifier())
{
Symbol ident = find(lookChild.getValue());
if (ident.isIntVar())
{
return ident.getIntValue();
}
else if (ident.isRealVar())
{
return ident.getRealValue();
}
else if (ident.isTheArray())
{
return ident;
}
else { throw new Exception(lookChild.getValue() + " 数值类型错误 "); }
}
else if (lookChild.getNumType() == 1)
{
return Convert.ToInt32(lookChild.getValue());
}
else if (lookChild.getNumType() == 2)
{
return Convert.ToDouble(lookChild.getValue());
}
else { throw new Exception("数值类型转换错误"); }
}
else if (theChild is TreeNode)
{
TreeNode lookChild = (TreeNode)theChild;
//又见表达式
if (lookChild.getType() == 18)
{
return the_expression_action(lookChild);
}
//方法调用式
else if (lookChild.getType() == 15)
{
if (this.the_stack.Count >= 2)
{
return the_funct_call_action(lookChild);
}
else { throw new Exception("当前不可进行方法调用"); }
}
//数组元素
else if (lookChild.getType() == 21)
{
return array_elem_action(lookChild);
}
else { throw new Exception("因子错误"); }
}
else { throw new Exception("因子错误"); }
}
catch (Exception e)
{
errorCount++; MyInvoke mi = new MyInvoke(change);
theOutIn.BeginInvoke(mi, e.Message); return 0;
}
}
//数组元素动作
Object array_elem_action(TreeNode the_array_elem)
{
try
{
string arrayName = ((Token)the_array_elem.getAChild(0)).getValue();
//根据数组名找到数组
Symbol the_array = find(arrayName);
//确定是一个数组
if (the_array.isTheArray())
{
//索引
Token index = (Token)the_array_elem.getAChild(1);
int indexValue;
if (index.isPositiveInt())
{
indexValue = Convert.ToInt32(index.getValue());
}
else if (index.isIdentifier())
{
indexValue = find(index.getValue()).getIntValue();
}
else
{
throw new Exception("数组元素索引错误" + index.getValue());
}
//索引在正确范围内
if (indexValue >= 0 && indexValue < the_array.getElemCount())
{
string enterName = arrayName + "[" + indexValue + "]";
int type = the_array.getType();
if (type == 1)
{
return find(enterName).getIntValue();
}
else
{ return find(enterName).getRealValue(); }
}
else { throw new Exception("数组元素索引错误" + index.getValue()); }
}
else { throw new Exception("不存在数组 " + arrayName); }
}
catch (Exception e)
{
errorCount++; MyInvoke mi = new MyInvoke(change);
theOutIn.BeginInvoke(mi, e.Message); return null;
}
}
//方法调用 动作
Object the_funct_call_action(TreeNode the_funct_call)
{
try
{
//加一层
this.the_stack.Push(new ArrayList());
string name = ((Token)the_funct_call.getAChild(0)).getValue();
//方法部分节点
TreeNode the_function = findFunction(name).getFunction();
//如果有传参数节点
if (the_funct_call.getChildCount() > 1)
{
TreeNode the_para_send = (TreeNode)the_funct_call.getAChild(1);
// 进入方法部分
Object enterin= the_function_action(the_function, the_para_send);
this.the_stack.Pop();
return enterin;
}
else
{
Object enterin= the_function_action(the_function, null);
this.the_stack.Pop();
return enterin;
}
}
catch (Exception e)
{
errorCount++; MyInvoke mi = new MyInvoke(change);
theOutIn.BeginInvoke(mi, e.Message); return null;
}
}
//方法部分 动作
Object the_function_action(TreeNode the_function, TreeNode the_para_send)
{
try
{
//记录孩子的个数
int child = the_function.getChildCount();
// 有参数
if (the_para_send != null && child > 3)
{
// 进行参数匹配
matchPara_action(the_para_send, (TreeNode)the_function.getAChild(2));
}
// 有没有参数都要进行
// 记录返回类型
string returnType = ((Token)((TreeNode)the_function.getAChild(0)).getAChild(0)).getValue();
// 进入方法体动作
return the_funct_in_action((TreeNode)the_function.getAChild(child - 1), returnType,0);
}
catch (Exception e)
{
errorCount++; MyInvoke mi = new MyInvoke(change);
theOutIn.BeginInvoke(mi, e.Message); return null;
}
}
//方法体动作
Object the_funct_in_action(TreeNode the_funct_in, string returnType,int whereIs)
{
try
{
//记录孩子的个数
int child = the_funct_in.getChildCount();
if (returnType == "void")
{
// 有或没有 return语句 有return则后面不能有表达式
//进入方法体内容 只有一个孩子
for (int i = 0; i < child; i++)
{
TreeNode the_funct_in_cont = (TreeNode)the_funct_in.getAChild(i);
Object sentence = the_funct_in_cont.getAChild(0);
if (sentence is Token)
{
//空语句
continue;
}
else if (sentence is TreeNode)
{
TreeNode useSentence = (TreeNode)sentence;
//节点类型
int type = useSentence.getType();
//数组声明
if (type == 5)
{
action((TreeNode)useSentence.getAChild(1), useSentence);
}
//变量声明
else if (type == 7)
{
the_var_decl_action(useSentence);
}
//方法调用语句
else if (type == 15)
{
Object shouldNull = the_funct_call_action(useSentence);
if (shouldNull != null)
{
throw new Exception("语句错误 " + useSentence.getName() + ((Token)useSentence.getAChild(0)).getValue());
}
}
//数组元素赋值语句
else if (type == 17)
{
this.arrayElem_give_action(useSentence);
}
//变量赋值语句
else if (type == 22)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -