aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-01-12 23:32:57 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-01-12 23:34:52 +0100
commit4e0bb7a6cc583443f1a3bd83f39083ffa0b26a54 (patch)
tree2f7a6e29cda0085644577fa9472cbdb3dbbc9e0b
parentd8bbd362b80e07742eca5c8382ebfb19f505422c (diff)
downloadsylt-4e0bb7a6cc583443f1a3bd83f39083ffa0b26a54.tar.gz
move most of main to lib.rs
-rw-r--r--Cargo.toml3
-rw-r--r--src/lib.rs67
-rw-r--r--src/main.rs65
3 files changed, 71 insertions, 64 deletions
diff --git a/Cargo.toml b/Cargo.toml
index c6a109d..4f90bfc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,9 @@ version = "0.1.0"
authors = ["Edvard Thörnros <edvard.thornros@gmail.com>"]
edition = "2018"
+[lib]
+name = "tihdy"
+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..aef8c53
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,67 @@
+use std::path::Path;
+
+pub mod compiler;
+pub mod tokenizer;
+pub mod vm;
+
+mod error;
+
+use error::Error;
+use tokenizer::TokenStream;
+
+pub fn run_file(path: &Path) -> Result<(), Vec<Error>> {
+ run(tokenizer::file_to_tokens(path), path)
+}
+
+pub fn run_string(s: &str) -> Result<(), Vec<Error>> {
+ run(tokenizer::string_to_tokens(s), Path::new("builtin"))
+}
+
+pub fn run(tokens: TokenStream, path: &Path) -> Result<(), Vec<Error>> {
+ match compiler::compile("main", path, tokens) {
+ Ok(block) => vm::run_block(block).or_else(|e| Err(vec![e])),
+ Err(errors) => Err(errors),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{run_file, run_string};
+ use crate::error::{Error, ErrorKind};
+ use std::path::Path;
+
+ macro_rules! assert_errs {
+ ($result:expr, [ $( $kind:pat ),* ]) => {
+ println!("{} => {:?}", stringify!($result), $result);
+ assert!(matches!(
+ $result.unwrap_err().as_slice(),
+ &[$(Error {
+ kind: $kind,
+ file: _,
+ line: _,
+ message: _,
+ },
+ )*]
+ ))
+ };
+ }
+
+ #[test]
+ fn unreachable_token() {
+ assert_errs!(run_string("<!>\n"), [ErrorKind::Unreachable]);
+ }
+
+ macro_rules! test_file {
+ ($fn:ident, $path:literal) => {
+ #[test]
+ fn $fn() {
+ let file = Path::new($path);
+ assert!(run_file(&file).is_ok());
+ }
+ };
+ }
+ test_file!(order_of_operations, "tests/order-of-operations.tdy");
+ test_file!(variables, "tests/variables.tdy");
+ test_file!(scoping, "tests/scoping.tdy");
+ test_file!(if_, "tests/if.tdy");
+}
diff --git a/src/main.rs b/src/main.rs
index e25aab4..ab5f4a0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,6 @@
use std::path::{Path, PathBuf};
-mod error;
-mod tokenizer;
-mod vm;
-mod compiler;
-
-use error::Error;
-use tokenizer::TokenStream;
+use tihdy::run_file;
fn main() {
let file = file_from_args().unwrap_or_else(|| Path::new("tests/simple.tdy").to_owned());
@@ -21,60 +15,3 @@ fn main() {
fn file_from_args() -> Option<PathBuf> {
std::env::args().skip(1).map(|s| Path::new(&s).to_owned()).find(|p| p.is_file())
}
-
-fn run_file(path: &Path) -> Result<(), Vec<Error>> {
- run(tokenizer::file_to_tokens(path), path)
-}
-
-fn run_string(s: &str) -> Result<(), Vec<Error>> {
- run(tokenizer::string_to_tokens(s), Path::new("builtin"))
-}
-
-fn run(tokens: TokenStream, path: &Path) -> Result<(), Vec<Error>> {
- match compiler::compile("main", path, tokens) {
- Ok(block) => vm::run_block(block).or_else(|e| Err(vec![e])),
- Err(errors) => Err(errors),
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::{run_file, run_string};
- use crate::error::{Error, ErrorKind};
- use std::path::Path;
-
- macro_rules! assert_errs {
- ($result:expr, [ $( $kind:pat ),* ]) => {
- println!("{} => {:?}", stringify!($result), $result);
- assert!(matches!(
- $result.unwrap_err().as_slice(),
- &[$(Error {
- kind: $kind,
- file: _,
- line: _,
- message: _,
- },
- )*]
- ))
- };
- }
-
- #[test]
- fn unreachable_token() {
- assert_errs!(run_string("<!>\n"), [ErrorKind::Unreachable]);
- }
-
- macro_rules! test_file {
- ($fn:ident, $path:literal) => {
- #[test]
- fn $fn() {
- let file = Path::new($path);
- assert!(run_file(&file).is_ok());
- }
- };
- }
- test_file!(order_of_operations, "tests/order-of-operations.tdy");
- test_file!(variables, "tests/variables.tdy");
- test_file!(scoping, "tests/scoping.tdy");
- test_file!(ifs, "tests/if.tdy");
-}