Browse Source

- Separated calls from casts

- Fixed priorities for brackets in the code formatter
- Added code position to definitions
- In a FieldMember the final status is now stored in the modifiers instead of a separate field
- WIP on implementing function definitions properly
Stan Hebben 6 years ago
parent
commit
ae21c007d9
32 changed files with 176 additions and 73 deletions
  1. 15
    11
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java
  2. 2
    2
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/OperatorPriority.java
  3. 4
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java
  4. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/AliasDefinition.java
  5. 5
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java
  6. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/EnumDefinition.java
  7. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java
  8. 18
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java
  9. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/InterfaceDefinition.java
  10. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/StructDefinition.java
  11. 6
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallArguments.java
  12. 32
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CastExpression.java
  13. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java
  14. 2
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java
  15. 8
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/CasterMember.java
  16. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/DefinitionMember.java
  17. 3
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/FieldMember.java
  18. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ICasterMember.java
  19. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialStaticMemberGroupExpression.java
  20. 5
    6
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java
  21. 6
    6
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java
  22. 11
    2
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java
  23. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedAlias.java
  24. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedClass.java
  25. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedEnum.java
  26. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedExpansion.java
  27. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedFunction.java
  28. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedInterface.java
  29. 1
    1
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedStruct.java
  30. 2
    1
      Parser/src/main/java/org/openzen/zenscript/parser/member/ParsedField.java
  31. 24
    7
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java
  32. 4
    3
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java

+ 15
- 11
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java View File

16
 import org.openzen.zenscript.codemodel.expression.CapturedLocalVariableExpression;
16
 import org.openzen.zenscript.codemodel.expression.CapturedLocalVariableExpression;
17
 import org.openzen.zenscript.codemodel.expression.CapturedParameterExpression;
17
 import org.openzen.zenscript.codemodel.expression.CapturedParameterExpression;
18
 import org.openzen.zenscript.codemodel.expression.CapturedThisExpression;
18
 import org.openzen.zenscript.codemodel.expression.CapturedThisExpression;
19
+import org.openzen.zenscript.codemodel.expression.CastExpression;
19
 import org.openzen.zenscript.codemodel.expression.CheckNullExpression;
20
 import org.openzen.zenscript.codemodel.expression.CheckNullExpression;
20
 import org.openzen.zenscript.codemodel.expression.CoalesceExpression;
21
 import org.openzen.zenscript.codemodel.expression.CoalesceExpression;
21
 import org.openzen.zenscript.codemodel.expression.ConditionalExpression;
22
 import org.openzen.zenscript.codemodel.expression.ConditionalExpression;
63
 import org.openzen.zenscript.codemodel.expression.StaticSetterExpression;
64
 import org.openzen.zenscript.codemodel.expression.StaticSetterExpression;
64
 import org.openzen.zenscript.codemodel.expression.ThisExpression;
65
 import org.openzen.zenscript.codemodel.expression.ThisExpression;
65
 import org.openzen.zenscript.codemodel.expression.WrapOptionalExpression;
66
 import org.openzen.zenscript.codemodel.expression.WrapOptionalExpression;
66
-import org.openzen.zenscript.codemodel.member.CasterMember;
67
 import org.openzen.zenscript.codemodel.member.OperatorMember;
67
 import org.openzen.zenscript.codemodel.member.OperatorMember;
68
 import org.openzen.zenscript.codemodel.type.ITypeID;
68
 import org.openzen.zenscript.codemodel.type.ITypeID;
69
 import org.openzen.zenscript.shared.StringUtils;
69
 import org.openzen.zenscript.shared.StringUtils;
232
 				default:
232
 				default:
233
 					throw new UnsupportedOperationException("Unknown operator: " + operator.operator);
233
 					throw new UnsupportedOperationException("Unknown operator: " + operator.operator);
234
 			}
234
 			}
235
-		} else if (expression.member instanceof CasterMember) {
236
-			CasterMember casterMember = (CasterMember) expression.member;
237
-			
238
-			StringBuilder result = new StringBuilder();
239
-			result.append(expression.target.accept(this).value);
240
-			if (!casterMember.isImplicit()) {
241
-				result.append(" as ");
242
-				result.append(casterMember.toType.accept(typeFormatter));
243
-			}
244
-			return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
245
 		} else {
235
 		} else {
246
 			StringBuilder result = new StringBuilder();
236
 			StringBuilder result = new StringBuilder();
247
 			result.append(expression.target.accept(this).value);
237
 			result.append(expression.target.accept(this).value);
263
 	}
253
 	}
264
 	
254
 	
265
 	private void format(StringBuilder result, CallArguments arguments) {
255
 	private void format(StringBuilder result, CallArguments arguments) {
256
+		if (arguments == null || arguments.typeArguments == null)
257
+			throw new IllegalArgumentException("Arguments cannot be null!");
258
+		
266
 		if (arguments.typeArguments.length > 0) {
259
 		if (arguments.typeArguments.length > 0) {
267
 			result.append("<");
260
 			result.append("<");
268
 			
261
 			
310
 	public ExpressionString visitCapturedThis(CapturedThisExpression expression) {
303
 	public ExpressionString visitCapturedThis(CapturedThisExpression expression) {
311
 		return new ExpressionString("this", OperatorPriority.PRIMARY);
304
 		return new ExpressionString("this", OperatorPriority.PRIMARY);
312
 	}
305
 	}
306
+	
307
+	@Override
308
+	public ExpressionString visitCast(CastExpression expression) {
309
+		StringBuilder result = new StringBuilder();
310
+		result.append(expression.target.accept(this).value);
311
+		if (!expression.isImplicit) {
312
+			result.append(" as ");
313
+			result.append(expression.member.toType.accept(typeFormatter));
314
+		}
315
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
316
+	}
313
 
317
 
314
 	@Override
318
 	@Override
315
 	public ExpressionString visitCheckNull(CheckNullExpression expression) {
319
 	public ExpressionString visitCheckNull(CheckNullExpression expression) {

+ 2
- 2
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/OperatorPriority.java View File

64
 	}
64
 	}
65
 	
65
 	
66
 	public static boolean shouldWrapLeft(OperatorPriority inner, OperatorPriority outer) {
66
 	public static boolean shouldWrapLeft(OperatorPriority inner, OperatorPriority outer) {
67
-		return inner == outer || inner.priority > outer.priority;
67
+		return inner == outer || inner.priority <= outer.priority;
68
 	}
68
 	}
69
 	
69
 	
70
 	public static boolean shouldWrapRight(OperatorPriority inner, OperatorPriority outer) {
70
 	public static boolean shouldWrapRight(OperatorPriority inner, OperatorPriority outer) {
71
-		return (inner == outer && inner.isCommutative) || (inner.priority > outer.priority);
71
+		return (inner == outer && inner.isCommutative) || (inner.priority <= outer.priority);
72
 	}
72
 	}
73
 }
73
 }

+ 4
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java View File

13
 import org.openzen.zenscript.codemodel.member.FieldMember;
13
 import org.openzen.zenscript.codemodel.member.FieldMember;
14
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
14
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
15
 import org.openzen.zenscript.codemodel.type.ITypeID;
15
 import org.openzen.zenscript.codemodel.type.ITypeID;
16
+import org.openzen.zenscript.shared.CodePosition;
16
 import org.openzen.zenscript.shared.Taggable;
17
 import org.openzen.zenscript.shared.Taggable;
17
 
18
 
18
 /**
19
 /**
20
  * @author Hoofdgebruiker
21
  * @author Hoofdgebruiker
21
  */
22
  */
22
 public abstract class HighLevelDefinition extends Taggable {
23
 public abstract class HighLevelDefinition extends Taggable {
24
+	public final CodePosition position;
23
 	public final ZSPackage pkg;
25
 	public final ZSPackage pkg;
24
 	public final String name;
26
 	public final String name;
25
 	public final int modifiers;
27
 	public final int modifiers;
29
 	public HighLevelDefinition outerDefinition;
31
 	public HighLevelDefinition outerDefinition;
30
 	public ITypeID superType;
32
 	public ITypeID superType;
31
 	
33
 	
32
-	public HighLevelDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
34
+	public HighLevelDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
35
+		this.position = position;
33
 		this.pkg = pkg;
36
 		this.pkg = pkg;
34
 		this.name = name;
37
 		this.name = name;
35
 		this.modifiers = modifiers;
38
 		this.modifiers = modifiers;

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/AliasDefinition.java View File

7
 
7
 
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
 import org.openzen.zenscript.codemodel.type.ITypeID;
9
 import org.openzen.zenscript.codemodel.type.ITypeID;
10
+import org.openzen.zenscript.shared.CodePosition;
10
 
11
 
11
 /**
12
 /**
12
  *
13
  *
15
 public class AliasDefinition extends HighLevelDefinition {
16
 public class AliasDefinition extends HighLevelDefinition {
16
 	public ITypeID type;
17
 	public ITypeID type;
17
 	
18
 	
18
-	public AliasDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
19
-		super(pkg, name, modifiers, outerDefinition);
19
+	public AliasDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
20
+		super(position, pkg, name, modifiers, outerDefinition);
20
 	}
21
 	}
21
 	
22
 	
22
 	public void setType(ITypeID type) {
23
 	public void setType(ITypeID type) {

+ 5
- 4
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java View File

7
 
7
 
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
 import org.openzen.zenscript.codemodel.type.ITypeID;
9
 import org.openzen.zenscript.codemodel.type.ITypeID;
10
+import org.openzen.zenscript.shared.CodePosition;
10
 
11
 
11
 /**
12
 /**
12
  *
13
  *
13
  * @author Hoofdgebruiker
14
  * @author Hoofdgebruiker
14
  */
15
  */
15
 public class ClassDefinition extends HighLevelDefinition {
16
 public class ClassDefinition extends HighLevelDefinition {
16
-	public ClassDefinition(ZSPackage pkg, String name, int modifiers) {
17
-		this(pkg, name, modifiers, null);
17
+	public ClassDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers) {
18
+		this(position, pkg, name, modifiers, null);
18
 	}
19
 	}
19
 	
20
 	
20
-	public ClassDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
21
-		super(pkg, name, modifiers, outerDefinition);
21
+	public ClassDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
22
+		super(position, pkg, name, modifiers, outerDefinition);
22
 	}
23
 	}
23
 	
24
 	
24
 	public void setSuperclass(ITypeID superclass) {
25
 	public void setSuperclass(ITypeID superclass) {

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/EnumDefinition.java View File

6
 package org.openzen.zenscript.codemodel.definition;
6
 package org.openzen.zenscript.codemodel.definition;
7
 
7
 
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.shared.CodePosition;
9
 
10
 
10
 /**
11
 /**
11
  *
12
  *
12
  * @author Hoofdgebruiker
13
  * @author Hoofdgebruiker
13
  */
14
  */
14
 public class EnumDefinition extends HighLevelDefinition {
15
 public class EnumDefinition extends HighLevelDefinition {
15
-	public EnumDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
16
-		super(pkg, name, modifiers, outerDefinition);
16
+	public EnumDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
17
+		super(position, pkg, name, modifiers, outerDefinition);
17
 	}
18
 	}
18
 
19
 
19
 	@Override
20
 	@Override

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java View File

7
 
7
 
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
 import org.openzen.zenscript.codemodel.type.ITypeID;
9
 import org.openzen.zenscript.codemodel.type.ITypeID;
10
+import org.openzen.zenscript.shared.CodePosition;
10
 
11
 
11
 /**
12
 /**
12
  *
13
  *
15
 public class ExpansionDefinition extends HighLevelDefinition {
16
 public class ExpansionDefinition extends HighLevelDefinition {
16
 	public ITypeID target;
17
 	public ITypeID target;
17
 	
18
 	
18
-	public ExpansionDefinition(ZSPackage pkg, int modifiers, HighLevelDefinition outerDefinition) {
19
-		super(pkg, null, modifiers, outerDefinition);
19
+	public ExpansionDefinition(CodePosition position, ZSPackage pkg, int modifiers, HighLevelDefinition outerDefinition) {
20
+		super(position, pkg, null, modifiers, outerDefinition);
20
 	}
21
 	}
21
 
22
 
22
 	@Override
23
 	@Override

+ 18
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java View File

8
 import java.util.List;
8
 import java.util.List;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11
+import org.openzen.zenscript.codemodel.Modifiers;
12
+import org.openzen.zenscript.codemodel.OperatorType;
13
+import org.openzen.zenscript.codemodel.member.OperatorMember;
11
 import org.openzen.zenscript.codemodel.statement.Statement;
14
 import org.openzen.zenscript.codemodel.statement.Statement;
15
+import org.openzen.zenscript.codemodel.type.member.DefinitionMemberGroup;
16
+import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
17
+import org.openzen.zenscript.shared.CodePosition;
12
 
18
 
13
 /**
19
 /**
14
  *
20
  *
17
 public class FunctionDefinition extends HighLevelDefinition {
23
 public class FunctionDefinition extends HighLevelDefinition {
18
 	public FunctionHeader header;
24
 	public FunctionHeader header;
19
 	public List<Statement> statements;
25
 	public List<Statement> statements;
26
+	public OperatorMember caller;
27
+	public DefinitionMemberGroup callerGroup = new DefinitionMemberGroup();
20
 	
28
 	
21
-	public FunctionDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
22
-		super(pkg, name, modifiers, outerDefinition);
29
+	public FunctionDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
30
+		super(position, pkg, name, modifiers, outerDefinition);
31
+	}
32
+	
33
+	public FunctionDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, FunctionHeader header) {
34
+		this(position, pkg, name, modifiers, (HighLevelDefinition) null);
35
+		setHeader(header);
23
 	}
36
 	}
24
 	
37
 	
25
 	public void setHeader(FunctionHeader header) {
38
 	public void setHeader(FunctionHeader header) {
26
 		this.header = header;
39
 		this.header = header;
40
+		addMember(caller = new OperatorMember(position, modifiers | Modifiers.MODIFIER_STATIC, OperatorType.CALL, header));
41
+		callerGroup.addMethod(caller, TypeMemberPriority.SPECIFIED);
27
 	}
42
 	}
28
 	
43
 	
29
 	public void setCode(List<Statement> statements) {
44
 	public void setCode(List<Statement> statements) {
30
 		this.statements = statements;
45
 		this.statements = statements;
46
+		caller.setBody(statements);
31
 	}
47
 	}
32
 
48
 
33
 	@Override
49
 	@Override

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/InterfaceDefinition.java View File

9
 import java.util.List;
9
 import java.util.List;
10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11
 import org.openzen.zenscript.codemodel.type.ITypeID;
11
 import org.openzen.zenscript.codemodel.type.ITypeID;
12
+import org.openzen.zenscript.shared.CodePosition;
12
 
13
 
13
 /**
14
 /**
14
  *
15
  *
17
 public class InterfaceDefinition extends HighLevelDefinition {
18
 public class InterfaceDefinition extends HighLevelDefinition {
18
 	private final List<ITypeID> baseInterfaces = new ArrayList<>();
19
 	private final List<ITypeID> baseInterfaces = new ArrayList<>();
19
 	
20
 	
20
-	public InterfaceDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
21
-		super(pkg, name, modifiers, outerDefinition);
21
+	public InterfaceDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
22
+		super(position, pkg, name, modifiers, outerDefinition);
22
 	}
23
 	}
23
 	
24
 	
24
 	public void addBaseInterface(ITypeID baseInterface) {
25
 	public void addBaseInterface(ITypeID baseInterface) {

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/StructDefinition.java View File

6
 package org.openzen.zenscript.codemodel.definition;
6
 package org.openzen.zenscript.codemodel.definition;
7
 
7
 
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.shared.CodePosition;
9
 
10
 
10
 /**
11
 /**
11
  *
12
  *
12
  * @author Hoofdgebruiker
13
  * @author Hoofdgebruiker
13
  */
14
  */
14
 public class StructDefinition extends HighLevelDefinition {
15
 public class StructDefinition extends HighLevelDefinition {
15
-	public StructDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
16
-		super(pkg, name, modifiers, outerDefinition);
16
+	public StructDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
17
+		super(position, pkg, name, modifiers, outerDefinition);
17
 	}
18
 	}
18
 
19
 
19
 	@Override
20
 	@Override

+ 6
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallArguments.java View File

12
  * @author Hoofdgebruiker
12
  * @author Hoofdgebruiker
13
  */
13
  */
14
 public class CallArguments {
14
 public class CallArguments {
15
-	public static final CallArguments EMPTY = new CallArguments(new Expression[0]);
16
 	public static final ITypeID[] NO_TYPE_ARGUMENTS = new ITypeID[0];
15
 	public static final ITypeID[] NO_TYPE_ARGUMENTS = new ITypeID[0];
16
+	public static final CallArguments EMPTY = new CallArguments(new Expression[0]);
17
 	
17
 	
18
 	public final ITypeID[] typeArguments;
18
 	public final ITypeID[] typeArguments;
19
 	public final Expression[] arguments;
19
 	public final Expression[] arguments;
24
 	}
24
 	}
25
 	
25
 	
26
 	public CallArguments(ITypeID[] typeArguments, Expression[] arguments) {
26
 	public CallArguments(ITypeID[] typeArguments, Expression[] arguments) {
27
+		if (typeArguments == null)
28
+			throw new IllegalArgumentException("Type arguments cannot be null!");
29
+		if (arguments == null)
30
+			throw new IllegalArgumentException("Arguments cannot be null!");
31
+		
27
 		this.typeArguments = typeArguments;
32
 		this.typeArguments = typeArguments;
28
 		this.arguments = arguments;
33
 		this.arguments = arguments;
29
 	}
34
 	}

+ 32
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CastExpression.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.CasterMember;
9
+import org.openzen.zenscript.shared.CodePosition;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public class CastExpression extends Expression {
16
+	public final Expression target;
17
+	public final CasterMember member;
18
+	public final boolean isImplicit;
19
+	
20
+	public CastExpression(CodePosition position, Expression target, CasterMember member, boolean isImplicit) {
21
+		super(position, member.getTargetType());
22
+		
23
+		this.target = target;
24
+		this.member = member;
25
+		this.isImplicit = isImplicit;
26
+	}
27
+
28
+	@Override
29
+	public <T> T accept(ExpressionVisitor<T> visitor) {
30
+		return visitor.visitCast(this);
31
+	}
32
+}

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java View File

51
 	}
51
 	}
52
 	
52
 	
53
 	public Expression castImplicit(CodePosition position, TypeScope scope, ITypeID asType) {
53
 	public Expression castImplicit(CodePosition position, TypeScope scope, ITypeID asType) {
54
-		return scope.getTypeMembers(type).castImplicit(position, this, asType);
54
+		return scope.getTypeMembers(type).castImplicit(position, this, asType, true);
55
 	}
55
 	}
56
 	
56
 	
57
 	@Override
57
 	@Override

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

30
 	
30
 	
31
 	public T visitCapturedThis(CapturedThisExpression expression);
31
 	public T visitCapturedThis(CapturedThisExpression expression);
32
 	
32
 	
33
+	public T visitCast(CastExpression expression);
34
+	
33
 	public T visitCheckNull(CheckNullExpression expression);
35
 	public T visitCheckNull(CheckNullExpression expression);
34
 	
36
 	
35
 	public T visitCoalesce(CoalesceExpression expression);
37
 	public T visitCoalesce(CoalesceExpression expression);

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

9
 import org.openzen.zenscript.codemodel.FunctionHeader;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10
 import org.openzen.zenscript.codemodel.Modifiers;
10
 import org.openzen.zenscript.codemodel.Modifiers;
11
 import org.openzen.zenscript.codemodel.expression.CallArguments;
11
 import org.openzen.zenscript.codemodel.expression.CallArguments;
12
-import org.openzen.zenscript.codemodel.expression.CallExpression;
12
+import org.openzen.zenscript.codemodel.expression.CastExpression;
13
 import org.openzen.zenscript.codemodel.expression.Expression;
13
 import org.openzen.zenscript.codemodel.expression.Expression;
14
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
15
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
15
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
52
 	}
52
 	}
53
 
53
 
54
 	@Override
54
 	@Override
55
-	public Expression cast(CodePosition position, Expression value, ITypeID toType) {
56
-		return new CallExpression(position, value, this, header, CallArguments.EMPTY);
55
+	public Expression call(CodePosition position, Expression target, FunctionHeader instancedHeader, CallArguments arguments) {
56
+		throw new UnsupportedOperationException("Cannot call a caster!");
57
+	}
58
+
59
+	@Override
60
+	public Expression cast(CodePosition position, Expression value, boolean implicit) {
61
+		return new CastExpression(position, value, this, implicit);
57
 	}
62
 	}
58
 	
63
 	
59
 	@Override
64
 	@Override

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/DefinitionMember.java View File

30
 	public boolean isStatic() {
30
 	public boolean isStatic() {
31
 		return Modifiers.isStatic(modifiers);
31
 		return Modifiers.isStatic(modifiers);
32
 	}
32
 	}
33
+	
34
+	public boolean isFinal() {
35
+		return Modifiers.isFinal(modifiers);
36
+	}
33
 }
