[READ-ONLY] Mirror of https://github.com/probablykasper/cpc. Text calculator with support for units and conversion cpc.kasper.space
calculator cli conversion converter library math package units units-converter wasm website
0

Configure Feed

Select the types of activity you want to include in your feed.

Remove `TokenVector` type

Kasper (Apr 21, 2021, 9:57 PM +0200) 1abe436f c7ee968e

+19 -21
+5 -4
src/lexer.rs
··· 1 1 use std::str::FromStr; 2 2 use decimal::d128; 3 - use crate::{Token, TokenVector}; 3 + use crate::Token; 4 4 use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, RightParen}; 5 5 use crate::UnaryOperator::{Percent, Factorial}; 6 6 use crate::TextOperator::{Of, To}; ··· 18 18 } 19 19 } 20 20 21 - /// Lex an input string and return a [`TokenVector`] 22 - pub fn lex(input: &str, allow_trailing_operators: bool, default_degree: Unit) -> Result<TokenVector, String> { 21 + /// Lex an input string and returns [`Token`]s 22 + pub fn lex(input: &str, allow_trailing_operators: bool, default_degree: Unit) -> Result<Vec<Token>, String> { 23 23 24 24 let mut input = input.replace(",", ""); // ignore commas 25 + 25 26 input = input.to_lowercase(); 26 27 27 28 if allow_trailing_operators { ··· 34 35 } 35 36 36 37 let mut chars = input.chars().peekable(); 37 - let mut tokens: TokenVector = vec![]; 38 + let mut tokens: Vec<Token> = vec![]; 38 39 let max_word_length = 30; 39 40 40 41 let mut left_paren_count = 0;
+3 -6
src/lib.rs
··· 28 28 29 29 /// Units, and functions you can use with them 30 30 pub mod units; 31 - /// Turns a string into a [`TokenVector`] 31 + /// Turns a string into [`Token`]s 32 32 pub mod lexer; 33 - /// Turns a [`TokenVector`] into an [`AstNode`](parser::AstNode) 33 + /// Turns [`Token`]s into an [`AstNode`](parser::AstNode) 34 34 pub mod parser; 35 35 /// Turns an [`AstNode`](parser::AstNode) into a [`Number`] 36 36 pub mod evaluator; ··· 123 123 } 124 124 125 125 #[derive(Clone, Debug)] 126 - /// A constants like [`Pi`](Constant::Pi) or [`E`](Constant::E). 126 + /// A constant like [`Pi`](Constant::Pi) or [`E`](Constant::E). 127 127 pub enum Constant { 128 128 Pi, 129 129 E, ··· 190 190 Negative, 191 191 Unit(units::Unit), 192 192 } 193 - 194 - /// A vector of [`Token`] 195 - pub type TokenVector = Vec<Token>; 196 193 197 194 /// Evaluates a string into a resulting [`Number`]. 198 195 ///
+11 -11
src/parser.rs
··· 1 - use crate::{Token, TokenVector}; 1 + use crate::Token; 2 2 use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, RightParen}; 3 3 use crate::UnaryOperator::{Percent, Factorial}; 4 4 use crate::TextOperator::{To, Of}; ··· 22 22 } 23 23 } 24 24 25 - /// Parse [`TokenVector`] into an Abstract Syntax Tree ([`AstNode`]) 26 - pub fn parse(tokens: &TokenVector) -> Result<AstNode, String> { 25 + /// Parse [`Token`]s into an Abstract Syntax Tree ([`AstNode`]) 26 + pub fn parse(tokens: &Vec<Token>) -> Result<AstNode, String> { 27 27 parse_level_1(tokens, 0).and_then(|(ast, next_pos)| if next_pos == tokens.len() { 28 28 Ok(ast) 29 29 } else { ··· 33 33 34 34 // level 1 precedence (lowest): to, of 35 35 /// Parse [`To`](crate::TextOperator::To) and [`Of`](crate::TextOperator::Of) 36 - pub fn parse_level_1(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 36 + pub fn parse_level_1(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 37 37 // do higher precedences first, then come back down 38 38 let (mut node, mut pos) = parse_level_2(tokens, pos)?; 39 39 // now we loop through the next tokens ··· 61 61 62 62 // level 2 precedence: +, - 63 63 /// Parse [`Plus`](crate::Operator::Plus) and [`Minus`](crate::Operator::Minus) 64 - pub fn parse_level_2(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 64 + pub fn parse_level_2(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 65 65 let (mut node, mut pos) = parse_level_3(tokens, pos)?; 66 66 loop { 67 67 let token = tokens.get(pos); ··· 83 83 84 84 // level 3 precedence: *, /, modulo, implicative multiplication, foot-inch 6'4" 85 85 /// Parse [`Multiply`](crate::Operator::Multiply), [`Divide`](crate::Operator::Divide), [`Modulo`](crate::Operator::Modulo) and implicative multiplication (for example`2pi`) 86 - pub fn parse_level_3(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 86 + pub fn parse_level_3(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 87 87 88 88 // parse foot-inch syntax 6'4" 89 89 let token0 = tokens.get(pos); ··· 135 135 // such will only end up here if they were unable to be parsed as part of 136 136 // other operators. 137 137 // Note that this match statement matches an AstNode token, but the 138 - // matches nested inside check the TokenVector. That's why we for example 138 + // matches nested inside check the [`Token`]s. That's why we for example 139 139 // match a FunctionIdentifier, and inside that, a RightParen. 140 140 141 141 // pi2, )2 ··· 215 215 216 216 // level 4 precedence: ^ 217 217 /// Parse [`Caret`](crate::Operator::Caret) 218 - pub fn parse_level_4(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 218 + pub fn parse_level_4(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 219 219 let (mut node, mut pos) = parse_level_5(tokens, pos)?; 220 220 loop { 221 221 let token = tokens.get(pos); ··· 237 237 238 238 // level 5 precedence: - (as in -5, but not 4-5) 239 239 /// Parse [`Negative`](Token::Negative) 240 - pub fn parse_level_5(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 240 + pub fn parse_level_5(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 241 241 // Here we parse the negative unary operator. If the current token 242 242 // is a minus, we wrap the right_node inside a Negative AstNode. 243 243 // ··· 264 264 265 265 // level 6 precedence: !, percent, units attached to values 266 266 /// Parse [`Factorial`](crate::UnaryOperator::Factorial) and [`Percent`](crate::UnaryOperator::Percent) 267 - pub fn parse_level_6(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 267 + pub fn parse_level_6(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 268 268 let (mut node, mut pos) = parse_level_7(tokens, pos)?; 269 269 loop { 270 270 let token = tokens.get(pos); ··· 300 300 /// [`Constant`](Token::Constant), 301 301 /// [`FunctionIdentifier`](Token::FunctionIdentifier), 302 302 /// [`Paren`](Token::Paren) 303 - pub fn parse_level_7(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> { 303 + pub fn parse_level_7(tokens: &Vec<Token>, pos: usize) -> Result<(AstNode, usize), String> { 304 304 let token: &Token = tokens.get(pos).ok_or(format!("Unexpected end of input at {}", pos))?; 305 305 match token { 306 306 &Token::Number(_number) => {