Browse Source

Added main specialized in testing the Java Bytecode implementation.

Stan Hebben 6 years ago
parent
commit
a680f453fa

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

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

+ 1
- 1
Constructor/src/main/java/org/openzen/zenscript/constructor/Module.java View File

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

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

71
 
71
 
72
 			// TODO: annotation type registration
72
 			// TODO: annotation type registration
73
 			ModuleSpace space = new ModuleSpace(unit, new ArrayList<>());
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
 			Module module = new Module(moduleName, directory, jsonFile, exceptionLogger);
82
 			Module module = new Module(moduleName, directory, jsonFile, exceptionLogger);
78
 			ZSPackage pkg = isStdlib ? unit.globalTypeRegistry.stdlib : new ZSPackage(null, module.packageName);
83
 			ZSPackage pkg = isStdlib ? unit.globalTypeRegistry.stdlib : new ZSPackage(null, module.packageName);
79
 			CompilingPackage compilingPackage = new CompilingPackage(pkg);
84
 			CompilingPackage compilingPackage = new CompilingPackage(pkg);
80
 			
85
 			
81
 			ParsedFile[] parsedFiles = module.parse(compilingPackage);
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
 			JSONObject globals = json.optJSONObject("globals");
89
 			JSONObject globals = json.optJSONObject("globals");
85
 			if (globals != null) {
90
 			if (globals != null) {

+ 31
- 0
IDE/src/main/java/org/openzen/zenscript/ide/JavaBytecodeMain.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.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 View File

26
 		
26
 		
27
 		Project project = new Project(directory);
27
 		Project project = new Project(directory);
28
 		DevelopmentHost host = new LocalProjectDevelopmentHost(project);
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
 		IDEPropertyStore properties = host.getPropertyStore();
33
 		IDEPropertyStore properties = host.getPropertyStore();
34
 		
34
 		
35
-		IDEWindow window = new IDEWindow(host);
35
+		IDEWindow window = new IDEWindow(host, target);
36
 		WindowView root = new WindowView(window, host);
36
 		WindowView root = new WindowView(window, host);
37
 		
37
 		
38
 		IDEPropertyDirectory uiState = properties.getRoot().getSubdirectory("uiState");
38
 		IDEPropertyDirectory uiState = properties.getRoot().getSubdirectory("uiState");

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

134
 	}
134
 	}
135
 	
135
 	
136
 	private boolean compileDependencies(ModuleLoader loader, ZenCodeCompiler compiler, Set<String> compiledModules, Stack<String> compilingModules, SemanticModule module, Consumer<ValidationLogEntry> logger) {
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
 				continue;
139
 				continue;
140
-			compiledModules.add(dependency);
140
+			compiledModules.add(dependency.name);
141
 			System.out.println("== Compiling module " + dependency + " ==");
141
 			System.out.println("== Compiling module " + dependency + " ==");
142
 			
142
 			
143
-			if (compilingModules.contains(dependency)) {
143
+			if (compilingModules.contains(dependency.name)) {
144
 				StringBuilder message = new StringBuilder("Circular dependency:\n");
144
 				StringBuilder message = new StringBuilder("Circular dependency:\n");
145
 				for (String s : compilingModules)
145
 				for (String s : compilingModules)
146
 					message.append("    ").append(s).append("\n");
146
 					message.append("    ").append(s).append("\n");
147
 				
147
 				
148
 				throw new IllegalStateException(message.toString());
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
 				compilingModules.pop();
153
 				compilingModules.pop();
155
 				return false;
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
 				compilingModules.pop();
160
 				compilingModules.pop();
162
 				return false;
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
 				compilingModules.pop();
165
 				compilingModules.pop();
167
 				return false;
166
 				return false;
168
 			}
167
 			}
169
 			
168
 			
170
-			dependencyModule.compile(compiler);
169
+			dependency.compile(compiler);
171
 			compilingModules.pop();
170
 			compilingModules.pop();
172
 		}
171
 		}
173
 		
172
 		

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

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

+ 5
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeJarTarget.java View File

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

+ 2
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunTarget.java View File

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