37
 }

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

6
 package org.openzen.zenscript.codemodel.member;
6
 package org.openzen.zenscript.codemodel.member;
7
 
7
 
8
 import java.util.Map;
8
 import java.util.Map;
9
+import org.openzen.zenscript.codemodel.Modifiers;
9
 import org.openzen.zenscript.codemodel.expression.Expression;
10
 import org.openzen.zenscript.codemodel.expression.Expression;
10
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
11
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
11
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
12
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
21
 public class FieldMember extends DefinitionMember {
22
 public class FieldMember extends DefinitionMember {
22
 	public final String name;
23
 	public final String name;
23
 	public final ITypeID type;
24
 	public final ITypeID type;
24
-	public final boolean isFinal;
25
 	public Expression initializer;
25
 	public Expression initializer;
26
 	
26
 	
27
-	public FieldMember(CodePosition position, int modifiers, String name, ITypeID type, boolean isFinal) {
27
+	public FieldMember(CodePosition position, int modifiers, String name, ITypeID type) {
28
 		super(position, modifiers);
28
 		super(position, modifiers);
29
 		
29
 		
30
 		this.name = name;
30
 		this.name = name;
31
 		this.type = type;
31
 		this.type = type;
32
-		this.isFinal = isFinal;
33
 	}
32
 	}
34
 	
33
 	
35
 	public void setInitializer(Expression initializer) {
34
 	public void setInitializer(Expression initializer) {
43
 
42
 
44
 	@Override
43
 	@Override
45
 	public DefinitionMember instance(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> mapping) {
44
 	public DefinitionMember instance(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> mapping) {
46
-		return new FieldMember(position, modifiers, name, type.withGenericArguments(registry, mapping), isFinal);
45
+		return new FieldMember(position, modifiers, name, type.withGenericArguments(registry, mapping));
47
 	}
46
 	}
48
 
47
 
49
 	@Override
48
 	@Override

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

16
 public interface ICasterMember extends IDefinitionMember {
16
 public interface ICasterMember extends IDefinitionMember {
17
 	public ITypeID getTargetType();
17
 	public ITypeID getTargetType();
18
 	
18
 	
19
-	public Expression cast(CodePosition position, Expression value, ITypeID toType);
19
+	public Expression cast(CodePosition position, Expression value, boolean implicit);
20
 	
20
 	
21
 	public boolean isImplicit();
21
 	public boolean isImplicit();
22
 }
22
 }

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialStaticMemberGroupExpression.java View File

62
 
62
 
63
 	@Override
63
 	@Override
64
 	public Expression call(CodePosition position, TypeScope scope, List<ITypeID> hints, CallArguments arguments) {
64
 	public Expression call(CodePosition position, TypeScope scope, List<ITypeID> hints, CallArguments arguments) {
65
-		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
65
+		return group.callStatic(position, scope, arguments);
66
 	}
66
 	}
67
 	
67
 	
68
 	@Override
68
 	@Override

+ 5
- 6
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java View File

16
 import org.openzen.zenscript.codemodel.definition.ClassDefinition;
16
 import org.openzen.zenscript.codemodel.definition.ClassDefinition;
17
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
17
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
18
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
18
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
19
-import org.openzen.zenscript.codemodel.expression.CallArguments;
20
 import org.openzen.zenscript.codemodel.expression.CallTranslator;
19
 import org.openzen.zenscript.codemodel.expression.CallTranslator;
21
 import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
20
 import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
22
 import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
21
 import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
144
 			FunctionHeader addHeader = new FunctionHeader(VOID, new FunctionParameter(baseType, "value"));
143
 			FunctionHeader addHeader = new FunctionHeader(VOID, new FunctionParameter(baseType, "value"));
145
 			members.addMethod(new MethodMember(CodePosition.BUILTIN, 0, "add", addHeader), TypeMemberPriority.SPECIFIED);
144
 			members.addMethod(new MethodMember(CodePosition.BUILTIN, 0, "add", addHeader), TypeMemberPriority.SPECIFIED);
146
 
145
 
147
-			members.addField(new FieldMember(CodePosition.BUILTIN, 0, "length", INT, false), TypeMemberPriority.SPECIFIED);
146
+			members.addField(new FieldMember(CodePosition.BUILTIN, Modifiers.MODIFIER_FINAL, "length", INT), TypeMemberPriority.SPECIFIED);
148
 		}
147
 		}
149
 
148
 
150
 		members.addGetter(new GetterMember(CodePosition.BUILTIN, 0, "empty", BOOL), TypeMemberPriority.SPECIFIED);
149
 		members.addGetter(new GetterMember(CodePosition.BUILTIN, 0, "empty", BOOL), TypeMemberPriority.SPECIFIED);
172
 		
171
 		
173
 		members.addOperator(new OperatorMember(BUILTIN, 0, OperatorType.CONTAINS, new FunctionHeader(BOOL, new FunctionParameter(keyType, "key"))), TypeMemberPriority.SPECIFIED);
172
 		members.addOperator(new OperatorMember(BUILTIN, 0, OperatorType.CONTAINS, new FunctionHeader(BOOL, new FunctionParameter(keyType, "key"))), TypeMemberPriority.SPECIFIED);
174
 
173
 
175
-		members.addField(new FieldMember(BUILTIN, 0, "length", INT, true), TypeMemberPriority.SPECIFIED);
174
+		members.addField(new FieldMember(BUILTIN, Modifiers.MODIFIER_FINAL, "length", INT), TypeMemberPriority.SPECIFIED);
176
 		members.addGetter(new GetterMember(BUILTIN, 0, "empty", BOOL), TypeMemberPriority.SPECIFIED);
175
 		members.addGetter(new GetterMember(BUILTIN, 0, "empty", BOOL), TypeMemberPriority.SPECIFIED);
177
 		members.addGetter(new GetterMember(BUILTIN, 0, "keys", cache.getRegistry().getArray(keyType, 1)), TypeMemberPriority.SPECIFIED);
176
 		members.addGetter(new GetterMember(BUILTIN, 0, "keys", cache.getRegistry().getArray(keyType, 1)), TypeMemberPriority.SPECIFIED);
178
 		return null;
177
 		return null;
252
 		ITypeID fromType = range.from;
251
 		ITypeID fromType = range.from;
253
 		ITypeID toType = range.to;
252
 		ITypeID toType = range.to;
254
 
253
 
255
-		members.addField(new FieldMember(BUILTIN, 0, "from", fromType, true), TypeMemberPriority.SPECIFIED);
256
-		members.addField(new FieldMember(BUILTIN, 0, "to", toType, true), TypeMemberPriority.SPECIFIED);
254
+		members.addField(new FieldMember(BUILTIN, Modifiers.MODIFIER_FINAL, "from", fromType), TypeMemberPriority.SPECIFIED);
255
+		members.addField(new FieldMember(BUILTIN, Modifiers.MODIFIER_FINAL, "to", toType), TypeMemberPriority.SPECIFIED);
257
 		members.addIterator(new RangeIterator(range), TypeMemberPriority.SPECIFIED);
256
 		members.addIterator(new RangeIterator(range), TypeMemberPriority.SPECIFIED);
258
 		return null;
257
 		return null;
259
 	}
258
 	}
