Project

General

Profile

Revision 12

Added by over 7 years ago

latest version of the ooas compiler, with grammar version 1.10

View differences:

OoaResolveExpressionsVisitor.java
77 77
import org.momut.ooas.ast.types.IntType;
78 78
import org.momut.ooas.ast.types.ListType;
79 79
import org.momut.ooas.ast.types.MapType;
80
import org.momut.ooas.ast.types.MetaType;
80 81
import org.momut.ooas.ast.types.NullType;
81 82
import org.momut.ooas.ast.types.OoActionSystemType;
82 83
import org.momut.ooas.ast.types.TupleType;
......
436 437
			{
437 438
				if (!Type.TypeEqual(item.type(), type))
438 439
				{
439
					final Expression cast = new UnaryOperator(ExpressionKind.Cast, item, item.line(), item.pos());
440
					cast.SetType(type);
441
					newitems.add(cast);
440
					// one exception: lists will not be cast. instead we descend into the inner-most lists and statically update their inner types
441
					if(item.kind() == ExpressionKind.ListConstr)
442
					{
443
						item.Accept(new OoaStaticListCastVisitor((ListType) type));
444
						item.SetType(type);
445
						newitems.add(item);
446
					}
447
					else
448
					{
449
						final Expression cast = new UnaryOperator(ExpressionKind.Cast, item, item.line(), item.pos());
450
						cast.SetType(type);
451
						newitems.add(cast);
452
					}
442 453
				}
443 454
				else
444 455
					newitems.add(item);
......
904 915
		expression.SetLeftChild(lhs);
905 916

  
906 917

  
918
		if (staticAccess) {
919
			if (atype.kind() != TypeKind.MetaType)
920
				return Error(access, "Expected meta type.");
921
			atype = ((MetaType)atype).Type();
922
		}
923

  
924

  
907 925
		switch (atype.kind())
908 926
		{
909 927
		case OoActionSystemType:
......
957 975
			break;
958 976
		default:
959 977
			/*error, we can not access an element with '.' in any other type*/
960
			return Error(expression, "Expected: System, Enum, Func, or QR type");
978
			return Error(expression, "Expected: System, Enum, or Function");
961 979
		}
962 980

  
963 981
		expression.SetType(expression.right().type());
......
1157 1175
			if (lt.kind() != TypeKind.MapType)
1158 1176
				return Error(expression, "Range restriction operator expects map on LHS");
1159 1177
			if (rt.kind() != TypeKind.ListType)
1160
				return Error(expression, "Rangle restriction operator expects list on RHS");
1178
				return Error(expression, "Range restriction operator expects list on RHS");
1161 1179
			final ListType rangelist = (ListType)rt;
1162 1180
			domMap = (MapType)lt;
1163 1181
			if (!Type.TypeEqual(rangelist.innerType(), domMap.fromType()))
......
1191 1209
				return lhs;
1192 1210
			if (la.innerType().kind() == TypeKind.Null || la.maxNumberOfElements() == 0)
1193 1211
				return rhs;
1194
			if (!Type.TypeEqual(la.innerType(), lb.innerType()))
1195
				return Error(expression, String.format("Set/List concatenation expects two lists of same type. (%s <> %s)", la.toString(), lb.toString()));
1196
			ListType resultList = new ListType(la.innerType(),
1197
					la.maxNumberOfElements() + lb.maxNumberOfElements(), null);
1198
			expression.SetType(resultList);
1212

  
1213
			if (!Type.TypeEqual(la.innerType(), lb.innerType())) {
1214
				final ListType coverType = (ListType) Type.CoverType(la, lb);
1215
				if(coverType == null)
1216
					return Error(expression, String.format("Set/List concatenation expects two lists of same type. (%s <> %s)", la.toString(), lb.toString())); // FIXME adjust error message
1217

  
1218
				if(!Type.TypeEqual(la, coverType))
1219
					lhs.Accept(new OoaStaticListCastVisitor(coverType));
1220
				if(!Type.TypeEqual(lb, coverType))
1221
					rhs.Accept(new OoaStaticListCastVisitor(coverType));
1222

  
1223
				expression.SetType(new ListType(coverType.innerType(), la.maxNumberOfElements() + lb.maxNumberOfElements(), null));
1224
			} else
1225
				expression.SetType(new ListType(la.innerType(), la.maxNumberOfElements() + lb.maxNumberOfElements(), null));
1226

  
1199 1227
			break;
1200 1228
		case diff:       // list of A * list of A -> list of A (does not respect dupes)
1201
			if (lt.kind() != TypeKind.ListType ||
1202
			rt.kind() != TypeKind.ListType)
1229
			if (lt.kind() != TypeKind.ListType || rt.kind() != TypeKind.ListType)
1203 1230
				return Error(expression, "Set difference expects two lists.");
1204 1231
			la = (ListType)lt;
1205 1232
			lb = (ListType)rt;
1206
			if (!Type.TypeEqual(la.innerType(), lb.innerType()))
1207
				return Error(expression, "Set difference expects two lists of same type.");
1208
			expression.SetType(la);
1233
			if (!Type.TypeEqual(la.innerType(), lb.innerType())) {
1234
				final ListType coverType = (ListType) Type.CoverType(la, lb);
1235
				if(coverType == null)
1236
					return Error(expression, "Set difference expects two lists of same type."); // FIXME adjust error message
1237

  
1238
				if(!Type.TypeEqual(la, coverType))
1239
					lhs.Accept(new OoaStaticListCastVisitor(coverType));
1240
				if(!Type.TypeEqual(lb, coverType))
1241
					rhs.Accept(new OoaStaticListCastVisitor(coverType));
1242

  
1243
				expression.SetType(coverType);
1244
			} else
1245
				expression.SetType(la);
1209 1246
			break;
1210 1247
		case inter:      // list of A * list of A -> list of A (does not respect dupes)
1211 1248
			if (lt.kind() != TypeKind.ListType ||
......
1213 1250
				return Error(expression, "Set intersection expects two lists.");
1214 1251
			la = (ListType)lt;
1215 1252
			lb = (ListType)rt;
1216
			if (!Type.TypeEqual(la.innerType(), lb.innerType()))
1217
				return Error(expression, "Set intersection expects two lists of same type.");
1218
			expression.SetType(la.maxNumberOfElements() > lb.maxNumberOfElements() ? la : lb);
1253
			if (!Type.TypeEqual(la.innerType(), lb.innerType())) {
1254
				final ListType coverType = (ListType) Type.CoverType(la, lb);
1255
				if(coverType == null)
1256
					return Error(expression, "Set intersection expects two lists of same type.");
1257
				if(!Type.TypeEqual(la, coverType))
1258
					lhs.Accept(new OoaStaticListCastVisitor(coverType));
1259
				if(!Type.TypeEqual(lb, coverType))
1260
					rhs.Accept(new OoaStaticListCastVisitor(coverType));
1261
				expression.SetType(coverType);
1262
			} else
1263
				expression.SetType(la.maxNumberOfElements() > lb.maxNumberOfElements() ? la : lb);
1219 1264
			break;
1220 1265
		case elemin:     // A * list of A -> bool
1221 1266
		case notelemin:  // A * list of A -> bool
......
1231 1276
			expression.SetType(new BoolType(null));
1232 1277
			break;
1233 1278
		case subset:     // list of A * list of A -> bool (does not respect dupes)
1234
			if (lt.kind() != TypeKind.ListType ||
1235
			rt.kind() != TypeKind.ListType)
1279
			if (lt.kind() != TypeKind.ListType || rt.kind() != TypeKind.ListType)
1236 1280
				return Error(expression, "Subset operation expects two lists.");
1237 1281
			la = (ListType)lt;
1238 1282
			lb = (ListType)rt;
1239
			if (!Type.TypeEqual(la.innerType(), lb.innerType()))
1240
				return Error(expression, "Subset operation expects two lists of same type.");
1283
			if (!Type.TypeEqual(la.innerType(), lb.innerType())) {
1284
				final ListType coverType = (ListType) Type.CoverType(la, lb);
1285
				if(coverType == null)
1286
					return Error(expression, "Subset operation expects two lists of same type.");
1287

  
1288
				if(!Type.TypeEqual(la, coverType))
1289
					lhs.Accept(new OoaStaticListCastVisitor(coverType));
1290
				if(!Type.TypeEqual(lb, coverType))
1291
					rhs.Accept(new OoaStaticListCastVisitor(coverType));
1292
			}
1241 1293
			expression.SetType(new BoolType(null));
1242 1294
			break;
1243 1295
		case union:      // list of A * list of A -> list of A (does not respect dupes)
......
1252 1304
			if (lb.innerType().kind() == TypeKind.Null || lb.maxNumberOfElements() == 0)
1253 1305
				return lhs;
1254 1306

  
1255
			if (!Type.TypeEqual(la.innerType(), lb.innerType()))
1256
				return Error(expression, "Set union expects two lists of same type.");
1257
			resultList = new ListType(la.innerType(),
1258
					la.maxNumberOfElements() + lb.maxNumberOfElements(), null);
1259
			expression.SetType(resultList);
1307
			if (!Type.TypeEqual(la.innerType(), lb.innerType())) {
1308
				final ListType coverType = (ListType) Type.CoverType(la, lb);
1309
				if(coverType == null)
1310
					return Error(expression, "Set union expects two lists of same type.");
1311
				if(!Type.TypeEqual(la, coverType))
1312
					lhs.Accept(new OoaStaticListCastVisitor(coverType));
1313
				if(!Type.TypeEqual(lb, coverType))
1314
					rhs.Accept(new OoaStaticListCastVisitor(coverType));
1315

  
1316
				expression.SetType(new ListType(coverType.innerType(), la.maxNumberOfElements() + lb.maxNumberOfElements(), null));
1317
			} else
1318
				expression.SetType(new ListType(la.innerType(), la.maxNumberOfElements() + lb.maxNumberOfElements(), null));
1260 1319
			break;
1261 1320

  
1262 1321
			/*numeric binary*/
......
1411 1470
							return Error(expression, "Free variables on both sides of the equality sign in tuple constructors.");
1412 1471
						m_matcherList.remove(rhs);
1413 1472
					}
1473

  
1474
					/* This adds support for "equality-style assignments" in expressions. Not yet supported by the backend though :(
1475
					if (lhs.type().kind() == TypeKind.Any || rhs.type().kind() == TypeKind.Any)
1476
					{
1477
						if (lhs.type().kind() == TypeKind.Any && rhs.type().kind() == TypeKind.Any)
1478
							return Error(expression, "Free variables on both sides of equality sign.");
1479
						// free var - so set type and mark initialized
1480
						final AnyType freevar =  lhs.type().kind() == TypeKind.Any ? (AnyType)lhs.type() : (AnyType)rhs.type();
1481
						freevar.VariableIdentifier().SetType(cover);
1482
						freevar.VariableIdentifier().SetInitialized(true);
1483
						lhs.SetType(cover);
1484
					}
1485
					*/
1414 1486
				}
1415 1487

  
1416 1488
				lhs = UnaryOperator.TryCoerceUp(lhs, cover);

Also available in: Unified diff