This commit is contained in:
2026-01-18 01:15:53 +11:00
parent 9df6b70b75
commit 169fa29f16
15 changed files with 1007 additions and 528 deletions

View File

@@ -6,6 +6,7 @@ use crate::{
evaluator::{numerics::*, utils::*},
grid::Grid,
parser::*,
tokenizer::{Token, Tokenizer},
};
use std::{collections::HashSet, f64, fmt};
@@ -49,6 +50,27 @@ pub fn evaluate(str: String, grid: Option<&Grid>) -> (Eval, HashSet<CellRef>) {
}
}
pub fn evaluate_literal(input: String) -> Eval {
let mut tokenizer = match Tokenizer::new(&input) {
Ok(t) => t,
Err(_) => {
return Eval::Literal(Literal::String(input.to_owned()));
}
};
if tokenizer.len() != 1 {
return Eval::Literal(Literal::String(input.to_owned()));
}
match tokenizer.next() {
Token::Literal(lit) => match lit {
Literal::Number(_) | Literal::String(_) => Eval::Literal(lit),
Literal::Boolean(_) => Eval::Literal(Literal::String(input.to_owned())),
},
_ => Eval::Literal(Literal::String(input.to_owned())),
}
}
fn evaluate_expr(
expr: &Expr,
precs: &mut HashSet<CellRef>,
@@ -266,7 +288,6 @@ fn eval_range(
}
}
fn eval_pos(val: &Eval) -> Result<Eval, LeadErr> {
match val {
Eval::Literal(Literal::Number(it)) => Ok(Eval::Literal(Literal::Number(*it))),

View File

@@ -5,7 +5,7 @@ use log::info;
use crate::{
cell::{Cell, CellRef},
common::{LeadErr, LeadErrCode, Literal},
evaluator::{Eval, evaluate},
evaluator::{Eval, evaluate, evaluate_literal},
};
pub struct Grid {
@@ -33,7 +33,7 @@ impl Grid {
let mut updated_cells = vec![cell_ref];
if raw_val.chars().nth(0) != Some('=') {
eval = Eval::Literal(Literal::String(raw_val.to_owned()));
eval = evaluate_literal(raw_val.to_owned());
} else {
// Evaluate raw expr and get precedents
let (res_eval, res_precs) = evaluate(raw_val[1..].to_owned(), Some(&self));

View File

@@ -3,7 +3,7 @@ use log::info;
use crate::{
cell::CellRef,
common::{LeadErr, LeadErrCode, Literal},
tokenizer::*,
tokenizer::{self, *},
};
use std::{collections::HashSet, fmt};
@@ -99,7 +99,6 @@ impl fmt::Display for Expr {
}
}
#[allow(dead_code)]
impl Expr {
pub fn pretty(&self) -> String {
// entry point for users — root printed without └──

View File

@@ -124,6 +124,10 @@ impl Tokenizer {
pub fn peek(&mut self) -> Token {
self.tokens.last().cloned().unwrap_or(Token::Eof)
}
pub fn len(&self) -> usize {
self.tokens.len()
}
}
#[cfg(test)]