Skip to content

Commit 8123fbd

Browse files
Merge pull request #9 from ian-h-chamberlain/develop
Add label support, CLI, and more
2 parents 7c370d2 + a973065 commit 8123fbd

19 files changed

+1095
-463
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: rust
2+
3+
rust:
4+
- stable
5+
- beta
6+
- nightly
7+
8+
cache: cargo
9+
10+
matrix:
11+
allow_failures:
12+
- rust: nightly
13+
- rust: beta
14+
15+
fast_finish: true

Cargo.lock

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ itertools = "0.8.0"
99
lazy_static = "1.2.0"
1010
pest_derive = "2.1.0"
1111
pest = "2.1.0"
12+
structopt = "0.2.15"

src/cli.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::{error::Error, fs, path::PathBuf};
2+
3+
use structopt::StructOpt;
4+
5+
use crate::parser;
6+
7+
#[derive(Debug, StructOpt)]
8+
/// Parse and save Redcode files
9+
struct CliOptions {
10+
/// Input file
11+
#[structopt(parse(from_os_str))]
12+
input_file: PathBuf,
13+
14+
/// Output file; defaults to stdout
15+
#[structopt(long, short, parse(from_os_str))]
16+
output_file: Option<PathBuf>,
17+
}
18+
19+
pub fn run() -> Result<(), Box<dyn Error>> {
20+
let cli_options = CliOptions::from_args();
21+
22+
let input_program = fs::read_to_string(cli_options.input_file)?;
23+
24+
let parsed_input = parser::parse(input_program.as_str())?;
25+
let parse_output = parsed_input.dump();
26+
27+
if let Some(output_path) = cli_options.output_file {
28+
fs::write(output_path, parse_output)?;
29+
} else {
30+
println!("{}", parse_output);
31+
};
32+
33+
Ok(())
34+
}

src/data/redcode.pest

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ WHITESPACE = _{ " " | "\t" }
22

33
COMMENT = @{ ";" ~ (!NEWLINE ~ ANY)* ~ &NEWLINE }
44

5-
AssemblyFile = { SOI ~ Line+ ~ EOI }
5+
File = { SOI ~ (Line ~ NEWLINE)+ ~ Line? ~ EOI }
66

7-
Line = { Instruction? ~ NEWLINE }
7+
Line = { LabelDeclaration* ~ Instruction? }
88

99
Instruction = _{ Operation ~ Field ~ ("," ~ Field)? }
1010

11+
LabelDeclaration = _{ !Operation ~ Label ~ (":")? }
12+
13+
Label = @{ Alpha ~ Alphanumeral* }
14+
1115
Operation = ${ Opcode ~ ("." ~ Modifier)? }
1216

1317
Field = !{ AddressMode? ~ Expr }
@@ -22,6 +26,10 @@ Modifier = { ^"AB" | ^"BA" | ^"A" | ^"B" | ^"F" | ^"X" | ^"I" }
2226

2327
AddressMode = { "#" | "$" | "*" | "@" | "{" | "<" | "}" | ">" }
2428

25-
Expr = { Number }
29+
Expr = { Number | Label }
30+
31+
Number = @{ ("-" | "+")? ~ ASCII_DIGIT+ }
32+
33+
Alpha = _{ ASCII_ALPHA | "_" }
2634

27-
Number = @{ ASCII_DIGIT+ }
35+
Alphanumeral = _{ ASCII_ALPHANUMERIC | "_" }

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ extern crate pest_derive;
55
// Extern crates
66
extern crate itertools;
77
extern crate pest;
8+
extern crate structopt;
89

10+
// Public modules
11+
pub mod cli;
12+
13+
// Exported functions
914
pub use parser::parse;
1015

1116
// Modules requiring macro_use
1217
#[macro_use]
1318
mod util;
1419

20+
// Private modules
1521
mod load_file;
1622
mod parser;

0 commit comments

Comments
 (0)