소스 검색

- Added tagging system to tag classes and members with compile-time information

- Improved scripting example to use those tags
- Generated script block methods use script names
- Minor code improvements
Stan Hebben 6 년 전
부모
커밋
ba5d0f73b5
27개의 변경된 파일229개의 추가작업 그리고 89개의 파일을 삭제
  1. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java
  2. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/ScriptBlock.java
  3. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java
  4. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/DefinitionMember.java
  5. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/EqualsMember.java
  6. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java
  7. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ArrayIteratorKeyValues.java
  8. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ArrayIteratorValues.java
  9. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ComparatorMember.java
  10. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ConstantGetterMember.java
  11. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/RangeIterator.java
  12. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/StringConcatMember.java
  13. 2
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/SubstringMember.java
  14. 5
    1
      Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java
  15. BIN
      ScriptingExample/Scripts.class
  16. 29
    11
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java
  17. 18
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaClassInfo.java
  18. 22
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaFieldInfo.java
  19. 22
    0
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaMethodInfo.java
  20. 16
    4
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java
  21. 0
    24
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/NativeFieldMember.java
  22. 0
    26
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/NativeMethodMember.java
  23. 21
    10
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaExpressionVisitor.java
  24. 1
    2
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaWriter.java
  25. 18
    0
      Shared/src/main/java/org/openzen/zenscript/shared/SourceFile.java
  26. 25
    0
      Shared/src/main/java/org/openzen/zenscript/shared/TagDictionary.java
  27. 22
    0
      Shared/src/main/java/org/openzen/zenscript/shared/Taggable.java

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java 파일 보기

@@ -12,12 +12,13 @@ import org.openzen.zenscript.codemodel.generic.TypeParameter;
12 12
 import org.openzen.zenscript.codemodel.member.FieldMember;
13 13
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
14 14
 import org.openzen.zenscript.codemodel.type.ITypeID;
15
+import org.openzen.zenscript.shared.Taggable;
15 16
 
16 17
 /**
17 18
  *
18 19
  * @author Hoofdgebruiker
19 20
  */