+ 12
- 5
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.javabytecode;
6
 package org.openzen.zenscript.javabytecode;
7
 
7
 
8
+import java.io.File;
8
 import java.util.HashMap;
9
 import java.util.HashMap;
9
 import java.util.Map;
10
 import java.util.Map;
10
 
11
 
34
 	private int generatedScriptBlockCounter = 0;
35
 	private int generatedScriptBlockCounter = 0;
35
 	private boolean finished = false;
36
 	private boolean finished = false;
36
 	private JavaModule compiled = null;
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
 		target = new JavaModule();
45
 		target = new JavaModule();
46
+		this.jarFile = jarFile;
44
 		
47
 		
45
 		scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
48
 		scriptsClassWriter = new JavaClassWriter(ClassWriter.COMPUTE_FRAMES);
46
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
49
 		scriptsClassWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
98
 	
101
 	
99
 	@Override
102
 	@Override
100
 	public void finish() {
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
 	@Override
111
 	@Override
106
 		if (compiled == null)
113
 		if (compiled == null)
107
 			throw new IllegalStateException("Not yet built!");
114
 			throw new IllegalStateException("Not yet built!");
108
 		
115
 		
109
-		
116
+		// TODO: execute this
110
 	}
117
 	}
111
 	
118
 	
112
 	public JavaModule finishAndGetModule() {
119
 	public JavaModule finishAndGetModule() {

+ 5
- 5
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

43
 		
43
 		
44
 		ZSPackage pkg = new ZSPackage(null, "");
44
 		ZSPackage pkg = new ZSPackage(null, "");
45
 		CompilingPackage compilingPkg = new CompilingPackage(pkg);
45
 		CompilingPackage compilingPkg = new CompilingPackage(pkg);
46
-		ParsedFile[] parsedFiles = parse(pkg, compilingPkg, inputFiles);
46
+		ParsedFile[] parsedFiles = parse(compilingPkg, inputFiles);
47
 		
47
 		
48
 		ZSPackage global = new ZSPackage(null, "");
48
 		ZSPackage global = new ZSPackage(null, "");
49
 		GlobalRegistry registry = new GlobalRegistry(global);
49
 		GlobalRegistry registry = new GlobalRegistry(global);
50
-		SemanticModule module = compileSyntaxToSemantic(pkg, compilingPkg, parsedFiles, registry);
50
+		SemanticModule module = compileSyntaxToSemantic(compilingPkg, parsedFiles, registry);
51
 		
51
 		
52
 		//formatFiles(pkg, module);
52
 		//formatFiles(pkg, module);
53
 		
53
 		
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
 		ParsedFile[] parsedFiles = new ParsedFile[files.length];
63
 		ParsedFile[] parsedFiles = new ParsedFile[files.length];
64
 		for (int i = 0; i < files.length; i++) {
64
 		for (int i = 0; i < files.length; i++) {
65
 			parsedFiles[i] = ParsedFile.parse(compilingPkg, new TestBracketParser(), files[i]);
65
 			parsedFiles[i] = ParsedFile.parse(compilingPkg, new TestBracketParser(), files[i]);
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
 		ModuleSpace space = new ModuleSpace(new CompilationUnit(), new ArrayList<>());
106
 		ModuleSpace space = new ModuleSpace(new CompilationUnit(), new ArrayList<>());
107
 		for (Map.Entry<String, ISymbol> global : registry.collectGlobals().entrySet()) {
107
 		for (Map.Entry<String, ISymbol> global : registry.collectGlobals().entrySet()) {
108
 			space.addGlobal(global.getKey(), global.getValue());
108
 			space.addGlobal(global.getKey(), global.getValue());
109
 		}
109
 		}
110
 		SemanticModule result = Module.compileSyntaxToSemantic(
110
 		SemanticModule result = Module.compileSyntaxToSemantic(
111
 				"scripts",
111
 				"scripts",
112
-				new String[0],
112
+				new SemanticModule[0],
113
 				compiling,
113
 				compiling,
114
 				files,
114
 				files,
115
 				space,
115
 				space,

Loading…
Cancel
Save