Browse Source

[WIP] implemented getReturnTyp() for some Statements

WIP: Not really tested and contains RuntimeExceptions that I used for testing
WIP: Not all possible return types covered
WIP: Not really clear which order/priority some statement contents should be checked
WIP: Not dealing with type mismatches or one branch returning something a subclass of the other branch's return type
kindlich 5 years ago
parent
commit
6086179d88
No known key found for this signature in database

+ 27
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/BlockStatement.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.codemodel.statement;
6
 package org.openzen.zenscript.codemodel.statement;
7
 
7
 
8
+import java.util.Arrays;
9
+import java.util.List;
8
 import java.util.function.Consumer;
10
 import java.util.function.Consumer;
11
+import java.util.stream.Collectors;
12
+
9
 import org.openzen.zencode.shared.CodePosition;
13
 import org.openzen.zencode.shared.CodePosition;
14
+import org.openzen.zencode.shared.CompileException;
15
+import org.openzen.zencode.shared.CompileExceptionCode;
10
 import org.openzen.zencode.shared.ConcatMap;
16
 import org.openzen.zencode.shared.ConcatMap;
11
 import org.openzen.zenscript.codemodel.expression.Expression;
17
 import org.openzen.zenscript.codemodel.expression.Expression;
12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
18
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
19
 import org.openzen.zenscript.codemodel.scope.TypeScope;
20
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
14
 import org.openzen.zenscript.codemodel.type.StoredType;
21
 import org.openzen.zenscript.codemodel.type.StoredType;
15
 
22
 