478
 	}
477
 	}
479
 	
478
 	
480
 	private static CallTranslator castedTargetCall(FunctionalMember member, CasterMember caster) {
479
 	private static CallTranslator castedTargetCall(FunctionalMember member, CasterMember caster) {
481
-		return call -> member.call(call.position, caster.call(call.position, call.target, CallArguments.EMPTY), call.arguments);
480
+		return call -> member.call(call.position, caster.cast(call.position, call.target, true), call.arguments);
482
 	}
481
 	}
483
 }
482
 }

+ 6
- 6
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java View File

305
 		return false;
305
 		return false;
306
 	}
306
 	}
307
 	
307
 	
308
-	public Expression castImplicit(CodePosition position, Expression value, ITypeID toType) {
308
+	public Expression castImplicit(CodePosition position, Expression value, ITypeID toType, boolean implicit) {
309
 		if (toType == type || toType == BasicTypeID.UNDETERMINED)
309
 		if (toType == type || toType == BasicTypeID.UNDETERMINED)
310
 			return value;
310
 			return value;
311
 		if (toType == null)
311
 		if (toType == null)
314
 		if (type == BasicTypeID.NULL && toType.isOptional())
314
 		if (type == BasicTypeID.NULL && toType.isOptional())
315
 			return new NullExpression(position, toType);
315
 			return new NullExpression(position, toType);
316
 		if (toType.isOptional() && canCastImplicit(toType.unwrap()))
316
 		if (toType.isOptional() && canCastImplicit(toType.unwrap()))
317
-			return new WrapOptionalExpression(position, castImplicit(position, value, toType.unwrap()), toType);
317
+			return new WrapOptionalExpression(position, castImplicit(position, value, toType.unwrap(), implicit), toType);
318
 		if (toType.isConst() && canCastImplicit(toType.unwrap()))
318
 		if (toType.isConst() && canCastImplicit(toType.unwrap()))
319
-			return new MakeConstExpression(position, castImplicit(position, value, toType.unwrap()), toType);
319
+			return new MakeConstExpression(position, castImplicit(position, value, toType.unwrap(), implicit), toType);
320
 		if (type.isOptional() && type.unwrap() == toType)
320
 		if (type.isOptional() && type.unwrap() == toType)
321
 			return new CheckNullExpression(position, value);
321
 			return new CheckNullExpression(position, value);
322
 		
322
 		
323
 		for (TypeMember<ICasterMember> caster : casters) {
323
 		for (TypeMember<ICasterMember> caster : casters) {
324
 			if (caster.member.isImplicit() && toType == caster.member.getTargetType())
324
 			if (caster.member.isImplicit() && toType == caster.member.getTargetType())
325
-				return caster.member.cast(position, value, toType);
325
+				return caster.member.cast(position, value, implicit);
326
 		}
326
 		}
327
 		for (TypeMember<ImplementationMember> implementation : implementations) {
327
 		for (TypeMember<ImplementationMember> implementation : implementations) {
328
 			if (implementation.member.type == toType)
328
 			if (implementation.member.type == toType)
334
 	
334
 	
335
 	public Expression castExplicit(CodePosition position, Expression value, ITypeID toType, boolean optional) {
335
 	public Expression castExplicit(CodePosition position, Expression value, ITypeID toType, boolean optional) {
336
 		if (this.canCastImplicit(toType))
336
 		if (this.canCastImplicit(toType))
337
-			return castImplicit(position, value, toType);
337
+			return castImplicit(position, value, toType, false);
338
 		
338
 		
339
 		for (TypeMember<ICasterMember> caster : casters)
339
 		for (TypeMember<ICasterMember> caster : casters)
340
 			if (toType == caster.member.getTargetType())
340
 			if (toType == caster.member.getTargetType())
341
-				return caster.member.cast(position, value, toType);
341
+				return caster.member.cast(position, value, false);
342
 		
342
 		
343
 		throw new CompileException(position, CompileExceptionCode.INVALID_CAST, "Cannot cast " + toString() + " to " + toType + ", even explicitly");
343
 		throw new CompileException(position, CompileExceptionCode.INVALID_CAST, "Cannot cast " + toString() + " to " + toType + ", even explicitly");
344
 	}
344
 	}

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

85
     public Void visitCapturedThis(CapturedThisExpression expression) {
85
     public Void visitCapturedThis(CapturedThisExpression expression) {
86
         return null;
86
         return null;
87
     }
87
     }
88
+	
89
+	@Override
90
+    public Void visitCast(CastExpression expression) {
91
+		expression.target.accept(this);
92
+        if (!checkAndExecuteByteCodeImplementation(expression.member) && !checkAndExecuteMethodInfo(expression.member))
93
+            throw new IllegalStateException("Call target has no method info!");
94
+		
95
+        return null;
96
+	}
88
 
97
 
89
     @Override
98
     @Override
90
     public Void visitCheckNull(CheckNullExpression expression) {
99
     public Void visitCheckNull(CheckNullExpression expression) {
326
 
335
 
327
     @Override
336
     @Override
328
     public Void visitSetField(SetFieldExpression expression) {
337
     public Void visitSetField(SetFieldExpression expression) {
329
-        if (expression.field.isFinal)
338
+        if (expression.field.isFinal())
330
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
339
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
331
         expression.value.accept(this);
340
         expression.value.accept(this);
332
         if (!checkAndPutFieldInfo(expression.field, false))
341
         if (!checkAndPutFieldInfo(expression.field, false))
353
 
362
 
354
     @Override
363
     @Override
355
     public Void visitSetStaticField(SetStaticFieldExpression expression) {
364
     public Void visitSetStaticField(SetStaticFieldExpression expression) {
356
-        if (expression.field.isFinal)
365
+        if (expression.field.isFinal())
357
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
366
             throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final field!");
358
         expression.value.accept(this);
367
         expression.value.accept(this);
359
         if (!checkAndPutFieldInfo(expression.field, true))
368
         if (!checkAndPutFieldInfo(expression.field, true))

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedAlias.java View File

43
 		this.parameters = parameters;
43
 		this.parameters = parameters;
44
 		this.type = type;
44
 		this.type = type;
45
 		
45
 		
46
-		compiled = new AliasDefinition(pkg, name, modifiers, outerDefinition);
46
+		compiled = new AliasDefinition(position, pkg, name, modifiers, outerDefinition);
47
 	}
47
 	}
48
 	
48
 	
49
 	@Override
49
 	@Override

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

52
 		this.genericParameters = genericParameters;
52
 		this.genericParameters = genericParameters;
53
 		this.superclass = superclass;
53
 		this.superclass = superclass;
54
 		
54
 		
55
-		compiled = new ClassDefinition(pkg, name, modifiers, outerDefinition);
55
+		compiled = new ClassDefinition(position, pkg, name, modifiers, outerDefinition);
56
 		for (ParsedGenericParameter parameter : genericParameters)
56
 		for (ParsedGenericParameter parameter : genericParameters)
57
 			compiled.addGenericParameter(parameter.compiled);
57
 			compiled.addGenericParameter(parameter.compiled);
58
 	}
58
 	}

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedEnum.java View File

51
 		
51
 		
52
 		this.enumValues = enumValues;
52
 		this.enumValues = enumValues;
53
 		
53
 		
54
-		compiled = new EnumDefinition(pkg, name, modifiers, outerDefinition);
54
+		compiled = new EnumDefinition(position, pkg, name, modifiers, outerDefinition);
55
 	}
55
 	}
56
 
56
 
57
 	@Override
57
 	@Override

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedExpansion.java View File

45
 		this.parameters = parameters;
45
 		this.parameters = parameters;
46
 		this.target = target;
46
 		this.target = target;
47
 		
47
 		
48
-		this.compiled = new ExpansionDefinition(pkg, modifiers, outerDefinition);
48
+		this.compiled = new ExpansionDefinition(position, pkg, modifiers, outerDefinition);
49
 		for (ParsedGenericParameter parameter : parameters)
49
 		for (ParsedGenericParameter parameter : parameters)
50
 			compiled.addGenericParameter(parameter.compiled);
50
 			compiled.addGenericParameter(parameter.compiled);
51
 	}
51
 	}

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedFunction.java View File

41
 		this.header = header;
41
 		this.header = header;
42
 		this.body = body;
42
 		this.body = body;
43
 		
43
 		
44
-		compiled = new FunctionDefinition(pkg, name, modifiers, outerDefinition);
44
+		compiled = new FunctionDefinition(position, pkg, name, modifiers, outerDefinition);
45
 	}
45
 	}
