Explorar el Código

[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 hace 5 años
padre
commit
6086179d88
No se encontró ninguna clave conocida en la base de datos para esta firma

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

@@ -5,12 +5,19 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.statement;
7 7
 
8
+import java.util.Arrays;
9
+import java.util.List;
8 10
 import java.util.function.Consumer;
11
+import java.util.stream.Collectors;
12
+
9 13
 import org.openzen.zencode.shared.CodePosition;
14
+import org.openzen.zencode.shared.CompileException;
15
+import org.openzen.zencode.shared.CompileExceptionCode;
10 16
 import org.openzen.zencode.shared.ConcatMap;
11 17
 import org.openzen.zenscript.codemodel.expression.Expression;
12 18
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13 19
 import org.openzen.zenscript.codemodel.scope.TypeScope;
20
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
14 21
 import org.openzen.zenscript.codemodel.type.StoredType;
15 22
 
16 23
 /**
@@ -84,4 +91,24 @@ public class BlockStatement extends Statement {
84 91
 			normalized[i++] = statement.normalize(scope, modified);
85 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 Ver fichero

@@ -11,6 +11,7 @@ import org.openzen.zencode.shared.ConcatMap;
11 11
 import org.openzen.zenscript.codemodel.expression.Expression;
12 12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13 13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14
+import org.openzen.zenscript.codemodel.type.StoredType;
14 15
 
15 16
 /**
16 17
  *
@@ -72,4 +73,9 @@ public class DoWhileStatement extends LoopStatement {
72 73
 		result.content = content.normalize(scope, modified.concat(this, result));
73 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 Ver fichero

@@ -12,6 +12,7 @@ import org.openzen.zenscript.codemodel.expression.Expression;
12 12
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
13 13
 import org.openzen.zenscript.codemodel.member.ref.IteratorMemberRef;
14 14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
15
+import org.openzen.zenscript.codemodel.type.StoredType;
15 16
 
16 17
 /**
17 18
  *
@@ -77,4 +78,9 @@ public class ForeachStatement extends LoopStatement {
77 78
 		result.content = content.normalize(scope, modified.concat(this, result));
78 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 Ver fichero

@@ -83,4 +83,24 @@ public class IfStatement extends Statement {
83 83
 				onThen.normalize(scope, modified),
84 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 Ver fichero

@@ -6,13 +6,18 @@
6 6
 package org.openzen.zenscript.codemodel.statement;
7 7
 
8 8
 import java.util.ArrayList;
9
+import java.util.Arrays;
9 10
 import java.util.List;
11
+import java.util.Objects;
10 12
 import java.util.function.Consumer;
13
+import java.util.stream.Collectors;
14
+
11 15
 import org.openzen.zencode.shared.CodePosition;
12 16
 import org.openzen.zencode.shared.ConcatMap;
13 17
 import org.openzen.zenscript.codemodel.expression.Expression;
14 18
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
15 19
 import org.openzen.zenscript.codemodel.scope.TypeScope;
20
+import org.openzen.zenscript.codemodel.type.StoredType;
16 21
 
17 22
 /**
18 23
  *
@@ -78,4 +83,27 @@ public class SwitchStatement extends LoopStatement {
78 83
 		}
79 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 Ver fichero

@@ -12,6 +12,7 @@ import org.openzen.zencode.shared.CodePosition;
12 12
 import org.openzen.zencode.shared.ConcatMap;
13 13
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
14 14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
15
+import org.openzen.zenscript.codemodel.type.StoredType;
15 16
 
16 17
 /**
17 18
  *
@@ -89,4 +90,14 @@ public class TryCatchStatement extends Statement {
89 90
 		Statement tFinallyClause = finallyClause == null ? null : finallyClause.normalize(scope, modified);
90 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 Ver fichero

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

Loading…
Cancelar
Guardar