Skip to content

Commit 4929eac

Browse files
committed
Reorganized code for subcommand modularity
1 parent a68124a commit 4929eac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+236
-139
lines changed

Cargo.lock

+60-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
1+
[workspace]
2+
resolver = "2"
3+
members = [
4+
"carlotk",
5+
"subcommands/repl",
6+
"subcommands/latex",
7+
"subcommands/version",
8+
"subcommands/help",
9+
]
10+
111
[package]
212
name = "carlo"
313
description = "A simple interpreted programming language."
414
license = "MIT"
515
repository = "https://github.com/hobbsbros/carlo.git"
6-
version = "0.10.0"
16+
version = "0.11.0"
717
edition = "2021"
818

9-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10-
11-
[lib]
12-
name = "carlotk"
13-
path = "src/lib.rs"
14-
1519
[[bin]]
1620
name = "carlo"
1721
path = "src/main.rs"
1822

1923
[dependencies]
2024
colored = "2.1.0"
2125
rustyline = "13.0.0"
26+
27+
[dependencies.carlotk]
28+
path = "./carlotk"
29+
30+
[dependencies.help]
31+
path = "./subcommands/help"
32+
33+
[dependencies.latex]
34+
path = "./subcommands/latex"
35+
36+
[dependencies.run]
37+
path = "./subcommands/run"
38+
39+
[dependencies.repl]
40+
path = "./subcommands/repl"
41+
42+
[dependencies.version]
43+
path = "./subcommands/version"

DEVELOPER_GUIDE.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ version number.
1313

1414
The following instructions will guide you to adding a Carlo langauge subcommand.
1515

16-
1. Create `src/subcommands/your-new-subcommand.rs`. Write a new function corresponding
17-
to your subcommand
16+
1. Create a Cargo library package in the `subcommands` directory with the name `sc`, where `sc` is your desired subcommand name.
1817

19-
2. Open `src/main.rs`. Add `Subcommand::YourNewSubcommand` to the `main` function match
20-
statement.
18+
2. Create a function in your subcommand library named `sc::sc`. It is recommended that you import `carlotk::prelude::*` so you can use the Carlo tokenizer, parser, and environment utilities. Your function must have the following signature: `fn(CliArgs) -> ()`.
2119

22-
3. Open `src/lib.rs`. Add `./subcommands/help_your-new-subcommand.txt` to the `HELP`
23-
hashmap.
20+
3. Create the following entry in `Cargo.toml`.
2421

25-
4. Open `src/subcommands/mod.rs`. Add `YourNewSubcommand` to the `Subcommand` enumeration.
26-
Add `"your-new-subcommand" => YourNewSubcommand` in the `From<&str>` trait implementation
27-
block.
22+
```
23+
[dependencies.sc]
24+
path = "./subcommands/sc"
25+
```
2826

29-
5. Open `src/help.txt`. Add `your-new-subcommand` to the help menu.
30-
31-
6. Create a file called `src/subcommands/help_your-new-subcommand.txt`. Add a help menu.
27+
4. In `src/main.rs`, add `subcommand sc` (on a new line) to the `include_subcommands!` macro invocation.

carlotk/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "carlotk"
3+
description = "The main library for Carlo, a simple interpreted programming language."
4+
license = "MIT"
5+
repository = "https://github.com/hobbsbros/carlo.git"
6+
version = "0.11.0"
7+
edition = "2021"
8+
9+
[lib]
10+
name = "carlotk"
11+
path = "src/lib.rs"
12+
13+
[dependencies]
14+
colored = "2.1.0"
15+
rustyline = "13.0.0"
File renamed without changes.

src/cli.rs carlotk/src/cli.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use std::{
55
path::PathBuf,
66
};
77

8-
use crate::{
9-
Error,
10-
Subcommand,
11-
};
8+
use crate::Error;
129

