Browse Source

Adjusted handling of coalesce operator so it can also return an optional if the second operand is an optional.

Stan Hebben 6 years ago
parent
commit
a0f0b9d906

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GlobalTypeRegistry.java View File

125
 		}
125
 		}
126
 	}
126
 	}
127
 	
127
 	
128
-	private OptionalTypeID getOptional(ITypeID original) {
128
+	public OptionalTypeID getOptional(ITypeID original) {
129
 		if (optionalTypes.containsKey(original)) {
129
 		if (optionalTypes.containsKey(original)) {
130
 			return optionalTypes.get(original);
130
 			return optionalTypes.get(original);
131
 		} else {
131
 		} else {

+ 9
- 1
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionCoalesce.java View File

9
 import org.openzen.zenscript.codemodel.expression.Expression;
9
 import org.openzen.zenscript.codemodel.expression.Expression;
10
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
10
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
11
 import org.openzen.zenscript.codemodel.type.ITypeID;
11
 import org.openzen.zenscript.codemodel.type.ITypeID;
12
+import org.openzen.zenscript.codemodel.type.OptionalTypeID;
13
+import org.openzen.zenscript.codemodel.type.member.TypeMembers;
12
 import org.openzen.zenscript.linker.ExpressionScope;
14
 import org.openzen.zenscript.linker.ExpressionScope;
13
 import org.openzen.zenscript.shared.CodePosition;
15
 import org.openzen.zenscript.shared.CodePosition;
14
 import org.openzen.zenscript.shared.CompileException;
16
 import org.openzen.zenscript.shared.CompileException;
37
 			throw new CompileException(position, CompileExceptionCode.COALESCE_TARGET_NOT_OPTIONAL, "Type of the first expression is not optional");
39
 			throw new CompileException(position, CompileExceptionCode.COALESCE_TARGET_NOT_OPTIONAL, "Type of the first expression is not optional");
38
 		
40
 		
39
 		ITypeID resultType = cLeftType.getOptionalBase();
41
 		ITypeID resultType = cLeftType.getOptionalBase();
40
-		Expression cRight = right.compile(scope.withHint(resultType)).eval().castImplicit(position, scope, resultType);
42
+		Expression cRight = right.compile(scope.withHint(resultType)).eval();
43
+		
44
+		TypeMembers resultTypeMembers = scope.getTypeMembers(resultType);
45
+		resultType = resultTypeMembers.union(cRight.type);
46
+		cLeft = cLeft.castImplicit(position, scope, resultType.isOptional() ? resultType : scope.getTypeRegistry().getOptional(resultType));
47
+		cRight = cRight.castImplicit(position, scope, resultType);
48
+		
41
 		return new CoalesceExpression(position, cLeft, cRight);
49
 		return new CoalesceExpression(position, cLeft, cRight);
42
 	}
50
 	}
43
 
51
 

Loading…
Cancel
Save