mirror of
https://github.com/tiffany352/rink-rs
synced 2024-11-10 13:44:15 +00:00
Rework .print() and one_line() slightly
This commit is contained in:
parent
fadab5c53b
commit
c136c8a216
3 changed files with 14 additions and 7 deletions
|
@ -15,7 +15,7 @@ fn main() {
|
|||
Ok(_) => (),
|
||||
Err(_) => return
|
||||
};
|
||||
match one_line(&mut ctx, line.trim()) {
|
||||
match one_line(&mut ctx, &*line) {
|
||||
Ok(v) => ctx.print(&v),
|
||||
Err(e) => println!("{}", e)
|
||||
};
|
||||
|
|
17
src/eval.rs
17
src/eval.rs
|
@ -82,15 +82,22 @@ impl Value {
|
|||
}
|
||||
|
||||
impl Context {
|
||||
pub fn print(&self, value: &Value) {
|
||||
print!("{}", value.0);
|
||||
pub fn show(&self, value: &Value) -> String {
|
||||
use std::io::Write;
|
||||
|
||||
let mut out = vec![];
|
||||
write!(out, "{}", value.0).unwrap();
|
||||
for (&dim, &exp) in &value.1 {
|
||||
print!(" {}^{}", self.dimensions[dim], exp);
|
||||
write!(out, " {}^{}", self.dimensions[dim], exp).unwrap();
|
||||
}
|
||||
if let Some(name) = self.aliases.get(&value.1) {
|
||||
print!(" ({})", name);
|
||||
write!(out, " ({})", name).unwrap();
|
||||
}
|
||||
println!("");
|
||||
String::from_utf8(out).unwrap()
|
||||
}
|
||||
|
||||
pub fn print(&self, value: &Value) {
|
||||
println!("{}", self.show(value));
|
||||
}
|
||||
|
||||
pub fn lookup(&self, name: &str) -> Option<Value> {
|
||||
|
|
|
@ -18,7 +18,7 @@ pub fn load() -> Context {
|
|||
}
|
||||
|
||||
pub fn one_line(ctx: &mut Context, line: &str) -> Result<Value, String> {
|
||||
let mut iter = unit_defs::TokenIterator::new(&*line).peekable();
|
||||
let mut iter = unit_defs::TokenIterator::new(line.trim()).peekable();
|
||||
let expr = unit_defs::parse_expr(&mut iter);
|
||||
ctx.eval(&expr)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue