Просмотр исходного кода

- Added visitors for generic parameter bounds

- Separated this vs super base call into separate classes
Stan Hebben 6 лет назад
Родитель
Сommit
38bc27bdaf
23 измененных файлов: 114 добавлений и 37 удалений
  1. 14
    8
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/CompareType.java
  2. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallExpression.java
  3. 34
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorSuperCallExpression.java
  4. 3
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorThisCallExpression.java
  5. 3
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java
  6. 2
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/GenericParameterBound.java
  7. 16
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/GenericParameterBoundVisitor.java
  8. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterSuperBound.java
  9. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterTypeBound.java
  10. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/CallerMember.java
  11. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/CasterMember.java
  12. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstructorMember.java
  13. 3
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/FunctionalMember.java
  14. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/GetterMember.java
  15. 1
    7
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/MethodMember.java
  16. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/OperatorMember.java
  17. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/SetterMember.java
  18. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java
  19. 6
    1
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java
  20. 1
    1
      Parser/src/main/java/org/openzen/zenscript/lexer/CharStream.java
  21. 2
    3
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedClass.java
  22. 3
    3
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionCall.java
  23. 4
    1
      ScriptingExample/scripts/statements.zs

+ 14
- 8
CodeModel/src/main/java/org/openzen/zenscript/codemodel/CompareType.java Просмотреть файл

@@ -6,12 +6,18 @@ package org.openzen.zenscript.codemodel;
6 6
  * @author Stan Hebben
7 7
  */
8 8
 public enum CompareType {
9
-	LT,
10
-	GT,
11
-	EQ,
12
-	NE,
13
-	LE,
14
-	GE,
15
-	SAME,
16
-	NOTSAME
9
+	LT("<"),
10
+	GT(">"),
11
+	EQ("=="),
12
+	NE("!="),
13
+	LE("<="),
14
+	GE(">="),
15
+	SAME("==="),
16
+	NOTSAME("!==");
17
+	
18
+	public final String str;
19
+	
20
+	CompareType(String str) {
21
+		this.str = str;
22
+	}
17 23
 }

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallExpression.java Просмотреть файл

@@ -25,6 +25,10 @@ public class CallExpression extends Expression {
25 25
 		this.member = member;
26 26
 		this.arguments = arguments;
27 27
 	}
28
+	
29
+	public Expression getFirstArgument() {
30
+		return arguments.arguments[0];
31
+	}
28 32
 
29 33
 	@Override
