Skip to content

Commit

Permalink
Merge pull request #336 from sychoo/command-line-arguments
Browse files Browse the repository at this point in the history
Resolved issue #326, supported command line arguments, added example program
  • Loading branch information
JonathanAldrich authored Sep 16, 2019
2 parents dbd5bf0 + 3e87841 commit 0fa29bf
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
17 changes: 17 additions & 0 deletions examples/commandLineArguments.wyv
Original file line number Diff line number Diff line change
@@ -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")

20 changes: 20 additions & 0 deletions stdlib/platform/java/cmdArgs.wyv
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions tools/src/wyvern/stdlib/support/CommandLineUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
22 changes: 17 additions & 5 deletions tools/src/wyvern/tools/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <filename>");
// 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 <filename> <arguments>");
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);
Expand Down
9 changes: 8 additions & 1 deletion tools/src/wyvern/tools/tests/ExampleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public void testPython3Webserver() {
PythonCompiler.main(args);
}


@Test
public void testBinarySearchTree() throws ParseException {
TestUtil.doTestScriptModularly(PATH, "dataStructures.bsttest",
Expand Down Expand Up @@ -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);
}
}

@Test
public void testCommandLineArguments() throws ParseException {
TestUtil.doTestScriptModularly(PATH, "commandLineArguments", Util.unitType(),
Util.unitValue());
}
}

0 comments on commit 0fa29bf

Please sign in to comment.