Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPDATE: Adds implicit intersection operator! #264

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions base/src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::{LAST_COLUMN, LAST_ROW};
use crate::expressions::parser::stringify::DisplaceData;
use crate::expressions::parser::stringify::{to_string, to_string_displaced, DisplaceData};
use crate::expressions::types::CellReferenceRC;
use crate::model::Model;

// NOTE: There is a difference with Excel behaviour when deleting cells/rows/columns
Expand All @@ -8,16 +9,45 @@ use crate::model::Model;
// I feel this is unimportant for now.

impl Model {
fn shift_cell_formula(
&mut self,
sheet: u32,
row: i32,
column: i32,
displace_data: &DisplaceData,
) -> Result<(), String> {
if let Some(f) = self
.workbook
.worksheet(sheet)?
.cell(row, column)
.and_then(|c| c.get_formula())
{
let node = &self.parsed_formulas[sheet as usize][f as usize].clone();
let cell_reference = CellReferenceRC {
sheet: self.workbook.worksheets[sheet as usize].get_name(),
row,
column,
};
// FIXME: This is not a very performant way if the formula has changed :S.
let formula = to_string(node, &cell_reference);
let formula_displaced = to_string_displaced(node, &cell_reference, displace_data);
if formula != formula_displaced {
self.update_cell_with_formula(sheet, row, column, format!("={formula_displaced}"))?;
}
}
Ok(())
}
/// This function iterates over all cells in the model and shifts their formulas according to the displacement data.
///
/// # Arguments
///
/// * `displace_data` - A reference to `DisplaceData` describing the displacement's direction and magnitude.
fn displace_cells(&mut self, displace_data: &DisplaceData) {
fn displace_cells(&mut self, displace_data: &DisplaceData) -> Result<(), String> {
let cells = self.get_all_cells();
for cell in cells {
self.shift_cell_formula(cell.index, cell.row, cell.column, displace_data);
self.shift_cell_formula(cell.index, cell.row, cell.column, displace_data)?;
}
Ok(())
}

/// Retrieves the column indices for a specific row in a given sheet, sorted in ascending or descending order.
Expand Down Expand Up @@ -134,7 +164,7 @@ impl Model {
column,
delta: column_count,
}),
);
)?;

Ok(())
}
Expand Down Expand Up @@ -187,7 +217,7 @@ impl Model {
column,
delta: -column_count,
}),
);
)?;
let worksheet = &mut self.workbook.worksheet_mut(sheet)?;

// deletes all the column styles
Expand Down Expand Up @@ -311,7 +341,7 @@ impl Model {
row,
delta: row_count,
}),
);
)?;

Ok(())
}
Expand Down Expand Up @@ -372,7 +402,7 @@ impl Model {
row,
delta: -row_count,
}),
);
)?;
Ok(())
}

Expand All @@ -393,14 +423,14 @@ impl Model {
sheet: u32,
column: i32,
delta: i32,
) -> Result<(), &'static str> {
) -> Result<(), String> {
// Check boundaries
let target_column = column + delta;
if !(1..=LAST_COLUMN).contains(&target_column) {
return Err("Target column out of boundaries");
return Err("Target column out of boundaries".to_string());
}
if !(1..=LAST_COLUMN).contains(&column) {
return Err("Initial column out of boundaries");
return Err("Initial column out of boundaries".to_string());
}

// TODO: Add the actual displacement of data and styles
Expand All @@ -412,7 +442,7 @@ impl Model {
column,
delta,
}),
);
)?;

Ok(())
}
Expand Down
55 changes: 15 additions & 40 deletions base/src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
calc_result::{CalcResult, Range},
expressions::{parser::Node, token::Error, types::CellReferenceIndex},
implicit_intersection::implicit_intersection,
model::Model,
};

