Skip to content

Commit

Permalink
Resolved issue wyvernlang#326, supported command line arguments, adde…
Browse files Browse the repository at this point in the history
…d example Wyvern program that demonstrate the usage of command line argument
  • Loading branch information
sychoo committed Jul 10, 2019
1 parent 8b92a6d commit 8bce47e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
19 changes: 19 additions & 0 deletions examples/commandLineArguments.wyv
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tools/src/wyvern/stdlib/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions tools/src/wyvern/stdlib/support/CommandLineUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
21 changes: 16 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,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 <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(args);
}
String filename = args[0];
Path filepath = Paths.get(filename);
Expand Down

0 comments on commit 8bce47e

Please sign in to comment.