Browse Source

- Added visitors for generic parameter bounds

- Separated this vs super base call into separate classes
Stan Hebben 6 years ago
parent
commit
38bc27bdaf
23 changed files with 114 additions and 37 deletions
  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 View File

6
  * @author Stan Hebben
6
  * @author Stan Hebben
7
  */
7
  */
8
 public enum CompareType {
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 View File

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

+ 34
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorSuperCallExpression.java View File

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 View File

14
  *
14
  *
15
  * @author Hoofdgebruiker
15
  * @author Hoofdgebruiker
16
  */
16
  */
17
-public class ConstructorCallExpression extends Expression {
17
+public class ConstructorThisCallExpression extends Expression {
18
 	public final ITypeID objectType;
18
 	public final ITypeID objectType;
19
 	public final ConstructorMember constructor;
19
 	public final ConstructorMember constructor;
20
 	public final CallArguments arguments;
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
 		super(position, BasicTypeID.VOID);
23
 		super(position, BasicTypeID.VOID);
24
 		
24
 		
25
 		this.objectType = type;
25
 		this.objectType = type;
29
 
29
 
30
 	@Override
30
 	@Override
31
 	public <T> T accept(ExpressionVisitor<T> visitor) {
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 View File

62
 	
62
 	
63
 	public T visitConstantUShort(ConstantUShortExpression expression);
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
 	public T visitEnumConstant(EnumConstantExpression expression);
69
 	public T visitEnumConstant(EnumConstantExpression expression);
68
 	
70
 	

+ 2
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/GenericParameterBound.java View File

16
  * @author Hoofdgebruiker
16
  * @author Hoofdgebruiker
17
  */
17
  */
18
 public abstract class GenericParameterBound {
18
 public abstract class GenericParameterBound {
19
+	public abstract <T> T accept(GenericParameterBoundVisitor<T> visitor);
20
+	
19
 	public abstract void registerMembers(LocalMemberCache cache, TypeMembers type);
21
 	public abstract void registerMembers(LocalMemberCache cache, TypeMembers type);
20
 	
22
 	
21
 	public abstract boolean matches(ITypeID type);
23
 	public abstract boolean matches(ITypeID type);

+ 16
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/GenericParameterBoundVisitor.java View File

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 View File

36
 	public GenericParameterBound withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
36
 	public GenericParameterBound withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
37
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
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 View File

40
 	public GenericParameterBound withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
40
 	public GenericParameterBound withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
41
 		return new ParameterTypeBound(position, type.withGenericArguments(registry, arguments));
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 View File

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

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/CasterMember.java View File

26
 	public final ITypeID toType;
26
 	public final ITypeID toType;
27
 	
27
 	
28
 	public CasterMember(CodePosition position, int modifiers, ITypeID toType) {
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
 		this.toType = toType;
31
 		this.toType = toType;
32
 	}
32
 	}

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstructorMember.java View File

21
  */
21
  */
22
 public class ConstructorMember extends FunctionalMember {
22
 public class ConstructorMember extends FunctionalMember {
23
 	public ConstructorMember(CodePosition position, int modifiers, FunctionHeader header) {
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
 	@Override
27
 	@Override

+ 3
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/FunctionalMember.java View File

22
  */
22
  */
23
 public abstract class FunctionalMember extends DefinitionMember implements ICallableMember {
23
 public abstract class FunctionalMember extends DefinitionMember implements ICallableMember {
24
 	public final FunctionHeader header;
24
 	public final FunctionHeader header;
25
+	public final String name;
25
 	public List<Statement> body;
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
 		super(position, modifiers);
29
 		super(position, modifiers);
29
 		
30
 		
31
+		this.name = name;
30
 		this.header = header;
32
 		this.header = header;
31
 	}
33
 	}
32
 	
34
 	

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/GetterMember.java View File

28
 	public String compiledName;
28
 	public String compiledName;
29
 	
29
 	
30
 	public GetterMember(CodePosition position, int modifiers, String name, ITypeID type) {
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
 		this.name = name;
33
 		this.name = name;
34
 		this.type = type;
34
 		this.type = type;

+ 1
- 7
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/MethodMember.java View File

19
  * @author Hoofdgebruiker
19
  * @author Hoofdgebruiker
20
  */
20
  */
21
 public class MethodMember extends FunctionalMember {
21
 public class MethodMember extends FunctionalMember {
22
-	public final String name;
23
-	
24
-	public String compiledName;
25
-	
26
 	public MethodMember(CodePosition position, int modifiers, String name, FunctionHeader header) {
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
 	@Override
26
 	@Override

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/OperatorMember.java View File

23
 	public final OperatorType operator;
23
 	public final OperatorType operator;
24
 	
24
 	
25
 	public OperatorMember(CodePosition position, int modifiers, OperatorType operator, FunctionHeader header) {
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
 		this.operator = operator;
28
 		this.operator = operator;
29
 	}
29
 	}

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/SetterMember.java View File

25
 	public final ITypeID type;
25
 	public final ITypeID type;
26
 	
26
 	
27
 	public SetterMember(CodePosition position, int modifiers, String name, ITypeID type) {
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
 		this.name = name;
30
 		this.name = name;
31
 		this.type = type;
31
 		this.type = type;

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java View File

36
 	
36
 	
37
 	public static final List<ITypeID> HINT_BOOL = Collections.singletonList(BOOL);
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
 	BasicTypeID(String name, String camelCaseName) {
42
 	BasicTypeID(String name, String camelCaseName) {
43
 		this.name = name;
43
 		this.name = name;

+ 6
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

184
     }
184
     }
185
 
185
 
186
     @Override
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
         return null;
193
         return null;
189
     }
194
     }
190
 
195
 

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/lexer/CharStream.java View File

10
  */
10
  */
11
 public class CharStream
11
 public class CharStream
12
 {
12
 {
13
-    private char[] data;
13
+    private final char[] data;
14
     private int index;
14
     private int index;
15
 
15
 
16
     /**
16
     /**

+ 2
- 3
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedClass.java View File

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

+ 3
- 3
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionCall.java View File

10
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.OperatorType;
11
 import org.openzen.zenscript.codemodel.OperatorType;
12
 import org.openzen.zenscript.codemodel.expression.CallArguments;
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
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
14
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
15
 import org.openzen.zenscript.codemodel.member.ICallableMember;
15
 import org.openzen.zenscript.codemodel.member.ICallableMember;
16
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
16
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
52
 			if (!(member instanceof ConstructorMember))
52
 			if (!(member instanceof ConstructorMember))
53
 				throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "Constructor is not a constructor!");
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
 		} else if (receiver instanceof ParsedExpressionThis) {
56
 		} else if (receiver instanceof ParsedExpressionThis) {
57
 			// this call (intended as first call in constructor)
57
 			// this call (intended as first call in constructor)
58
 			ITypeID targetType = scope.getThisType();
58
 			ITypeID targetType = scope.getThisType();
63
 			if (!(member instanceof ConstructorMember))
63
 			if (!(member instanceof ConstructorMember))
64
 				throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "Constructor is not a constructor!");
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
 		List<FunctionHeader> headers = cReceiver.getPossibleFunctionHeaders(scope, scope.hints, arguments.arguments.size());
69
 		List<FunctionHeader> headers = cReceiver.getPossibleFunctionHeaders(scope, scope.hints, arguments.arguments.size());

+ 4
- 1
ScriptingExample/scripts/statements.zs View File

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

Loading…
Cancel
Save