diff --git a/examples/commandLineArguments.wyv b/examples/commandLineArguments.wyv new file mode 100644 index 000000000..27d67915c --- /dev/null +++ b/examples/commandLineArguments.wyv @@ -0,0 +1,17 @@ +// program to print the 0th command line argument and all the +// command line arguments given by the user delimited by comma + +require cmdArgs +require stdout + +stdout.print("The 0th indexed command line argument is: ") +stdout.print("\"" + cmdArgs.get(0) + "\"\n") + +stdout.print("The last command line argument is: ") +stdout.print("\"" + cmdArgs.get(cmdArgs.size() - 1) + "\"\n") + +stdout.print("All command line arguments are: ") +stdout.print("\"") +cmdArgs.print() +stdout.print("\"\n") + diff --git a/stdlib/platform/java/cmdArgs.wyv b/stdlib/platform/java/cmdArgs.wyv new file mode 100644 index 000000000..2ac8d320f --- /dev/null +++ b/stdlib/platform/java/cmdArgs.wyv @@ -0,0 +1,20 @@ +module def cmdArgs(java: Java) +import java:wyvern.stdlib.support.CommandLineUtils.utils +import java:wyvern.stdlib.support.Stdio.stdio + +def printCmdLineArgs(index: Int): Unit + if (index != size() - 1) + stdio.print(utils.get(index)) + stdio.print(", ") + printCmdLineArgs(index + 1) + else + stdio.print(utils.get(size() - 1)) + +def print(): Unit + printCmdLineArgs(0) + +def size(): Int + utils.size() + +def get(index: Int): String + utils.get(index) diff --git a/tools/src/wyvern/stdlib/support/CommandLineUtils.java b/tools/src/wyvern/stdlib/support/CommandLineUtils.java new file mode 100644 index 000000000..012286839 --- /dev/null +++ b/tools/src/wyvern/stdlib/support/CommandLineUtils.java @@ -0,0 +1,24 @@ +package wyvern.stdlib.support; + +public class CommandLineUtils { + private static DynArrayList argumentList; // array list of command line arguments. + public static final CommandLineUtils utils = new CommandLineUtils(); + + public CommandLineUtils() { + } + + public String get(int index) { + return (String) argumentList.get(index); + } + + public int size() { + return argumentList.size(); + } + + public void setArgumentList(String[] arguments) { + this.argumentList = new DynArrayList(); + for (String str : arguments) { + argumentList.add(str); + } + } +} diff --git a/tools/src/wyvern/tools/Interpreter.java b/tools/src/wyvern/tools/Interpreter.java index 88ab30869..4b7749bf4 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,27 @@ 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(); + commandLineUtilsObject.setArgumentList(args); } String filename = args[0]; Path filepath = Paths.get(filename); diff --git a/tools/src/wyvern/tools/tests/ExampleTests.java b/tools/src/wyvern/tools/tests/ExampleTests.java index 6ce9f2f10..18155b1fd 100644 --- a/tools/src/wyvern/tools/tests/ExampleTests.java +++ b/tools/src/wyvern/tools/tests/ExampleTests.java @@ -134,6 +134,7 @@ public void testPython3Webserver() { PythonCompiler.main(args); } + @Test public void testBinarySearchTree() throws ParseException { TestUtil.doTestScriptModularly(PATH, "dataStructures.bsttest", @@ -162,4 +163,10 @@ public void testThreads() throws ParseException { public void testTextEditorApplication() throws ParseException { TestUtil.doTestScriptModularly(PATH + "text-editor", "main", Util.unitType(), Util.unitValue(), false); } -} \ No newline at end of file + + @Test + public void testCommandLineArguments() throws ParseException { + TestUtil.doTestScriptModularly(PATH, "commandLineArguments", Util.unitType(), + Util.unitValue()); + } +}