1310
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
1411
/// Flags for the Carlo language executable.
@@ -49,7 +46,7 @@ impl From<char> for Flag {
4946
/// Command-line arguments for the Carlo language executable.
5047
pub struct CliArgs {
5148
/// Executable subcommand
52-
pub subcommand: Subcommand,
49+
pub subcommand: String,
5350

5451
/// Argument to subcommand
5552
pub argument: Option<String>,
@@ -71,15 +68,15 @@ impl CliArgs {
7168

7269
// Parse subcommand
7370
let subcommand = if args.len() > 1 {
74-
args[1].as_str().into()
71+
args[1].to_owned()
7572
} else {
76-
Subcommand::Repl
73+
"repl".to_string()
7774
};
7875

7976
// Parse argument or input file
80-
if subcommand == Subcommand::Help && args.len() > 2 && !args[2].starts_with("-") {
77+
if subcommand == "help" && args.len() > 2 && !args[2].starts_with("-") {
8178
argument = Some (args[2].to_owned());
82-
} else if subcommand != Subcommand::Help && args.len() > 2 && !args[2].starts_with("-") {
79+
} else if subcommand != "help" && args.len() > 2 && !args[2].starts_with("-") {
8380
inputfile = Some (args[2].as_str().into());
8481
}
8582

File renamed without changes.

src/error.rs carlotk/src/error.rs

File renamed without changes.
File renamed without changes.

src/lib.rs carlotk/src/lib.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ mod environment;
66
mod error;
77
mod expression;
88
mod parser;
9-
pub mod subcommands;
109
mod tokenizer;
1110
mod unit;
1211

@@ -44,23 +43,12 @@ pub use tokenizer::{
4443

4544
pub use parser::Parser;
4645

47-
pub use subcommands::Subcommand;
48-
4946
pub use unit::{
5047
PREFIXES,
5148
UNITS,
5249
};
5350

54-
pub const VERSION: &str = "0.10.0";
55-
56-
pub const HELP: [(&str, &str); 6] = [
57-
("", include_str!("./help.txt")),
58-
("repl", include_str!("./subcommands/help_repl.txt")),
59-
("run", include_str!("./subcommands/help_run.txt")),
60-
("latex", include_str!("./subcommands/help_latex.txt")),
61-
("help", include_str!("./subcommands/help_help.txt")),
62-
("version", include_str!("./subcommands/help_version.txt")),
63-
];
51+
pub const VERSION: &str = "0.11.0";
6452

6553
/// A prelude for writing subcommands.
6654
pub mod prelude {
@@ -75,9 +63,10 @@ pub mod prelude {
7563
pub use rustyline::DefaultEditor;
7664

7765
pub use crate::{
66+
CliArgs,
7867
Environment,
7968
Error,
80-
HELP,
69+
Flag,
8170
read,
8271
parse,
8372
Parser,
@@ -138,4 +127,18 @@ pub fn read(prompt: &str) -> String {
138127
};
139128

140129
buffer.trim().to_owned()
130+
}
131+
132+
#[macro_export]
133+
/// Includes subcommands in the Carlo binary.
134+
macro_rules! include_subcommands {
135+
(
136+
using $args: ident
137+
$(subcommand $subcommand: ident)*
138+
) => {
139+
match $args.subcommand.as_str() {
140+
$( stringify!($subcommand) => $subcommand::$subcommand($args), )*
141+
_ => repl::repl($args),
142+
};
143+
};
141144
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/unit.rs carlotk/src/unit.rs

File renamed without changes.

src/main.rs

+9-24
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,19 @@
22
33
use carlotk::{
44
CliArgs,
5-
Flag,
6-
Subcommand,
7-
subcommands::*,
5+
include_subcommands,
86
};
97

108
fn main() {
11-
use Flag::*;
12-
use Subcommand::*;
13-
149
let args = CliArgs::parse();
1510

16-
match args.subcommand {
17-
Help => help(
18-
&args.argument.clone().unwrap_or(String::new()),
19-
args.contains(Interactive),
20-
),
21-
Repl => repl(
22-
args.inputfile.clone(),
23-
args.contains(Debug),
24-
),
25-
Run => run(
26-
args.inputfile.clone(),
27-
args.contains(Debug),
28-
),
29-
Latex => latex(
30-
args.inputfile.clone(),
31-
args.contains(Debug),
32-
),
33-
Version => version(),
11+
include_subcommands!{
12+
using args
13+
14+
subcommand run
15+
subcommand repl
16+
subcommand help
17+
subcommand latex
18+
subcommand version
3419
};
3520
}

0 commit comments

Comments
 (0)