mirror of
https://github.com/rm-dr/daisy
synced 2025-08-02 01:34:50 -07:00
Reorganized variable code
This commit is contained in:
@ -17,22 +17,26 @@ pub use self::{
|
||||
token::Function,
|
||||
};
|
||||
|
||||
use crate::context::Context;
|
||||
|
||||
|
||||
pub fn parse(
|
||||
s: &String
|
||||
) -> Result<
|
||||
Token,
|
||||
(LineLocation, ParserError)
|
||||
> {
|
||||
s: &String, context: &Context
|
||||
) -> Result<Token, (LineLocation, ParserError)> {
|
||||
|
||||
let tokens = stage::tokenize(s);
|
||||
let (_, tokens) = stage::find_subs(tokens);
|
||||
let g = stage::groupify(tokens)?;
|
||||
let g = stage::treeify(g)?;
|
||||
|
||||
let g = stage::treeify(g, context)?;
|
||||
|
||||
return Ok(g);
|
||||
}
|
||||
|
||||
pub fn parse_no_context(s: &String) -> Result<Token, (LineLocation, ParserError)> {
|
||||
parse(s, &Context::new())
|
||||
}
|
||||
|
||||
|
||||
pub fn substitute(
|
||||
s: &String, // The string to substitute
|
||||
|
@ -1,6 +1,7 @@
|
||||
use std::collections::VecDeque;
|
||||
use crate::quantity::Unit;
|
||||
use crate::quantity::Quantity;
|
||||
use crate::context::Context;
|
||||
|
||||
use super::{
|
||||
LineLocation,
|
||||
@ -55,7 +56,7 @@ impl PreToken {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn to_token(self) -> Result<Token, (LineLocation, ParserError)>{
|
||||
pub fn to_token(self, context: &Context) -> Result<Token, (LineLocation, ParserError)>{
|
||||
match self {
|
||||
PreToken::PreQuantity(l, mut s) => {
|
||||
|
||||
@ -74,17 +75,15 @@ impl PreToken {
|
||||
},
|
||||
|
||||
PreToken::PreWord(l, s) => {
|
||||
let c = Constant::from_string(&s);
|
||||
|
||||
if c.is_some() {
|
||||
return Ok(Token::Constant(c.unwrap()));
|
||||
}
|
||||
let c = Constant::from_string(&s);
|
||||
if c.is_some() { return Ok(Token::Constant(c.unwrap())); }
|
||||
|
||||
let c = Unit::from_string(&s);
|
||||
if c.is_some() { return Ok(Token::Quantity(c.unwrap())); }
|
||||
|
||||
|
||||
if s == "ans" { return Ok(Token::Variable(String::from("ans"))); }
|
||||
let c = context.get_variable(&s);
|
||||
if c.is_some() { return Ok(Token::Variable(s)); }
|
||||
|
||||
return Err((l, ParserError::Undefined(s)));
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
use std::collections::VecDeque;
|
||||
use crate::context::Context;
|
||||
|
||||
use super::super::{
|
||||
PreToken,
|
||||
@ -10,7 +11,8 @@ use super::super::{
|
||||
|
||||
fn treeify_binary(
|
||||
i: usize,
|
||||
g_inner: &mut VecDeque<PreToken>
|
||||
g_inner: &mut VecDeque<PreToken>,
|
||||
context: &Context
|
||||
) -> Result<bool, (LineLocation, ParserError)> {
|
||||
|
||||
let this: &PreToken = &g_inner[i];
|
||||
@ -125,8 +127,8 @@ fn treeify_binary(
|
||||
let this_pre = g_inner.remove(i-1).unwrap();
|
||||
let right_pre = g_inner.remove(i-1).unwrap();
|
||||
let left: Token; let right: Token;
|
||||
if let PreToken::PreGroup(_, _) = right_pre { right = treeify(right_pre)?; } else {right = right_pre.to_token()?;}
|
||||
if let PreToken::PreGroup(_, _) = left_pre { left = treeify(left_pre)?; } else {left = left_pre.to_token()?;}
|
||||
if let PreToken::PreGroup(_, _) = right_pre { right = treeify(right_pre, context)?; } else {right = right_pre.to_token(context)?;}
|
||||
if let PreToken::PreGroup(_, _) = left_pre { left = treeify(left_pre, context)?; } else {left = left_pre.to_token(context)?;}
|
||||
|
||||
let o = {
|
||||
let PreToken::PreOperator(_, s) = this_pre else {panic!()};
|
||||
@ -150,7 +152,8 @@ fn treeify_binary(
|
||||
fn treeify_unary(
|
||||
i: usize,
|
||||
g_inner: &mut VecDeque<PreToken>,
|
||||
left_associative: bool
|
||||
left_associative: bool,
|
||||
context: &Context
|
||||
) -> Result<bool, (LineLocation, ParserError)> {
|
||||
|
||||
let this: &PreToken = &g_inner[i];
|
||||
@ -242,7 +245,7 @@ fn treeify_unary(
|
||||
} else {
|
||||
next_pre = g_inner.remove(i).unwrap();
|
||||
}
|
||||
if let PreToken::PreGroup(_, _) = next_pre { next = treeify(next_pre)?; } else { next = next_pre.to_token()? }
|
||||
if let PreToken::PreGroup(_, _) = next_pre { next = treeify(next_pre, context)?; } else { next = next_pre.to_token(context)? }
|
||||
|
||||
let o = {
|
||||
let PreToken::PreOperator(_, s) = this_pre else {panic!()};
|
||||
@ -272,6 +275,7 @@ fn treeify_unary(
|
||||
|
||||
pub fn treeify(
|
||||
mut g: PreToken,
|
||||
context: &Context
|
||||
) -> Result<Token, (LineLocation, ParserError)> {
|
||||
|
||||
let g_inner: &mut VecDeque<PreToken> = match g {
|
||||
@ -311,9 +315,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)?;
|
||||
changed = treeify_binary(i, g_inner, context)?;
|
||||
} else {
|
||||
changed = treeify_unary(i, g_inner, left_associative)?;
|
||||
changed = treeify_unary(i, g_inner, left_associative, context)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,9 +329,9 @@ pub fn treeify(
|
||||
} else {
|
||||
if !this_op.is_left_associative() {
|
||||
if this_op.is_binary() {
|
||||
treeify_binary(i, g_inner)?;
|
||||
treeify_binary(i, g_inner, context)?;
|
||||
} else {
|
||||
treeify_unary(i, g_inner, left_associative)?;
|
||||
treeify_unary(i, g_inner, left_associative, context)?;
|
||||
}
|
||||
}
|
||||
j -= 1
|
||||
@ -342,9 +346,9 @@ pub fn treeify(
|
||||
Err((l, ParserError::Syntax))
|
||||
},
|
||||
PreToken::PreGroup(_,_) => {
|
||||
treeify(g)
|
||||
treeify(g, context)
|
||||
},
|
||||
|
||||
_ => { Ok(g.to_token()?) }
|
||||
_ => { Ok(g.to_token(context)?) }
|
||||
};
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ pub use self::function::Function;
|
||||
pub use self::token::Token;
|
||||
|
||||
|
||||
use super::parse;
|
||||
use super::parse_no_context;
|
||||
include!(concat!(env!("OUT_DIR"), "/constants.rs"));
|
Reference in New Issue
Block a user