Пример #1
0
Tr_exp transDec_typeDec(Tr_level level, S_table venv, S_table tenv, A_dec d, Temp_label breakk) {
  for (A_nametyList decs = d->u.type; decs; decs = decs->tail) {
    S_symbol name = decs->head->name;

    for (A_nametyList ds = d->u.type; ds != decs; ds = ds->tail)
      if (ds->head->name == name)
        EM_error(d->pos,
                 "there are two types with the same name in the same batch of "
                 "mutually recursive types");

    S_enter(tenv, name, Ty_Name(name, NULL));
  }
  for (A_nametyList decs = d->u.type; decs; decs = decs->tail) {
    Ty_ty type = S_look(tenv, decs->head->name);
    type->u.name.ty = transTy(level, tenv, decs->head->ty);
  }
  for (A_nametyList decs = d->u.type; decs; decs = decs->tail) {
    Ty_ty type = S_look(tenv, decs->head->name);
    if (type->u.name.sym == actual_ty(type)->u.name.sym) {
      EM_error(decs->head->ty->pos, "mutually recursive types declaration");
      type->u.name.ty = Ty_Int();
    }
  }
  return Tr_noExp();
}
Пример #2
0
/* translate type declaration
 */
void transTypeDec(S_table venv, S_table tenv, A_dec d)
{
  Ty_ty ty = transTy(tenv, d->u.type.name, d->u.type.ty);
  if (ty)
    S_enter(tenv, d->u.type.name, ty);
  else
    EM_error(d->pos, "type defined error %s", d->u.type.name);
}
Пример #3
0
void transDec(S_table venv, S_table tenv, A_dec d) {
	if(d == NULL) {
		return;
	}
	switch(d->kind) {
	case A_varDec: {
		struct expty e = transExp(venv, tenv, d->u.var.init);
		S_enter(venv, d->u.var.var, E_VarEntry(e.ty));
		break;
	}
	case A_typeDec: {
		S_enter(venv, d->u.type->head->name, transTy(tenv, d->u.type->head->ty));
		break;
	}
	case A_functionDec: {
		A_fundec f = d->u.function->head;
		Ty_ty resultTy = S_look(tenv, f->result);
		Ty_tyList formalTys = makeFormalTyList(tenv, f->params);
		S_enter(venv,  f->name, E_FunEntry(formalTys, resultTy));
		S_beginScope(venv);
		{
			A_fieldList l;
			Ty_tyList t;
			for (l = f->params, t = formalTys; l; l = l->tail, t = t->tail) {
				S_enter(venv, l->head->name, E_VarEntry(t->head));
			}
		}
		transExp(venv, tenv, d->u.function->head->body);
		S_endScope(venv);
		break;
	}
	default: {
		assert(0);
			 }
	}
}
Пример #4
0
void transDec (S_table venv, S_table tenv, A_dec d)
{
  switch (d->kind) {
    case A_varDec: {
        Ty_ty typ = NULL;
        if (d->u.var.typ) {
          typ = S_look(tenv, d->u.var.typ);
        }

        struct expty e = transExp (venv, tenv, d->u.var.init);
        if (!typ || typ->kind == e.ty->kind) {
          if (e.ty->kind == Ty_nil && (!typ || typ->kind != Ty_record)) {
            EM_error (d->u.var.init->pos, "nil should be constrained by record");
          }
          S_enter (venv, d->u.var.var, E_VarEntry (e.ty));
        } else {
          EM_error (d->u.var.init->pos, "var type should be same as init");
        }
        break;
      }

    case A_typeDec: {
        A_nametyList nList = NULL;

        for (nList = d->u.type; nList; nList = nList->tail) {
          bool flag;
          A_nametyList scanList = NULL;
          for (scanList = nList->tail; scanList; scanList = scanList->tail) {
            if (strcmp(S_name(nList->head->name), S_name(scanList->head->name)) == 0) {
              flag = TRUE;
              break;
            }
          }
          if (flag) {
            EM_error (d->pos, "type redefined error");
          }
          S_enter(tenv, nList->head->name, Ty_Name (nList->head->ty->u.name, NULL));
        }
        for (nList = d->u.type; nList; nList = nList->tail) {
          Ty_ty waitFill = S_look(tenv, nList->head->name);
          if (waitFill->kind == Ty_name) {
            waitFill->u.name.ty = transTy (tenv, nList->head->ty);
          }
          Ty_ty trueType = actual_ty(waitFill);
          if (trueType) {
            S_enter(tenv, nList->head->name, actual_ty(waitFill));
          } else {
            EM_error (d->pos, "recursive types should through record or array");
            break;
          }

        }

        break;
      }

    case A_functionDec: {
        A_fundecList funList = NULL;

        for (funList = d->u.function; funList; funList = funList->tail) {
           bool flag;
          A_fundecList scanList = NULL;
          for (scanList = funList->tail; scanList; scanList = scanList->tail) {
            if (strcmp(S_name(funList->head->name), S_name(scanList->head->name)) == 0) {
              flag = TRUE;
              break;
            }
          }
          if (flag) {
            EM_error (d->pos, "function redefined error");
          }
          A_fundec f = funList->head;
          if (!f->result) {
            f->result = S_Symbol("void");
          }
          Ty_ty resultTy = S_look (tenv, f->result);
          Ty_tyList formalTys = makeFormalTyList (tenv, f->params);
          S_enter (venv, f->name, E_FunEntry (formalTys, resultTy));
        }
        for (funList = d->u.function; funList; funList = funList->tail) {
          A_fundec f = funList->head;
          Ty_tyList formalTys = makeFormalTyList (tenv, f->params);

          S_beginScope (venv);
          {
            A_fieldList l;
            Ty_tyList t;

            for (l = f->params, t = formalTys; l; l = l->tail, t = t->tail) {
              S_enter (venv, l->head->name, E_VarEntry (t->head));
            }
          }
          Ty_ty returnTy = S_look (tenv, f->result);
          if (returnTy->kind != transExp (venv, tenv, f->body).ty->kind) {
            EM_error (f->body->pos, "return type wrong");
          }
          S_endScope (venv);

        }

        break;
      }
  }
}
Пример #5
0
void transDec(S_table venv, S_table tenv, A_dec d) {
	if (!d) {
		return;
	}
	switch (d->kind) {
		case A_varDec: {
			struct expty e = transExp(venv, tenv, d->u.var.init);
			if (d->u.var.typ) {
				Ty_ty ty = (Ty_ty)S_look(tenv, d->u.var.typ);
				if (!ty) {
					EM_error(d->pos, "undefined type '%s'", S_name(d->u.var.typ));
					return;
				}
				ty = actual_ty(ty);
				if (!isSameTy(ty, e.ty)) {
					EM_error(d->pos, (string)"type mismatch");
					return;
				}
				S_enter(venv, d->u.var.var, E_VarEntry(ty));
				return;
			}
			if (e.ty->kind == Ty_nil) {
				EM_error(d->pos, "initializing nil expressions not constrained by record type");
				return;
			}
			S_enter(venv, d->u.var.var, E_VarEntry(e.ty));
			return;
		}
		case A_typeDec: {
			S_table tmp = S_empty();
			A_nametyList ntl;
			for (ntl = d->u.type; ntl; ntl = ntl->tail) {
				if (S_look(tmp, ntl->head->name)) {
					EM_error(d->pos, "type '%s' redefined", S_name(ntl->head->name));
					return;
				}
				S_enter(tmp, ntl->head->name, "-tmp-");
				S_enter(tenv, ntl->head->name, Ty_Name(ntl->head->name, NULL));
			}
			for (ntl = d->u.type; ntl; ntl = ntl->tail) {
				Ty_ty ty = (Ty_ty)S_look(tenv, ntl->head->name);
				ty->u.name.ty = transTy(tenv, ntl->head->ty);
			}
			for (ntl = d->u.type; ntl; ntl = ntl->tail) {
				Ty_ty ty = (Ty_ty)S_look(tenv, ntl->head->name);
				if (ty->u.name.ty->kind != Ty_name) {
					return;
				}
			}
			EM_error(d->pos, "infinite recursive");
			return;
		}
		case A_functionDec: {
			S_table tmp = S_empty();
			A_fundecList fdl;
			for (fdl = d->u.function; fdl; fdl = fdl->tail) {
				if (S_look(tmp, fdl->head->name)) {
					EM_error(d->pos, "function '%s' redefined", S_name(fdl->head->name));
					return;
				}
				S_enter(tmp, fdl->head->name, "-tmp-");
			}
			for (fdl = d->u.function; fdl; fdl = fdl->tail) {
				Ty_tyList formalTys = makeFormalTyList(tenv, fdl->head->params);
				if (fdl->head->result) {
					Ty_ty resultTy = S_look(tenv, fdl->head->result);
					S_enter(venv, fdl->head->name, E_FunEntry(formalTys, resultTy));
				}
				else {
					S_enter(venv, fdl->head->name, E_FunEntry(formalTys, NULL));
				}
			}
			for (fdl = d->u.function; fdl; fdl = fdl->tail) {
				Ty_tyList formalTys = makeFormalTyList(tenv, fdl->head->params);
				S_beginScope(venv);
				{
					A_fieldList l;
					Ty_tyList t;
					for (l = fdl->head->params, t = formalTys; l; l = l->tail, t = t->tail) {
						S_enter(venv, l->head->name, E_VarEntry(t->head));
					}
				}
				transExp(venv, tenv, fdl->head->body);
				S_endScope(venv);
			}
			break;
		}
		default: {
			assert(0);
		}
	}
}