Browse Source

Fixed enum constants

Stan Hebben 6 years ago
parent
commit
565a59947d

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

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.Modifiers;
10
+import org.openzen.zenscript.codemodel.expression.CallExpression;
11
+import org.openzen.zenscript.codemodel.expression.NewExpression;
10
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
11
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
13
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
12
 import org.openzen.zenscript.codemodel.type.ITypeID;
14
 import org.openzen.zenscript.codemodel.type.ITypeID;
20
  */
22
  */
21
 public class EnumConstantMember extends DefinitionMember {
23
 public class EnumConstantMember extends DefinitionMember {
22
 	public final String name;
24
 	public final String name;
25
+	public final int value;
23
 	
26
 	
24
-	public EnumConstantMember(CodePosition position, String name) {
27
+	public NewExpression constructor;
28
+	
29
+	public EnumConstantMember(CodePosition position, String name, int value) {
25
 		super(position, Modifiers.MODIFIER_STATIC);
30
 		super(position, Modifiers.MODIFIER_STATIC);
26
 		
31
 		
27
 		this.name = name;
32
 		this.name = name;
33
+		this.value = value;
28
 	}
34
 	}
29
 
35
 
30
 	@Override
36
 	@Override

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

211
 		if (constructors.getMethodMembers().isEmpty()) {
211
 		if (constructors.getMethodMembers().isEmpty()) {
212
 			if (definition instanceof ClassDefinition) {
212
 			if (definition instanceof ClassDefinition) {
213
 				// add default constructor
213
 				// add default constructor
214
-				constructors.addMethod(new ConstructorMember(BUILTIN, 0, new FunctionHeader(VOID)), TypeMemberPriority.SPECIFIED);
214
+				constructors.addMethod(new ConstructorMember(BUILTIN, Modifiers.MODIFIER_PUBLIC, new FunctionHeader(VOID)), TypeMemberPriority.SPECIFIED);
215
 			} else if (definition instanceof StructDefinition) {
215
 			} else if (definition instanceof StructDefinition) {
216
 				// add default struct constructors
216
 				// add default struct constructors
217
-				constructors.addMethod(new ConstructorMember(BUILTIN, 0, new FunctionHeader(VOID)), TypeMemberPriority.SPECIFIED);
217
+				constructors.addMethod(new ConstructorMember(BUILTIN, Modifiers.MODIFIER_PUBLIC, new FunctionHeader(VOID)), TypeMemberPriority.SPECIFIED);
218
 				
218
 				
219
 				List<FieldMember> fields = ((StructDefinition)definition).getFields();
219
 				List<FieldMember> fields = ((StructDefinition)definition).getFields();
220
 				if (!fields.isEmpty()) {
220
 				if (!fields.isEmpty()) {
225
 					}
225
 					}
226
 					constructors.addMethod(new ConstructorMember(BUILTIN, 0, new FunctionHeader(VOID, parameters)), TypeMemberPriority.SPECIFIED);
226
 					constructors.addMethod(new ConstructorMember(BUILTIN, 0, new FunctionHeader(VOID, parameters)), TypeMemberPriority.SPECIFIED);
227
 				}
227
 				}
228
+			} else if (definition instanceof EnumDefinition) {
229
+				// add default constructor
230
+				constructors.addMethod(new ConstructorMember(BUILTIN, Modifiers.MODIFIER_PRIVATE, new FunctionHeader(VOID)), TypeMemberPriority.SPECIFIED);
228
 			}
231
 			}
229
 		}
232
 		}
230
 		
233
 		

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

1
 package org.openzen.zenscript.javabytecode.compiler;
1
 package org.openzen.zenscript.javabytecode.compiler;
2
 
2
 
3
-import com.sun.istack.internal.Nullable;
4
 import org.objectweb.asm.*;
3
 import org.objectweb.asm.*;
5
 import org.objectweb.asm.commons.LocalVariablesSorter;
4
 import org.objectweb.asm.commons.LocalVariablesSorter;
6
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
5
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
1114
             visitor.visitLocalVariable(name, type.getDescriptor(), null, start, end, local);
1113
             visitor.visitLocalVariable(name, type.getDescriptor(), null, start, end, local);