46
 
46
 
47
 	@Override
47
 	@Override

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedInterface.java View File

54
 		this.genericParameters = genericParameters;
54
 		this.genericParameters = genericParameters;
55
 		this.superInterfaces = superInterfaces;
55
 		this.superInterfaces = superInterfaces;
56
 		
56
 		
57
-		compiled = new InterfaceDefinition(pkg, name, modifiers, outerDefinition);
57
+		compiled = new InterfaceDefinition(position, pkg, name, modifiers, outerDefinition);
58
 	}
58
 	}
59
 
59
 
60
 	@Override
60
 	@Override

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedStruct.java View File

42
 		
42
 		
43
 		this.parameters = parameters;
43
 		this.parameters = parameters;
44
 		
44
 		
45
-		compiled = new StructDefinition(pkg, name, modifiers, outerDefinition);
45
+		compiled = new StructDefinition(position, pkg, name, modifiers, outerDefinition);
46
 		for (ParsedGenericParameter parameter : parameters)
46
 		for (ParsedGenericParameter parameter : parameters)
47
 			compiled.addGenericParameter(parameter.compiled);
47
 			compiled.addGenericParameter(parameter.compiled);
48
 	}
48
 	}

+ 2
- 1
Parser/src/main/java/org/openzen/zenscript/parser/member/ParsedField.java View File