16
 /**
23
 /**
84
 			normalized[i++] = statement.normalize(scope, modified);
91
 			normalized[i++] = statement.normalize(scope, modified);
85
 		return new BlockStatement(position, normalized);
92
 		return new BlockStatement(position, normalized);
86
 	}
93
 	}
94
+
95
+	@Override
96
+	public StoredType getReturnType() {
97
+		final List<StoredType> collect = Arrays.stream(statements)
98
+				.map(Statement::getReturnType)
99
+				.distinct()
100
+				.collect(Collectors.toList());
101
+		if(collect.isEmpty())
102
+			return super.getReturnType();
103
+		if(collect.size() == 1)
104
+			return collect.get(0);
105
+
106
+		final long count = collect.stream().map(s -> s.type).distinct().count();
107
+		if(count == 1L)
108
+			return collect.get(0);
109
+
110
+		else
111
+			//TODO make this real?
112
+			throw new IllegalStateException("More than one possible storedType: " + count);
113
+	}
87
 }
114
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/DoWhileStatement.java View File

11
 import org.openzen.zenscript.codemodel.expression.Expression;
11
 import org.openzen.zenscript.codemodel.expression.Expression;
12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14
+import org.openzen.zenscript.codemodel.type.StoredType;
14
 
15
 
15
 /**
16
 /**
16
  *
17
  *
72
 		result.content = content.normalize(scope, modified.concat(this, result));
73
 		result.content = content.normalize(scope, modified.concat(this, result));
73
 		return result;
74
 		return result;
74
 	}
75
 	}
76
+
77
+	@Override
78
+	public StoredType getReturnType() {
79
+		return content.getReturnType();
80
+	}
75
 }
81
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/ForeachStatement.java View File

12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13
 import org.openzen.zenscript.codemodel.member.ref.IteratorMemberRef;
13
 import org.openzen.zenscript.codemodel.member.ref.IteratorMemberRef;
14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
15
+import org.openzen.zenscript.codemodel.type.StoredType;
15
 
16
 
16
 /**
17
 /**
17
  *
18
  *
77
 		result.content = content.normalize(scope, modified.concat(this, result));
78
 		result.content = content.normalize(scope, modified.concat(this, result));
78
 		return result;
79
 		return result;
79
 	}
80
 	}
81
+
82
+	@Override
83
+	public StoredType getReturnType() {
84
+		return content.getReturnType();
85
+	}
80
 }
86
 }

+ 20
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/IfStatement.java View File

83
 				onThen.normalize(scope, modified),
83
 				onThen.normalize(scope, modified),
84
 				onElse == null ? null : onElse.normalize(scope, modified));
84
 				onElse == null ? null : onElse.normalize(scope, modified));
85
 	}
85
 	}
86
+
87
+	@Override
88
+	public StoredType getReturnType() {
89
+		final StoredType thenType = onThen.getReturnType();
90
+		if(onElse == null)
91
+			return thenType;
92
+		final StoredType elseType = onElse.getReturnType();
93
+		if(thenType == elseType)
94
+			return thenType;
95
+
96
+		if(thenType == null)
97
+			return elseType;
98
+		if(elseType == null)
99
+			return thenType;
100
+
101
+		if(thenType.type != elseType.type)
102
+			//TODO make this real?
103
+			throw new IllegalStateException("If and Else return different things?!?");
104
+		return thenType;
105
+	}
86
 }
106
 }

+ 28
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/SwitchStatement.java View File

6
 package org.openzen.zenscript.codemodel.statement;
6
 package org.openzen.zenscript.codemodel.statement;
7
 
7
 
8
 import java.util.ArrayList;
8
 import java.util.ArrayList;
9
+import java.util.Arrays;
9
 import java.util.List;
10
 import java.util.List;
11
+import java.util.Objects;
10
 import java.util.function.Consumer;
12
 import java.util.function.Consumer;
13
+import java.util.stream.Collectors;
14
+
11
 import org.openzen.zencode.shared.CodePosition;
15
 import org.openzen.zencode.shared.CodePosition;
12
 import org.openzen.zencode.shared.ConcatMap;
16
 import org.openzen.zencode.shared.ConcatMap;
13
 import org.openzen.zenscript.codemodel.expression.Expression;
17
 import org.openzen.zenscript.codemodel.expression.Expression;
14
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
18
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
15
 import org.openzen.zenscript.codemodel.scope.TypeScope;
19
 import org.openzen.zenscript.codemodel.scope.TypeScope;
20
+import org.openzen.zenscript.codemodel.type.StoredType;
16
 
21
 
17
 /**
22
 /**
18
  *
23
  *
78
 		}
83
 		}
79
 		return result;
84
 		return result;
80
 	}
85
 	}
86
+
87
+	@Override
88
+	public StoredType getReturnType() {
89
+		//return super.getReturnType();
90
+		final List<StoredType> collect = cases.stream()
91
+				.flatMap(aCase -> Arrays.stream(aCase.statements))
92
+				.map(Statement::getReturnType)
93
+				.filter(Objects::nonNull)
94
+				.collect(Collectors.toList());
95
+
96
+		if(collect.isEmpty())
97
+			return null;
98
+
99
+		if(collect.size() == 1)
100
+			return collect.get(0);
101
+
102
+		final long c = collect.stream().map(s -> s.type).distinct().count();
103
+		if(c == 1)
104
+			return collect.get(0);
105
+		else
106
+			//TODO make this real
107
+			throw new IllegalStateException("Too many possible types: " + c);
108
+	}
81
 }
109
 }

+ 11
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/TryCatchStatement.java View File

12
 import org.openzen.zencode.shared.ConcatMap;
12
 import org.openzen.zencode.shared.ConcatMap;
13
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
15
+import org.openzen.zenscript.codemodel.type.StoredType;
15
 
16
 
16
 /**
17
 /**
17
  *
18
  *
89
 		Statement tFinallyClause = finallyClause == null ? null : finallyClause.normalize(scope, modified);
90
 		Statement tFinallyClause = finallyClause == null ? null : finallyClause.normalize(scope, modified);
90
 		return new TryCatchStatement(position, tResource, tContent, tCatchClauses, tFinallyClause);
91
 		return new TryCatchStatement(position, tResource, tContent, tCatchClauses, tFinallyClause);
91
 	}
92
 	}
93
+
94
+	@Override
95
+	public StoredType getReturnType() {
96
+		if(finallyClause != null && finallyClause.getReturnType() != null)
97
+			return finallyClause.getReturnType();
98
+
99
+		final StoredType contentType = content.getReturnType();
100
+		//TODO check catch clauses and do stuff I guess?
101
+		return contentType;
102
+	}
92
 }
103
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/WhileStatement.java View File

11
 import org.openzen.zenscript.codemodel.expression.Expression;
11
 import org.openzen.zenscript.codemodel.expression.Expression;
12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14
+import org.openzen.zenscript.codemodel.type.StoredType;
14
 
15
 
15
 /**
16
 /**
16
  *
17
  *
72
 		result.content = content.normalize(scope, modified.concat(this, result));
73
 		result.content = content.normalize(scope, modified.concat(this, result));
73
 		return result;
74
 		return result;
74
 	}
75
 	}
76
+
77
+	@Override
78
+	public StoredType getReturnType() {
79
+		return content.getReturnType();
80
+	}
75
 }
81
 }

Loading…
Cancel
Save