1115
     }
1114
     }
1116
 
1115
 
1117
-    public void nameParameter(int modifier, @Nullable String name) {
1116
+    public void nameParameter(int modifier, String name) {
1118
         if (nameVariables)
1117
         if (nameVariables)
1119
             visitor.visitParameter(name, modifier);
1118
             visitor.visitParameter(name, modifier);
1120
     }
1119
     }

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

10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
11
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
12
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
12
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
13
+import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
14
+import org.openzen.zenscript.codemodel.type.ITypeID;
13
 import org.openzen.zenscript.lexer.ZSTokenStream;
15
 import org.openzen.zenscript.lexer.ZSTokenStream;
14
 import org.openzen.zenscript.lexer.ZSTokenType;
16
 import org.openzen.zenscript.lexer.ZSTokenType;
17
+import org.openzen.zenscript.linker.BaseScope;
18
+import org.openzen.zenscript.linker.ExpressionScope;
15
 import org.openzen.zenscript.parser.member.ParsedDefinitionMember;
19
 import org.openzen.zenscript.parser.member.ParsedDefinitionMember;
16
 import org.openzen.zenscript.shared.CodePosition;
20
 import org.openzen.zenscript.shared.CodePosition;
17
 
21
 
26
 		
30
 		
27
 		List<ParsedEnumConstant> enumValues = new ArrayList<>();
31
 		List<ParsedEnumConstant> enumValues = new ArrayList<>();
28
 		while (!tokens.isNext(ZSTokenType.T_ACLOSE) && !tokens.isNext(ZSTokenType.T_SEMICOLON)) {
32
 		while (!tokens.isNext(ZSTokenType.T_ACLOSE) && !tokens.isNext(ZSTokenType.T_SEMICOLON)) {
29
-			enumValues.add(ParsedEnumConstant.parse(tokens));
33
+			enumValues.add(ParsedEnumConstant.parse(tokens, enumValues.size()));
30
 			if (tokens.optional(ZSTokenType.T_COMMA) == null)
34
 			if (tokens.optional(ZSTokenType.T_COMMA) == null)
31
 				break;
35
 				break;
32
 		}
36
 		}
58
 	public HighLevelDefinition getCompiled() {
62
 	public HighLevelDefinition getCompiled() {
59
 		return compiled;
63
 		return compiled;
60
 	}
64
 	}
65
+	
66
+	@Override
67
+	public void compileMembers(BaseScope scope) {
68
+		super.compileMembers(scope);
69
+		
70
+		for (ParsedEnumConstant constant : enumValues) {
71
+			compiled.addMember(constant.getCompiled());
72
+		}
73
+	}
74
+
75
+	@Override
76
+	public void compileCode(BaseScope scope) {
77
+		super.compileCode(scope);
78
+		
79
+		DefinitionTypeID type = new DefinitionTypeID(compiled, new ITypeID[0]);
80
+		ExpressionScope evalScope = new ExpressionScope(scope);
81
+		for (ParsedEnumConstant constant : enumValues) {
82
+			constant.compileCode(type, evalScope);
83
+		}
84
+	}
61
 }
85
 }

+ 17
- 4
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedEnumConstant.java View File

8
 import java.util.ArrayList;
8
 import java.util.ArrayList;
9
 import java.util.List;
9
 import java.util.List;
10
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
10
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
11
+import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
11
 import org.openzen.zenscript.lexer.ZSToken;
12
 import org.openzen.zenscript.lexer.ZSToken;
12
 import org.openzen.zenscript.lexer.ZSTokenStream;
13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13
 import org.openzen.zenscript.lexer.ZSTokenType;
14
 import org.openzen.zenscript.lexer.ZSTokenType;
15
+import org.openzen.zenscript.linker.ExpressionScope;
16
+import org.openzen.zenscript.parser.expression.ParsedCallArguments;
14
 import org.openzen.zenscript.parser.expression.ParsedExpression;
17
 import org.openzen.zenscript.parser.expression.ParsedExpression;
18
+import org.openzen.zenscript.parser.expression.ParsedNewExpression;
15
 import org.openzen.zenscript.shared.CodePosition;
