Explorar el Código

Added main specialized in testing the Java Bytecode implementation.

Stan Hebben hace 6 años
padre
commit
a680f453fa

+ 2
- 2
CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java Ver fichero

@@ -29,7 +29,7 @@ import org.openzen.zenscript.validator.Validator;
29 29
  */
30 30
 public class SemanticModule {
31 31
 	public final String name;
32
-	public final String[] dependencies;
32
+	public final SemanticModule[] dependencies;
33 33
 	
34 34
 	private State state;
35 35
 	public final ZSPackage rootPackage;
@@ -44,7 +44,7 @@ public class SemanticModule {
44 44
 	
45 45
 	public SemanticModule(
46 46
 			String name,
47
-			String[] dependencies,
47
+			SemanticModule[] dependencies,
48 48
 			State state,
49 49
 			ZSPackage rootPackage,
50 50
 			ZSPackage modulePackage,

+ 1
- 1
Constructor/src/main/java/org/openzen/zenscript/constructor/Module.java Ver fichero

@@ -89,7 +89,7 @@ public class Module {
89 89
 	
90 90
 	public static SemanticModule compileSyntaxToSemantic(
91 91
 			String name,
92
-			String[] dependencies,
92
+			SemanticModule[] dependencies,
93 93
 			CompilingPackage pkg,
94 94
 			ParsedFile[] files,
95 95
 			ModuleSpace registry,

+ 8
- 3
Constructor/src/main/java/org/openzen/zenscript/constructor/module/DirectoryModuleReference.java Ver fichero

@@ -71,15 +71,20 @@ public class DirectoryModuleReference implements ModuleReference {
71 71
 
72 72
 			// TODO: annotation type registration
73 73
 			ModuleSpace space = new ModuleSpace(unit, new ArrayList<>());
74
-			for (String dependencyName : dependencyNames)
75
-				space.addModule(dependencyName, loader.getModule(dependencyName));
74
+			SemanticModule[] dependencies = new SemanticModule[dependencyNames.size()];
75
+			for (int i = 0; i < dependencies.length; i++) {
76
+				String dependencyName = dependencyNames.get(i);
77
+				SemanticModule module = loader.getModule(dependencyName);
78
+				dependencies[i] = module;
79
+				space.addModule(dependencyName, module);
80
+			}
76 81
 
77 82
 			Module module = new Module(moduleName, directory, jsonFile, exceptionLogger);
78 83
 			ZSPackage pkg = isStdlib ? unit.globalTypeRegistry.stdlib : new ZSPackage(null, module.packageName);
79 84
 			CompilingPackage compilingPackage = new CompilingPackage(pkg);
80 85
 			
81 86
 			ParsedFile[] parsedFiles = module.parse(compilingPackage);
82
-			SemanticModule result = Module.compileSyntaxToSemantic(module.name, module.dependencies, compilingPackage, parsedFiles, space, exceptionLogger);
87
+			SemanticModule result = Module.compileSyntaxToSemantic(module.name, dependencies, compilingPackage, parsedFiles, space, exceptionLogger);
83 88
 			
84 89
 			JSONObject globals = json.optJSONObject("globals");
85 90
 			if (globals != null) {

+ 31
- 0
IDE/src/main/java/org/openzen/zenscript/ide/JavaBytecodeMain.java Ver fichero

@@ -0,0 +1,31 @@
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.ide;
7
+
8
+import java.io.File;
9
+import java.io.IOException;
10
+import org.openzen.zenscript.constructor.Project;
11
+import static org.openzen.zenscript.ide.Main.open;
12
+import org.openzen.zenscript.ide.host.DevelopmentHost;
13
+import org.openzen.zenscript.ide.host.local.LocalProjectDevelopmentHost;
14
+
15
+/**
16
+ *
17
+ * @author Hoofdgebruiker
18
+ */
19
+public class JavaBytecodeMain {
20
+	/**
21
+     * @param args the command line arguments
22
+     */
23
+    public static void main(String[] args) throws IOException {
24
+		Arguments arguments = new Arguments(args);
25
+		File directory = arguments.projectDirectory;
26
+		
27
+		Project project = new Project(directory);
28
+		DevelopmentHost host = new LocalProjectDevelopmentHost(project);
29
+		open(host, "SharedJavaBytecode");
30
+    }
31
+}

+ 3
- 3
IDE/src/main/java/org/openzen/zenscript/ide/Main.java Ver fichero

@@ -26,13 +26,13 @@ public class Main {
26 26
 		
27 27
 		Project project = new Project(directory);
28 28
 		DevelopmentHost host = new LocalProjectDevelopmentHost(project);
29
-		open(host);
29
+		open(host, "SharedJavaSource");
30 30
     }
31 31
 	
32
-	private static void open(DevelopmentHost host) {
32
+	public static void open(DevelopmentHost host, String target) {
33 33
 		IDEPropertyStore properties = host.getPropertyStore();
34 34
 		
35
-		IDEWindow window = new IDEWindow(host);
35
+		IDEWindow window = new IDEWindow(host, target);
36 36
 		WindowView root = new WindowView(window, host);
37 37
 		
38 38
 		IDEPropertyDirectory uiState = properties.getRoot().getSubdirectory("uiState");

+ 11
- 12
IDE/src/main/java/org/openzen/zenscript/ide/host/local/LocalTarget.java Ver fichero

@@ -134,40 +134,39 @@ public class LocalTarget implements IDETarget {
134 134
 	}
135 135
 	
136 136
 	private boolean compileDependencies(ModuleLoader loader, ZenCodeCompiler compiler, Set<String> compiledModules, Stack<String> compilingModules, SemanticModule module, Consumer<ValidationLogEntry> logger) {
137
-		for (String dependency : module.dependencies) {
138
-			if (compiledModules.contains(dependency))
137
+		for (SemanticModule dependency : module.dependencies) {
138
+			if (compiledModules.contains(dependency.name))
139 139
 				continue;
140
-			compiledModules.add(dependency);
140
+			compiledModules.add(dependency.name);
141 141
 			System.out.println("== Compiling module " + dependency + " ==");
142 142
 			
143
-			if (compilingModules.contains(dependency)) {
143
+			if (compilingModules.contains(dependency.name)) {
144 144
 				StringBuilder message = new StringBuilder("Circular dependency:\n");
145 145
 				for (String s : compilingModules)
146 146
 					message.append("    ").append(s).append("\n");
147 147
 				
148 148
 				throw new IllegalStateException(message.toString());
149 149
 			}
150
-			compilingModules.push(dependency);
150
+			compilingModules.push(dependency.name);
151 151
 			
152
-			SemanticModule dependencyModule = loader.getModule(dependency);
153
-			if (!dependencyModule.isValid()) {
152
+			if (!dependency.isValid()) {
154 153
 				compilingModules.pop();
155 154
 				return false;
156 155
 			}
157 156
 			
158
-			dependencyModule = dependencyModule.normalize();
159
-			dependencyModule.validate(logger);
160
-			if (!dependencyModule.isValid()) {
157
+			dependency = dependency.normalize();
158
+			dependency.validate(logger);
159
+			if (!dependency.isValid()) {
161 160
 				compilingModules.pop();
162 161
 				return false;
163 162
 			}
164 163
 			
165
-			if (!compileDependencies(loader, compiler, compiledModules, compilingModules, dependencyModule, logger)) {
164
+			if (!compileDependencies(loader, compiler, compiledModules, compilingModules, dependency, logger)) {
166 165
 				compilingModules.pop();
167 166
 				return false;
168 167
 			}
169 168
 			
170
-			dependencyModule.compile(compiler);
169
+			dependency.compile(compiler);
171 170
 			compilingModules.pop();
172 171
 		}
173 172
 		

+ 7
- 4
IDE/src/main/java/org/openzen/zenscript/ide/ui/IDEWindow.java Ver fichero

@@ -25,6 +25,7 @@ import org.openzen.zenscript.ide.ui.view.output.OutputLine;
25 25
  */
26 26
 public class IDEWindow {
27 27
 	private final DevelopmentHost host;
28
+	private final String target;
28 29
 	
29 30
 	public final IDEAspectBar aspectBar;
30 31
 	public final IDEDockWindow dockWindow;
@@ -33,8 +34,9 @@ public class IDEWindow {
33 34
 	
34 35
 	public IDEAspectToolbar projectToolbar;
35 36
 	
36
-	public IDEWindow(DevelopmentHost host) {
37
+	public IDEWindow(DevelopmentHost host, String target) {
37 38
 		this.host = host;
39
+		this.target = target;
38 40
 		
39 41
 		aspectBar = new IDEAspectBar();
40 42
 		dockWindow = new IDEDockWindow();
@@ -42,8 +44,9 @@ public class IDEWindow {
42 44
 		init();
43 45
 	}
44 46
 	
45
-	public IDEWindow(DevelopmentHost host, IDEAspectBar aspectBar, IDEDockWindow dockWindow, IDEStatusBar statusBar) {
47
+	public IDEWindow(DevelopmentHost host, IDEAspectBar aspectBar, IDEDockWindow dockWindow, IDEStatusBar statusBar, String target) {
46 48
 		this.host = host;
49
+		this.target = target;
47 50
 		
48 51
 		this.aspectBar = aspectBar;
49 52
 		this.dockWindow = dockWindow;
@@ -63,13 +66,13 @@ public class IDEWindow {
63 66
 		projectToolbar.controls.add(() -> new IconButtonControl(DStyleClass.EMPTY, BuildIcon.BLUE, new ImmutableLiveString("Build"), e -> {
64 67
 			output.clear();
65 68
 			for (IDETarget target : host.getTargets()) {
66
-				if (target.canBuild())
69
+				if (target.getName().equals(this.target) && target.canBuild())
67 70
 					target.build(line -> output.add(line));
68 71
 			}
69 72
 		}));
70 73
 		projectToolbar.controls.add(() -> new IconButtonControl(DStyleClass.EMPTY, PlayIcon.GREEN, new ImmutableLiveString("Run"), e -> {
71 74
 			for (IDETarget target : host.getTargets()) {
72
-				if (target.canRun())
75
+				if (target.getName().equals(this.target) && target.canRun())
73 76
 					target.run(line -> output.add(line));
74 77
 			}
75 78
 		}));

+ 5
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeJarTarget.java Ver fichero

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.javabytecode;
7 7
 
8
+import java.io.File;
8 9
 import org.json.JSONObject;
9 10
 import org.openzen.zenscript.compiler.SemanticModule;
10 11
 import org.openzen.zenscript.compiler.Target;
@@ -16,17 +17,19 @@ import org.openzen.zenscript.compiler.Target;
16 17
 public class JavaBytecodeJarTarget implements Target {
17 18
 	private final String module;
18 19
 	private final String name;
20
+	private final File file;
19 21
 	private final boolean debugCompiler;
20 22
 	
21 23
 	public JavaBytecodeJarTarget(JSONObject definition) {
22 24
 		module = definition.getString("module");
23
-		name = definition.optString("name", "Java Bytecode: " + module);
25
+		name = definition.getString("name");
26
+		file = new File(definition.getString("output"));
24 27
 		debugCompiler = definition.optBoolean("debugCompiler", false);
25 28
 	}
26 29
 	
27 30
 	@Override
28 31
 	public JavaCompiler createCompiler(SemanticModule module) {
29
-		return new JavaCompiler(debugCompiler);
32
+		return new JavaCompiler(debugCompiler, file);
30 33
 	}
31 34
 
32 35
 	@Override

+ 2
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunTarget.java Ver fichero

@@ -20,13 +20,13 @@ public class JavaBytecodeRunTarget implements Target {
20 20
 	
21 21
 	public JavaBytecodeRunTarget(JSONObject definition) {
22 22
 		module = definition.getString("module");
23
-		name = definition.optString("name", "Java Run: " + module);
23
+		name = definition.getString("name");
24 24
 		debugCompiler = definition.optBoolean("debugCompiler", false);
25 25
 	}
26 26
 	
27 27
 	@Override
28 28
 	public JavaCompiler createCompiler(SemanticModule module) {
29
-		return new JavaCompiler(debugCompiler);
29
+		return new JavaCompiler(debugCompiler, null);
30 30
 	}
31 31
 
32 32
 	@Override

+ 12
- 5
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java Ver fichero

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.javabytecode;
7 7
 
8
+import java.io.File;
8 9
 import java.util.HashMap;
9 10
 import java.util.Map;
10 11
 
@@ -34,13 +35,15 @@ public class JavaCompiler implements ZenCodeCompiler {
34 35
 	private int generatedScriptBlockCounter = 0;
35 36
 	private boolean finished = false;
36 37
 	private JavaModule compiled = null;
38
+	private final File jarFile;
37 39
 	
38
-	public JavaCompiler() {
39
-		this(false);
40
+	public JavaCompiler(File jarFile) {
41
+		this(false, jarFile);
40 42
 	}
41 43
 	
42
-	public JavaCompiler(boolean debug) {
44
+	public JavaCompiler(boolean debug, File jarFile) {
43 45
 		target = new JavaModule();
46
+		this.jarFile = jarFile;
44 47
 		
45 48
 		scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
46 49
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
@@ -98,7 +101,11 @@ public class JavaCompiler implements ZenCodeCompiler {
98 101
 	
99 102
 	@Override
100 103
 	public void finish() {
101
-		finishAndGetModule();
104
+		JavaModule module = finishAndGetModule();
105
+		
106
+		if (jarFile != null) {
107
+			// TODO: write module to a Jar file
108
+		}
102 109
 	}
103 110
 	
104 111
 	@Override
@@ -106,7 +113,7 @@ public class JavaCompiler implements ZenCodeCompiler {
106 113
 		if (compiled == null)
107 114
 			throw new IllegalStateException("Not yet built!");
108 115
 		
109
-		
116
+		// TODO: execute this
110 117
 	}
111 118
 	
112 119
 	public JavaModule finishAndGetModule() {

+ 5
- 5
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java Ver fichero

@@ -43,11 +43,11 @@ public class Main {
43 43
 		
44 44
 		ZSPackage pkg = new ZSPackage(null, "");
45 45
 		CompilingPackage compilingPkg = new CompilingPackage(pkg);
46
-		ParsedFile[] parsedFiles = parse(pkg, compilingPkg, inputFiles);
46
+		ParsedFile[] parsedFiles = parse(compilingPkg, inputFiles);
47 47
 		
48 48
 		ZSPackage global = new ZSPackage(null, "");
49 49
 		GlobalRegistry registry = new GlobalRegistry(global);
50
-		SemanticModule module = compileSyntaxToSemantic(pkg, compilingPkg, parsedFiles, registry);
50
+		SemanticModule module = compileSyntaxToSemantic(compilingPkg, parsedFiles, registry);
51 51
 		
52 52
 		//formatFiles(pkg, module);
53 53
 		
@@ -59,7 +59,7 @@ public class Main {
59 59
 		}
60 60
     }
61 61
 	
62
-	private static ParsedFile[] parse(ZSPackage pkg, CompilingPackage compilingPkg, File[] files) throws IOException {
62
+	private static ParsedFile[] parse(CompilingPackage compilingPkg, File[] files) throws IOException {
63 63
 		ParsedFile[] parsedFiles = new ParsedFile[files.length];
64 64
 		for (int i = 0; i < files.length; i++) {
65 65
 			parsedFiles[i] = ParsedFile.parse(compilingPkg, new TestBracketParser(), files[i]);
@@ -102,14 +102,14 @@ public class Main {
102 102
 		}
103 103
 	}
104 104
 	
105
-	private static SemanticModule compileSyntaxToSemantic(ZSPackage pkg, CompilingPackage compiling, ParsedFile[] files, GlobalRegistry registry) {
105
+	private static SemanticModule compileSyntaxToSemantic(CompilingPackage compiling, ParsedFile[] files, GlobalRegistry registry) {
106 106
 		ModuleSpace space = new ModuleSpace(new CompilationUnit(), new ArrayList<>());
107 107
 		for (Map.Entry<String, ISymbol> global : registry.collectGlobals().entrySet()) {
108 108
 			space.addGlobal(global.getKey(), global.getValue());
109 109
 		}
110 110
 		SemanticModule result = Module.compileSyntaxToSemantic(
111 111
 				"scripts",
112
-				new String[0],
112
+				new SemanticModule[0],
113 113
 				compiling,
114 114
 				files,
115 115
 				space,

Loading…
Cancelar
Guardar