Esempio n. 1
0
static struct expty transVar(Tr_level level, Tr_exp breakk, S_table venv, S_table tenv, A_var v) {
	if (!v) {return expTy(Tr_noExp(), Ty_Void());}
    /*!those several var is ugly*/
	E_enventry x;
	struct expty et,et2;
	Ty_fieldList fl;
	Tr_exp trans;

	switch (v->kind) {
	case A_simpleVar:/* var id (a)*/
		x = S_look(venv, v->u.simple); 
		trans = Tr_noExp();
		if (x && x->kind == E_varEntry) {
			trans = Tr_simpleVar(x->u.var.access, level);
			return expTy(trans, actual_ty(x->u.var.ty));
		} else {
			EM_error(v->pos, "undefined var %s", S_name(v->u.simple));
			return expTy(trans, Ty_Int());
		}
		break;
	case A_fieldVar:/* var record (a.b)*/
		et = transVar(level, breakk, venv, tenv, v->u.field.var);
		trans = Tr_noExp();
		if (et.ty->kind != Ty_record) {
			EM_error(v->pos, "not a record type");
		} else {
			int i = 0;
			for (fl = et.ty->u.record; fl; fl = fl->tail, i++) { /*fl is Ty_fieldList*/
				if (fl->head->name == v->u.field.sym) {
					trans = Tr_fieldVar(et.exp, i);
					return expTy(trans, actual_ty(fl->head->ty));
				}
			}
			EM_error(v->pos, "no such field in record %s", S_name(v->u.field.sym));
		}
		return expTy(trans, Ty_Int());
		break;
	case A_subscriptVar: /*var array (a[b])*/ 
		et  = transVar(level, breakk, venv, tenv, v->u.subscript.var);
		trans = Tr_noExp();
		if (et.ty->kind != Ty_array) {
			EM_error(v->pos, "not a array type");
		} else {
			et2 = transExp(level, breakk, venv, tenv, v->u.subscript.exp);
			if (et2.ty->kind != Ty_int) {
				EM_error(v->pos, "int required");
			} else {
				trans = Tr_subscriptVar(et.exp, et2.exp);
				return expTy(trans, actual_ty(et.ty->u.array));
			}
		}
		return expTy(trans, Ty_Int());
	default:
		assert(0);
	}
}
Esempio n. 2
0
struct expty transVar_subscriptVar(Tr_level level, S_table venv, S_table tenv, A_var v, Temp_label breakk) {
  struct expty var = transVar(level, venv, tenv, v->u.subscript.var, breakk);
  struct expty sub = transExp(level, venv, tenv, v->u.subscript.exp, breakk);

  if (var.ty->kind != Ty_array) {
    EM_error(v->u.subscript.var->pos, "variable type '%s' is not array",
             type_msg(var.ty));
    return expTy(NULL, Ty_Int());
  }
  if (sub.ty->kind != Ty_int) {
    EM_error(v->u.subscript.exp->pos, "invalid types '%s' for array subscript",
             type_msg(sub.ty));
    return expTy(NULL, Ty_Int());
  }

  return expTy(Tr_subscriptVar(var.exp, sub.exp, level),
               actual_ty(var.ty->u.array));
}
Esempio n. 3
0
File: semant.c Progetto: 0XCC1/tiger
static struct expty transVar(Tr_level level,Tr_exp breakk, S_table venv,S_table tenv,A_var v){
	if (!v) {
		return expTy(Tr_noExp(), Ty_Void());
	}
	E_enventry x;
	struct expty et,et2;
	Ty_fieldList fl;
	Tr_exp trans;

	switch(v->kind){
		case A_simpleVar: /*var id*/
			x=S_look(venv,v->u.simple);
			trans = Tr_noExp();
			if (x&&x->kind==E_varEntry){
				trans= Tr_simpleVar(x->u.var.access, level);
				return expTy(trans,actual_ty(x->u.var.ty));
			}else{
				//变量未定义
				EM_error(v->pos, "undefined var %s", S_name(v->u.simple));
				return expTy(trans, Ty_Int());
			}
		break;
		case A_fieldVar:/* a.b   a.b.c*/
			//得到 a  的type 是 record还是 array还是 simplevar
			et=transVar(level,breakk,venv,tenv,v->u.field.var);
			trans = Tr_noExp();
			if (et.ty->kind!=Ty_record)
			{
				EM_error(v->pos, "not a record type");
				return expTy(NULL, Ty_Record(NULL));
			}else{
				int i=0;
				for (fl = et.ty->u.record;fl;fl=fl->tail,i++)
				{
					//地址比较
					if (fl->head->name==v->u.field.sym)
					{
						trans = Tr_fieldVar(et.exp,i);
						return expTy(trans, actual_ty(fl->head->ty));
					}
				}
				EM_error(v->pos, "no such field in record %s", S_name(v->u.field.sym));
			}
			return expTy(trans, Ty_Int());	
		break;
		case A_subscriptVar:/*a[i]*/
			et  = transVar(level,breakk,venv, tenv, v->u.subscript.var);
			trans = Tr_noExp();
			if (et.ty->kind!=Ty_array){
				EM_error(v->pos, "not a array type");
			}else{
				et2=transExp(level,breakk,venv,tenv,v->u.subscript.exp);
				if (et2.ty->kind != Ty_int) {
					EM_error(v->pos, "int required");
				}else{
					trans = Tr_subscriptVar(et.exp, et2.exp);
					return expTy(trans, actual_ty(et.ty->u.array));
				}
			}
			return expTy(trans, Ty_Int());
		break;
	}

}