mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-03-01 06:41:15 +01:00
Fixed eg. 'xy' being parsed as 'y' inside functions
This commit is contained in:
parent
9bb053c791
commit
eaa520fa67
@ -558,14 +558,29 @@ fn parse_identifier(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
while let Some(c) = right_chars.next() {
|
while let Some(c) = right_chars.next() {
|
||||||
// If last iteration
|
// If last iteration
|
||||||
let right = if right_chars.peek().is_none() {
|
let right = if right_chars.peek().is_none() {
|
||||||
context.pos -= 1;
|
// Temporarily change the token content, so that
|
||||||
context.tokens[context.pos] = Token {
|
// the parse_exponent step will parse it as its
|
||||||
|
// new name. It will later be switched back,
|
||||||
|
// since the parser sometimes rewinds a bit,
|
||||||
|
// and may get confused by a sudden change.
|
||||||
|
let pos = context.pos - 1;
|
||||||
|
context.pos = pos;
|
||||||
|
context.tokens[pos] = Token {
|
||||||
kind: TokenKind::Identifier,
|
kind: TokenKind::Identifier,
|
||||||
value: c.to_string(),
|
value: c.to_string(),
|
||||||
span: (0, 0),
|
span: (0, 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
parse_exponent(context)?
|
let last_var = parse_exponent(context)?;
|
||||||
|
|
||||||
|
// Revert back to how it was before.
|
||||||
|
context.tokens[pos] = Token {
|
||||||
|
kind: TokenKind::Identifier,
|
||||||
|
value: identifier.full_name.to_string(),
|
||||||
|
span: (0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
last_var
|
||||||
} else {
|
} else {
|
||||||
Expr::Var(Identifier::from_full_name(&c.to_string()))
|
Expr::Var(Identifier::from_full_name(&c.to_string()))
|
||||||
};
|
};
|
||||||
@ -651,6 +666,37 @@ mod tests {
|
|||||||
assert_eq!(parse(tokens).unwrap(), Stmt::Expr(var("x")));
|
assert_eq!(parse(tokens).unwrap(), Stmt::Expr(var("x")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn test_var_multiplication() {
|
||||||
|
let mut context = Context::new();
|
||||||
|
context.symbol_table.insert(Stmt::VarDecl(
|
||||||
|
Identifier::from_full_name("x"),
|
||||||
|
literal(1f64),
|
||||||
|
));
|
||||||
|
context.symbol_table.insert(Stmt::VarDecl(
|
||||||
|
Identifier::from_full_name("y"),
|
||||||
|
literal(2f64),
|
||||||
|
));
|
||||||
|
|
||||||
|
// xy²
|
||||||
|
let tokens = vec![
|
||||||
|
token(Identifier, "xy"),
|
||||||
|
token(Power, ""),
|
||||||
|
token(Literal, "2"),
|
||||||
|
token(EOF, ""),
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
parse(tokens).unwrap(),
|
||||||
|
Stmt::Expr(binary(
|
||||||
|
var("x"),
|
||||||
|
Star,
|
||||||
|
binary(var("y"), Power, literal(2f64))
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn test_binary() {
|
fn test_binary() {
|
||||||
|
Loading…
Reference in New Issue
Block a user