6
 package org.openzen.zenscript.parser.member;
6
 package org.openzen.zenscript.parser.member;
7
 
7
 
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.codemodel.Modifiers;
9
 import org.openzen.zenscript.codemodel.expression.Expression;
10
 import org.openzen.zenscript.codemodel.expression.Expression;
10
 import org.openzen.zenscript.codemodel.member.FieldMember;
11
 import org.openzen.zenscript.codemodel.member.FieldMember;
11
 import org.openzen.zenscript.linker.BaseScope;
12
 import org.openzen.zenscript.linker.BaseScope;
44
 
45
 
45
 	@Override
46
 	@Override
46
 	public void linkTypes(BaseScope scope) {
47
 	public void linkTypes(BaseScope scope) {
47
-		compiled = new FieldMember(position, modifiers, name, type.compile(scope), isFinal);
48
+		compiled = new FieldMember(position, modifiers | (isFinal ? Modifiers.MODIFIER_FINAL : 0), name, type.compile(scope));
48
 	}
49
 	}
49
 
50
 
50
 	@Override
51
 	@Override

+ 24
- 7
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.scriptingexample;
6
 package org.openzen.zenscript.scriptingexample;
7
 
7
 
8
+import java.io.PrintStream;
8
 import java.util.ArrayList;
9
 import java.util.ArrayList;
