Library structure

This commit is contained in:
Tiffany Bennett 2016-08-02 21:17:55 -04:00
parent a1d3444a72
commit fadab5c53b
3 changed files with 48 additions and 36 deletions

24
src/bin/repl.rs Normal file
View file

@ -0,0 +1,24 @@
extern crate units_rs;
use units_rs::*;
fn main() {
use std::io::{stdin, stdout, Write};
let mut ctx = load();
let f = stdin();
let mut line = String::new();
loop {
print!("> ");
stdout().flush().unwrap();
match f.read_line(&mut line) {
Ok(_) => (),
Err(_) => return
};
match one_line(&mut ctx, line.trim()) {
Ok(v) => ctx.print(&v),
Err(e) => println!("{}", e)
};
line.clear();
}
}

24
src/lib.rs Normal file
View file

@ -0,0 +1,24 @@
pub mod unit_defs;
pub mod eval;
pub use eval::{Context, Value};
pub fn load() -> Context {
use std::io::Read;
use std::fs::File;
let mut f = File::open("units.txt").unwrap();
let mut buf = vec![];
f.read_to_end(&mut buf).unwrap();
let string = String::from_utf8_lossy(&*buf);
let mut iter = unit_defs::TokenIterator::new(&*string).peekable();
let res = unit_defs::parse(&mut iter);
eval::Context::new(res)
}
pub fn one_line(ctx: &mut Context, line: &str) -> Result<Value, String> {
let mut iter = unit_defs::TokenIterator::new(&*line).peekable();
let expr = unit_defs::parse_expr(&mut iter);
ctx.eval(&expr)
}

View file

@ -1,36 +0,0 @@
pub mod unit_defs;
pub mod eval;
fn main() {
use std::io::{stdin, stdout, Read, Write};
use std::fs::File;
let mut f = File::open("units.txt").unwrap();
let mut buf = vec![];
f.read_to_end(&mut buf).unwrap();
let string = String::from_utf8_lossy(&*buf);
let mut iter = unit_defs::TokenIterator::new(&*string).peekable();
//let res = unit_defs::tokens(&mut iter);
let res = unit_defs::parse(&mut iter);
let ctx = eval::Context::new(res);
//println!("{:#?}", res);
let f = stdin();
let mut line = String::new();
loop {
print!("> ");
stdout().flush().unwrap();
match f.read_line(&mut line) {
Ok(_) => (),
Err(_) => return
};
{
let mut iter = unit_defs::TokenIterator::new(&*line).peekable();
let expr = unit_defs::parse_expr(&mut iter);
match ctx.eval(&expr) {
Ok(value) => ctx.print(&value),
Err(e) => println!("{}", e)
};
}
line.clear();
}
}