20
-public abstract class HighLevelDefinition {
21
+public abstract class HighLevelDefinition extends Taggable {
21 22
 	public final String name;
22 23
 	public final int modifiers;
23 24
 	public final List<IDefinitionMember> members = new ArrayList<>();

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/ScriptBlock.java 파일 보기

@@ -7,12 +7,13 @@ package org.openzen.zenscript.codemodel;
7 7
 
8 8
 import java.util.List;
9 9
 import org.openzen.zenscript.codemodel.statement.Statement;
10
+import org.openzen.zenscript.shared.Taggable;
10 11
 
11 12
 /**
12 13
  *
13 14
  * @author Hoofdgebruiker
14 15
  */
15
-public class ScriptBlock {
16
+public class ScriptBlock extends Taggable {
16 17
 	public final List<Statement> statements;
17 18
 	
18 19
 	public ScriptBlock(List<Statement> statements) {

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java 파일 보기

@@ -13,6 +13,10 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
13 13
  * @author Hoofdgebruiker
14 14
  */
15 15
 public class ClassDefinition extends HighLevelDefinition {
16
+	public ClassDefinition(String name, int modifiers) {
17
+		this(name, modifiers, null);
18
+	}
19
+	
16 20
 	public ClassDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
17 21
 		super(name, modifiers, outerDefinition);
18 22
 	}

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/DefinitionMember.java 파일 보기

@@ -7,12 +7,13 @@ package org.openzen.zenscript.codemodel.member;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.Modifiers;
9 9
 import org.openzen.zenscript.shared.CodePosition;
10
+import org.openzen.zenscript.shared.Taggable;
10 11
 
11 12
 /**
12 13
  *
13 14
  * @author Hoofdgebruiker
14 15
  */
15
-public abstract class DefinitionMember implements IDefinitionMember {
16
+public abstract class DefinitionMember extends Taggable implements IDefinitionMember {
16 17
 	public final CodePosition position;
17 18
 	public final int modifiers;
18 19
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/EqualsMember.java 파일 보기

@@ -20,12 +20,13 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
20 20
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
21 21
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
22 22
 import org.openzen.zenscript.shared.CodePosition;
23
+import org.openzen.zenscript.shared.Taggable;
23 24
 
24 25
 /**
25 26
  *
26 27
  * @author Hoofdgebruiker
27 28
  */
28
-public class EqualsMember implements ICallableMember {
29
+public class EqualsMember extends Taggable implements ICallableMember {
29 30
 	private final ITypeID type;
30 31
 	private final FunctionHeader header;
31 32
 	

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java 파일 보기

@@ -27,4 +27,8 @@ public interface IDefinitionMember {
27 27
 	public IDefinitionMember instance(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> mapping);
28 28
 	
29 29
 	public <T> T accept(MemberVisitor<T> visitor);
30
+	
31
+	public <T> T getTag(Class<T> tag);
32
+	
33
+	public <T> void setTag(Class<T> tag, T value);
30 34
 }

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ArrayIteratorKeyValues.java 파일 보기

@@ -18,12 +18,13 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
18 18
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
19 19
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
20 20
 import org.openzen.zenscript.shared.CodePosition;
21
+import org.openzen.zenscript.shared.Taggable;
21 22
 
22 23
 /**
23 24
  *
24 25
  * @author Hoofdgebruiker
25 26
  */
26
-public class ArrayIteratorKeyValues implements IIteratorMember {
27
+public class ArrayIteratorKeyValues extends Taggable implements IIteratorMember {
27 28
 	private final ArrayTypeID type;
28 29
 	private final ITypeID[] loopVariableTypes;
29 30
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ArrayIteratorValues.java 파일 보기

@@ -17,12 +17,13 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
17 17
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
18 18
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
19 19
 import org.openzen.zenscript.shared.CodePosition;
20
+import org.openzen.zenscript.shared.Taggable;
20 21
 
21 22
 /**
22 23
  *
23 24
  * @author Hoofdgebruiker
24 25
  */
25
-public class ArrayIteratorValues implements IIteratorMember {
26
+public class ArrayIteratorValues extends Taggable implements IIteratorMember {
26 27
 	private final ArrayTypeID type;
27 28
 	private final ITypeID[] loopVariableTypes;
28 29
 	

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ComparatorMember.java 파일 보기

@@ -18,6 +18,7 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
18 18
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
19 19
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
20 20
 import org.openzen.zenscript.shared.CodePosition;
21
+import org.openzen.zenscript.shared.Taggable;
21 22
 
22 23
 /*
23 24
  * To change this license header, choose License Headers in Project Properties.
@@ -29,7 +30,7 @@ import org.openzen.zenscript.shared.CodePosition;
29 30
  *
30 31
  * @author Hoofdgebruiker
31 32
  */
32
-public class ComparatorMember implements ICallableMember {
33
+public class ComparatorMember extends Taggable implements ICallableMember {
33 34
 	private final FunctionHeader header;
34 35
 	
35 36
 	public ComparatorMember(ITypeID type) {

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/ConstantGetterMember.java 파일 보기

@@ -19,12 +19,13 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
19 19
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
20 20
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
21 21
 import org.openzen.zenscript.shared.CodePosition;
22
+import org.openzen.zenscript.shared.Taggable;
22 23
 
23 24
 /**
24 25
  *
25 26
  * @author Hoofdgebruiker
26 27
  */
27
-public class ConstantGetterMember implements IGettableMember {
28
+public class ConstantGetterMember extends Taggable implements IGettableMember {
28 29
 	private final String name;
29 30
 	private final Function<CodePosition, Expression> value;
30 31
 	private final ITypeID type;

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/RangeIterator.java 파일 보기

@@ -18,12 +18,13 @@ import org.openzen.zenscript.codemodel.type.RangeTypeID;
18 18
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
19 19
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
20 20
 import org.openzen.zenscript.shared.CodePosition;
21
+import org.openzen.zenscript.shared.Taggable;
21 22
 
22 23
 /**
23 24
  *
24 25
  * @author Hoofdgebruiker
25 26
  */
26
-public class RangeIterator implements IIteratorMember {
27
+public class RangeIterator extends Taggable implements IIteratorMember {
27 28
 	private final ITypeID[] loopVariableTypes;
28 29
 	
29 30
 	public RangeIterator(RangeTypeID type) {

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/StringConcatMember.java 파일 보기

@@ -21,12 +21,13 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
21 21
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
22 22
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
23 23
 import org.openzen.zenscript.shared.CodePosition;
24
+import org.openzen.zenscript.shared.Taggable;
24 25
 
25 26
 /**
26 27
  *
27 28
  * @author Hoofdgebruiker
28 29
  */
29
-public class StringConcatMember implements ICallableMember {
30
+public class StringConcatMember extends Taggable implements ICallableMember {
30 31
 	private final FunctionHeader header;
31 32
 	
32 33
 	public StringConcatMember(FunctionHeader header) {

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/SubstringMember.java 파일 보기

@@ -21,12 +21,13 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
21 21
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
22 22
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
23 23
 import org.openzen.zenscript.shared.CodePosition;
24
+import org.openzen.zenscript.shared.Taggable;
24 25
 
25 26
 /**
26 27
  *
27 28
  * @author Hoofdgebruiker
28 29
  */
29
-public class SubstringMember implements ICallableMember {
30
+public class SubstringMember extends Taggable implements ICallableMember {
30 31
 	private final FunctionHeader header;
31 32
 	
32 33
 	public SubstringMember(FunctionHeader header) {

+ 5
- 1
Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java 파일 보기

@@ -30,6 +30,7 @@ import org.openzen.zenscript.linker.GlobalScriptScope;
30 30
 import org.openzen.zenscript.linker.symbol.ISymbol;
31 31
 import org.openzen.zenscript.linker.StatementScope;
32 32
 import org.openzen.zenscript.parser.statements.ParsedStatement;
33
+import org.openzen.zenscript.shared.SourceFile;
33 34
 
34 35
 /**
35 36
  *
@@ -144,7 +145,10 @@ public class ParsedFile {
144 145
 			for (ParsedStatement statement : this.statements) {
145 146
 				statements.add(statement.compile(statementScope));
146 147
 			}
147
-			scripts.add(new ScriptBlock(statements));
148
+			
149
+			ScriptBlock block = new ScriptBlock(statements);
150
+			block.setTag(SourceFile.class, new SourceFile(filename));
151
+			scripts.add(block);
148 152
 		}
149 153
 	}
150 154
 	

BIN
ScriptingExample/Scripts.class 파일 보기


+ 29
- 11
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java 파일 보기

@@ -16,6 +16,8 @@ import org.openzen.zenscript.codemodel.definition.ClassDefinition;
16 16
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
17 17
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
18 18
 import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
19
+import org.openzen.zenscript.codemodel.member.FieldMember;
20
+import org.openzen.zenscript.codemodel.member.MethodMember;
19 21
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
20 22
 import org.openzen.zenscript.codemodel.partial.PartialMemberGroupExpression;
21 23
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
@@ -42,8 +44,12 @@ public class GlobalRegistry {
42 44
 			// eg. package my.package with a class MyClass with a single native method test() returning a string
43 45
 			// the visitors can then during compilation check if a method is an instance of NativeMethodMember and treat it accordingly
44 46
 			ClassDefinition myClassDefinition = new ClassDefinition("MyClass", Modifiers.MODIFIER_PUBLIC, null);
45
-			myClassDefinition.addMember(new NativeMethodMember(Modifiers.MODIFIER_PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING), "Lmy/test/MyClass;", "()Ljava/lang/String;"));
46
-
47
+			JavaClassInfo myClassInfo = new JavaClassInfo("my/test/MyClass");
48
+			
49
+			MethodMember member = new MethodMember(CodePosition.NATIVE, Modifiers.MODIFIER_PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING));
50
+			member.setTag(JavaMethodInfo.class, new JavaMethodInfo(myClassInfo, "test", "()Ljava/lang/String;"));
51
+			myClassDefinition.addMember(member);
52
+			
47 53
 			ZSPackage packageMyPackage = rootPackage.getOrCreatePackage("my").getOrCreatePackage("package");
48 54
 			packageMyPackage.register(myClassDefinition);
49 55
 		}
@@ -66,15 +72,27 @@ public class GlobalRegistry {
66 72
 		return globals;
67 73
 	}
68 74
 	
69
-	private static final ClassDefinition printStream = new ClassDefinition("Ljava/io/PrintStream;", Modifiers.MODIFIER_EXPORT, null);
70
-	private static final NativeMethodMember printStreamPrintln = new NativeMethodMember(
75
+	private static final ClassDefinition PRINTSTREAM = new ClassDefinition("PrintStream", Modifiers.MODIFIER_EXPORT);
76
+	private static final MethodMember PRINTSTREAM_PRINTLN = new MethodMember(
77
+			CodePosition.NATIVE,
71 78
 			Modifiers.MODIFIER_EXPORT,
72 79
 			"println",
73
-			new FunctionHeader(BasicTypeID.VOID, new FunctionParameter(BasicTypeID.STRING)),
74
-			"Ljava/io/PrintStream;",
75
-			"(Ljava/lang/String;)V");
80
+			new FunctionHeader(BasicTypeID.VOID, new FunctionParameter(BasicTypeID.STRING)));
81
+	
82
+	private static final FieldMember SYSTEM_OUT = new FieldMember(
83
+			CodePosition.NATIVE,
84
+			Modifiers.MODIFIER_EXPORT,
85
+			"out",
86
+			DefinitionTypeID.forType(PRINTSTREAM),
87
+			true);
76 88
 	
77
-	private static final NativeFieldMember systemOut = new NativeFieldMember(Modifiers.MODIFIER_EXPORT, "out", DefinitionTypeID.forType(printStream), true, "java/lang/System");
89
+	static {
90
+		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
91
+		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V"));
92
+		
93
+		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
94
+		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
95
+	}
78 96
 	
79 97
 	private class PrintlnSymbol implements ISymbol {
80 98
 
@@ -82,15 +100,15 @@ public class GlobalRegistry {
82 100
 		public IPartialExpression getExpression(CodePosition position, GlobalTypeRegistry types, List<ITypeID> typeArguments) {
83 101
 			return new PartialMemberGroupExpression(
84 102
 					position,
85
-					new GetStaticFieldExpression(position, systemOut),
86
-					printStreamPrintln,
103
+					new GetStaticFieldExpression(position, SYSTEM_OUT),
104
+					PRINTSTREAM_PRINTLN,
87 105
 					false);
88 106
 		}
89 107
 
90 108
 		@Override
91 109
 		public ITypeID getType(CodePosition position, GlobalTypeRegistry types, List<ITypeID> typeArguments) {
92 110
 			// don't be fooled! this symbol is the System.out.println bound method and thus its type is a function
93
-			return new FunctionTypeID(printStreamPrintln.header);
111
+			return new FunctionTypeID(PRINTSTREAM_PRINTLN.header);
94 112
 		}
95 113
 	}
96 114
 }

+ 18
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaClassInfo.java 파일 보기

@@ -0,0 +1,18 @@
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.scriptingexample;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class JavaClassInfo {
13
+	public final String internalClassName;
14
+	
15
+	public JavaClassInfo(String internalClassName) {
16
+		this.internalClassName = internalClassName;
17
+	}
18
+}

+ 22
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaFieldInfo.java 파일 보기

@@ -0,0 +1,22 @@
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.scriptingexample;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class JavaFieldInfo {
13
+	public final JavaClassInfo javaClass;
14
+	public final String name;
15
+	public final String signature;
16
+	
17
+	public JavaFieldInfo(JavaClassInfo javaClass, String name, String signature) {
18
+		this.javaClass = javaClass;
19
+		this.name = name;
20
+		this.signature = signature;
21
+	}
22
+}

+ 22
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaMethodInfo.java 파일 보기

@@ -0,0 +1,22 @@
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.scriptingexample;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class JavaMethodInfo {
13
+	public final JavaClassInfo javaClass;
14
+	public final String name;
15
+	public final String signature;
16
+	
17
+	public JavaMethodInfo(JavaClassInfo javaClass, String name, String signature) {
18
+		this.javaClass = javaClass;
19
+		this.name = name;
20
+		this.signature = signature;
21
+	}
22
+}

+ 16
- 4
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java 파일 보기

@@ -15,12 +15,12 @@ import org.openzen.zenscript.codemodel.ScriptBlock;
15 15
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
16 16
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
17 17
 import org.openzen.zenscript.codemodel.statement.Statement;
18
-import org.openzen.zenscript.codemodel.statement.StatementVisitor;
19 18
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
20 19
 import org.openzen.zenscript.linker.symbol.ISymbol;
21 20
 import org.openzen.zenscript.parser.ParsedFile;
22 21
 import org.openzen.zenscript.scriptingexample.writer.JavaStatementVisitor;
23 22
 import org.openzen.zenscript.scriptingexample.writer.JavaWriter;
23
+import org.openzen.zenscript.shared.SourceFile;
24 24
 
25 25
 public class Main {
26 26
     /**
@@ -95,10 +95,22 @@ public class Main {
95 95
 
96 96
 		}
97 97
 		int statementNo = 0;
98
+		List<String> scriptBlockNames = new ArrayList<>();
98 99
 		for (ScriptBlock script : module.scripts) {
100
+			SourceFile sourceFile = script.getTag(SourceFile.class);
101
+			String methodName;
102
+			if (sourceFile == null) {
103
+				methodName = "generatedBlock" + (statementNo++);
104
+			} else {
105
+				// TODO: remove special characters
106
+				methodName = sourceFile.filename.substring(0, sourceFile.filename.lastIndexOf('.')).replace("/", "_");
107
+			}
108
+			
109
+			scriptBlockNames.add(methodName);
110
+			
99 111
 			// convert scripts into methods (add them to a Scripts class?)
100 112
 			// (TODO: can we break very long scripts into smaller methods? for the extreme scripts)
101
-			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(new JavaWriter(classWriter, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, makeName(statementNo++), "()V", null, null));
113
+			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(new JavaWriter(classWriter, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, methodName, "()V", null, null));
102 114
 			statementVisitor.start();
103 115
 			for (Statement statement : script.statements) {
104 116
 				statement.accept(statementVisitor);
@@ -109,8 +121,8 @@ public class Main {
109 121
 		// create a Scripts.run() method to run all scripts compiled above
110 122
 		final JavaWriter runWriter = new JavaWriter(classWriter, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()V", null, null);
111 123
 		runWriter.start();
112
-		for (int i = 0; i < statementNo; i++) {
113
-			runWriter.invokeStatic("Scripts", makeName(i), "()V");
124
+		for (String scriptBlockName : scriptBlockNames) {
125
+			runWriter.invokeStatic("Scripts", scriptBlockName, "()V");
114 126
 		}
115 127
 		runWriter.ret();
116 128
 		runWriter.end();

+ 0
- 24
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/NativeFieldMember.java 파일 보기

@@ -1,24 +0,0 @@
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.scriptingexample;
7
-
8
-import org.openzen.zenscript.codemodel.member.FieldMember;
9
-import org.openzen.zenscript.codemodel.type.ITypeID;
10
-import org.openzen.zenscript.shared.CodePosition;
11
-
12
-/**
13
- *
14
- * @author Hoofdgebruiker
15
- */
16
-public class NativeFieldMember extends FieldMember {
17
-	public final String className;
18
-	
19
-	public NativeFieldMember(int modifiers, String name, ITypeID type, boolean isFinal, String className) {
20
-		super(CodePosition.NATIVE, modifiers, name, type, isFinal);
21
-		
22
-		this.className = className;
23
-	}
24
-}

+ 0
- 26
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/NativeMethodMember.java 파일 보기

@@ -1,26 +0,0 @@
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.scriptingexample;
7
-
8
-import org.openzen.zenscript.codemodel.FunctionHeader;
9
-import org.openzen.zenscript.codemodel.member.MethodMember;
10
-import org.openzen.zenscript.shared.CodePosition;
11
-
12
-/**
13
- *
14
- * @author Hoofdgebruiker
15
- */
16
-public class NativeMethodMember extends MethodMember {
17
-	public final String className;
18
-	public final String signature;
19
-
20
-	public NativeMethodMember(int modifiers, String name, FunctionHeader header, String className, String signature) {
21
-		super(CodePosition.NATIVE, modifiers, name, header);
22
-
23
-		this.className = className;
24
-		this.signature = signature;
25
-	}
26
-}

+ 21
- 10
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaExpressionVisitor.java 파일 보기

@@ -1,9 +1,8 @@
1 1
 package org.openzen.zenscript.scriptingexample.writer;
2 2
 
3
-import org.objectweb.asm.Type;
4 3
 import org.openzen.zenscript.codemodel.expression.*;
5
-import org.openzen.zenscript.scriptingexample.NativeFieldMember;
6
-import org.openzen.zenscript.scriptingexample.NativeMethodMember;
4
+import org.openzen.zenscript.scriptingexample.JavaFieldInfo;
5
+import org.openzen.zenscript.scriptingexample.JavaMethodInfo;
7 6
 
8 7
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
9 8
 
@@ -35,9 +34,16 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
35 34
         for (Expression argument : expression.arguments.arguments) {
36 35
             argument.accept(this);
37 36
         }
38
-        if (expression.member instanceof NativeMethodMember) {
39
-            javaWriter.invokeVirtual(((NativeMethodMember) expression.member).className.replaceFirst("L", "").replace(";", ""), ((NativeMethodMember) expression.member).name, ((NativeMethodMember) expression.member).signature);
40
-        }
37
+		
38
+		JavaMethodInfo methodInfo = expression.member.getTag(JavaMethodInfo.class);
39
+		if (methodInfo == null)
40
+			throw new IllegalStateException("Call target has no method info!");
41
+		
42
+        javaWriter.invokeVirtual(
43
+				methodInfo.javaClass.internalClassName,
44
+				methodInfo.name,
45
+				methodInfo.signature);
46
+		
41 47
         return null;
42 48
     }
43 49
 
@@ -206,10 +212,15 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
206 212
 
207 213
     @Override
208 214
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
209
-
210
-        if (expression.field instanceof NativeFieldMember)
211
-
212
-            javaWriter.getStaticField(((NativeFieldMember) expression.field).className, expression.field.name, Type.getType(expression.type.accept(new JavaTypeVisitor())).getDescriptor());
215
+		JavaFieldInfo fieldInfo = expression.field.getTag(JavaFieldInfo.class);
216
+		if (fieldInfo == null)
217
+			throw new IllegalStateException("Missing field info on a field member!");
218
+		
219
+		javaWriter.getStaticField(
220
+				fieldInfo.javaClass.internalClassName,
221
+				fieldInfo.name,
222
+				fieldInfo.signature);
223
+		
213 224
         return null;
214 225
     }
215 226
 

+ 1
- 2
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaWriter.java 파일 보기

@@ -1,6 +1,5 @@
1 1
 package org.openzen.zenscript.scriptingexample.writer;
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.shared.CodePosition;
@@ -17,7 +16,7 @@ public class JavaWriter {
17 16
     private int labelIndex = 1;
18 17
     private Map<Label, String> labelNames = new HashMap<>();
19 18
 
20
-    public JavaWriter(ClassVisitor visitor, int access, String name, String description, @Nullable String signature, @Nullable String[] exceptions, String... annotations) {
19
+    public JavaWriter(ClassVisitor visitor, int access, String name, String description, String signature, String[] exceptions, String... annotations) {
21 20
         final MethodVisitor methodVisitor = visitor.visitMethod(access, name, description, signature, exceptions);
22 21
 
23 22
         for (String annotation : annotations) {

+ 18
- 0
Shared/src/main/java/org/openzen/zenscript/shared/SourceFile.java 파일 보기

@@ -0,0 +1,18 @@
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.shared;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class SourceFile {
13
+	public final String filename;
14
+	
15
+	public SourceFile(String filename) {
16
+		this.filename = filename;
17
+	}
18
+}

+ 25
- 0
Shared/src/main/java/org/openzen/zenscript/shared/TagDictionary.java 파일 보기

@@ -0,0 +1,25 @@
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.shared;
7
+
8
+import java.util.HashMap;
9
+import java.util.Map;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public class TagDictionary {
16
+	private final Map<Class<?>, Object> tags = new HashMap<>();
17
+	
18
+	public <T> void put(Class<T> cls, T tag) {
19
+		tags.put(cls, tag);
20
+	}
21
+	
22
+	public <T> T get(Class<T> cls) {
23
+		return (T) tags.get(cls);
24
+	}
25
+}

+ 22
- 0
Shared/src/main/java/org/openzen/zenscript/shared/Taggable.java 파일 보기

@@ -0,0 +1,22 @@
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.shared;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public abstract class Taggable {
13
+	private final TagDictionary tags = new TagDictionary();
14
+	
15
+	public final <T> void setTag(Class<T> cls, T tag) {
16
+		tags.put(cls, tag);
17
+	}
18
+	
19
+	public final <T> T getTag(Class<T> cls) {
20
+		return tags.get(cls);
21
+	}
22
+}

Loading…
취소
저장