Stan Hebben пре 6 година
родитељ
комит
565a59947d

+ 7
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/EnumConstantMember.java Прегледај датотеку

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

+ 5
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java Прегледај датотеку

@@ -211,10 +211,10 @@ public class TypeMemberBuilder implements ITypeVisitor<Void> {
211 211
 		if (constructors.getMethodMembers().isEmpty()) {
212 212
 			if (definition instanceof ClassDefinition) {
213 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 215
 			} else if (definition instanceof StructDefinition) {
216 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 219
 				List<FieldMember> fields = ((StructDefinition)definition).getFields();
220 220
 				if (!fields.isEmpty()) {
@@ -225,6 +225,9 @@ public class TypeMemberBuilder implements ITypeVisitor<Void> {
225 225
 					}
226 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 Прегледај датотеку

@@ -1,6 +1,5 @@
1 1
 package org.openzen.zenscript.javabytecode.compiler;
2 2
 
3
-import com.sun.istack.internal.Nullable;
4 3
 import org.objectweb.asm.*;
5 4
 import org.objectweb.asm.commons.LocalVariablesSorter;
6 5
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
@@ -1114,7 +1113,7 @@ public class JavaWriter {
1114 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 1117
         if (nameVariables)
1119 1118
             visitor.visitParameter(name, modifier);
1120 1119
     }

+ 25
- 1
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedEnum.java Прегледај датотеку

@@ -10,8 +10,12 @@ import java.util.List;
10 10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11 11
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
12 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 15
 import org.openzen.zenscript.lexer.ZSTokenStream;
14 16
 import org.openzen.zenscript.lexer.ZSTokenType;
17
+import org.openzen.zenscript.linker.BaseScope;
18
+import org.openzen.zenscript.linker.ExpressionScope;
15 19
 import org.openzen.zenscript.parser.member.ParsedDefinitionMember;
16 20
 import org.openzen.zenscript.shared.CodePosition;
17 21
 
@@ -26,7 +30,7 @@ public class ParsedEnum extends BaseParsedDefinition {
26 30
 		
27 31
 		List<ParsedEnumConstant> enumValues = new ArrayList<>();
28 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 34
 			if (tokens.optional(ZSTokenType.T_COMMA) == null)
31 35
 				break;
32 36
 		}
@@ -58,4 +62,24 @@ public class ParsedEnum extends BaseParsedDefinition {
58 62
 	public HighLevelDefinition getCompiled() {
59 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 Прегледај датотеку

@@ -8,10 +8,14 @@ package org.openzen.zenscript.parser.definitions;
8 8
 import java.util.ArrayList;
9 9
 import java.util.List;
10 10
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
11
+import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
11 12
 import org.openzen.zenscript.lexer.ZSToken;
12 13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13 14
 import org.openzen.zenscript.lexer.ZSTokenType;
15
+import org.openzen.zenscript.linker.ExpressionScope;
16
+import org.openzen.zenscript.parser.expression.ParsedCallArguments;
14 17
 import org.openzen.zenscript.parser.expression.ParsedExpression;
18
+import org.openzen.zenscript.parser.expression.ParsedNewExpression;
15 19
 import org.openzen.zenscript.shared.CodePosition;
16 20
 
17 21
 /**
@@ -19,7 +23,7 @@ import org.openzen.zenscript.shared.CodePosition;
19 23
  * @author Hoofdgebruiker
20 24
  */
21 25
 public class ParsedEnumConstant {
22
-	public static ParsedEnumConstant parse(ZSTokenStream tokens) {
26
+	public static ParsedEnumConstant parse(ZSTokenStream tokens, int value) {
23 27
 		ZSToken name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected");
24 28
 		List<ParsedExpression> arguments = new ArrayList<>();
25 29
 		if (tokens.optional(ZSTokenType.T_BROPEN) != null) {
@@ -29,7 +33,7 @@ public class ParsedEnumConstant {
29 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 39
 	public final CodePosition position;
@@ -38,11 +42,20 @@ public class ParsedEnumConstant {
38 42
 	
39 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 46
 		this.position = position;
43 47
 		this.name = name;
44 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 Прегледај датотеку

@@ -47,7 +47,7 @@ public class ParsedNewExpression extends ParsedExpression{
47 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 51
 		DefinitionMemberGroup constructors = scope.getTypeMembers(type).getOrCreateGroup(OperatorType.CONSTRUCTOR);
52 52
 		List<ITypeID>[] predictedTypes = constructors.predictCallTypes(scope, scope.hints, arguments.arguments.size());
53 53
 		CallArguments compiledArguments = arguments.compileCall(position, scope, constructors);

+ 15
- 0
ScriptingExample/scripts/enums.zs Прегледај датотеку

@@ -0,0 +1,15 @@
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…
Откажи
Сачувај