public void parse_sema_whi(Exp_whi whi) { s++; Exp_Operate opter = new Exp_Operate(); opter.op = type_op.LABEL; opter.result = getLabel(); list_op.add(opter); Exp_Operate opt = new Exp_Operate(); Exp_cond condition = whi.condition; Exp_mark m1 = (Exp_mark) condition.left; Exp_mark m2 = (Exp_mark) condition.right; opt.op = get_compare_mark(condition.exp_comp); opt.s = backString(m1, type_cal.NUM); opt.d = backString(m2, type_cal.NUM); opt.result = getLabel(); // 跳转标签 list_op.add(opt); Exp_B exp = (Exp_B) whi.next; exec_exp_B(exp, null); Exp_Operate op1 = new Exp_Operate(); op1.op = type_op.JMP; op1.result = opter.result; list_op.add(op1); Exp_Operate op2 = new Exp_Operate(); op2.op = type_op.LABEL; op2.result = opt.result; list_op.add(op2); s--; st.check_Dimension(s); }
public void parse_sema_typedef(Exp_type typedef, int Dimension) { Exp_Operate exp_op = new Exp_Operate(); exp_op.op = type_op.DEFINE; // List<Table> list_tab=new ArrayList<Table>(); Table tab = new Table(); tab.Dimension = Dimension; if (typedef.type == type_cal.NUM) { exp_op.s = "num"; // tab.apoint=0; tab.type = type_cal.NUM; } else if (typedef.type == type_cal.STRING) { tab.type = type_cal.STRING; exp_op.s = "string"; } else { exp_op.s = "bool"; tab.type = type_cal.BOOL; } // list_tab.add(tab); Exp_mark mark = (Exp_mark) typedef.Id; exp_op.result = mark.value; tab.code_index = list_op.size(); list_op.add(exp_op); // System.out.println("define var.."+mark.value); st.put(mark.value, clone_tab(tab)); if (mark.list_value != null) { int i = 0; while (i < mark.list_value.size()) { Exp_mark m = (Exp_mark) mark.list_value.get(i); Table t = clone_tab(tab); t.code_index = list_op.size(); st.put(m.value, t); Exp_Operate operate = clone_Operate(exp_op); operate.result = m.value; list_op.add(operate); // System.out.println("define var.."+m.value); i++; } } }
public void parse_sema_ife(Exp_ife ife) { s++; Exp_Operate opt = new Exp_Operate(); Exp_cond condition = ife.condition; Exp_mark m1 = (Exp_mark) condition.left; Exp_mark m2 = (Exp_mark) condition.right; opt.op = get_compare_mark(condition.exp_comp); if (m1 != null) { opt.s = backString(m1, type_cal.NUM); } if (m2 != null) { opt.d = backString(m2, type_cal.NUM); } opt.result = getLabel(); // 跳转标签 list_op.add(opt); Exp_B exp_left = (Exp_B) ife.left; Exp_B exp_right = (Exp_B) ife.right; if (exp_left != null) { exec_exp_B(exp_left, null); } // 如果有else if (exp_right != null) { exec_exp_B(exp_right, opt.result); } else { Exp_Operate operate = new Exp_Operate(); operate.op = type_op.LABEL; operate.result = opt.result; list_op.add(operate); } s--; List<Integer> list = st.check_Dimension(s); // 返回那些引用数目为0的变量 // 优化部分 if (list != null) { int i = 0; while (i < list.size()) { list_op.remove(list.get(i)); i++; } } }
// 单纯的检查变量的定义 public type_cal check_define(String key) { return st.check_def(key); }