/// <summary> /// 出現検査 /// </summary> /// <param name="id">型変数ID</param> /// <param name="type">型</param> /// <returns>型中に型変数IDが含まれればtureそうでなければfalse</returns> static bool OccursCheck(int id, MType type) { if (type is TypeVar) { var t = (TypeVar)type; if (t.Id == id) { return true; } else { if (t.Type == null) { return false; } else { return OccursCheck(id, t.Type); } } } else if (type is DotNetType) { return false; } else { throw new NotImplementedException(); } }
/// <summary> /// 関数情報の構築 /// </summary> /// <param name="args">引数</param> /// <param name="ret_type">戻り値の型</param> public FunctionInfo(List<MArg> args, MType ret_type) { Args = args; RetType = ret_type; }
/// <summary> /// 単一化 /// </summary> /// <param name="pos">ファイル位置</param> /// <param name="type1">型1</param> /// <param name="type2">型2</param> static void Unification(string pos, MType type1, MType type2) { if (type1 is TypeVar && type2 is TypeVar && ((TypeVar)type1).Id == ((TypeVar)type2).Id) { return; } else if (type1 is TypeVar && ((TypeVar)type1).Type != null) { Unification(pos, ((TypeVar)type1).Type, type2); } else if (type2 is TypeVar && ((TypeVar)type2).Type != null) { Unification(pos, type1, ((TypeVar)type2).Type); } else if (type1 is TypeVar) { var t1 = (TypeVar)type1; if (OccursCheck(t1.Id, type2)) { throw new MError(pos + ": 出現エラー"); } else { t1.Type = type2; } } else if (type2 is TypeVar) { var t2 = (TypeVar)type2; if (OccursCheck(t2.Id, type1)) { throw new MError(pos + ": 型エラー (出現エラー)"); } else { t2.Type = type1; } } else if (type1 is DotNetType && type2 is DotNetType && ((DotNetType)type1).Type == ((DotNetType)type2).Type) { return; } else { throw new MError(pos + ": 型エラー (単一化エラー)"); } }
/// <summary> /// 仮引数を構築 /// </summary> /// <param name="name">名前</param> /// <param name="type">型</param> public MArg(string name, Type type) { Name = name; Type = new TypeVar(); }