9
 import java.util.HashMap;
10
 import java.util.HashMap;
10
 import java.util.List;
11
 import java.util.List;
14
 import org.openzen.zenscript.codemodel.Modifiers;
15
 import org.openzen.zenscript.codemodel.Modifiers;
15
 import org.openzen.zenscript.codemodel.definition.ClassDefinition;
16
 import org.openzen.zenscript.codemodel.definition.ClassDefinition;
16
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
17
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
18
+import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
17
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
19
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
18
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
20
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
19
 import org.openzen.zenscript.codemodel.member.FieldMember;
21
 import org.openzen.zenscript.codemodel.member.FieldMember;
25
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
27
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
26
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
28
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
27
 import org.openzen.zenscript.codemodel.type.ITypeID;
29
 import org.openzen.zenscript.codemodel.type.ITypeID;
30
+import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
28
 import org.openzen.zenscript.javabytecode.JavaClassInfo;
31
 import org.openzen.zenscript.javabytecode.JavaClassInfo;
29
 import org.openzen.zenscript.javabytecode.JavaFieldInfo;
32
 import org.openzen.zenscript.javabytecode.JavaFieldInfo;
30
 import org.openzen.zenscript.javabytecode.JavaMethodInfo;
33
 import org.openzen.zenscript.javabytecode.JavaMethodInfo;
36
  * @author Hoofdgebruiker
39
  * @author Hoofdgebruiker
37
  */
40
  */
38
 public class GlobalRegistry {
41
 public class GlobalRegistry {
42
+	public final ZSPackage globals = new ZSPackage("");
39
 	private final ZSPackage rootPackage = new ZSPackage("");
43
 	private final ZSPackage rootPackage = new ZSPackage("");
40
 	private final ZSPackage javaIo = rootPackage.getOrCreatePackage("java").getOrCreatePackage("io");
44
 	private final ZSPackage javaIo = rootPackage.getOrCreatePackage("java").getOrCreatePackage("io");
41
 	private final ZSPackage javaLang = rootPackage.getOrCreatePackage("java").getOrCreatePackage("lang");
45
 	private final ZSPackage javaLang = rootPackage.getOrCreatePackage("java").getOrCreatePackage("lang");
42
 	
46
 	
43
-	public GlobalRegistry() {
47
+	public GlobalRegistry(ZSPackage globals) {
44
 		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
48
 		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
45
 		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V"));
49
 		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V"));
46
 		
50
 		
47
 		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
51
 		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
48
 		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
52
 		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
53
+		
54
+		PRINTLN.caller.setTag(JavaBytecodeImplementation.class, writer -> {
55
+			writer.getField(System.class, "out", PrintStream.class);
56
+			writer.invokeVirtual("java/io/PrintStream", "println", "(Ljava/lang/String;)V");
57
+		});
49
 	}
58
 	}
50
 	
59
 	
51
 	public ZSPackage collectPackages() {
60
 	public ZSPackage collectPackages() {
55
 			// eg. package my.package with a class MyClass with a single native method test() returning a string
64
 			// eg. package my.package with a class MyClass with a single native method test() returning a string
56
 			// the visitors can then during compilation check if a method is an instance of NativeMethodMember and treat it accordingly
65
 			// the visitors can then during compilation check if a method is an instance of NativeMethodMember and treat it accordingly
57
 			ZSPackage packageMyPackage = rootPackage.getOrCreatePackage("my").getOrCreatePackage("package");
66
 			ZSPackage packageMyPackage = rootPackage.getOrCreatePackage("my").getOrCreatePackage("package");
58
-			ClassDefinition myClassDefinition = new ClassDefinition(packageMyPackage, "MyClass", Modifiers.MODIFIER_PUBLIC, null);
67
+			ClassDefinition myClassDefinition = new ClassDefinition(CodePosition.NATIVE, packageMyPackage, "MyClass", Modifiers.MODIFIER_PUBLIC, null);
59
 			JavaClassInfo myClassInfo = new JavaClassInfo("my/test/MyClass");
68
 			JavaClassInfo myClassInfo = new JavaClassInfo("my/test/MyClass");
60
 			
69
 			
61
 			MethodMember member = new MethodMember(CodePosition.NATIVE, Modifiers.MODIFIER_PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING));
70
 			MethodMember member = new MethodMember(CodePosition.NATIVE, Modifiers.MODIFIER_PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING));
83
 		return globals;
92
 		return globals;
84
 	}
93
 	}
85
 	
94
 	
86
-	private final ClassDefinition PRINTSTREAM = new ClassDefinition(javaIo, "PrintStream", Modifiers.MODIFIER_EXPORT);
87
-	private final ClassDefinition SYSTEM = new ClassDefinition(javaLang, "System", Modifiers.MODIFIER_EXPORT);
95
+	private final ClassDefinition PRINTSTREAM = new ClassDefinition(CodePosition.NATIVE, javaIo, "PrintStream", Modifiers.MODIFIER_EXPORT);
96
+	private final ClassDefinition SYSTEM = new ClassDefinition(CodePosition.NATIVE, javaLang, "System", Modifiers.MODIFIER_EXPORT);
88
 	private final MethodMember PRINTSTREAM_PRINTLN = new MethodMember(
97
 	private final MethodMember PRINTSTREAM_PRINTLN = new MethodMember(
89
 			CodePosition.NATIVE,
98
 			CodePosition.NATIVE,
90
 			Modifiers.MODIFIER_EXPORT,
99
 			Modifiers.MODIFIER_EXPORT,
93
 	
102
 	
94
 	private final FieldMember SYSTEM_OUT = new FieldMember(
103
 	private final FieldMember SYSTEM_OUT = new FieldMember(
95
 			CodePosition.NATIVE,
104
 			CodePosition.NATIVE,
96
-			Modifiers.MODIFIER_EXPORT,
105
+			Modifiers.MODIFIER_EXPORT | Modifiers.MODIFIER_FINAL,
97
 			"out",
106
 			"out",
98
-			DefinitionTypeID.forType(SYSTEM),
99
-			true);
107
+			DefinitionTypeID.forType(SYSTEM));
108
+	
109
+	
110
+	private final FunctionDefinition PRINTLN = new FunctionDefinition(
111
+			CodePosition.NATIVE,
112
+			globals,
113
+			"println",
114
+			Modifiers.MODIFIER_EXPORT,
115
+			new FunctionHeader(BasicTypeID.VOID, new FunctionParameter(BasicTypeID.STRING)));
100
 	
116
 	
101
 	private class PrintlnSymbol implements ISymbol {
117
 	private class PrintlnSymbol implements ISymbol {
102
 
118
 
103
 		@Override
119
 		@Override
104
 		public IPartialExpression getExpression(CodePosition position, GlobalTypeRegistry types, List<ITypeID> typeArguments) {
120
 		public IPartialExpression getExpression(CodePosition position, GlobalTypeRegistry types, List<ITypeID> typeArguments) {
121
+			//return new PartialStaticMemberGroupExpression(position, PRINTLN.callerGroup);
105
 			return new PartialMemberGroupExpression(
122
 			return new PartialMemberGroupExpression(
106
 					position,
123
 					position,
107
 					new GetStaticFieldExpression(position, SYSTEM_OUT),
124
 					new GetStaticFieldExpression(position, SYSTEM_OUT),

+ 4
- 3
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

34
 		ZSPackage pkg = new ZSPackage("");
34
 		ZSPackage pkg = new ZSPackage("");
35
 		ParsedFile[] parsedFiles = parse(pkg, inputFiles);
35
 		ParsedFile[] parsedFiles = parse(pkg, inputFiles);
36
 		
36
 		
37
-		GlobalRegistry registry = new GlobalRegistry();
37
+		ZSPackage global = new ZSPackage("");
38
+		GlobalRegistry registry = new GlobalRegistry(global);
38
 		SemanticModule module = compileSyntaxToSemantic(parsedFiles, registry);
39
 		SemanticModule module = compileSyntaxToSemantic(parsedFiles, registry);
39
 		
40
 		
40
-		FormattingSettings settings = new FormattingSettings.Builder().build();
41
+		/*FormattingSettings settings = new FormattingSettings.Builder().build();
41
 		for (ScriptBlock block : module.scripts) {
42
 		for (ScriptBlock block : module.scripts) {
42
 			FileFormatter formatter = new FileFormatter(settings);
43
 			FileFormatter formatter = new FileFormatter(settings);
43
 			System.out.println("== " + block.getTag(SourceFile.class).filename + " ==");
44
 			System.out.println("== " + block.getTag(SourceFile.class).filename + " ==");
44
 			System.out.println(formatter.format(pkg, block, Collections.emptyList()));
45
 			System.out.println(formatter.format(pkg, block, Collections.emptyList()));
45
-		}
46
+		}*/
46
 		
47
 		
47
 		JavaModule javaModule = compileSemanticToJava(module);
48
 		JavaModule javaModule = compileSemanticToJava(module);
48
 		javaModule.execute();
49
 		javaModule.execute();

Loading…
Cancel
Save