mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-24 11:41:28 +02:00
Added 'Copy' to TokenKind, and added a missing line related to the sum function
This commit is contained in:
parent
b3720ed6dc
commit
c92512d79d
@ -278,6 +278,8 @@ fn eval_fn_call_expr(
|
|||||||
sum += eval_expr(context, &expressions[2], "")?.value;
|
sum += eval_expr(context, &expressions[2], "")?.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context.sum_n_value = None;
|
||||||
|
|
||||||
return Ok(KalkNum::new(sum, unit.into()));
|
return Ok(KalkNum::new(sum, unit.into()));
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -2,7 +2,7 @@ use std::iter::Peekable;
|
|||||||
use std::str;
|
use std::str;
|
||||||
use std::str::Chars;
|
use std::str::Chars;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq, Copy)]
|
||||||
pub enum TokenKind {
|
pub enum TokenKind {
|
||||||
Unknown,
|
Unknown,
|
||||||
Literal,
|
Literal,
|
||||||
|
@ -215,7 +215,7 @@ fn parse_to(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
let left = parse_sum(context)?;
|
let left = parse_sum(context)?;
|
||||||
|
|
||||||
if match_token(context, TokenKind::ToKeyword) {
|
if match_token(context, TokenKind::ToKeyword) {
|
||||||
let op = advance(context).kind.clone();
|
let op = advance(context).kind;
|
||||||
let right = Expr::Var(advance(context).value.clone()); // Parse this as a variable for now.
|
let right = Expr::Var(advance(context).value.clone()); // Parse this as a variable for now.
|
||||||
|
|
||||||
return Ok(Expr::Binary(Box::new(left), op, Box::new(right)));
|
return Ok(Expr::Binary(Box::new(left), op, Box::new(right)));
|
||||||
@ -228,7 +228,7 @@ fn parse_sum(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
let mut left = parse_factor(context)?;
|
let mut left = parse_factor(context)?;
|
||||||
|
|
||||||
while match_token(context, TokenKind::Plus) || match_token(context, TokenKind::Minus) {
|
while match_token(context, TokenKind::Plus) || match_token(context, TokenKind::Minus) {
|
||||||
let op = peek(context).kind.clone();
|
let op = peek(context).kind;
|
||||||
advance(context);
|
advance(context);
|
||||||
let right = parse_factor(context)?;
|
let right = parse_factor(context)?;
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ fn parse_factor(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
// If the token is an identifier, assume it's multiplication. Eg. 3y
|
// If the token is an identifier, assume it's multiplication. Eg. 3y
|
||||||
let op = match peek(context).kind {
|
let op = match peek(context).kind {
|
||||||
TokenKind::Identifier | TokenKind::Literal => TokenKind::Star,
|
TokenKind::Identifier | TokenKind::Literal => TokenKind::Star,
|
||||||
_ => advance(context).kind.clone(),
|
_ => advance(context).kind,
|
||||||
};
|
};
|
||||||
|
|
||||||
let parse_next = parse_unit(context);
|
let parse_next = parse_unit(context);
|
||||||
@ -298,14 +298,14 @@ fn parse_unit(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
|
|
||||||
fn parse_unary(context: &mut Context) -> Result<Expr, CalcError> {
|
fn parse_unary(context: &mut Context) -> Result<Expr, CalcError> {
|
||||||
if match_token(context, TokenKind::Minus) {
|
if match_token(context, TokenKind::Minus) {
|
||||||
let op = advance(context).kind.clone();
|
let op = advance(context).kind;
|
||||||
let expr = Box::new(parse_unary(context)?);
|
let expr = Box::new(parse_unary(context)?);
|
||||||
return Ok(Expr::Unary(op, expr));
|
return Ok(Expr::Unary(op, expr));
|
||||||
}
|
}
|
||||||
|
|
||||||
let expr = parse_exponent(context)?;
|
let expr = parse_exponent(context)?;
|
||||||
if match_token(context, TokenKind::Percent) {
|
if match_token(context, TokenKind::Percent) {
|
||||||
Ok(Expr::Unary(advance(context).kind.clone(), Box::new(expr)))
|
Ok(Expr::Unary(advance(context).kind, Box::new(expr)))
|
||||||
} else {
|
} else {
|
||||||
Ok(expr)
|
Ok(expr)
|
||||||
}
|
}
|
||||||
@ -315,7 +315,7 @@ fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
let left = parse_factorial(context)?;
|
let left = parse_factorial(context)?;
|
||||||
|
|
||||||
if match_token(context, TokenKind::Power) {
|
if match_token(context, TokenKind::Power) {
|
||||||
let op = advance(context).kind.clone();
|
let op = advance(context).kind;
|
||||||
let right = Box::new(parse_exponent(context)?);
|
let right = Box::new(parse_exponent(context)?);
|
||||||
return Ok(Expr::Binary(Box::new(left), op, right));
|
return Ok(Expr::Binary(Box::new(left), op, right));
|
||||||
}
|
}
|
||||||
@ -447,7 +447,7 @@ fn advance(context: &mut Context) -> &Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn consume(context: &mut Context, kind: TokenKind) -> Result<&Token, CalcError> {
|
fn consume(context: &mut Context, kind: TokenKind) -> Result<&Token, CalcError> {
|
||||||
if match_token(context, kind.clone()) {
|
if match_token(context, kind) {
|
||||||
return Ok(advance(context));
|
return Ok(advance(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user