Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add variable definitions via let #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Query {
UnitsFor(Expr),
Search(String),
Error(String),
Let(String, Expr),
}

impl fmt::Display for Conversion {
Expand Down
5 changes: 5 additions & 0 deletions core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Context {
pub short_output: bool,
pub use_humanize: bool,
pub previous_result: Option<Number>,
pub variables: BTreeMap<String, Number>,
}

impl Default for Context {
Expand Down Expand Up @@ -65,6 +66,7 @@ impl Context {
substance_symbols: BTreeMap::new(),
temporaries: BTreeMap::new(),
previous_result: None,
variables: BTreeMap::new(),
}
}

Expand Down Expand Up @@ -104,6 +106,9 @@ impl Context {
});
}
}
if let Some(v) = ctx.variables.get(name).cloned() {
return Some(v);
}
None
}

Expand Down
15 changes: 13 additions & 2 deletions core/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ impl Context {
}

/// Evaluates an expression, include `->` conversions.
pub fn eval_outer(&self, expr: &Query) -> Result<QueryReply, QueryError> {
pub fn eval_outer(&mut self, expr: &Query) -> Result<QueryReply, QueryError> {
match *expr {
Query::Expr(Expr::Unit { ref name })
if {
Expand Down Expand Up @@ -1178,7 +1178,18 @@ impl Context {
s.to_reply(self).map_err(QueryError::generic)?,
)),
}
}
},
Query::Let(ref var_name, ref expr) => {
// todo: check whether already defined with self.lookup(var_name)
let res = self.eval(expr)?;
if let Value::Number(num) = res {
let parts = num.to_parts(self);
self.variables.insert(var_name.clone(), num);
Ok(QueryReply::Number(parts))
} else {
Err(QueryError::generic("let expressions currently only supported for numeric results".to_string()))
}
},
Query::Error(ref e) => Err(QueryError::generic(e.clone())),
}
}
Expand Down
11 changes: 11 additions & 0 deletions core/src/text_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,17 @@ pub fn parse_query(iter: &mut Iter<'_>) -> Query {
return Query::Search(s.clone());
}
}
Some(Token::Ident(ref s)) if s == "let" => {
iter.next();
if let Some(Token::Ident(ref s)) = iter.peek().cloned() {
iter.next();
if let Some(Token::Equals) = iter.peek() {
iter.next();
return Query::Let(s.clone(), parse_eq(iter));
}
}
return Query::Error("let needs to have form `let var = expr`".to_string());
}
_ => (),
}
let left = parse_eq(iter);
Expand Down