Skip to content

Commit

Permalink
feat: do compile time math for range neq (#469)
Browse files Browse the repository at this point in the history
* feat: do compile time math for range neq

* Update src/modules/expression/binop/range.rs

Co-authored-by: Maksym Hazevych <[email protected]>

* allow anything other than Num literal in range neq

* use usize instead of i64

* use isize instead of usize

---------

Co-authored-by: Maksym Hazevych <[email protected]>
Co-authored-by: Huw Walters <[email protected]>
  • Loading branch information
3 people authored Nov 24, 2024
1 parent 14f3ebc commit 0e051c0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/modules/expression/binop/range.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::ops::Sub;
use heraclitus_compiler::prelude::*;
use crate::docs::module::DocumentationModule;
use crate::{handle_binop, error_type_match};
use crate::modules::{expression::expr::Expr, types::{Type, Typed}};
use crate::modules::expression::expr::{Expr, ExprType};
use crate::modules::types::{Type, Typed};
use crate::utils::metadata::ParserMetadata;
use crate::translate::compute::{translate_computation, ArithOp};
use crate::translate::module::TranslateModule;
use crate::utils::TranslateMetadata;
use crate::{handle_binop, error_type_match};
use super::BinOp;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -59,7 +61,11 @@ impl TranslateModule for Range {
let from = self.from.translate(meta);
let to = self.to.translate(meta);
if self.neq {
let to_neq = translate_computation(meta, ArithOp::Sub, Some(to), Some("1".to_string()));
let to_neq = if let Some(ExprType::Number(_)) = &self.to.value {
to.parse::<isize>().unwrap_or_default().sub(1).to_string()
} else {
translate_computation(meta, ArithOp::Sub, Some(to), Some("1".to_string()))
};
meta.gen_subprocess(&format!("seq {} {}", from, to_neq))
} else {
meta.gen_subprocess(&format!("seq {} {}", from, to))
Expand Down

0 comments on commit 0e051c0

Please sign in to comment.