Expand Down Expand Up @@ -39,19 +38,11 @@ impl Model {
}
CalcResult::EmptyCell | CalcResult::EmptyArg => Ok(0.0),
error @ CalcResult::Error { .. } => Err(error),
CalcResult::Range { left, right } => {
match implicit_intersection(&cell, &Range { left, right }) {
Some(cell_reference) => {
let result = self.evaluate_cell(cell_reference);
self.cast_to_number(result, cell_reference)
}
None => Err(CalcResult::Error {
error: Error::VALUE,
origin: cell,
message: "Invalid reference (number)".to_string(),
}),
}
}
CalcResult::Range { .. } => Err(CalcResult::Error {
error: Error::NIMPL,
origin: cell,
message: "Arrays not supported yet".to_string(),
}),
}
}

Expand Down Expand Up @@ -99,19 +90,11 @@ impl Model {
}
CalcResult::EmptyCell | CalcResult::EmptyArg => Ok("".to_string()),
error @ CalcResult::Error { .. } => Err(error),
CalcResult::Range { left, right } => {
match implicit_intersection(&cell, &Range { left, right }) {
Some(cell_reference) => {
let result = self.evaluate_cell(cell_reference);
self.cast_to_string(result, cell_reference)
}
None => Err(CalcResult::Error {
error: Error::VALUE,
origin: cell,
message: "Invalid reference (string)".to_string(),
}),
}
}
CalcResult::Range { .. } => Err(CalcResult::Error {
error: Error::NIMPL,
origin: cell,
message: "Arrays not supported yet".to_string(),
}),
}
}

Expand Down Expand Up @@ -151,19 +134,11 @@ impl Model {
CalcResult::Boolean(b) => Ok(b),
CalcResult::EmptyCell | CalcResult::EmptyArg => Ok(false),
error @ CalcResult::Error { .. } => Err(error),
CalcResult::Range { left, right } => {
match implicit_intersection(&cell, &Range { left, right }) {
Some(cell_reference) => {
let result = self.evaluate_cell(cell_reference);
self.cast_to_bool(result, cell_reference)
}
None => Err(CalcResult::Error {
error: Error::VALUE,
origin: cell,
message: "Invalid reference (bool)".to_string(),
}),
}
}
CalcResult::Range { .. } => Err(CalcResult::Error {
error: Error::NIMPL,
origin: cell,
message: "Arrays not supported yet".to_string(),
}),
}
}

Expand Down
138 changes: 0 additions & 138 deletions base/src/diffs.rs

This file was deleted.

1 change: 1 addition & 0 deletions base/src/expressions/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Lexer {
']' => TokenType::RightBracket,
':' => TokenType::Colon,
';' => TokenType::Semicolon,
'@' => TokenType::At,
',' => {
if self.locale.numbers.symbols.decimal == "," {
match self.consume_number(',') {
Expand Down
1 change: 1 addition & 0 deletions base/src/expressions/lexer/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test_common;
mod test_implicit_intersection;
mod test_language;
mod test_locale;
mod test_ranges;
Expand Down
25 changes: 25 additions & 0 deletions base/src/expressions/lexer/test/test_implicit_intersection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(clippy::unwrap_used)]

use crate::expressions::{
lexer::{Lexer, LexerMode},
token::TokenType::*,
};
use crate::language::get_language;
use crate::locale::get_locale;

fn new_lexer(formula: &str) -> Lexer {
let locale = get_locale("en").unwrap();
let language = get_language("en").unwrap();
Lexer::new(formula, LexerMode::A1, locale, language)
}

#[test]
fn sum_implicit_intersection() {
let mut lx = new_lexer("sum(@A1:A3)");
assert_eq!(lx.next_token(), Ident("sum".to_string()));
assert_eq!(lx.next_token(), LeftParenthesis);
assert_eq!(lx.next_token(), At);
assert!(matches!(lx.next_token(), Range { .. }));
assert_eq!(lx.next_token(), RightParenthesis);
assert_eq!(lx.next_token(), EOF);
}
Loading