Skip to content

Commit c353461

Browse files
committed
Add single file debug mode.
1 parent dc4a907 commit c353461

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.byteskript</groupId>
88
<artifactId>byteskript</artifactId>
9-
<version>1.0.22</version>
9+
<version>1.0.23</version>
1010
<name>ByteSkript</name>
1111
<description>A compiled JVM implementation of the Skript language.</description>
1212

src/main/java/org/byteskript/skript/app/ByteSkriptApp.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String... args) throws Throwable {
2626
makeFiles();
2727
System.out.println(RESET + "Welcome to " + BRIGHT_PURPLE + "ByteSkript" + RESET + "!");
2828
System.out.println(RESET + "Available arguments:");
29-
System.out.println(RESET + "\trun <file> | " + CYAN + "Run scripts in the " + CYAN_UNDERLINED + "skripts/" + CYAN + " directory.");
29+
System.out.println(RESET + "\trun <file> | " + CYAN + "Run scripts in the " + CYAN_UNDERLINED + "skript/" + CYAN + " directory.");
3030
System.out.println(RESET + "\t | " + CYAN + "If a file-arg is given, only this script will be run.");
3131
System.out.println(RESET + "\tcompile | " + CYAN + "Compile library class files for all scripts.");
3232
System.out.println(RESET + "\t | " + CYAN + "Syntax-providing classes can be moved to the " + CYAN_UNDERLINED + "libraries/" + CYAN + " folder.");
@@ -36,7 +36,8 @@ public static void main(String... args) throws Throwable {
3636
System.out.println(RESET + "\ttest <file> | " + CYAN + "Runs available scripts in test mode.");
3737
System.out.println(RESET + "\t | " + CYAN + "Test-only features will be available here.");
3838
System.out.println(RESET + "\t | " + CYAN + "If a file-arg is given, only this script will be tested.");
39-
System.out.println(RESET + "\tdebug | " + CYAN + "Generates a debug information file (for bug reports!)");
39+
System.out.println(RESET + "\tdebug | " + CYAN + "Generates a debug information file for all scripts (for bug reports!)");
40+
System.out.println(RESET + "\tdebug <file>| " + CYAN + "Generates a debug information file for a given script (for bug reports!)");
4041
System.out.print(RESET);
4142
System.out.println("Visit https://docs.byteskript.org for help and tutorials.");
4243
} else if (args[0].equalsIgnoreCase("clean")) {
@@ -85,8 +86,15 @@ public static void main(String... args) throws Throwable {
8586
}
8687
new SimpleThrottleController(SKRIPT).run();
8788
} else if (args[0].equalsIgnoreCase("debug")) {
88-
System.out.println(RESET + "Generating a debug report of your current scripts." + RESET);
89-
ScriptDebugger.main();
89+
if (args.length < 2) {
90+
System.out.println(RESET + "Generating a debug report of your current scripts." + RESET);
91+
ScriptDebugger.main();
92+
} else {
93+
final String name = args[1];
94+
System.out.println(RESET + "Generating a debug report of " + CYAN + CYAN_UNDERLINED + name + RESET + "." + RESET);
95+
final File file = new File(name);
96+
ScriptDebugger.debug(file);
97+
}
9098
System.out.println(RESET + "This has been stored in " + CYAN + CYAN_UNDERLINED + "debug.txt" + RESET + ".");
9199
}
92100
}

src/main/java/org/byteskript/skript/app/ScriptDebugger.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.byteskript.skript.runtime.Skript;
1212

1313
import java.io.File;
14+
import java.io.FileInputStream;
1415
import java.io.FileOutputStream;
1516
import java.io.IOException;
1617

@@ -24,4 +25,15 @@ public static void main(String... args) throws IOException {
2425
skript.compileScripts(SOURCE);
2526
}
2627
}
28+
29+
public static void debug(File file) throws IOException {
30+
final File debug = new File(ROOT, "debug.txt");
31+
try (final FileOutputStream stream = new FileOutputStream(debug)) {
32+
try (final FileInputStream input = new FileInputStream(file)) {
33+
final Skript skript = new Skript(new DebugSkriptCompiler(Stream.controller(stream)));
34+
registerLibraries(skript);
35+
skript.compileScript(input, "skript/" + file.getName());
36+
}
37+
}
38+
}
2739
}

src/main/java/org/byteskript/skript/app/SkriptApp.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.FileInputStream;
1515
import java.io.IOException;
1616
import java.io.InputStream;
17-
import java.lang.reflect.InvocationTargetException;
1817
import java.net.URISyntaxException;
1918
import java.nio.file.DirectoryStream;
2019
import java.nio.file.Files;
@@ -52,20 +51,19 @@ protected static void registerLibraries(final Skript skript) {
5251
final List<File> files = getFiles(new ArrayList<>(), LIBRARIES.toPath());
5352
for (final File file : files) {
5453
if (file.getName().endsWith(".jar")) {
55-
try {
56-
final JarFile jar = new JarFile(file);
54+
try (final JarFile jar = new JarFile(file)) {
5755
final String main = jar.getManifest().getMainAttributes().getValue("Main-Class");
5856
if (main == null)
59-
throw new ScriptLibraryError("Library '" + file.getName() + "' is missing main class in manifest.");
57+
throw new ScriptLibraryError("Library '" + file.getName() + "' is missing a main class in its Jar manifest.");
6058
callLibrary(file, main, skript);
6159
} catch (Throwable ex) {
6260
ex.printStackTrace();
6361
}
6462
} else if (file.getName().endsWith(".class")) {
6563
try (final InputStream stream = new FileInputStream(file)) {
6664
skript.registerLibraryClass(stream.readAllBytes());
67-
} catch (IOException exception) {
68-
throw new ScriptLibraryError("Error while loading library '" + file.getName() + "'", exception);
65+
} catch (Throwable exception) {
66+
throw new ScriptLibraryError("Error while loading class library '" + file.getName() + "'", exception);
6967
}
7068
}
7169
}
@@ -76,9 +74,14 @@ protected static void callLibrary(final File file, final String main, final Skri
7674
final Class<?> target = Class.forName(main, true, child);
7775
try {
7876
target.getMethod("load", Skript.class).invoke(null, skript);
79-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
80-
throw new ScriptLibraryError("Library '" + file.getName() + "' main class is missing load method:\n" +
77+
} catch (NoSuchMethodException ex) {
78+
throw new ScriptLibraryError("Library '" + file.getName() + "' main class is missing a load method:\n" +
8179
"public static void load(Skript skript)\n");
80+
} catch (IllegalAccessException ex) {
81+
throw new ScriptLibraryError("Library '" + file.getName() + "' main class has no public load method:\n" +
82+
"public static void load(Skript skript)\n");
83+
} catch (Throwable ex) {
84+
throw new ScriptLibraryError("Error encountered while loading '" + file.getName() + "':", ex);
8285
}
8386
}
8487

src/main/java/org/byteskript/skript/compiler/FileContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class FileContext extends Context {
4545
LanguageElement expected;
4646
SyntaxElement currentEffect;
4747
private HandlerType mode = StandardHandlers.GET;
48+
4849
public FileContext(Type type) {
4950
this.type = type;
5051
this.state = CompileState.ROOT;

0 commit comments

Comments
 (0)