From 8bce47e9d1824fce3d93f05a13bafd4fda71e7ea Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Tue, 9 Jul 2019 22:46:38 -0400 Subject: [PATCH] Resolved issue #326, supported command line arguments, added example Wyvern program that demonstrate the usage of command line argument --- examples/commandLineArguments.wyv | 19 ++++++++++++ tools/src/wyvern/stdlib/Globals.java | 1 + .../stdlib/support/CommandLineUtils.java | 30 +++++++++++++++++++ tools/src/wyvern/tools/Interpreter.java | 21 +++++++++---- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 examples/commandLineArguments.wyv create mode 100644 tools/src/wyvern/stdlib/support/CommandLineUtils.java diff --git a/examples/commandLineArguments.wyv b/examples/commandLineArguments.wyv new file mode 100644 index 000000000..c37c95fed --- /dev/null +++ b/examples/commandLineArguments.wyv @@ -0,0 +1,19 @@ +// program to print all the command line arguments given by the user +// delimited by comma + +require stdout +require java +import java:wyvern.stdlib.support.CommandLineUtils.utils + +val cmdLineArgs = utils.getArgs() // store the arguments in a dynamic array. + +def printCmdLineArgs(index: Int): Unit + if (index != utils.getLength() - 1) + stdout.print(cmdLineArgs.get(index)) + stdout.print(", ") + printCmdLineArgs(index + 1) + else + stdout.print(cmdLineArgs.get(utils.getLength() - 1)) + stdout.println() + +printCmdLineArgs(0) diff --git a/tools/src/wyvern/stdlib/Globals.java b/tools/src/wyvern/stdlib/Globals.java index 54410c5bb..b3e67ce2e 100644 --- a/tools/src/wyvern/stdlib/Globals.java +++ b/tools/src/wyvern/stdlib/Globals.java @@ -65,6 +65,7 @@ private Globals() { } javaWhiteList.add("wyvern.stdlib.support.Sys.utils"); javaWhiteList.add("wyvern.stdlib.support.HashMapWrapper.hashmapwrapper"); javaWhiteList.add("wyvern.stdlib.support.ArrayWrapper.arr"); + javaWhiteList.add("wyvern.stdlib.support.CommandLineUtils.utils"); } static { diff --git a/tools/src/wyvern/stdlib/support/CommandLineUtils.java b/tools/src/wyvern/stdlib/support/CommandLineUtils.java new file mode 100644 index 000000000..6e714bc18 --- /dev/null +++ b/tools/src/wyvern/stdlib/support/CommandLineUtils.java @@ -0,0 +1,30 @@ +package wyvern.stdlib.support; + +public class CommandLineUtils { + private static String[] arguments; // array of command line arguments. + private static DynArrayList argumentsArrayList; // array list of command line arguments. + public static final CommandLineUtils utils = new CommandLineUtils(); + + private CommandLineUtils() { + } + + public CommandLineUtils(String[] arguments) { + this.arguments = arguments; + this.convertToDynArrayList(); + } + + public DynArrayList getArgs() { + return argumentsArrayList; + } + + public int getLength() { + return arguments.length; + } + + private void convertToDynArrayList() { + argumentsArrayList = new DynArrayList(); + for (String str : arguments) { + argumentsArrayList.add(str); + } + } +} \ No newline at end of file diff --git a/tools/src/wyvern/tools/Interpreter.java b/tools/src/wyvern/tools/Interpreter.java index 88ab30869..5cafe1c9a 100644 --- a/tools/src/wyvern/tools/Interpreter.java +++ b/tools/src/wyvern/tools/Interpreter.java @@ -6,6 +6,7 @@ import java.nio.file.Paths; import wyvern.stdlib.Globals; +import wyvern.stdlib.support.CommandLineUtils; import wyvern.target.corewyvernIL.ASTNode; import wyvern.target.corewyvernIL.astvisitor.PlatformSpecializationVisitor; import wyvern.target.corewyvernIL.astvisitor.TailCallVisitor; @@ -18,16 +19,26 @@ public final class Interpreter { private Interpreter() { } /** - * The interpreter only supports 1 argument, which is the path to the Wyvern - * file. If more arguments are supplied, it will exit with an error. Then, + * The interpreter supports multiple arguments, the first argument (0th index) + * supplied will always be the path to the Wyvern file. The command line arguments + * will be stored in the dynamic array defined in stdlib/support/CommandLineArgumentsUtils.java + * and can be accessed by Wyvern program via the following import statement: + * + * require java + * import java:wyvern.stdlib.support.CommandLineUtils.utils + * * the file is read in to memory in it's entirety, before being executed in * an empty context. The resulting value is printed to the screen. */ public static void main(String[] args) { - // check if 1 argument is supplied. - if (args.length != 1) { - System.err.println("usage: wyvern "); + // check if at least one argument is supplied. + if (args.length < 1) { + // prompt usage message if the number of arguments is less than one. + System.err.println("usage: wyvern "); System.exit(1); + } else { + // create CommandLineUtils object to store the command line arguments. + CommandLineUtils commandLineUtilsObject = new CommandLineUtils(args); } String filename = args[0]; Path filepath = Paths.get(filename);