aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2021-10-25 00:15:04 -0700
committerBryan Newbold <bnewbold@robocracy.org>2021-10-25 00:15:04 -0700
commitb3141319b98199d75f1317ef3754c0b6b14b1f78 (patch)
tree70802b1170fa58a0b02ae9d2d02e985ab4b0b9af /src
parent04c53bb88bfbbc5c65b25104959c66d06e2b4fa1 (diff)
downloadcasual-b3141319b98199d75f1317ef3754c0b6b14b1f78.tar.gz
casual-b3141319b98199d75f1317ef3754c0b6b14b1f78.zip
refactor into lib-style crate, and add some early tests
Diffstat (limited to 'src')
-rw-r--r--src/bin/casual.rs (renamed from src/main.rs)35
-rw-r--r--src/cexpr.rs4
-rw-r--r--src/lib.rs34
3 files changed, 38 insertions, 35 deletions
diff --git a/src/main.rs b/src/bin/casual.rs
index f09e0f0..828c4b6 100644
--- a/src/main.rs
+++ b/src/bin/casual.rs
@@ -1,38 +1,7 @@
use std::env;
-use std::io;
-use std::io::Write;
use std::path::Path;
-mod cexpr;
-mod sexpr;
-
-use cexpr::CExpr;
-
-fn repl(_verbose: bool) {
- let stdin = io::stdin();
- let mut stdout = io::stdout();
-
- loop {
- let raw_input = &mut String::new();
- stdout.write(b"\ncasual> ").unwrap();
- stdout.flush().unwrap();
- stdin.read_line(raw_input).unwrap();
- let raw_input = raw_input; // mutable to immutable reference
- if raw_input.len() == 0 {
- // end-of-line, aka Ctrl-D. Blank line will still have newline char
- stdout.write(b"\nCiao!\n").unwrap();
- return;
- }
- let expr = match CExpr::from_str(&raw_input) {
- Ok(expr) => expr,
- Err(e) => {
- println!("error: {}", e);
- continue;
- }
- };
- println!("{}", expr);
- }
-}
+use casual::{repl, sexpr_parse_file};
fn usage() {
println!("usage:\tcasual [-h] [-v] [--no-repl] [<files>]");
@@ -81,7 +50,7 @@ fn main() {
return;
}
println!("Loading {}...", fname);
- match sexpr::sexpr_parse_file(&fpath) {
+ match sexpr_parse_file(&fpath) {
Err(e) => {
println!("Error loading file: {}\n {}", fname, e);
return;
diff --git a/src/cexpr.rs b/src/cexpr.rs
index b183c4a..357caff 100644
--- a/src/cexpr.rs
+++ b/src/cexpr.rs
@@ -37,7 +37,7 @@ impl NumericConstant {
}
}
-#[derive(Clone, PartialEq, PartialOrd)]
+#[derive(Clone, PartialEq, PartialOrd, Debug)]
pub enum CNumber {
// order here is important for sorting, etc
Integer(i64),
@@ -99,7 +99,7 @@ impl CNumber {
}
}
-#[derive(Clone, PartialEq, PartialOrd)]
+#[derive(Clone, PartialEq, PartialOrd, Debug)]
pub enum CExpr {
// order here is important for sorting, etc
Number(CNumber),
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..950329b
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,34 @@
+use std::io;
+use std::io::Write;
+
+mod cexpr;
+mod sexpr;
+
+pub use cexpr::{CExpr, CNumber};
+pub use sexpr::{sexpr_parse_file, SExpr};
+
+pub fn repl(_verbose: bool) {
+ let stdin = io::stdin();
+ let mut stdout = io::stdout();
+
+ loop {
+ let raw_input = &mut String::new();
+ stdout.write(b"\ncasual> ").unwrap();
+ stdout.flush().unwrap();
+ stdin.read_line(raw_input).unwrap();
+ let raw_input = raw_input; // mutable to immutable reference
+ if raw_input.len() == 0 {
+ // end-of-line, aka Ctrl-D. Blank line will still have newline char
+ stdout.write(b"\nCiao!\n").unwrap();
+ return;
+ }
+ let expr = match CExpr::from_str(&raw_input) {
+ Ok(expr) => expr,
+ Err(e) => {
+ println!("error: {}", e);
+ continue;
+ }
+ };
+ println!("{}", expr);
+ }
+}