mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-26 04:31:49 +02:00
Allow unary after power, eg. x^-1
This commit is contained in:
parent
33067b9a46
commit
2699ce8fee
@ -481,7 +481,7 @@ fn parse_factor(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_unit(context: &mut Context) -> Result<Expr, CalcError> {
|
fn parse_unit(context: &mut Context) -> Result<Expr, CalcError> {
|
||||||
let expr = parse_unary(context)?;
|
let expr = parse_exponent(context)?;
|
||||||
|
|
||||||
if match_token(context, TokenKind::Identifier) {
|
if match_token(context, TokenKind::Identifier) {
|
||||||
let peek = &peek(context).value.clone();
|
let peek = &peek(context).value.clone();
|
||||||
@ -496,23 +496,8 @@ fn parse_unit(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
Ok(expr)
|
Ok(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_unary(context: &mut Context) -> Result<Expr, CalcError> {
|
|
||||||
if match_token(context, TokenKind::Minus) {
|
|
||||||
let op = advance(context).kind;
|
|
||||||
let expr = Box::new(parse_unary(context)?);
|
|
||||||
return Ok(Expr::Unary(op, expr));
|
|
||||||
}
|
|
||||||
|
|
||||||
let expr = parse_exponent(context)?;
|
|
||||||
if match_token(context, TokenKind::Percent) {
|
|
||||||
Ok(Expr::Unary(advance(context).kind, Box::new(expr)))
|
|
||||||
} else {
|
|
||||||
Ok(expr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
|
fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
|
||||||
let left = parse_indexer(context)?;
|
let left = parse_unary(context)?;
|
||||||
|
|
||||||
if match_token(context, TokenKind::Power) {
|
if match_token(context, TokenKind::Power) {
|
||||||
let op = advance(context).kind;
|
let op = advance(context).kind;
|
||||||
@ -523,6 +508,21 @@ fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
Ok(left)
|
Ok(left)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_unary(context: &mut Context) -> Result<Expr, CalcError> {
|
||||||
|
if match_token(context, TokenKind::Minus) {
|
||||||
|
let op = advance(context).kind;
|
||||||
|
let expr = Box::new(parse_unary(context)?);
|
||||||
|
return Ok(Expr::Unary(op, expr));
|
||||||
|
}
|
||||||
|
|
||||||
|
let expr = parse_indexer(context)?;
|
||||||
|
if match_token(context, TokenKind::Percent) {
|
||||||
|
Ok(Expr::Unary(advance(context).kind, Box::new(expr)))
|
||||||
|
} else {
|
||||||
|
Ok(expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_indexer(context: &mut Context) -> Result<Expr, CalcError> {
|
fn parse_indexer(context: &mut Context) -> Result<Expr, CalcError> {
|
||||||
let left = parse_factorial(context)?;
|
let left = parse_factorial(context)?;
|
||||||
|
|
||||||
@ -872,6 +872,22 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn test_pow_unary() {
|
||||||
|
let tokens = vec![
|
||||||
|
token(Literal, "10"),
|
||||||
|
token(Power, ""),
|
||||||
|
token(Minus, ""),
|
||||||
|
token(Literal, "1"),
|
||||||
|
token(Eof, ""),
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
parse(tokens).unwrap(),
|
||||||
|
Stmt::Expr(binary(literal(10f64), Power, unary(Minus, literal(1f64)))),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn test_percent() {
|
fn test_percent() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user