Add option to run internal scripts

This commit is contained in:
aNNiMON 2024-02-18 00:07:08 +02:00
parent b511d57940
commit c8996095b2
3 changed files with 26 additions and 0 deletions

View File

@ -94,6 +94,17 @@ public final class Main {
Sandbox.main(createOwnLangArgs(args, i + 1)); Sandbox.main(createOwnLangArgs(args, i + 1));
return; return;
case "run":
final String scriptName;
if (i + 1 < args.length) {
scriptName = args[i + 1];
createOwnLangArgs(args, i + 2);
} else {
scriptName = "listScripts";
}
run(RunOptions.script(scriptName));
return;
default: default:
if (options.programSource == null) { if (options.programSource == null) {
options.programSource = args[i]; options.programSource = args[i];
@ -124,6 +135,7 @@ public final class Main {
-a, --showast Show AST of program -a, --showast Show AST of program
-t, --showtokens Show lexical tokens -t, --showtokens Show lexical tokens
-m, --showtime Show elapsed time of parsing and execution -m, --showtime Show elapsed time of parsing and execution
ownlang run <scriptName>
"""); """);
} }

View File

@ -6,6 +6,7 @@ import static com.annimon.ownlang.util.input.InputSourceDetector.RESOURCE_PREFIX
public class RunOptions { public class RunOptions {
private static final String DEFAULT_PROGRAM = "program.own"; private static final String DEFAULT_PROGRAM = "program.own";
private static final String RES_SCRIPTS = "/scripts/";
// input // input
String programPath; String programPath;
@ -21,6 +22,13 @@ public class RunOptions {
private final InputSourceDetector inputSourceDetector = new InputSourceDetector(); private final InputSourceDetector inputSourceDetector = new InputSourceDetector();
static RunOptions script(String name) {
final var options = new RunOptions();
options.programPath = InputSourceDetector.RESOURCE_PREFIX
+ RES_SCRIPTS + name.toLowerCase() + ".own";
return options;
}
boolean linterEnabled() { boolean linterEnabled() {
return lintMode != null && lintMode != LinterStage.Mode.NONE; return lintMode != null && lintMode != LinterStage.Mode.NONE;
} }

View File

@ -0,0 +1,6 @@
println "Available scripts:
checkUpdate - checks updates on GitHub
To run a script use command:
ownlang run checkUpdate
"