From 8bce47e9d1824fce3d93f05a13bafd4fda71e7ea Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Tue, 9 Jul 2019 22:46:38 -0400 Subject: [PATCH 1/8] 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); From fbb1f817914bd3f1570c7e02a901a8dd3e2ef905 Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Fri, 12 Jul 2019 17:30:50 -0400 Subject: [PATCH 2/8] Simplified the process of using command line argument. Created a new module cmdArgs under java platform. Updated example file examples/CommandLineArguments.wyv --- examples/commandLineArguments.wyv | 26 +++++++-------- stdlib/platform/java/cmdArgs.wyv | 32 +++++++++++++++++++ .../stdlib/support/CommandLineUtils.java | 6 ++-- 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 stdlib/platform/java/cmdArgs.wyv diff --git a/examples/commandLineArguments.wyv b/examples/commandLineArguments.wyv index c37c95fed..aeba20285 100644 --- a/examples/commandLineArguments.wyv +++ b/examples/commandLineArguments.wyv @@ -1,19 +1,17 @@ -// program to print all the command line arguments given by the user -// delimited by comma +// 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 -require java -import java:wyvern.stdlib.support.CommandLineUtils.utils -val cmdLineArgs = utils.getArgs() // store the arguments in a dynamic array. +stdout.print("The 0th indexed command line argument is: ") +stdout.print("\"" + cmdArgs.get(0) + "\"\n") -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() +stdout.print("The last command line argument is: ") +stdout.print("\"" + cmdArgs.get(cmdArgs.length() - 1) + "\"\n") + +stdout.print("All command line arguments are: ") +stdout.print("\"") +cmdArgs.print() +stdout.print("\"\n") -printCmdLineArgs(0) diff --git a/stdlib/platform/java/cmdArgs.wyv b/stdlib/platform/java/cmdArgs.wyv new file mode 100644 index 000000000..a4e4b970d --- /dev/null +++ b/stdlib/platform/java/cmdArgs.wyv @@ -0,0 +1,32 @@ +module def cmdArgs(java: Java) +import java:wyvern.stdlib.support.CommandLineUtils.utils +import java:wyvern.stdlib.support.Stdio.stdio + +//val cmdLineArgs = utils.getArgs() // store the arguments in a dynamic array. + +/* +def printCmdLineArgs(index: Int): Unit + if (index != utils.getLength() - 1) + stdio.print(utils.get(index)) + stdio.print(", ") + printCmdLineArgs(index + 1) + else + stdio.print(utils.get(utils.getLength() - 1)) +*/ + +def printCmdLineArgs(index: Int): Unit + if (index != utils.getLength() - 1) + stdio.print(utils.get(index)) + stdio.print(", ") + printCmdLineArgs(index + 1) + else + stdio.print(utils.get(utils.getLength() - 1)) + +def print(): Unit + printCmdLineArgs(0) + +def length(): Int + utils.getLength() + +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 index 6e714bc18..0785b88d1 100644 --- a/tools/src/wyvern/stdlib/support/CommandLineUtils.java +++ b/tools/src/wyvern/stdlib/support/CommandLineUtils.java @@ -13,8 +13,8 @@ public CommandLineUtils(String[] arguments) { this.convertToDynArrayList(); } - public DynArrayList getArgs() { - return argumentsArrayList; + public String get(int index) { + return (String) argumentsArrayList.get(index); } public int getLength() { @@ -27,4 +27,4 @@ private void convertToDynArrayList() { argumentsArrayList.add(str); } } -} \ No newline at end of file +} From 9b2cf67ba55a4ab8553708983f3b456c6c828def Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Fri, 12 Jul 2019 17:33:28 -0400 Subject: [PATCH 3/8] Removed unused commented code. --- stdlib/platform/java/cmdArgs.wyv | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/stdlib/platform/java/cmdArgs.wyv b/stdlib/platform/java/cmdArgs.wyv index a4e4b970d..38c56a2c9 100644 --- a/stdlib/platform/java/cmdArgs.wyv +++ b/stdlib/platform/java/cmdArgs.wyv @@ -2,18 +2,6 @@ module def cmdArgs(java: Java) import java:wyvern.stdlib.support.CommandLineUtils.utils import java:wyvern.stdlib.support.Stdio.stdio -//val cmdLineArgs = utils.getArgs() // store the arguments in a dynamic array. - -/* -def printCmdLineArgs(index: Int): Unit - if (index != utils.getLength() - 1) - stdio.print(utils.get(index)) - stdio.print(", ") - printCmdLineArgs(index + 1) - else - stdio.print(utils.get(utils.getLength() - 1)) -*/ - def printCmdLineArgs(index: Int): Unit if (index != utils.getLength() - 1) stdio.print(utils.get(index)) From 47c90041a7c2f60a177c33198b69d25a9dd3e09c Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Sun, 15 Sep 2019 09:10:15 -0400 Subject: [PATCH 4/8] Took CommandLineUtils module out of javaWhiteList in Globals.java, Omitted the CommandLineUtils constructors that accepts arguments, renamed convertToDynArrayList() function to setArgumentList(). --- tools/src/wyvern/tools/Interpreter.java | 3 ++- tools/src/wyvern/tools/tests/ExampleTests.java | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/src/wyvern/tools/Interpreter.java b/tools/src/wyvern/tools/Interpreter.java index 5cafe1c9a..4b7749bf4 100644 --- a/tools/src/wyvern/tools/Interpreter.java +++ b/tools/src/wyvern/tools/Interpreter.java @@ -38,7 +38,8 @@ public static void main(String[] args) { System.exit(1); } else { // create CommandLineUtils object to store the command line arguments. - CommandLineUtils commandLineUtilsObject = new CommandLineUtils(args); + 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 c1e842e20..5fb9decb5 100644 --- a/tools/src/wyvern/tools/tests/ExampleTests.java +++ b/tools/src/wyvern/tools/tests/ExampleTests.java @@ -158,4 +158,9 @@ public void testThreads() throws ParseException { Util.unitValue()); } + @Test + public void testCommandLineArguments() throws ParseException { + TestUtil.doTestScriptModularly(PATH, "commandLineArguments", Util.unitType(), + Util.unitValue()); + } } From 7c681906dc2b3db8f24705ba405625248627268c Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Sun, 15 Sep 2019 09:11:35 -0400 Subject: [PATCH 5/8] Revert "Took CommandLineUtils module out of javaWhiteList in Globals.java, Omitted the CommandLineUtils constructors that accepts arguments, renamed convertToDynArrayList() function to setArgumentList()." This reverts commit 47c90041a7c2f60a177c33198b69d25a9dd3e09c. --- tools/src/wyvern/tools/Interpreter.java | 3 +-- tools/src/wyvern/tools/tests/ExampleTests.java | 5 ----- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/tools/src/wyvern/tools/Interpreter.java b/tools/src/wyvern/tools/Interpreter.java index 4b7749bf4..5cafe1c9a 100644 --- a/tools/src/wyvern/tools/Interpreter.java +++ b/tools/src/wyvern/tools/Interpreter.java @@ -38,8 +38,7 @@ public static void main(String[] args) { System.exit(1); } else { // create CommandLineUtils object to store the command line arguments. - CommandLineUtils commandLineUtilsObject = new CommandLineUtils(); - commandLineUtilsObject.setArgumentList(args); + CommandLineUtils commandLineUtilsObject = new CommandLineUtils(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 5fb9decb5..c1e842e20 100644 --- a/tools/src/wyvern/tools/tests/ExampleTests.java +++ b/tools/src/wyvern/tools/tests/ExampleTests.java @@ -158,9 +158,4 @@ public void testThreads() throws ParseException { Util.unitValue()); } - @Test - public void testCommandLineArguments() throws ParseException { - TestUtil.doTestScriptModularly(PATH, "commandLineArguments", Util.unitType(), - Util.unitValue()); - } } From 98f4cb305e619eb9fe0088f4c9836f7115b6e80b Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Sun, 15 Sep 2019 09:12:57 -0400 Subject: [PATCH 6/8] Took CommandLineUtils module out of javaWhiteList in Globals.java, Omitted the CommandLineUtils constructors that accepts arguments, renamed convertToDynArrayList() function to setArgumentList(). --- examples/commandLineArguments.wyv | 2 +- stdlib/platform/java/cmdArgs.wyv | 8 +++---- tools/src/wyvern/stdlib/Globals.java | 1 - .../stdlib/support/CommandLineUtils.java | 22 +++++++------------ 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/examples/commandLineArguments.wyv b/examples/commandLineArguments.wyv index aeba20285..27d67915c 100644 --- a/examples/commandLineArguments.wyv +++ b/examples/commandLineArguments.wyv @@ -8,7 +8,7 @@ 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.length() - 1) + "\"\n") +stdout.print("\"" + cmdArgs.get(cmdArgs.size() - 1) + "\"\n") stdout.print("All command line arguments are: ") stdout.print("\"") diff --git a/stdlib/platform/java/cmdArgs.wyv b/stdlib/platform/java/cmdArgs.wyv index 38c56a2c9..2ac8d320f 100644 --- a/stdlib/platform/java/cmdArgs.wyv +++ b/stdlib/platform/java/cmdArgs.wyv @@ -3,18 +3,18 @@ import java:wyvern.stdlib.support.CommandLineUtils.utils import java:wyvern.stdlib.support.Stdio.stdio def printCmdLineArgs(index: Int): Unit - if (index != utils.getLength() - 1) + if (index != size() - 1) stdio.print(utils.get(index)) stdio.print(", ") printCmdLineArgs(index + 1) else - stdio.print(utils.get(utils.getLength() - 1)) + stdio.print(utils.get(size() - 1)) def print(): Unit printCmdLineArgs(0) -def length(): Int - utils.getLength() +def size(): Int + utils.size() def get(index: Int): String utils.get(index) diff --git a/tools/src/wyvern/stdlib/Globals.java b/tools/src/wyvern/stdlib/Globals.java index b3e67ce2e..54410c5bb 100644 --- a/tools/src/wyvern/stdlib/Globals.java +++ b/tools/src/wyvern/stdlib/Globals.java @@ -65,7 +65,6 @@ 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 index 0785b88d1..012286839 100644 --- a/tools/src/wyvern/stdlib/support/CommandLineUtils.java +++ b/tools/src/wyvern/stdlib/support/CommandLineUtils.java @@ -1,30 +1,24 @@ 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. + private static DynArrayList argumentList; // 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 CommandLineUtils() { } public String get(int index) { - return (String) argumentsArrayList.get(index); + return (String) argumentList.get(index); } - public int getLength() { - return arguments.length; + public int size() { + return argumentList.size(); } - private void convertToDynArrayList() { - argumentsArrayList = new DynArrayList(); + public void setArgumentList(String[] arguments) { + this.argumentList = new DynArrayList(); for (String str : arguments) { - argumentsArrayList.add(str); + argumentList.add(str); } } } From f0a93b7953487272364d56984ca9fdbae03f491e Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Sun, 15 Sep 2019 09:18:47 -0400 Subject: [PATCH 7/8] Commit uncommitted files --- tools/src/wyvern/tools/Interpreter.java | 2 ++ tools/src/wyvern/tools/tests/ExampleTests.java | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/tools/src/wyvern/tools/Interpreter.java b/tools/src/wyvern/tools/Interpreter.java index 5cafe1c9a..29aea61ee 100644 --- a/tools/src/wyvern/tools/Interpreter.java +++ b/tools/src/wyvern/tools/Interpreter.java @@ -39,6 +39,8 @@ public static void main(String[] args) { } else { // create CommandLineUtils object to store the command line arguments. CommandLineUtils commandLineUtilsObject = new CommandLineUtils(args); + 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 c1e842e20..5fb9decb5 100644 --- a/tools/src/wyvern/tools/tests/ExampleTests.java +++ b/tools/src/wyvern/tools/tests/ExampleTests.java @@ -158,4 +158,9 @@ public void testThreads() throws ParseException { Util.unitValue()); } + @Test + public void testCommandLineArguments() throws ParseException { + TestUtil.doTestScriptModularly(PATH, "commandLineArguments", Util.unitType(), + Util.unitValue()); + } } From 67eb542fc8613f70f3466e18ce48c2fdc64dfb7a Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Sun, 15 Sep 2019 09:24:49 -0400 Subject: [PATCH 8/8] Modified Interpreter so that it doesn't invoke the deprecated constructor --- tools/src/wyvern/tools/Interpreter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/src/wyvern/tools/Interpreter.java b/tools/src/wyvern/tools/Interpreter.java index 29aea61ee..4b7749bf4 100644 --- a/tools/src/wyvern/tools/Interpreter.java +++ b/tools/src/wyvern/tools/Interpreter.java @@ -38,7 +38,6 @@ public static void main(String[] args) { System.exit(1); } else { // create CommandLineUtils object to store the command line arguments. - CommandLineUtils commandLineUtilsObject = new CommandLineUtils(args); CommandLineUtils commandLineUtilsObject = new CommandLineUtils(); commandLineUtilsObject.setArgumentList(args); }