Skip to content

Commit

Permalink
Implement CLI
Browse files Browse the repository at this point in the history
Use CLI (new 'rsc') instead of Runner (old 'RustScript')
  • Loading branch information
WilliamRagstad committed Aug 5, 2021
1 parent 2971ac5 commit d00fc04
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
65 changes: 65 additions & 0 deletions Cli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.util.List;
import java.util.Arrays;

/**
* @author William Rågstad <william.ragstad@gmail.com>
* @version 0.1.0
*
* The CLI is a simple command line interface for the RustScript
* language. It provides a simple way to run RustScript script files
* and interact in a playful way using the REPL mode. Sometime in the
* future, this might be extended to include a compiler to other
* languages like JavaScript, C++ or Python.
*/
public class Cli {
private static final String VERSION = "0.1.0";
private static final String DESCRIPTION = "A command line interface tool for the RustScript language.";
private static final String COPYRIGHT = "Copyright (c) 2021 William Rågstad";
private static final String HELP = String.format("""
RustScript CLI version %s.
%s
Usage: rsc (options) (files)
Options:
-h, --help
Prints this help message.
-v, --version
Prints the version of the program.
-r, --repl
Starts the REPL mode.
-c, --compile [files] (Not implemented)
Compiles the given files.
-l, --lint [files] (Not implemented)
Lints the given files.
Execute scripts: rsc [files]
Interprets the given script files one at a time.
%s""", VERSION, DESCRIPTION, COPYRIGHT);

public static void main(String[] args) {
List<String> argsList = Arrays.asList(args).stream()
.map((String option) -> option.startsWith("-") ? option.toLowerCase() : option).toList();
if (argsList.contains("--help") || argsList.contains("-h")) {
System.out.println(HELP);
} else if (argsList.contains("--version") || argsList.contains("-v")) {
System.out.println(VERSION);
} else if (argsList.contains("--repl") || argsList.contains("-r")) {
Repl.run();
} else {
if (argsList.size() > 0) {
if (argsList.stream().anyMatch((String option) -> option.startsWith("-"))) {
// Unknown option found
System.out.println("Unknown option");
} else {
// All args are files
Runner.run(args);
}
} else {
System.out.println(HELP);
}
}
// TODO: Add linting and compilation
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ There are global variables though.
Run a script using the following command.

```shell
java -jar RustScript.jar <script>
java -jar rsc.jar <script>
```

### 🔨 Build

If you want to build RustScript on your own, run the command below to build the `Runner.java` class into a standalone executable.

```shell
jar -cvmf manifest.txt RustScript.jar *.class core/*.class
jar -cvmf manifest.txt rsc.jar *.class core/*.class
```


Expand Down Expand Up @@ -230,10 +230,10 @@ fold(lcm, 1, [1..20])

> The sum of the squares of the first ten natural numbers is,
> 1^2 + 2^2 + ... + 10^2 = 385$$
>
>
> The square of the sum of the first ten natural numbers is,
> (1 + 2 + ... + 10)^2 = 55^2 = 3025$$
>
>
>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025 - 385 = 2640$.
>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Expand Down
2 changes: 1 addition & 1 deletion manifest.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Main-Class: Runner
Main-Class: Cli

0 comments on commit d00fc04

Please sign in to comment.