30 34
 	public <T> T accept(ExpressionVisitor<T> visitor) {

+ 34
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorSuperCallExpression.java Просмотреть файл

@@ -0,0 +1,34 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.expression;
7
+
8
+import org.openzen.zenscript.codemodel.member.ConstructorMember;
9
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
10
+import org.openzen.zenscript.codemodel.type.ITypeID;
11
+import org.openzen.zenscript.shared.CodePosition;
12
+
13
+/**
14
+ *
15
+ * @author Hoofdgebruiker
16
+ */
17
+public class ConstructorSuperCallExpression extends Expression {
18
+	public final ITypeID objectType;
19
+	public final ConstructorMember constructor;
20
+	public final CallArguments arguments;
21
+	
22
+	public ConstructorSuperCallExpression(CodePosition position, ITypeID type, ConstructorMember constructor, CallArguments arguments) {
23
+		super(position, BasicTypeID.VOID);
24
+		
25
+		this.objectType = type;
26
+		this.constructor = constructor;
27
+		this.arguments = arguments;
28
+	}
29
+
30
+	@Override
31
+	public <T> T accept(ExpressionVisitor<T> visitor) {
32
+		return visitor.visitConstructorSuperCall(this);
33
+	}
34
+}

CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorCallExpression.java → CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorThisCallExpression.java Просмотреть файл

@@ -14,12 +14,12 @@ import org.openzen.zenscript.shared.CodePosition;
14 14
  *
15 15
  * @author Hoofdgebruiker
16 16
  */
17
-public class ConstructorCallExpression extends Expression {
17
+public class ConstructorThisCallExpression extends Expression {
18 18
 	public final ITypeID objectType;
19 19
 	public final ConstructorMember constructor;
20 20
 	public final CallArguments arguments;
21 21
 	
22
-	public ConstructorCallExpression(CodePosition position, ITypeID type, ConstructorMember constructor, CallArguments arguments) {
22
+	public ConstructorThisCallExpression(CodePosition position, ITypeID type, ConstructorMember constructor, CallArguments arguments) {
23 23
 		super(position, BasicTypeID.VOID);
24 24
 		
25 25
 		this.objectType = type;
@@ -29,6 +29,6 @@ public class ConstructorCallExpression extends Expression {
29 29
 
30 30
 	@Override
31 31
 	public <T> T accept(ExpressionVisitor<T> visitor) {
32
-		return visitor.visitConstructorCall(this);
32
+		return visitor.visitConstructorThisCall(this);
33 33
 	}
34 34
 }

+ 3
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java Просмотреть файл

@@ -62,7 +62,9 @@ public interface ExpressionVisitor<T> {
62 62
 	
63 63
 	public T visitConstantUShort(ConstantUShortExpression expression);
64 64
 	
65
-	public T visitConstructorCall(ConstructorCallExpression expression);
65
+	public T visitConstructorThisCall(ConstructorThisCallExpression expression);
66
+	
67
+	public T visitConstructorSuperCall(ConstructorSuperCallExpression expression);
66 68
 	
67 69
 	public T visitEnumConstant(EnumConstantExpression expression);
68 70
 	

+ 2
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/GenericParameterBound.java Просмотреть файл

@@ -16,6 +16,8 @@ import org.openzen.zenscript.codemodel.type.member.LocalMemberCache;
16 16
  * @author Hoofdgebruiker
17 17
  */
18 18
 public abstract class GenericParameterBound {
19
+	public abstract <T> T accept(GenericParameterBoundVisitor<T> visitor);
20
+	
19 21
 	public abstract void registerMembers(LocalMemberCache cache, TypeMembers type);
20 22
 	
21 23
 	public abstract boolean matches(ITypeID type);

+ 16
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/GenericParameterBoundVisitor.java Просмотреть файл

@@ -0,0 +1,16 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.generic;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public interface GenericParameterBoundVisitor<T> {
13
+	public T visitSuper(ParameterSuperBound bound);
14
+	
15
+	public T visitType(ParameterTypeBound bound);
16
+}

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterSuperBound.java Просмотреть файл

@@ -36,4 +36,9 @@ public class ParameterSuperBound extends GenericParameterBound {
36 36
 	public GenericParameterBound withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
37 37
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
38 38
 	}
39
+
40
+	@Override
41
+	public <T> T accept(GenericParameterBoundVisitor<T> visitor) {
42
+		return visitor.visitSuper(this);
43
+	}
39 44
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterTypeBound.java Просмотреть файл

@@ -40,4 +40,9 @@ public class ParameterTypeBound extends GenericParameterBound {
40 40
 	public GenericParameterBound withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
41 41
 		return new ParameterTypeBound(position, type.withGenericArguments(registry, arguments));
42 42
 	}
43
+
44
+	@Override
45
+	public <T> T accept(GenericParameterBoundVisitor<T> visitor) {
46
+		return visitor.visitType(this);
47
+	}
43 48
 }

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/CallerMember.java Просмотреть файл

@@ -20,7 +20,7 @@ import org.openzen.zenscript.shared.CodePosition;
20 20
  */
21 21
 public class CallerMember extends FunctionalMember {
22 22
 	public CallerMember(CodePosition position, int modifiers, FunctionHeader header) {
23
-		super(position, modifiers, header);
23
+		super(position, modifiers, "()", header);
24 24
 	}
25 25
 
26 26
 	@Override

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/CasterMember.java Просмотреть файл

@@ -26,7 +26,7 @@ public class CasterMember extends FunctionalMember implements ICasterMember {
26 26
 	public final ITypeID toType;
27 27
 	
28 28
 	public CasterMember(CodePosition position, int modifiers, ITypeID toType) {
29
-		super(position, modifiers, new FunctionHeader(toType));
29
+		super(position, modifiers, "as", new FunctionHeader(toType));
30 30
 		
31 31
 		this.toType = toType;
32 32
 	}

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstructorMember.java Просмотреть файл

@@ -21,7 +21,7 @@ import org.openzen.zenscript.shared.CodePosition;
21 21
  */
22 22
 public class ConstructorMember extends FunctionalMember {
23 23
 	public ConstructorMember(CodePosition position, int modifiers, FunctionHeader header) {
24
-		super(position, modifiers, new FunctionHeader(header.typeParameters, BasicTypeID.VOID, header.parameters));
24
+		super(position, modifiers, "this", new FunctionHeader(header.typeParameters, BasicTypeID.VOID, header.parameters));
25 25
 	}
26 26
 
27 27
 	@Override

+ 3
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/FunctionalMember.java Просмотреть файл

@@ -22,11 +22,13 @@ import org.openzen.zenscript.shared.CodePosition;
22 22
  */
23 23
 public abstract class FunctionalMember extends DefinitionMember implements ICallableMember {
24 24
 	public final FunctionHeader header;
25
+	public final String name;
25 26
 	public List<Statement> body;
26 27
 	
27
-	public FunctionalMember(CodePosition position, int modifiers, FunctionHeader header) {
28
+	public FunctionalMember(CodePosition position, int modifiers, String name, FunctionHeader header) {
28 29
 		super(position, modifiers);
29 30
 		
31
+		this.name = name;
30 32
 		this.header = header;
31 33
 	}
32 34
 	

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/GetterMember.java Просмотреть файл

@@ -28,7 +28,7 @@ public class GetterMember extends FunctionalMember implements IGettableMember {
28 28
 	public String compiledName;
29 29
 	
30 30
 	public GetterMember(CodePosition position, int modifiers, String name, ITypeID type) {
31
-		super(position, modifiers, new FunctionHeader(type));
31
+		super(position, modifiers, name, new FunctionHeader(type));
32 32
 		
33 33
 		this.name = name;
34 34
 		this.type = type;

+ 1
- 7
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/MethodMember.java Просмотреть файл

@@ -19,14 +19,8 @@ import org.openzen.zenscript.shared.CodePosition;
19 19
  * @author Hoofdgebruiker
20 20
  */
21 21
 public class MethodMember extends FunctionalMember {
22
-	public final String name;
23
-	
24
-	public String compiledName;
25
-	
26 22
 	public MethodMember(CodePosition position, int modifiers, String name, FunctionHeader header) {
27
-		super(position, modifiers, header);
28
-		
29
-		this.name = name;
23
+		super(position, modifiers, name, header);
30 24
 	}
31 25
 
32 26
 	@Override

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/OperatorMember.java Просмотреть файл

@@ -23,7 +23,7 @@ public class OperatorMember extends FunctionalMember {
23 23
 	public final OperatorType operator;
24 24
 	
25 25
 	public OperatorMember(CodePosition position, int modifiers, OperatorType operator, FunctionHeader header) {
26
-		super(position, modifiers, header);
26
+		super(position, modifiers, operator.operator, header);
27 27
 		
28 28
 		this.operator = operator;
29 29
 	}

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/SetterMember.java Просмотреть файл

@@ -25,7 +25,7 @@ public class SetterMember extends FunctionalMember {
25 25
 	public final ITypeID type;
26 26
 	
27 27
 	public SetterMember(CodePosition position, int modifiers, String name, ITypeID type) {
28
-		super(position, modifiers, new FunctionHeader(BasicTypeID.VOID, new FunctionParameter(type, "$")));
28
+		super(position, modifiers, name, new FunctionHeader(BasicTypeID.VOID, new FunctionParameter(type, "$")));
29 29
 		
30 30
 		this.name = name;
31 31
 		this.type = type;

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java Просмотреть файл

@@ -36,8 +36,8 @@ public enum BasicTypeID implements ITypeID {
36 36
 	
37 37
 	public static final List<ITypeID> HINT_BOOL = Collections.singletonList(BOOL);
38 38
 	
39
-	private final String name;
40
-	private final String camelCaseName;
39
+	public final String name;
40
+	public final String camelCaseName;
41 41
 	
42 42
 	BasicTypeID(String name, String camelCaseName) {
43 43
 		this.name = name;

+ 6
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Просмотреть файл

@@ -184,7 +184,12 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
184 184
     }
185 185
 
186 186
     @Override
187
-    public Void visitConstructorCall(ConstructorCallExpression expression) {
187
+    public Void visitConstructorThisCall(ConstructorThisCallExpression expression) {
188
+        return null;
189
+    }
190
+
191
+    @Override
192
+    public Void visitConstructorSuperCall(ConstructorSuperCallExpression expression) {
188 193
         return null;
189 194
     }
190 195
 

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/lexer/CharStream.java Просмотреть файл

@@ -10,7 +10,7 @@ package org.openzen.zenscript.lexer;
10 10
  */
11 11
 public class CharStream
12 12
 {
13
-    private char[] data;
13
+    private final char[] data;
14 14
     private int index;
15 15
 
16 16
     /**

+ 2
- 3
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedClass.java Просмотреть файл

@@ -19,7 +19,7 @@ import org.openzen.zenscript.shared.CodePosition;
19 19
 
20 20
 /**
21 21
  *
22
- * @author Hoofdgebruiker
22
+ * @author Stan Hebben
23 23
  */
24 24
 public class ParsedClass extends BaseParsedDefinition {
25 25
 	public static ParsedClass parseClass(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
@@ -40,7 +40,6 @@ public class ParsedClass extends BaseParsedDefinition {
40 40
 		return result;
41 41
 	}
42 42
 	
43
-	private final String name;
44 43
 	private final List<ParsedGenericParameter> genericParameters;
45 44
 	private final IParsedType superclass;
46 45
 	
@@ -48,7 +47,7 @@ public class ParsedClass extends BaseParsedDefinition {
48 47
 	
49 48
 	public ParsedClass(CodePosition position, int modifiers, String name, List<ParsedGenericParameter> genericParameters, IParsedType superclass, HighLevelDefinition outerDefinition) {
50 49
 		super(position, modifiers);
51
-		this.name = name;
50
+		
52 51
 		this.genericParameters = genericParameters;
53 52
 		this.superclass = superclass;
54 53
 		

+ 3
- 3
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionCall.java Просмотреть файл

@@ -10,7 +10,7 @@ import java.util.List;
10 10
 import org.openzen.zenscript.codemodel.FunctionHeader;
11 11
 import org.openzen.zenscript.codemodel.OperatorType;
12 12
 import org.openzen.zenscript.codemodel.expression.CallArguments;
13
-import org.openzen.zenscript.codemodel.expression.ConstructorCallExpression;
13
+import org.openzen.zenscript.codemodel.expression.ConstructorThisCallExpression;
14 14
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
15 15
 import org.openzen.zenscript.codemodel.member.ICallableMember;
16 16
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
@@ -52,7 +52,7 @@ public class ParsedExpressionCall extends ParsedExpression {
52 52
 			if (!(member instanceof ConstructorMember))
53 53
 				throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "Constructor is not a constructor!");
54 54
 			
55
-			return new ConstructorCallExpression(position, scope.getThisType().getSuperType(), (ConstructorMember) member, callArguments);
55
+			return new ConstructorThisCallExpression(position, scope.getThisType().getSuperType(), (ConstructorMember) member, callArguments);
56 56
 		} else if (receiver instanceof ParsedExpressionThis) {
57 57
 			// this call (intended as first call in constructor)
58 58
 			ITypeID targetType = scope.getThisType();
@@ -63,7 +63,7 @@ public class ParsedExpressionCall extends ParsedExpression {
63 63
 			if (!(member instanceof ConstructorMember))
64 64
 				throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "Constructor is not a constructor!");
65 65
 			
66
-			return new ConstructorCallExpression(position, scope.getThisType().getSuperType(), (ConstructorMember) member, callArguments);
66
+			return new ConstructorThisCallExpression(position, scope.getThisType(), (ConstructorMember) member, callArguments);
67 67
 		}
68 68
 
69 69
 		List<FunctionHeader> headers = cReceiver.getPossibleFunctionHeaders(scope, scope.hints, arguments.arguments.size());

+ 4
- 1
ScriptingExample/scripts/statements.zs Просмотреть файл

@@ -1,2 +1,5 @@
1
-while true
1
+var i = 0;
2
+while i < 10 {
2 3
 	println("Hello!");
4
+	i = i + 1;
5
+}

Загрузка…
Отмена
Сохранить