Browse Source

Merge remote-tracking branch 'Stan/development' into development

kindlich 6 years ago
parent
commit
6c66704a78
No known key found for this signature in database
29 changed files with 341 additions and 35 deletions
  1. 6
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java
  2. 1
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ZenScriptOperator.java
  3. 3
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorSuperCallExpression.java
  4. 2
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java
  5. 26
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MatchExpression.java
  6. 33
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/PanicExpression.java
  7. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialTypeExpression.java
  8. 0
    1
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java
  9. 2
    0
      IDE/src/main/java/org/openzen/zenscript/ide/host/local/LocalTarget.java
  10. 11
    5
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java
  11. 6
    0
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaPreDecrementVisitor.java
  12. 6
    0
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaPreIncrementVisitor.java
  13. 6
    0
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/ExpressionHoistingChecker.java
  14. 69
    2
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java
  15. 7
    5
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaExpansionMemberCompiler.java
  16. 19
    1
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java
  17. 9
    2
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceExpressionFormatter.java
  18. 5
    3
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceFile.java
  19. 26
    5
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceStatementFormatter.java
  20. 29
    2
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceUtils.java
  21. 5
    0
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/prepare/JavaSourcePrepareDefinitionVisitor.java
  22. 10
    1
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/scope/JavaSourceFileScope.java
  23. 1
    0
      Parser/src/main/java/org/openzen/zenscript/lexer/ZSTokenType.java
  24. 5
    0
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpression.java
  25. 2
    1
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionCall.java
  26. 36
    0
      Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedPanicExpression.java
  27. 4
    0
      Shared/src/main/java/org/openzen/zenscript/shared/StringUtils.java
  28. 2
    1
      Validator/src/main/java/org/openzen/zenscript/validator/ValidationLogEntry.java
  29. 9
    0
      Validator/src/main/java/org/openzen/zenscript/validator/visitors/ExpressionValidator.java

+ 6
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java View File

56
 import org.openzen.zenscript.codemodel.expression.NewExpression;
56
 import org.openzen.zenscript.codemodel.expression.NewExpression;
57
 import org.openzen.zenscript.codemodel.expression.NullExpression;
57
 import org.openzen.zenscript.codemodel.expression.NullExpression;
58
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
58
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
59
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
59
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
60
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
60
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
61
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
61
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
62
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
545
 		return binary(expression.left, expression.right, ZenScriptOperator.OROR);
546
 		return binary(expression.left, expression.right, ZenScriptOperator.OROR);
546
 	}
547
 	}
547
 	
548
 	
549
+	@Override
550
+	public ExpressionString visitPanic(PanicExpression expression) {
551
+		return expression.value.accept(this).unaryPrefix(ZenScriptOperator.PANIC);
552
+	}
553
+	
548
 	@Override
554
 	@Override
