Support for 6'4" syntax
This commit is contained in:
parent
a0a33d175b
commit
3c53a22cfc
@ -38,6 +38,7 @@ pub fn lex(input: &str) -> Result<TokenVector, String> {
|
|||||||
tokens.push(Token::Operator(RightParen));
|
tokens.push(Token::Operator(RightParen));
|
||||||
},
|
},
|
||||||
'π' => tokens.push(Token::Constant(Pi)),
|
'π' => tokens.push(Token::Constant(Pi)),
|
||||||
|
'\'' => tokens.push(Token::Unit(Foot)),
|
||||||
'"' | '“' | '”' | '″' => tokens.push(Token::LexerKeyword(DoubleQuotes)),
|
'"' | '“' | '”' | '″' => tokens.push(Token::LexerKeyword(DoubleQuotes)),
|
||||||
value if value.is_whitespace() => {},
|
value if value.is_whitespace() => {},
|
||||||
value if value.is_alphabetic() => {
|
value if value.is_alphabetic() => {
|
||||||
@ -459,7 +460,6 @@ pub fn lex(input: &str) -> Result<TokenVector, String> {
|
|||||||
token_index += 1;
|
token_index += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("TOKENS {:?}", tokens);
|
|
||||||
|
|
||||||
Ok(tokens)
|
Ok(tokens)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ use crate::{Token, TokenVector};
|
|||||||
use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, RightParen};
|
use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, RightParen};
|
||||||
use crate::UnaryOperator::{Percent, Factorial};
|
use crate::UnaryOperator::{Percent, Factorial};
|
||||||
use crate::TextOperator::{To, Of};
|
use crate::TextOperator::{To, Of};
|
||||||
|
use crate::units::Unit::{Foot, Inch};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct AstNode {
|
pub struct AstNode {
|
||||||
@ -67,6 +68,20 @@ fn parse_level_2(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), S
|
|||||||
node = new_node;
|
node = new_node;
|
||||||
pos = next_pos;
|
pos = next_pos;
|
||||||
},
|
},
|
||||||
|
Some(&Token::Number(_)) => {
|
||||||
|
// parse 6'4"
|
||||||
|
let (right_node, next_pos) = parse_level_3(tokens, pos)?;
|
||||||
|
if let Token::Unit(Foot) = node.token {
|
||||||
|
if let Token::Unit(Inch) = right_node.token {
|
||||||
|
let mut new_node = AstNode::new(Token::Operator(Plus));
|
||||||
|
new_node.children.push(node);
|
||||||
|
new_node.children.push(right_node);
|
||||||
|
node = new_node;
|
||||||
|
pos = next_pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok((node, pos));
|
||||||
|
},
|
||||||
_ => {
|
_ => {
|
||||||
return Ok((node, pos));
|
return Ok((node, pos));
|
||||||
},
|
},
|
||||||
@ -151,8 +166,6 @@ fn parse_level_3(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), S
|
|||||||
// 2(3), pi(3), )(3)
|
// 2(3), pi(3), )(3)
|
||||||
Some(&Token::Operator(LeftParen)) => {
|
Some(&Token::Operator(LeftParen)) => {
|
||||||
let last_token = tokens.get(pos - 1);
|
let last_token = tokens.get(pos - 1);
|
||||||
println!("{:?}", token);
|
|
||||||
println!("{:?}", last_token);
|
|
||||||
match last_token {
|
match last_token {
|
||||||
Some(&Token::Number(_)) | Some(&Token::Constant(_)) | Some(&Token::Operator(RightParen)) => {
|
Some(&Token::Number(_)) | Some(&Token::Constant(_)) | Some(&Token::Operator(RightParen)) => {
|
||||||
let (right_node, next_pos) = parse_level_4(tokens, pos)?;
|
let (right_node, next_pos) = parse_level_4(tokens, pos)?;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user