19
 import org.openzen.zenscript.shared.CodePosition;
16
 
20
 
17
 /**
21
 /**
19
  * @author Hoofdgebruiker
23
  * @author Hoofdgebruiker
20
  */
24
  */
21
 public class ParsedEnumConstant {
25
 public class ParsedEnumConstant {
22
-	public static ParsedEnumConstant parse(ZSTokenStream tokens) {
26
+	public static ParsedEnumConstant parse(ZSTokenStream tokens, int value) {
23
 		ZSToken name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected");
27
 		ZSToken name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected");
24
 		List<ParsedExpression> arguments = new ArrayList<>();
28
 		List<ParsedExpression> arguments = new ArrayList<>();
25
 		if (tokens.optional(ZSTokenType.T_BROPEN) != null) {
29
 		if (tokens.optional(ZSTokenType.T_BROPEN) != null) {
29
 			tokens.required(ZSTokenType.T_BRCLOSE, ") expected");
33
 			tokens.required(ZSTokenType.T_BRCLOSE, ") expected");
30
 		}
34
 		}
31
 		
35
 		
32
-		return new ParsedEnumConstant(name.position, name.content, arguments);
36
+		return new ParsedEnumConstant(name.position, name.content, value, arguments);
33
 	}
37
 	}
34
 	
38
 	
35
 	public final CodePosition position;
39
 	public final CodePosition position;
38
 	
42
 	
39
 	private final EnumConstantMember compiled;
43
 	private final EnumConstantMember compiled;
40
 	
44
 	
41
-	public ParsedEnumConstant(CodePosition position, String name, List<ParsedExpression> arguments) {
45
+	public ParsedEnumConstant(CodePosition position, String name, int value, List<ParsedExpression> arguments) {
42
 		this.position = position;
46
 		this.position = position;
43
 		this.name = name;
47
 		this.name = name;
44
 		this.arguments = arguments;
48
 		this.arguments = arguments;
45
 		
49
 		
46
-		compiled = new EnumConstantMember(position, name);
50
+		compiled = new EnumConstantMember(position, name, value);
51
+	}
52
+	
53
+	public EnumConstantMember getCompiled() {
54
+		return compiled;
55
+	}
56
+	
57
+	public void compileCode(DefinitionTypeID type, ExpressionScope scope) {
58
+		ParsedCallArguments arguments = new ParsedCallArguments(this.arguments);
59
+		compiled.constructor = ParsedNewExpression.compile(position, type, arguments, scope);
47
 	}
60
 	}
48
 }
61
 }

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedNewExpression.java View File

47
 		return true;
47
 		return true;
48
 	}
48
 	}
49
 	
49
 	
50
-	public static Expression compile(CodePosition position, ITypeID type, ParsedCallArguments arguments, ExpressionScope scope) {
50
+	public static NewExpression compile(CodePosition position, ITypeID type, ParsedCallArguments arguments, ExpressionScope scope) {
51
 		DefinitionMemberGroup constructors = scope.getTypeMembers(type).getOrCreateGroup(OperatorType.CONSTRUCTOR);
51
 		DefinitionMemberGroup constructors = scope.getTypeMembers(type).getOrCreateGroup(OperatorType.CONSTRUCTOR);
52
 		List<ITypeID>[] predictedTypes = constructors.predictCallTypes(scope, scope.hints, arguments.arguments.size());
52
 		List<ITypeID>[] predictedTypes = constructors.predictCallTypes(scope, scope.hints, arguments.arguments.size());
53
 		CallArguments compiledArguments = arguments.compileCall(position, scope, constructors);
53
 		CallArguments compiledArguments = arguments.compileCall(position, scope, constructors);

+ 15
- 0
ScriptingExample/scripts/enums.zs View File

1
+enum TestEnum {
2
+	HELLO,
3
+	WORLD
4
+}
5
+
6
+enum TestEnum2 {
7
+	HELLO("Hello"),
8
+	WORLD("World");
9
+	
10
+	val value as string;
11
+	
12
+	this(value as string) {
13
+		this.value = value;
14
+	}
15
+}

Loading…
Cancel
Save