mirror of
https://github.com/rm-dr/daisy
synced 2025-08-02 01:34:50 -07:00
Cleaned up context argument
This commit is contained in:
@ -62,7 +62,7 @@ impl Operator {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn from_string(s: &str, context: &Context) -> Option<Operator> {
|
||||
pub fn from_string(context: &Context, s: &str) -> Option<Operator> {
|
||||
|
||||
let f = Function::from_string(s);
|
||||
if let Some(f) = f {
|
||||
@ -207,7 +207,11 @@ impl Operator {
|
||||
|
||||
let q = &args[1];
|
||||
|
||||
if q.is_unitless_integer() && !q.to_string().contains("e") {
|
||||
if {
|
||||
//context.config.enable_super_powers &&
|
||||
q.is_unitless_integer() &&
|
||||
!q.to_string().contains("e")
|
||||
} {
|
||||
// Write integer powers as a superscript
|
||||
let mut b = String::new();
|
||||
for c in q.to_string().chars() {
|
||||
|
@ -18,30 +18,30 @@ use crate::context::Context;
|
||||
use crate::errors::DaisyError;
|
||||
|
||||
pub fn parse(
|
||||
s: &String, context: &Context
|
||||
context: &Context, s: &String
|
||||
) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||
|
||||
let expressions = stage::tokenize(s, context);
|
||||
let expressions = stage::tokenize(context, s);
|
||||
let (_, expressions) = stage::find_subs(expressions);
|
||||
let g = stage::groupify(expressions, context)?;
|
||||
let g = stage::treeify(g, context)?;
|
||||
let g = stage::groupify(context, expressions)?;
|
||||
let g = stage::treeify(context, g)?;
|
||||
|
||||
return Ok(g);
|
||||
}
|
||||
|
||||
pub fn parse_no_context(s: &String) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||
parse(s, &Context::new())
|
||||
parse(&Context::new(), s)
|
||||
}
|
||||
|
||||
pub fn substitute(s: &String, context: &Context) -> String {
|
||||
let (_, s) = substitute_cursor(s, s.chars().count(), context);
|
||||
pub fn substitute(context: &Context, s: &String) -> String {
|
||||
let (_, s) = substitute_cursor(context, s, s.chars().count());
|
||||
return s;
|
||||
}
|
||||
|
||||
pub fn substitute_cursor(
|
||||
context: &Context,
|
||||
s: &String, // The string to substitute
|
||||
c: usize, // Location of the cursor right now
|
||||
context: &Context
|
||||
c: usize // Location of the cursor right now
|
||||
) -> (
|
||||
usize, // Location of cursor in substituted string
|
||||
String // String with substitutions
|
||||
@ -50,7 +50,7 @@ pub fn substitute_cursor(
|
||||
let mut new_s = s.clone();
|
||||
|
||||
let l = s.chars().count();
|
||||
let expressions = stage::tokenize(s, context);
|
||||
let expressions = stage::tokenize(context, s);
|
||||
let (mut subs, _) = stage::find_subs(expressions);
|
||||
let mut new_c = l - c;
|
||||
|
||||
|
@ -11,8 +11,8 @@ use crate::context::Context;
|
||||
|
||||
|
||||
fn lookback_signs(
|
||||
g: &mut VecDeque<Token>,
|
||||
context: &Context
|
||||
context: &Context,
|
||||
g: &mut VecDeque<Token>
|
||||
) -> Result<(), (LineLocation, DaisyError)> {
|
||||
|
||||
// Convert `-` operators to `neg` operators
|
||||
@ -44,7 +44,7 @@ fn lookback_signs(
|
||||
(Token::Operator(_, sa), Token::Operator(l,sb))
|
||||
=> {
|
||||
if {
|
||||
let o = Operator::from_string(sa, context);
|
||||
let o = Operator::from_string(context, sa);
|
||||
|
||||
o.is_some() &&
|
||||
(
|
||||
@ -101,11 +101,11 @@ fn lookback_signs(
|
||||
|
||||
// Inserts implicit operators
|
||||
fn lookback(
|
||||
g: &mut VecDeque<Token>,
|
||||
context: &Context
|
||||
context: &Context,
|
||||
g: &mut VecDeque<Token>
|
||||
) -> Result<(), (LineLocation, DaisyError)> {
|
||||
|
||||
lookback_signs(g, context)?;
|
||||
lookback_signs(context, g)?;
|
||||
|
||||
let mut i: usize = 0;
|
||||
while i < g.len() {
|
||||
@ -142,7 +142,7 @@ fn lookback(
|
||||
=> {
|
||||
let la = la.clone();
|
||||
let lb = lb.clone();
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
|
||||
g.insert(i-1, b);
|
||||
if o.is_some() {
|
||||
@ -164,7 +164,7 @@ fn lookback(
|
||||
=> {
|
||||
let la = la.clone();
|
||||
let lb = lb.clone();
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
|
||||
g.insert(i-1, b);
|
||||
if o.is_some() {
|
||||
@ -196,8 +196,8 @@ fn lookback(
|
||||
|
||||
|
||||
pub fn groupify(
|
||||
mut g: VecDeque<Token>,
|
||||
context: &Context
|
||||
context: &Context,
|
||||
mut g: VecDeque<Token>
|
||||
) -> Result<
|
||||
Token,
|
||||
(LineLocation, DaisyError)
|
||||
@ -240,7 +240,7 @@ pub fn groupify(
|
||||
|
||||
let (_, mut v) = levels.pop().unwrap();
|
||||
let (_, v_now) = levels.last_mut().unwrap();
|
||||
lookback(&mut v, context)?;
|
||||
lookback(context, &mut v)?;
|
||||
|
||||
let q = is_tuple.pop().unwrap();
|
||||
if q {
|
||||
@ -275,7 +275,7 @@ pub fn groupify(
|
||||
let (_, v_now) = levels.last_mut().unwrap();
|
||||
|
||||
if v.len() == 0 { return Err((l, DaisyError::EmptyGroup)) }
|
||||
lookback(&mut v, context)?;
|
||||
lookback(context, &mut v)?;
|
||||
|
||||
let q = is_tuple.pop().unwrap();
|
||||
if q {
|
||||
@ -294,7 +294,7 @@ pub fn groupify(
|
||||
return Err((l, DaisyError::BadTuple));
|
||||
}
|
||||
|
||||
lookback(&mut v, context)?;
|
||||
lookback(context, &mut v)?;
|
||||
|
||||
return Ok(Token::Group(
|
||||
LineLocation{pos:0, len:last_linelocation.pos + last_linelocation.len},
|
||||
|
@ -10,10 +10,10 @@ use super::super::{
|
||||
// Called whenever a token is finished.
|
||||
#[inline(always)]
|
||||
fn push_token(
|
||||
context: &Context,
|
||||
g: &mut VecDeque<Token>,
|
||||
t: Option<Token>,
|
||||
stop_i: usize,
|
||||
context: &Context
|
||||
stop_i: usize
|
||||
) {
|
||||
|
||||
if t.is_none() { return }
|
||||
@ -60,7 +60,7 @@ fn push_token(
|
||||
|
||||
// Some operators are written as words.
|
||||
if let Token::Word(l, s) = &t {
|
||||
if Operator::from_string(s, context).is_some() {
|
||||
if Operator::from_string(context, s).is_some() {
|
||||
t = Token::Operator(*l, s.clone());
|
||||
}
|
||||
}
|
||||
@ -69,7 +69,7 @@ fn push_token(
|
||||
}
|
||||
|
||||
/// Turns a string into Tokens. First stage of parsing.
|
||||
pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
pub fn tokenize(context: &Context, input: &String) -> VecDeque<Token> {
|
||||
let mut t: Option<Token> = None; // The current token we're reading
|
||||
let mut g: VecDeque<Token> = VecDeque::with_capacity(32);
|
||||
|
||||
@ -88,7 +88,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
// If we're not building a number, finalize
|
||||
// previous token and start one.
|
||||
_ => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::Quantity(LineLocation{pos: i, len: 0}, String::from(c)));
|
||||
}
|
||||
};
|
||||
@ -102,7 +102,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
Some(Token::Quantity(_, val)) => { val.push(c); },
|
||||
|
||||
_ => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::Word(LineLocation{pos: i, len: 0}, String::from(c)));
|
||||
}
|
||||
};
|
||||
@ -122,7 +122,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
} else {
|
||||
// Otherwise, end the number.
|
||||
// We probably have a subtraction.
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::Operator(
|
||||
LineLocation{pos: i, len: 1},
|
||||
String::from(c)
|
||||
@ -134,7 +134,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
// Multi-character operators with - and + are NOT supported!
|
||||
// (for example, we can't use -> for unit conversion)
|
||||
_ => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::Operator(
|
||||
LineLocation{pos: i, len: 1},
|
||||
String::from(c)
|
||||
@ -144,7 +144,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
},
|
||||
|
||||
',' => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::TupleDelim(LineLocation{pos: i, len: 1}));
|
||||
},
|
||||
|
||||
@ -157,7 +157,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
match &mut t {
|
||||
Some(Token::Operator(_, val)) => { val.push(c); },
|
||||
_ => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::Operator(LineLocation{pos: i, len: 0}, String::from(c)));
|
||||
}
|
||||
};
|
||||
@ -165,17 +165,17 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
|
||||
// Group
|
||||
'(' => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::GroupStart(LineLocation{pos: i, len: 0}));
|
||||
},
|
||||
')' => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::GroupEnd(LineLocation{pos: i, len: 0}));
|
||||
},
|
||||
|
||||
// Space. Basic seperator.
|
||||
' ' => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = None;
|
||||
}
|
||||
|
||||
@ -185,7 +185,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
Some(Token::Word(_, val)) => { val.push(c); },
|
||||
|
||||
_ => {
|
||||
push_token(&mut g, t, i, context);
|
||||
push_token(context, &mut g, t, i);
|
||||
t = Some(Token::Word(LineLocation{pos: i, len: 0}, String::from(c)));
|
||||
}
|
||||
};
|
||||
@ -193,7 +193,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
|
||||
};
|
||||
}
|
||||
|
||||
push_token(&mut g, t, input.chars().count(), context);
|
||||
push_token(context, &mut g, t, input.chars().count());
|
||||
|
||||
return g;
|
||||
}
|
@ -10,9 +10,9 @@ use super::super::{
|
||||
};
|
||||
|
||||
fn treeify_binary(
|
||||
context: &Context,
|
||||
i: usize,
|
||||
g_inner: &mut VecDeque<Token>,
|
||||
context: &Context
|
||||
g_inner: &mut VecDeque<Token>
|
||||
) -> Result<bool, (LineLocation, DaisyError)> {
|
||||
|
||||
let this: &Token = &g_inner[i];
|
||||
@ -56,7 +56,7 @@ fn treeify_binary(
|
||||
|
||||
|
||||
if let Token::Operator(l, s) = left {
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
|
||||
let o = o.unwrap();
|
||||
|
||||
@ -72,7 +72,7 @@ fn treeify_binary(
|
||||
}
|
||||
|
||||
if let Token::Operator(l, s) = right {
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
|
||||
let o = o.unwrap();
|
||||
|
||||
@ -91,7 +91,7 @@ fn treeify_binary(
|
||||
// This operator
|
||||
let this_op = {
|
||||
let Token::Operator(l, s) = this else {panic!()};
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // bad operator string
|
||||
o.unwrap()
|
||||
};
|
||||
@ -99,14 +99,14 @@ fn treeify_binary(
|
||||
// The operators contesting our arguments
|
||||
let left_op = if i > 1 {
|
||||
let Token::Operator(l, s) = &g_inner[i-2] else {panic!()};
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad operator string
|
||||
Some(o.unwrap())
|
||||
} else { None };
|
||||
|
||||
let right_op = if i < g_inner.len()-2 {
|
||||
let Token::Operator(l, s) = &g_inner[i+2] else {panic!()};
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad operator string
|
||||
Some(o.unwrap())
|
||||
} else { None };
|
||||
@ -122,20 +122,20 @@ fn treeify_binary(
|
||||
let right_pre = g_inner.remove(i-1).unwrap();
|
||||
let mut left: Expression; let mut right: Expression;
|
||||
if let Token::Group(l, _) = right_pre {
|
||||
right = treeify(right_pre, context)?;
|
||||
right = treeify(context, right_pre)?;
|
||||
right.set_linelocation(&(right.get_linelocation() + l));
|
||||
} else if let Token::Tuple(l, _) = right_pre {
|
||||
right = treeify(right_pre, context)?;
|
||||
right = treeify(context, right_pre)?;
|
||||
right.set_linelocation(&(right.get_linelocation() + l));
|
||||
} else {
|
||||
right = right_pre.to_expression(context)?;
|
||||
}
|
||||
|
||||
if let Token::Group(l, _) = left_pre {
|
||||
left = treeify(left_pre, context)?;
|
||||
left = treeify(context, left_pre)?;
|
||||
left.set_linelocation(&(left.get_linelocation() + l));
|
||||
} else if let Token::Tuple(l, _) = left_pre {
|
||||
left = treeify(left_pre, context)?;
|
||||
left = treeify(context, left_pre)?;
|
||||
left.set_linelocation(&(left.get_linelocation() + l));
|
||||
} else {
|
||||
left = left_pre.to_expression(context)?;
|
||||
@ -143,7 +143,7 @@ fn treeify_binary(
|
||||
|
||||
let (l, o) = {
|
||||
let Token::Operator(l, s) = this_pre else {panic!()};
|
||||
let o = Operator::from_string(&s, context);
|
||||
let o = Operator::from_string(context, &s);
|
||||
if o.is_none() { panic!() }
|
||||
(l, o.unwrap())
|
||||
};
|
||||
@ -161,10 +161,10 @@ fn treeify_binary(
|
||||
}
|
||||
|
||||
fn treeify_unary(
|
||||
context: &Context,
|
||||
i: usize,
|
||||
g_inner: &mut VecDeque<Token>,
|
||||
left_associative: bool,
|
||||
context: &Context
|
||||
left_associative: bool
|
||||
) -> Result<bool, (LineLocation, DaisyError)> {
|
||||
|
||||
let this: &Token = &g_inner[i];
|
||||
@ -224,7 +224,7 @@ fn treeify_unary(
|
||||
// This operator
|
||||
let this_op = {
|
||||
let Token::Operator(l, s) = this else {panic!()};
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
|
||||
o.unwrap()
|
||||
};
|
||||
@ -233,14 +233,14 @@ fn treeify_unary(
|
||||
let next_op = if left_associative {
|
||||
if i > 1 {
|
||||
let Token::Operator(l, s) = &g_inner[i-2] else {panic!()};
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
|
||||
Some(o.unwrap())
|
||||
} else { None }
|
||||
} else {
|
||||
if i < g_inner.len()-2 {
|
||||
let Token::Operator(l, s) = &g_inner[i+2] else {panic!()};
|
||||
let o = Operator::from_string(s, context);
|
||||
let o = Operator::from_string(context, s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); } // Bad string
|
||||
Some(o.unwrap())
|
||||
} else { None }
|
||||
@ -255,10 +255,10 @@ fn treeify_unary(
|
||||
next_pre = g_inner.remove(i).unwrap();
|
||||
}
|
||||
if let Token::Group(l, _) = next_pre {
|
||||
next = treeify(next_pre, context)?;
|
||||
next = treeify(context, next_pre)?;
|
||||
next.set_linelocation(&(next.get_linelocation() + l));
|
||||
} else if let Token::Tuple(l, _) = next_pre {
|
||||
next = treeify(next_pre, context)?;
|
||||
next = treeify(context, next_pre)?;
|
||||
next.set_linelocation(&(next.get_linelocation() + l));
|
||||
} else {
|
||||
next = next_pre.to_expression(context)?;
|
||||
@ -267,7 +267,7 @@ fn treeify_unary(
|
||||
|
||||
let (l, o) = {
|
||||
let Token::Operator(l, s) = this_pre else {panic!()};
|
||||
let o = Operator::from_string(&s, context);
|
||||
let o = Operator::from_string(context, &s);
|
||||
if o.is_none() { panic!() }
|
||||
(l, o.unwrap())
|
||||
};
|
||||
@ -292,8 +292,8 @@ fn treeify_unary(
|
||||
|
||||
|
||||
pub fn treeify(
|
||||
mut g: Token,
|
||||
context: &Context
|
||||
context: &Context,
|
||||
mut g: Token
|
||||
) -> Result<Expression, (LineLocation, DaisyError)> {
|
||||
|
||||
let (l, g_inner): (LineLocation, &mut VecDeque<Token>) = match g {
|
||||
@ -301,7 +301,7 @@ pub fn treeify(
|
||||
Token::Tuple(l, parts) => {
|
||||
let mut t: VecDeque<Expression> = VecDeque::new();
|
||||
for p in parts {
|
||||
t.push_back(treeify(p, context)?);
|
||||
t.push_back(treeify(context, p)?);
|
||||
};
|
||||
|
||||
return Ok(Expression::Tuple(l, t));
|
||||
@ -332,7 +332,7 @@ pub fn treeify(
|
||||
// If not an operator, move on.
|
||||
let this_op = match &g_inner[i] {
|
||||
Token::Operator(l, s) => {
|
||||
let o = Operator::from_string(&s, context);
|
||||
let o = Operator::from_string(context, &s);
|
||||
if o.is_none() { return Err((*l, DaisyError::Syntax)); }
|
||||
o.unwrap()
|
||||
},
|
||||
@ -346,9 +346,9 @@ pub fn treeify(
|
||||
let mut changed = false;
|
||||
if this_op.is_left_associative() {
|
||||
if this_op.is_binary() {
|
||||
changed = treeify_binary(i, g_inner, context)?;
|
||||
changed = treeify_binary(context, i, g_inner)?;
|
||||
} else {
|
||||
changed = treeify_unary(i, g_inner, left_associative, context)?;
|
||||
changed = treeify_unary(context, i, g_inner, left_associative)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -360,9 +360,9 @@ pub fn treeify(
|
||||
} else {
|
||||
if !this_op.is_left_associative() {
|
||||
if this_op.is_binary() {
|
||||
treeify_binary(i, g_inner, context)?;
|
||||
treeify_binary(context, i, g_inner)?;
|
||||
} else {
|
||||
treeify_unary(i, g_inner, left_associative, context)?;
|
||||
treeify_unary(context, i, g_inner, left_associative)?;
|
||||
}
|
||||
}
|
||||
j -= 1
|
||||
@ -378,7 +378,7 @@ pub fn treeify(
|
||||
},
|
||||
Token::Tuple(_, _) |
|
||||
Token::Group(_,_) => {
|
||||
treeify(g, context)
|
||||
treeify(context, g)
|
||||
},
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user