549
 	public ExpressionString visitPostCall(PostCallExpression expression) {
555
 	public ExpressionString visitPostCall(PostCallExpression expression) {
550
 		return unaryPostfix(expression.target, ZenScriptOperator.INCREMENT, expression.member.getOperator() == OperatorType.INCREMENT ? "++" : "--");
556
 		return unaryPostfix(expression.target, ZenScriptOperator.INCREMENT, expression.member.getOperator() == OperatorType.INCREMENT ? "++" : "--");

+ 1
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ZenScriptOperator.java View File

60
 	CALL(9, null),
60
 	CALL(9, null),
61
 	CAST(9, null),
61
 	CAST(9, null),
62
 	
62
 	
63
+	PANIC(10, "panic "),
63
 	PRIMARY(10, null);
64
 	PRIMARY(10, null);
64
 	
65
 	
65
 	private final int priority;
66
 	private final int priority;

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

5
  */
5
  */
6
 package org.openzen.zenscript.codemodel.expression;
6
 package org.openzen.zenscript.codemodel.expression;
7
 
7
 
8
-import org.openzen.zenscript.codemodel.member.ConstructorMember;
8
+import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
9
 import org.openzen.zenscript.codemodel.scope.TypeScope;
9
 import org.openzen.zenscript.codemodel.scope.TypeScope;
10
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
10
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
11
 import org.openzen.zenscript.codemodel.type.ITypeID;
11
 import org.openzen.zenscript.codemodel.type.ITypeID;
17
  */
17
  */
18
 public class ConstructorSuperCallExpression extends Expression {
18
 public class ConstructorSuperCallExpression extends Expression {
19
 	public final ITypeID objectType;
19
 	public final ITypeID objectType;
20
-	public final ConstructorMember constructor;
20
+	public final FunctionalMemberRef constructor;
21
 	public final CallArguments arguments;
21
 	public final CallArguments arguments;
22
 	
22
 	
23
-	public ConstructorSuperCallExpression(CodePosition position, ITypeID type, ConstructorMember constructor, CallArguments arguments, TypeScope scope) {
23
+	public ConstructorSuperCallExpression(CodePosition position, ITypeID type, FunctionalMemberRef constructor, CallArguments arguments, TypeScope scope) {
24
 		super(position, BasicTypeID.VOID, binaryThrow(position, constructor.header.thrownType, multiThrow(position, arguments.arguments)));
24
 		super(position, BasicTypeID.VOID, binaryThrow(position, constructor.header.thrownType, multiThrow(position, arguments.arguments)));
25
 		
25
 		
26
 		this.objectType = type;
26
 		this.objectType = type;

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

104
 	
104
 	
105
 	public T visitOrOr(OrOrExpression expression);
105
 	public T visitOrOr(OrOrExpression expression);
106
 	
106
 	
107
+	public T visitPanic(PanicExpression expression);
108
+	
107
 	public T visitPostCall(PostCallExpression expression);
109
 	public T visitPostCall(PostCallExpression expression);
108
 	
110
 	
109
 	public T visitRange(RangeExpression expression);
111
 	public T visitRange(RangeExpression expression);

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

5
  */
5
  */
6
 package org.openzen.zenscript.codemodel.expression;
6
 package org.openzen.zenscript.codemodel.expression;
7
 
7
 
8
+import java.util.ArrayList;
9
+import java.util.Arrays;
8
 import java.util.Collections;
10
 import java.util.Collections;
11
+import java.util.List;
9
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
12
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
13
+import org.openzen.zenscript.codemodel.statement.BreakStatement;
10
 import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
14
 import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
15
+import org.openzen.zenscript.codemodel.statement.Statement;
11
 import org.openzen.zenscript.codemodel.statement.SwitchCase;
16
 import org.openzen.zenscript.codemodel.statement.SwitchCase;
12
 import org.openzen.zenscript.codemodel.statement.SwitchStatement;
17
 import org.openzen.zenscript.codemodel.statement.SwitchStatement;
13
 import org.openzen.zenscript.codemodel.statement.VarStatement;
18
 import org.openzen.zenscript.codemodel.statement.VarStatement;
19
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
14
 import org.openzen.zenscript.codemodel.type.ITypeID;
20
 import org.openzen.zenscript.codemodel.type.ITypeID;
15
 import org.openzen.zenscript.shared.CodePosition;
21
 import org.openzen.zenscript.shared.CodePosition;
16
 
22
 
49
 	public SwitchedMatch convertToSwitch(String tempVariable) {
55
 	public SwitchedMatch convertToSwitch(String tempVariable) {
50
 		VarStatement result = new VarStatement(position, tempVariable, type, null, false);
56
 		VarStatement result = new VarStatement(position, tempVariable, type, null, false);
51
 		SwitchStatement switchStatement = new SwitchStatement(position, null, value);
57
 		SwitchStatement switchStatement = new SwitchStatement(position, null, value);
58
+		boolean hasDefault = false;
52
 		for (MatchExpression.Case matchCase : cases) {
59
 		for (MatchExpression.Case matchCase : cases) {
53
-			Expression caseExpression = new SetLocalVariableExpression(matchCase.value.position, result, matchCase.value);
54
-			SwitchCase switchCase = new SwitchCase(matchCase.key, Collections.singletonList(new ExpressionStatement(matchCase.value.position, caseExpression)));
60
+			Expression caseExpression;
61
+			boolean reachable = true;
62
+			if (matchCase.value instanceof ThrowExpression || matchCase.value instanceof PanicExpression) {
63
+				caseExpression = matchCase.value;
64
+				reachable = false;
65
+			} else {
66
+				caseExpression = new SetLocalVariableExpression(matchCase.value.position, result, matchCase.value);
67
+			}
68
+			List<Statement> statements = new ArrayList<>();
69
+			statements.add(new ExpressionStatement(matchCase.value.position, caseExpression));
70
+			if (reachable)
71
+				statements.add(new BreakStatement(matchCase.value.position, switchStatement));
72
+			SwitchCase switchCase = new SwitchCase(matchCase.key, statements);
55
 			switchStatement.cases.add(switchCase);
73
 			switchStatement.cases.add(switchCase);
74
+			
75
+			if (matchCase.key == null)
76
+				hasDefault = true;
77
+		}
78
+		if (!hasDefault) {
79
+			switchStatement.cases.add(new SwitchCase(null, Collections.singletonList(new ExpressionStatement(position, new PanicExpression(position, BasicTypeID.VOID, new ConstantStringExpression(position, "Missing case"))))));
56
 		}
80
 		}
57
 		return new SwitchedMatch(result, switchStatement);
81
 		return new SwitchedMatch(result, switchStatement);
58
 	}
82
 	}

+ 33
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/PanicExpression.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.type.ITypeID;
9
+import org.openzen.zenscript.shared.CodePosition;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public class PanicExpression extends Expression {
16
+	public final Expression value;
17
+	
18
+	public PanicExpression(CodePosition position, ITypeID type, Expression value) {
19
+		super(position, type, null);
20
+		
21
+		this.value = value;
22
+	}
23
+
24
+	@Override
25
+	public <T> T accept(ExpressionVisitor<T> visitor) {
26
+		return visitor.visitPanic(this);
27
+	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return new PanicExpression(position, type, transformer.transform(value));
32
+	}
33
+}

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

61
 
61
 
62
 	@Override
62
 	@Override
63
 	public Expression call(CodePosition position, TypeScope scope, List<ITypeID> hints, CallArguments arguments) {
63
 	public Expression call(CodePosition position, TypeScope scope, List<ITypeID> hints, CallArguments arguments) {
64
-		if (arguments.getNumberOfTypeArguments() == 0 && typeParameters.length > 0)
64
+		if (arguments.getNumberOfTypeArguments() == 0 && (typeParameters != null && typeParameters.length > 0))
65
 			arguments = new CallArguments(typeParameters, arguments.arguments);
65
 			arguments = new CallArguments(typeParameters, arguments.arguments);
66
 		
66
 		
67
 		return scope.getTypeMembers(type).getOrCreateGroup(OperatorType.CALL).callStatic(position, type, scope, arguments);
67
 		return scope.getTypeMembers(type).getOrCreateGroup(OperatorType.CALL).callStatic(position, type, scope, arguments);

+ 0
- 1
CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java View File

122
 		for (ScriptBlock script : scripts) {
122
 		for (ScriptBlock script : scripts) {
123
 			compiler.addScriptBlock(script);
123
 			compiler.addScriptBlock(script);
124
 		}
124
 		}
125
-		compiler.finish();
126
 	}
125
 	}
127
 	
126
 	
128
 	public enum State {
127
 	public enum State {

+ 2
- 0
IDE/src/main/java/org/openzen/zenscript/ide/host/local/LocalTarget.java View File

90
 			if (!stdlib.isValid())
90
 			if (!stdlib.isValid())
91
 				return compiler;
91
 				return compiler;
92
 			stdlib.compile(compiler);
92
 			stdlib.compile(compiler);
93
+			compiledModules.add(stdlib.name);
93
 			
94
 			
94
 			boolean isValid = compileDependencies(moduleLoader, compiler, compiledModules, module);
95
 			boolean isValid = compileDependencies(moduleLoader, compiler, compiledModules, module);
95
 			if (!isValid)
96
 			if (!isValid)
96
 				return compiler;
97
 				return compiler;
97
 			
98
 			
98
 			module.compile(compiler);
99
 			module.compile(compiler);
100
+			compiler.finish();
99
 			return compiler;
101
 			return compiler;
100
 		} catch (IOException ex) {
102
 		} catch (IOException ex) {
101
 			ex.printStackTrace();
103
 			ex.printStackTrace();

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

1985
 
1985
 
1986
         return null;
1986
         return null;
1987
     }
1987
     }
1988
-
1989
-    @Override
1990
-    public Void visitPostCall(PostCallExpression expression) {
1991
-        expression.target.accept(this);
1992
-        javaWriter.dup(expression.type.accept(new JavaTypeVisitor()));
1988
+	
1989
+	@Override
1990
+	public Void visitPanic(PanicExpression expression) {
1991
+		// TODO: compile to: throw new AssertionError(expression.value)
1992
+		throw new UnsupportedOperationException("Not yet supported");
1993
+	}
1994
+	
1995
+	@Override
1996
+	public Void visitPostCall(PostCallExpression expression) {
1997
+		expression.target.accept(this);
1998
+		javaWriter.dup(expression.type.accept(new JavaTypeVisitor()));
1993
         if (!checkAndExecuteByteCodeImplementation(expression.member) && !checkAndExecuteMethodInfo(expression.member))
1999
         if (!checkAndExecuteByteCodeImplementation(expression.member) && !checkAndExecuteMethodInfo(expression.member))
1994
             throw new IllegalStateException("Call target has no method info!");
2000
             throw new IllegalStateException("Call target has no method info!");
1995
 
2001
 

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

54
 import org.openzen.zenscript.codemodel.expression.NewExpression;
54
 import org.openzen.zenscript.codemodel.expression.NewExpression;
55
 import org.openzen.zenscript.codemodel.expression.NullExpression;
55
 import org.openzen.zenscript.codemodel.expression.NullExpression;
56
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
56
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
57
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
57
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
58
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
58
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
59
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
59
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
60
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
350
 	public Void visitOrOr(OrOrExpression expression) {
351
 	public Void visitOrOr(OrOrExpression expression) {
351
 		throw new UnsupportedOperationException("Invalid increment target");
352
 		throw new UnsupportedOperationException("Invalid increment target");
352
 	}
353
 	}
354
+	
355
+	@Override
356
+	public Void visitPanic(PanicExpression expression) {
357
+		throw new UnsupportedOperationException("Invalid increment target");
358
+	}
353
 
359
 
354
 	@Override
360
 	@Override
355
 	public Void visitPostCall(PostCallExpression expression) {
361
 	public Void visitPostCall(PostCallExpression expression) {

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

54
 import org.openzen.zenscript.codemodel.expression.NewExpression;
54
 import org.openzen.zenscript.codemodel.expression.NewExpression;
55
 import org.openzen.zenscript.codemodel.expression.NullExpression;
55
 import org.openzen.zenscript.codemodel.expression.NullExpression;
56
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
56
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
57
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
57
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
58
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
58
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
59
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
59
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
60
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
356
 	public Void visitOrOr(OrOrExpression expression) {
357
 	public Void visitOrOr(OrOrExpression expression) {
357
 		throw new UnsupportedOperationException("Invalid increment target");
358
 		throw new UnsupportedOperationException("Invalid increment target");
358
 	}
359
 	}
360
+	
361
+	@Override
362
+	public Void visitPanic(PanicExpression expression) {
363
+		throw new UnsupportedOperationException("Invalid increment target");
364
+	}
359
 
365
 
360
 	@Override
366
 	@Override
361
 	public Void visitPostCall(PostCallExpression expression) {
367
 	public Void visitPostCall(PostCallExpression expression) {

+ 6
- 0
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/ExpressionHoistingChecker.java View File

53
 import org.openzen.zenscript.codemodel.expression.NewExpression;
53
 import org.openzen.zenscript.codemodel.expression.NewExpression;
54
 import org.openzen.zenscript.codemodel.expression.NullExpression;
54
 import org.openzen.zenscript.codemodel.expression.NullExpression;
55
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
55
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
56
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
56
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
57
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
57
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
58
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
58
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
59
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
315
 	public Boolean visitOrOr(OrOrExpression expression) {
316
 	public Boolean visitOrOr(OrOrExpression expression) {
316
 		return true;
317
 		return true;
317
 	}
318
 	}
319
+	
320
+	@Override
321
+	public Boolean visitPanic(PanicExpression expression) {
322
+		return true;
323
+	}
318
 
324
 
319
 	@Override
325
 	@Override
320
 	public Boolean visitPostCall(PostCallExpression expression) {
326
 	public Boolean visitPostCall(PostCallExpression expression) {

+ 69
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java View File

42
 	public Void visitClass(ClassDefinition definition) {
42
 	public Void visitClass(ClassDefinition definition) {
43
 		convertModifiers(definition.modifiers);
43
 		convertModifiers(definition.modifiers);
44
 		output.append("class ").append(definition.name);
44
 		output.append("class ").append(definition.name);
45
+		JavaSourceUtils.formatTypeParameters(scope, output, definition.genericParameters);
45
 		if (definition.superType != null) {
46
 		if (definition.superType != null) {
46
 			output.append(" extends ");
47
 			output.append(" extends ");
47
 			output.append(scope.type(definition.superType));
48
 			output.append(scope.type(definition.superType));
73
 
74
 
74
 	@Override
75
 	@Override
75
 	public Void visitInterface(InterfaceDefinition definition) {
76
 	public Void visitInterface(InterfaceDefinition definition) {
76
-		convertModifiers(definition.modifiers);
77
+		convertModifiers(definition.modifiers | Modifiers.VIRTUAL); // to prevent 'final'
77
 		output.append("interface ").append(definition.name);
78
 		output.append("interface ").append(definition.name);
79
+		JavaSourceUtils.formatTypeParameters(scope, output, definition.genericParameters);
78
 		output.append(" {\n");
80
 		output.append(" {\n");
79
 		compileMembers(definition);
81
 		compileMembers(definition);
80
 		output.append("}\n");
82
 		output.append("}\n");
115
 	public Void visitStruct(StructDefinition definition) {
117
 	public Void visitStruct(StructDefinition definition) {
116
 		convertModifiers(definition.modifiers | Modifiers.FINAL);
118
 		convertModifiers(definition.modifiers | Modifiers.FINAL);
117
 		output.append("class ").append(definition.name);
119
 		output.append("class ").append(definition.name);
120
+		JavaSourceUtils.formatTypeParameters(scope, output, definition.genericParameters);
118
 		output.append(" {\n");
121
 		output.append(" {\n");
119
 		compileMembers(definition);
122
 		compileMembers(definition);
120
 		output.append("}\n");
123
 		output.append("}\n");
124
 	@Override
127
 	@Override
125
 	public Void visitFunction(FunctionDefinition definition) {
128
 	public Void visitFunction(FunctionDefinition definition) {
126
 		convertModifiers(definition.modifiers | Modifiers.STATIC);
129
 		convertModifiers(definition.modifiers | Modifiers.STATIC);
130
+		
127
 		return null;
131
 		return null;
128
 	}
132
 	}
129
 
133
 
133
 		output.append("class ");
137
 		output.append("class ");
134
 		output.append(scope.className);
138
 		output.append(scope.className);
135
 		output.append(" {\n");
139
 		output.append(" {\n");
140
+		output.append(settings.indent).append("private ").append(scope.className).append("() {}\n");
136
 		
141
 		
137
 		JavaExpansionMemberCompiler memberCompiler = new JavaExpansionMemberCompiler(settings, definition.target, "\t", output, scope);
142
 		JavaExpansionMemberCompiler memberCompiler = new JavaExpansionMemberCompiler(settings, definition.target, "\t", output, scope);
138
 		for (IDefinitionMember member : definition.members)
143
 		for (IDefinitionMember member : definition.members)
150
 
155
 
151
 	@Override
156
 	@Override
152
 	public Void visitVariant(VariantDefinition variant) {
157
 	public Void visitVariant(VariantDefinition variant) {
158
+		convertModifiers(variant.modifiers | Modifiers.VIRTUAL | Modifiers.ABSTRACT);
159
+		output.append("class ").append(variant.name);
160
+		JavaSourceUtils.formatTypeParameters(scope, output, variant.genericParameters);
161
+		output.append("{\n");
162
+		compileMembers(variant);
163
+		output.append(settings.indent).append("public abstract Discriminant getDiscriminant();\n");
164
+		
165
+		output.append(settings.indent).append("\n");
166
+		output.append(settings.indent).append("public static enum Discriminant {\n");
167
+		for (VariantDefinition.Option option : variant.options) {
168
+			output.append(settings.indent).append(settings.indent).append(option.name).append(",\n");
169
+		}
170
+		output.append(settings.indent).append("}\n");
171
+		
172
+		for (VariantDefinition.Option option : variant.options) {
173
+			output.append(settings.indent).append("\n");
174
+			output.append(settings.indent).append("public static class ").append(option.name);
175
+			JavaSourceUtils.formatTypeParameters(scope, output, variant.genericParameters);
176
+			output.append(" extends ");
177
+			output.append(variant.name);
178
+			if (variant.genericParameters != null && variant.genericParameters.length > 0) {
179
+				output.append('<');
180
+				for (int i = 0; i < variant.genericParameters.length; i++) {
181
+					if (i > 0)
182
+						output.append(", ");
183
+					output.append(variant.genericParameters[i].name);
184
+				}
185
+				output.append('>');
186
+			}
187
+			output.append(" {\n");
188
+			
189
+			for (int i = 0; i < option.types.length; i++) {
190
+				String name = option.types.length == 1 ? "value" : "value" + (i + 1);
191
+				output.append(settings.indent).append(settings.indent).append("public final ").append(scope.type(option.types[i])).append(" ").append(name).append(";\n");
192
+			}
193
+			output.append(settings.indent).append(settings.indent).append("\n");
194
+			output.append(settings.indent).append(settings.indent).append("public ").append(option.name).append("(");
195
+			for (int i = 0; i < option.types.length; i++) {
196
+				if (i > 0)
197
+					output.append(", ");
198
+				String name = option.types.length == 1 ? "value" : "value" + (i + 1);
199
+				output.append(scope.type(option.types[i])).append(' ').append(name);
200
+			}
201
+			output.append("){\n");
202
+			for (int i = 0; i < option.types.length; i++) {
203
+				if (i > 0)
204
+					output.append(settings.indent).append(settings.indent).append(settings.indent).append(";\n");
205
+				
206
+				String name = option.types.length == 1 ? "value" : "value" + (i + 1);
207
+				output.append(settings.indent).append(settings.indent).append(settings.indent).append("this.").append(name).append(" = ").append(name).append(";\n");
208
+			}
209
+			output.append(settings.indent).append(settings.indent).append("}\n");
210
+			
211
+			output.append(settings.indent).append(settings.indent).append("\n");
212
+			output.append(settings.indent).append(settings.indent).append("@Override\n");
213
+			output.append(settings.indent).append(settings.indent).append("public Discriminant getDiscriminant() {\n");
214
+			output.append(settings.indent).append(settings.indent).append(settings.indent).append("return Discriminant.").append(option.name).append(";\n");
215
+			output.append(settings.indent).append(settings.indent).append("}\n");
216
+			output.append(settings.indent).append("}\n");
217
+		}
218
+		
219
+		output.append("}\n");
153
 		return null;
220
 		return null;
154
 	}
221
 	}
155
 	
222
 	
173
 	}
240
 	}
174
 	
241
 	
175
 	private void compileMembers(HighLevelDefinition definition) {
242
 	private void compileMembers(HighLevelDefinition definition) {
176
-		JavaMemberCompiler memberCompiler = new JavaMemberCompiler(settings, "\t", output, scope, false);
243
+		JavaMemberCompiler memberCompiler = new JavaMemberCompiler(settings, "\t", output, scope, false, scope.isInterface);
177
 		for (IDefinitionMember member : definition.members)
244
 		for (IDefinitionMember member : definition.members)
178
 			member.accept(memberCompiler);
245
 			member.accept(memberCompiler);
179
 		memberCompiler.finish();
246
 		memberCompiler.finish();

+ 7
- 5
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaExpansionMemberCompiler.java View File

29
 import org.openzen.zenscript.codemodel.member.SetterMember;
29
 import org.openzen.zenscript.codemodel.member.SetterMember;
30
 import org.openzen.zenscript.codemodel.member.StaticInitializerMember;
30
 import org.openzen.zenscript.codemodel.member.StaticInitializerMember;
31
 import org.openzen.zenscript.codemodel.statement.BlockStatement;
31
 import org.openzen.zenscript.codemodel.statement.BlockStatement;
32
+import org.openzen.zenscript.codemodel.statement.EmptyStatement;
32
 import org.openzen.zenscript.codemodel.statement.Statement;
33
 import org.openzen.zenscript.codemodel.statement.Statement;
33
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
34
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
34
 import org.openzen.zenscript.codemodel.type.ITypeID;
35
 import org.openzen.zenscript.codemodel.type.ITypeID;
141
 		addSpacing();
142
 		addSpacing();
142
 		
143
 		
143
 		output.append(indent);
144
 		output.append(indent);
144
-		modifiers(member.modifiers);
145
+		
146
+		modifiers(member.modifiers | Modifiers.STATIC);
145
 		JavaSourceUtils.formatTypeParameters(scope, output, member.header.typeParameters);
147
 		JavaSourceUtils.formatTypeParameters(scope, output, member.header.typeParameters);
146
 		output.append(scope.type(member.header.returnType));
148
 		output.append(scope.type(member.header.returnType));
147
 		output.append(" ");
149
 		output.append(" ");
156
 		addSpacing();
158
 		addSpacing();
157
 		
159
 		
158
 		output.append(indent);
160
 		output.append(indent);
159
-		modifiers(member.modifiers);
161
+		modifiers(member.modifiers | Modifiers.STATIC);
160
 		output.append(scope.type(member.type));
162
 		output.append(scope.type(member.type));
161
 		output.append(" ");
163
 		output.append(" ");
162
 		output.append("get").append(StringUtils.capitalize(member.name));
164
 		output.append("get").append(StringUtils.capitalize(member.name));
170
 		addSpacing();
172
 		addSpacing();
171
 		
173
 		
172
 		output.append(indent);
174
 		output.append(indent);
173
-		modifiers(member.modifiers);
175
+		modifiers(member.modifiers | Modifiers.STATIC);
174
 		output.append("void set").append(StringUtils.capitalize(member.name));
176
 		output.append("void set").append(StringUtils.capitalize(member.name));
175
 		output.append("(");
177
 		output.append("(");
176
 		output.append(scope.type(member.type));
178
 		output.append(scope.type(member.type));
186
 		addSpacing();
188
 		addSpacing();
187
 		
189
 		
188
 		output.append(indent);
190
 		output.append(indent);
189
-		modifiers(member.modifiers);
191
+		modifiers(member.modifiers | Modifiers.STATIC);
190
 		output.append(scope.type(member.header.returnType));
192
 		output.append(scope.type(member.header.returnType));
191
 		output.append(' ');
193
 		output.append(' ');
192
 		output.append(getOperatorName(member.operator));
194
 		output.append(getOperatorName(member.operator));
200
 		addSpacing();
202
 		addSpacing();
201
 		
203
 		
202
 		output.append(indent);
204
 		output.append(indent);
203
-		modifiers(member.modifiers);
205
+		modifiers(member.modifiers | Modifiers.STATIC);
204
 		output.append(scope.type(member.toType));
206
 		output.append(scope.type(member.toType));
205
 		output.append(" ");
207
 		output.append(" ");
206
 		output.append("to").append(member.toType.accept(new JavaSourceTypeNameVisitor()));
208
 		output.append("to").append(member.toType.accept(new JavaSourceTypeNameVisitor()));

+ 19
- 1
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java View File

48
 	private final JavaSourceFormattingSettings settings;
48
 	private final JavaSourceFormattingSettings settings;
49
 	private final List<FieldMember> fields = new ArrayList<>();
49
 	private final List<FieldMember> fields = new ArrayList<>();
50
 	private final boolean isExpansion;
50
 	private final boolean isExpansion;
51
+	private final boolean isInterface;
51
 	private boolean first = true;
52
 	private boolean first = true;
52
 	
53
 	
53
-	public JavaMemberCompiler(JavaSourceFormattingSettings settings, String indent, StringBuilder output, JavaSourceFileScope scope, boolean isExpansion) {
54
+	public JavaMemberCompiler(JavaSourceFormattingSettings settings, String indent, StringBuilder output, JavaSourceFileScope scope, boolean isExpansion, boolean isInterface) {
54
 		this.indent = indent;
55
 		this.indent = indent;
55
 		this.output = output;
56
 		this.output = output;
56
 		this.scope = scope;
57
 		this.scope = scope;
57
 		this.settings = settings;
58
 		this.settings = settings;
58
 		this.isExpansion = isExpansion;
59
 		this.isExpansion = isExpansion;
60
+		this.isInterface = isInterface;
59
 		
61
 		
60
 		fieldInitializerScope = new JavaSourceStatementScope(
62
 		fieldInitializerScope = new JavaSourceStatementScope(
61
 				scope,
63
 				scope,
144
 	public Void visitMethod(MethodMember member) {
146
 	public Void visitMethod(MethodMember member) {
145
 		addSpacing();
147
 		addSpacing();
146
 		
148
 		
149
+		
147
 		output.append(indent);
150
 		output.append(indent);
151
+		if (isInterface && member.body != null && !(member.body instanceof EmptyStatement))
152
+			output.append("default ");
153
+		
148
 		modifiers(member.modifiers);
154
 		modifiers(member.modifiers);
149
 		JavaSourceUtils.formatTypeParameters(scope, output, member.header.typeParameters);
155
 		JavaSourceUtils.formatTypeParameters(scope, output, member.header.typeParameters);
150
 		output.append(member.header.returnType.accept(scope.typeVisitor));
156
 		output.append(member.header.returnType.accept(scope.typeVisitor));
160
 		addSpacing();
166
 		addSpacing();
161
 		
167
 		
162
 		output.append(indent);
168
 		output.append(indent);
169
+		if (isInterface && member.body != null && !(member.body instanceof EmptyStatement))
170
+			output.append("default ");
171
+		
163
 		modifiers(member.modifiers);
172
 		modifiers(member.modifiers);
164
 		output.append(scope.type(member.type));
173
 		output.append(scope.type(member.type));
165
 		output.append(" ");
174
 		output.append(" ");
174
 		addSpacing();
183
 		addSpacing();
175
 		
184
 		
176
 		output.append(indent);
185
 		output.append(indent);
186
+		if (isInterface && member.body != null && !(member.body instanceof EmptyStatement))
187
+			output.append("default ");
188
+		
177
 		modifiers(member.modifiers);
189
 		modifiers(member.modifiers);
178
 		output.append("void set").append(StringUtils.capitalize(member.name));
190
 		output.append("void set").append(StringUtils.capitalize(member.name));
179
 		output.append("(");
191
 		output.append("(");
193
 			throw new IllegalStateException("Missing method tag!");
205
 			throw new IllegalStateException("Missing method tag!");
194
 		
206
 		
195
 		output.append(indent);
207
 		output.append(indent);
208
+		if (isInterface && member.body != null && !(member.body instanceof EmptyStatement))
209
+			output.append("default ");
210
+		
196
 		modifiers(member.modifiers);
211
 		modifiers(member.modifiers);
197
 		output.append(scope.type(member.header.returnType));
212
 		output.append(scope.type(member.header.returnType));
198
 		output.append(' ');
213
 		output.append(' ');
207
 		addSpacing();
222
 		addSpacing();
208
 		
223
 		
209
 		output.append(indent);
224
 		output.append(indent);
225
+		if (isInterface && member.body != null && !(member.body instanceof EmptyStatement))
226
+			output.append("default ");
227
+		
210
 		modifiers(member.modifiers);
228
 		modifiers(member.modifiers);
211
 		output.append(scope.type(member.toType));
229
 		output.append(scope.type(member.toType));
212
 		output.append(" ");
230
 		output.append(" ");

+ 9
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceExpressionFormatter.java View File

57
 import org.openzen.zenscript.codemodel.expression.NewExpression;
57
 import org.openzen.zenscript.codemodel.expression.NewExpression;
58
 import org.openzen.zenscript.codemodel.expression.NullExpression;
58
 import org.openzen.zenscript.codemodel.expression.NullExpression;
59
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
59
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
60
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
60
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
61
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
61
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
62
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
62
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
63
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
504
 		return binary(expression.left, expression.right, JavaOperator.OROR);
505
 		return binary(expression.left, expression.right, JavaOperator.OROR);
505
 	}
506
 	}
506
 	
507
 	
508
+	@Override
509
+	public ExpressionString visitPanic(PanicExpression expression) {
510
+		return new ExpressionString("throw new AssertionError(" + expression.value.accept(this).value + ")", JavaOperator.PRIMARY);
511
+	}
512
+	
507
 	@Override
513
 	@Override
508
 	public ExpressionString visitPostCall(PostCallExpression expression) {
514
 	public ExpressionString visitPostCall(PostCallExpression expression) {
509
 		return unaryPostfix(expression.target, expression.member.getOperator() == OperatorType.INCREMENT ? JavaOperator.INCREMENT : JavaOperator.DECREMENT);
515
 		return unaryPostfix(expression.target, expression.member.getOperator() == OperatorType.INCREMENT ? JavaOperator.INCREMENT : JavaOperator.DECREMENT);
1371
 			case STRING_CONSTRUCTOR_CHARACTERS:
1377
 			case STRING_CONSTRUCTOR_CHARACTERS:
1372
 				return callStatic("new String", expression.arguments.arguments[0]);
1378
 				return callStatic("new String", expression.arguments.arguments[0]);
1373
 			case ASSOC_CONSTRUCTOR: {
1379
 			case ASSOC_CONSTRUCTOR: {
1374
-				String typeName = scope.fileScope.importer.importType("java.util.HashMap");
1380
+				String typeName = scope.type(new JavaSourceClass("HashMap", "java.util.HashMap"));
1375
 				AssocTypeID type = (AssocTypeID) expression.type;
1381
 				AssocTypeID type = (AssocTypeID) expression.type;
1376
 				
1382
 				
1377
 				StringBuilder result = new StringBuilder();
1383
 				StringBuilder result = new StringBuilder();
1383
 				return new ExpressionString(result.toString(), JavaOperator.NEW);
1389
 				return new ExpressionString(result.toString(), JavaOperator.NEW);
1384
 			}
1390
 			}
1385
 			case GENERICMAP_CONSTRUCTOR: {
1391
 			case GENERICMAP_CONSTRUCTOR: {
1386
-				String typeName = scope.fileScope.importer.importType("java.util.HashMap");
1392
+				String typeName = scope.type(new JavaSourceClass("HashMap", "java.util.HashMap"));
1387
 				StringBuilder result = new StringBuilder();
1393
 				StringBuilder result = new StringBuilder();
1388
 				result.append("new ").append(typeName).append("<Class<?>, Object>()");
1394
 				result.append("new ").append(typeName).append("<Class<?>, Object>()");
1389
 				return new ExpressionString(result.toString(), JavaOperator.NEW);
1395
 				return new ExpressionString(result.toString(), JavaOperator.NEW);
1438
 							.append(value.value)
1444
 							.append(value.value)
1439
 							.append(";")
1445
 							.append(";")
1440
 							.toString());
1446
 							.toString());
1447
+					return new ExpressionString(temp, JavaOperator.PRIMARY);
1441
 				} else {
1448
 				} else {
1442
 					// TODO: implement
1449
 					// TODO: implement
1443
 					throw new UnsupportedOperationException("Not yet supported!");
1450
 					throw new UnsupportedOperationException("Not yet supported!");

+ 5
- 3
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceFile.java View File

17
 import java.util.Map;
17
 import java.util.Map;
18
 import java.util.Set;
18
 import java.util.Set;
19
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
19
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
20
+import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
20
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
21
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
21
 import org.openzen.zenscript.compiler.CompileScope;
22
 import org.openzen.zenscript.compiler.CompileScope;
22
 import org.openzen.zenscript.compiler.SemanticModule;
23
 import org.openzen.zenscript.compiler.SemanticModule;
61
 	}
62
 	}
62
 	
63
 	
63
 	public void write() {
64
 	public void write() {
65
+		System.out.println("Calling write on " + file.getName());
66
+		
64
 		for (Map.Entry<HighLevelDefinition, SemanticModule> entry : definitions.entrySet()) {
67
 		for (Map.Entry<HighLevelDefinition, SemanticModule> entry : definitions.entrySet()) {
65
 			HighLevelDefinition definition = entry.getKey();
68
 			HighLevelDefinition definition = entry.getKey();
66
 			SemanticModule module = entry.getValue();
69
 			SemanticModule module = entry.getValue();
67
 			CompileScope scope = new CompileScope(definition.access, module.compilationUnit.globalTypeRegistry, module.expansions, module.annotations);
70
 			CompileScope scope = new CompileScope(definition.access, module.compilationUnit.globalTypeRegistry, module.expansions, module.annotations);
68
 			JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
71
 			JavaDefinitionVisitor visitor = new JavaDefinitionVisitor(
69
 					compiler.settings,
72
 					compiler.settings,
70
-					new JavaSourceFileScope(importer, compiler.typeGenerator, compiler.helperGenerator, getName(), scope),
73
+					new JavaSourceFileScope(importer, compiler.typeGenerator, compiler.helperGenerator, getName(), scope, definition instanceof InterfaceDefinition),
71
 					contents);
74
 					contents);
72
 			definition.accept(visitor);
75
 			definition.accept(visitor);
73
 		}
76
 		}
74
 		
77
 		
75
 		if (!file.getParentFile().exists())
78
 		if (!file.getParentFile().exists())
76
 			file.getParentFile().mkdirs();
79
 			file.getParentFile().mkdirs();
77
-		file.delete();
78
 		
80
 		
79
-		try (Writer writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), StandardCharsets.UTF_8)) {
81
+		try (Writer writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file, false)), StandardCharsets.UTF_8)) {
80
 			writer.write("package ");
82
 			writer.write("package ");
81
 			writer.write(pkg.fullName);
83
 			writer.write(pkg.fullName);
82
 			writer.write(";\n\n");
84
 			writer.write(";\n\n");

+ 26
- 5
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceStatementFormatter.java View File

8
 import java.util.ArrayList;
8
 import java.util.ArrayList;
9
 import java.util.Collections;
9
 import java.util.Collections;
10
 import java.util.List;
10
 import java.util.List;
11
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
11
 import org.openzen.zenscript.codemodel.OperatorType;
12
 import org.openzen.zenscript.codemodel.OperatorType;
12
 import org.openzen.zenscript.codemodel.expression.CallArguments;
13
 import org.openzen.zenscript.codemodel.expression.CallArguments;
13
 import org.openzen.zenscript.codemodel.expression.Expression;
14
 import org.openzen.zenscript.codemodel.expression.Expression;
39
 import org.openzen.zenscript.codemodel.statement.TryCatchStatement;
40
 import org.openzen.zenscript.codemodel.statement.TryCatchStatement;
40
 import org.openzen.zenscript.codemodel.statement.VarStatement;
41
 import org.openzen.zenscript.codemodel.statement.VarStatement;
41
 import org.openzen.zenscript.codemodel.statement.WhileStatement;
42
 import org.openzen.zenscript.codemodel.statement.WhileStatement;
43
+import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
42
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
44
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
43
 import org.openzen.zenscript.formattershared.ExpressionString;
45
 import org.openzen.zenscript.formattershared.ExpressionString;
44
 import org.openzen.zenscript.formattershared.StatementFormatter;
46
 import org.openzen.zenscript.formattershared.StatementFormatter;
45
 import org.openzen.zenscript.formattershared.StatementFormattingSubBlock;
47
 import org.openzen.zenscript.formattershared.StatementFormattingSubBlock;
46
 import org.openzen.zenscript.formattershared.StatementFormattingTarget;
48
 import org.openzen.zenscript.formattershared.StatementFormattingTarget;
47
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
49
 import org.openzen.zenscript.javasource.scope.JavaSourceStatementScope;
50
+import org.openzen.zenscript.javasource.tags.JavaSourceClass;
48
 import org.openzen.zenscript.shared.StringUtils;
51
 import org.openzen.zenscript.shared.StringUtils;
49
 
52
 
50
 /**
53
 /**
126
 			Expression value = scope.duplicable(target, statement.value);
129
 			Expression value = scope.duplicable(target, statement.value);
127
 			ExpressionString valueString = scope.expression(target, value);
130
 			ExpressionString valueString = scope.expression(target, value);
128
 			List<StatementFormattingSubBlock> blocks = new ArrayList<>();
131
 			List<StatementFormattingSubBlock> blocks = new ArrayList<>();
129
-			String variantType = scope.type(statement.value.type);
132
+			
133
+			DefinitionTypeID variantType = (DefinitionTypeID)statement.value.type;
134
+			HighLevelDefinition variant = variantType.definition;
135
+			String variantTypeName = scope.type(variant.getTag(JavaSourceClass.class));
136
+			JavaSourceStatementScope innerScope = scope.createBlockScope(statement);
130
 			for (SwitchCase switchCase : statement.cases) {
137
 			for (SwitchCase switchCase : statement.cases) {
131
 				VariantOptionSwitchValue switchValue = (VariantOptionSwitchValue)switchCase.value;
138
 				VariantOptionSwitchValue switchValue = (VariantOptionSwitchValue)switchCase.value;
132
 				String header = switchValue == null ? "default:" : "case " + switchValue.option.getName() + ":";
139
 				String header = switchValue == null ? "default:" : "case " + switchValue.option.getName() + ":";
133
 				List<String> statements = new ArrayList<>();
140
 				List<String> statements = new ArrayList<>();
134
 				if (switchValue != null) {
141
 				if (switchValue != null) {
135
-					for (VarStatement var : switchValue.parameters)
136
-						statements.add(scope.type(var.type) + " " + var.name + " = (" + variantType + "." + switchValue.option.getName() + ")" + valueString.value + ";");
142
+					for (VarStatement var : switchValue.parameters) {
143
+						StringBuilder statementOutput = new StringBuilder();
144
+						statementOutput.append(scope.type(var.type)).append(" ").append(var.name).append(" = ((").append(variantTypeName).append(".").append(switchValue.option.getName());
145
+						if (variant.genericParameters != null && variant.genericParameters.length > 0) {
146
+							statementOutput.append("<");
147
+							for (int i = 0; i < variantType.typeParameters.length; i++) {
148
+								if (i > 0)
149
+									statementOutput.append(", ");
150
+								statementOutput.append(scope.type(variantType.typeParameters[i]));
151
+							}
152
+							statementOutput.append(">");
153
+						}
154
+						statementOutput.append(")").append(valueString.value).append(").").append("value").append(";");
155
+						statements.add(statementOutput.toString());
156
+					}
137
 				}
157
 				}
138
 				blocks.add(new StatementFormattingSubBlock(header, statements, switchCase.statements));
158
 				blocks.add(new StatementFormattingSubBlock(header, statements, switchCase.statements));
139
 			}
159
 			}
225
 			String name = statement.loopVariables[0].name;
245
 			String name = statement.loopVariables[0].name;
226
 			
246
 			
227
 			if (statement.list instanceof RangeExpression) {
247
 			if (statement.list instanceof RangeExpression) {
248
+				String limitName = "limitFor" + StringUtils.capitalize(name);
228
 				RangeExpression range = (RangeExpression)(statement.list);
249
 				RangeExpression range = (RangeExpression)(statement.list);
229
-				target.writeLine("int limitFor = " + name + " = " + scope.expression(target, range.to) + ";");
250
+				target.writeLine("int " + limitName + " = " + scope.expression(target, range.to) + ";");
230
 				target.writeInner(
251
 				target.writeInner(
231
-						"for (int " + name + " = " + scope.expression(target, range.from) + "; " + name + " < limitFor" + name + "; " + name + "++)",
252
+						"for (int " + name + " = " + scope.expression(target, range.from) + "; " + name + " < " + limitName + "; " + name + "++)",
232
 						statement.content,
253
 						statement.content,
233
 						statement,
254
 						statement,
234
 						"");
255
 						"");

+ 29
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceUtils.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.javasource;
6
 package org.openzen.zenscript.javasource;
7
 
7
 
8
+import org.openzen.zenscript.codemodel.generic.GenericParameterBound;
9
+import org.openzen.zenscript.codemodel.generic.GenericParameterBoundVisitor;
10
+import org.openzen.zenscript.codemodel.generic.ParameterSuperBound;
11
+import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
8
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
9
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
13
 import org.openzen.zenscript.javasource.scope.JavaSourceFileScope;
10
 
14
 
19
 		if (parameters == null || parameters.length == 0)
23
 		if (parameters == null || parameters.length == 0)
20
 			return;
24
 			return;
21
 		
25
 		
26
+		TypeParameterBoundVisitor boundVisitor = new TypeParameterBoundVisitor(scope, output);
22
 		output.append("<");
27
 		output.append("<");
23
 		for (int i = 0; i < parameters.length; i++) {
28
 		for (int i = 0; i < parameters.length; i++) {
24
 			if (i > 0)
29
 			if (i > 0)
28
 			output.append(typeParameter.name);
33
 			output.append(typeParameter.name);
29
 			
34
 			
30
 			if (typeParameter.bounds.size() > 0) {
35
 			if (typeParameter.bounds.size() > 0) {
31
-				output.append(" ");
32
-				// TODO
36
+				for (GenericParameterBound bound : typeParameter.bounds)
37
+					bound.accept(boundVisitor);
33
 			}
38
 			}
34
 		}
39
 		}
35
 		output.append("> ");
40
 		output.append("> ");
36
 	}
41
 	}
42
+	
43
+	private static class TypeParameterBoundVisitor implements GenericParameterBoundVisitor<Void> {
44
+		private final JavaSourceFileScope scope;
45
+		private final StringBuilder output;
46
+		
47
+		public TypeParameterBoundVisitor(JavaSourceFileScope scope, StringBuilder output) {
48
+			this.scope = scope;
49
+			this.output = output;
50
+		}
51
+
52
+		@Override
53
+		public Void visitSuper(ParameterSuperBound bound) {
54
+			output.append(" super ").append(scope.type(bound.type));
55
+			return null;
56
+		}
57
+
58
+		@Override
59
+		public Void visitType(ParameterTypeBound bound) {
60
+			output.append(" extends ").append(scope.type(bound.type));
61
+			return null;
62
+		}
63
+	}
37
 }
64
 }

+ 5
- 0
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/prepare/JavaSourcePrepareDefinitionVisitor.java View File

19
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
19
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
20
 import org.openzen.zenscript.javasource.tags.JavaSourceClass;
20
 import org.openzen.zenscript.javasource.tags.JavaSourceClass;
21
 import org.openzen.zenscript.javasource.JavaSourceFile;
21
 import org.openzen.zenscript.javasource.JavaSourceFile;
22
+import org.openzen.zenscript.javasource.tags.JavaSourceMethod;
22
 import org.openzen.zenscript.javasource.tags.JavaSourceVariantOption;
23
 import org.openzen.zenscript.javasource.tags.JavaSourceVariantOption;
23
 
24
 
24
 /**
25
 /**
58
 
59
 
59
 	@Override
60
 	@Override
60
 	public Void visitFunction(FunctionDefinition definition) {
61
 	public Void visitFunction(FunctionDefinition definition) {
62
+		JavaSourceClass cls = new JavaSourceClass(file.getName(), definition.pkg.fullName + "." + file.getName());
63
+		definition.setTag(JavaSourceClass.class, cls);
64
+		JavaSourceMethod method = new JavaSourceMethod(cls, JavaSourceMethod.Kind.STATIC, definition.name);
65
+		definition.caller.setTag(JavaSourceMethod.class, method);
61
 		return null;
66
 		return null;
62
 	}
67
 	}
63
 
68
 

+ 10
- 1
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/scope/JavaSourceFileScope.java View File

25
 	public final JavaSourceTypeVisitor typeVisitor;
25
 	public final JavaSourceTypeVisitor typeVisitor;
26
 	public final JavaSourceObjectTypeVisitor objectTypeVisitor;
26
 	public final JavaSourceObjectTypeVisitor objectTypeVisitor;
27
 	public final TypeScope semanticScope;
27
 	public final TypeScope semanticScope;
28
+	public final boolean isInterface;
28
 	
29
 	
29
-	public JavaSourceFileScope(JavaSourceImporter importer, JavaSourceSyntheticTypeGenerator typeGenerator, JavaSourceSyntheticHelperGenerator helperGenerator, String className, TypeScope semanticScope) {
30
+	public JavaSourceFileScope(
31
+			JavaSourceImporter importer, 
32
+			JavaSourceSyntheticTypeGenerator typeGenerator,
33
+			JavaSourceSyntheticHelperGenerator helperGenerator,
34
+			String className,
35
+			TypeScope semanticScope,
36
+			boolean isInterface)
37
+	{
30
 		this.importer = importer;
38
 		this.importer = importer;
31
 		this.typeGenerator = typeGenerator;
39
 		this.typeGenerator = typeGenerator;
32
 		this.helperGenerator = helperGenerator;
40
 		this.helperGenerator = helperGenerator;
33
 		this.className = className;
41
 		this.className = className;
34
 		this.semanticScope = semanticScope;
42
 		this.semanticScope = semanticScope;
43
+		this.isInterface = isInterface;
35
 		
44
 		
36
 		typeVisitor = new JavaSourceTypeVisitor(importer, typeGenerator);
45
 		typeVisitor = new JavaSourceTypeVisitor(importer, typeGenerator);
37
 		objectTypeVisitor = typeVisitor.objectTypeVisitor;
46
 		objectTypeVisitor = typeVisitor.objectTypeVisitor;

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

128
 	K_WHILE(true, "while"),
128
 	K_WHILE(true, "while"),
129
 	K_FOR(true, "for"),
129
 	K_FOR(true, "for"),
130
 	K_THROW(true, "throw"),
130
 	K_THROW(true, "throw"),
131
+	K_PANIC(true, "panic"),
131
 	K_LOCK(true, "lock"),
132
 	K_LOCK(true, "lock"),
132
 	K_TRY(true, "try"),
133
 	K_TRY(true, "try"),
133
 	K_CATCH(true, "catch"),
134
 	K_CATCH(true, "catch"),

+ 5
- 0
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpression.java View File

512
 				ParsedExpression value = parse(parser);
512
 				ParsedExpression value = parse(parser);
513
 				return new ParsedThrowExpression(position, value);
513
 				return new ParsedThrowExpression(position, value);
514
 			}
514
 			}
515
+			case K_PANIC: {
516
+				parser.next();
517
+				ParsedExpression value = parse(parser);
518
+				return new ParsedPanicExpression(position, value);
519
+			}
515
 			case K_MATCH: {
520
 			case K_MATCH: {
516
 				parser.next();
521
 				parser.next();
517
 				ParsedExpression source = parse(parser);
522
 				ParsedExpression source = parse(parser);

+ 2
- 1
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.ConstructorSuperCallExpression;
13
 import org.openzen.zenscript.codemodel.expression.ConstructorThisCallExpression;
14
 import org.openzen.zenscript.codemodel.expression.ConstructorThisCallExpression;
14
 import org.openzen.zenscript.codemodel.expression.VariantValueExpression;
15
 import org.openzen.zenscript.codemodel.expression.VariantValueExpression;
15
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
16
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
70
 			if (!member.isConstructor())
71
 			if (!member.isConstructor())
71
 				throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "Constructor is not a constructor!");
72
 				throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "Constructor is not a constructor!");
72
 			
73
 			
73
-			return new ConstructorThisCallExpression(position, scope.getThisType().getSuperType(), member, callArguments, scope);
74
+			return new ConstructorSuperCallExpression(position, scope.getThisType().getSuperType(), member, callArguments, scope);
74
 		} else if (receiver instanceof ParsedExpressionThis) {
75
 		} else if (receiver instanceof ParsedExpressionThis) {
75
 			// this call (intended as first call in constructor)
76
 			// this call (intended as first call in constructor)
76
 			ITypeID targetType = scope.getThisType();
77
 			ITypeID targetType = scope.getThisType();

+ 36
- 0
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedPanicExpression.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.parser.expression;
7
+
8
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
9
+import org.openzen.zenscript.codemodel.partial.IPartialExpression;
10
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
11
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
12
+import org.openzen.zenscript.shared.CodePosition;
13
+
14
+/**
15
+ *
16
+ * @author Hoofdgebruiker
17
+ */
18
+public class ParsedPanicExpression extends ParsedExpression {
19
+	public final ParsedExpression value;
20
+	
21
+	public ParsedPanicExpression(CodePosition position, ParsedExpression value) {
22
+		super(position);
23
+		
24
+		this.value = value;
25
+	}
26
+	
27
+	@Override
28
+	public IPartialExpression compile(ExpressionScope scope) {
29
+		return new PanicExpression(position, scope.getResultTypeHints().isEmpty() ? BasicTypeID.VOID : scope.getResultTypeHints().get(0), value.compile(scope).eval());
30
+	}
31
+
32
+	@Override
33
+	public boolean hasStrongType() {
34
+		return true;
35
+	}
36
+}

+ 4
- 0
Shared/src/main/java/org/openzen/zenscript/shared/StringUtils.java View File

231
 				case '"':
231
 				case '"':
232
 					if (quote == '"')
232
 					if (quote == '"')
233
 						output.append("\\\"");
233
 						output.append("\\\"");
234
+					else
235
+						output.append('"');
234
 					break;
236
 					break;
235
 				case '\'':
237
 				case '\'':
236
 					if (quote == '\'')
238
 					if (quote == '\'')
237
 						output.append("\\\'");
239
 						output.append("\\\'");
240
+					else
241
+						output.append("'");
238
 					break;
242
 					break;
239
 				case '\n': output.append("\\n"); break;
243
 				case '\n': output.append("\\n"); break;
240
 				case '\r': output.append("\\r"); break;
244
 				case '\r': output.append("\\r"); break;

+ 2
- 1
Validator/src/main/java/org/openzen/zenscript/validator/ValidationLogEntry.java View File

62
 		SETTING_FINAL_FIELD,
62
 		SETTING_FINAL_FIELD,
63
 		SETTING_FINAL_VARIABLE,
63
 		SETTING_FINAL_VARIABLE,
64
 		INVALID_SUPERTYPE,
64
 		INVALID_SUPERTYPE,
65
-		MULTIPLE_DESTRUCTORS
65
+		MULTIPLE_DESTRUCTORS,
66
+		PANIC_ARGUMENT_NO_STRING
66
 	}
67
 	}
67
 }
68
 }

+ 9
- 0
Validator/src/main/java/org/openzen/zenscript/validator/visitors/ExpressionValidator.java View File

57
 import org.openzen.zenscript.codemodel.expression.NewExpression;
57
 import org.openzen.zenscript.codemodel.expression.NewExpression;
58
 import org.openzen.zenscript.codemodel.expression.NullExpression;
58
 import org.openzen.zenscript.codemodel.expression.NullExpression;
59
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
59
 import org.openzen.zenscript.codemodel.expression.OrOrExpression;
60
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
60
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
61
 import org.openzen.zenscript.codemodel.expression.PostCallExpression;
61
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
62
 import org.openzen.zenscript.codemodel.expression.RangeExpression;
62
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
63
 import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
445
 		return null;
446
 		return null;
446
 	}
447
 	}
447
 	
448
 	
449
+	@Override
450
+	public Void visitPanic(PanicExpression expression) {
451
+		expression.value.accept(this);
452
+		if (expression.type != BasicTypeID.STRING)
453
+			validator.logError(ValidationLogEntry.Code.PANIC_ARGUMENT_NO_STRING, expression.position, "Argument to a panic must be a string");
454
+		return null;
455
+	}
456
+	
448
 	@Override
457
 	@Override
449
 	public Void visitPostCall(PostCallExpression expression) {
458
 	public Void visitPostCall(PostCallExpression expression) {
450
 		expression.target.accept(this);
459
 		expression.target.accept(this);

Loading…
Cancel
Save