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

feat: do compile time math for range neq #469

Merged
merged 5 commits into from
Nov 24, 2024

Conversation

b1ek
Copy link
Member

@b1ek b1ek commented Sep 7, 2024

amber: 0..2

from: $(seq 0 $(echo 2 '-' 1 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//'))

to: $(seq 0 1)

(when the to is a number literal, if its a variable it will generate as before)

src/modules/expression/binop/range.rs Outdated Show resolved Hide resolved
src/modules/expression/binop/range.rs Outdated Show resolved Hide resolved
@mks-h
Copy link
Member

mks-h commented Sep 8, 2024

Other than these two nitpicks, LGTM.

@mks-h mks-h self-requested a review September 8, 2024 22:00
Copy link
Member

@KrosFire KrosFire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ph0enixKM wanna weigh in?

src/modules/expression/binop/range.rs Outdated Show resolved Hide resolved
Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the left and right branch as Expr and not narrow them down to other values. In this PR I'd just check for a number literal and if so then we can just apply the simpler translation variant

src/modules/expression/binop/range.rs Outdated Show resolved Hide resolved
@b1ek
Copy link
Member Author

b1ek commented Oct 15, 2024

@Ph0enixKM @KrosFire please re review

@Mte90 Mte90 requested review from KrosFire and Ph0enixKM October 15, 2024 08:42
src/modules/expression/binop/range.rs Outdated Show resolved Hide resolved
@@ -59,7 +60,10 @@ 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 = match &self.to.value.as_ref().unwrap() {
ExprType::Number(_) => (to.parse::<usize>().unwrap() - 1).to_string(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should be better handled. The ExprType::Number can be for instance:

  • 0.1
  • -123

In each case this unwrap will panic. I think that you should first parse left hand side and right hand side to usize without panic and also compare if a < b, then return the b-1 and otherwise fallback to the translate_computation method.

The problem with floating point numbers will come to end when we introduce the integer types. I'm finishing the divide function for fixed precision real numbers in Bash. I think that we could get rid of bc dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is out of scope for this PR, as it's part of a wider problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime, I've changed the unwrap() call to one of my favourite Rust features, unwrap_or_default().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work as well as can be expected with negative and float values:

hwalters@Ghostwheel ~/git/amber-lang (range-neq-opt) 
$ cat range.ab 
#!/usr/bin/env amber
loop num in -5..-1 { echo num }
loop num in 2.3..=5.7 { echo num }
hwalters@Ghostwheel ~/git/amber-lang (range-neq-opt) 
$ ./range.ab 
-5
-4
-3
-2
2.3
3.3
4.3
5.3

@hdwalters hdwalters self-assigned this Nov 23, 2024
@hdwalters hdwalters requested a review from Ph0enixKM November 24, 2024 08:08
@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it should try to parse and when failed, fallback. Without any unwrapping.

match (&self.to.value, to.parse::<isize>()) {
    (Some(ExprType::Number(_)), Some(value) => ...
    _ => ...
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this also return 0 without any warning, which will result in undefined behaviour?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We agreed that the current solution (i.e. your code with isize instead of usize, and some minor refactoring) was good enough for now.

@hdwalters hdwalters dismissed KrosFire’s stale review November 24, 2024 11:16

The only objection has been resolved.

@hdwalters hdwalters merged commit 0e051c0 into amber-lang:master Nov 24, 2024
1 check passed
lens0021 pushed a commit to lens0021/amber that referenced this pull request Dec 12, 2024
* feat: do compile time math for range neq

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

Co-authored-by: Maksym Hazevych <dpadar@protonmail.com>

* allow anything other than Num literal in range neq

* use usize instead of i64

* use isize instead of usize

---------

Co-authored-by: Maksym Hazevych <dpadar@protonmail.com>
Co-authored-by: Huw Walters <huw.walters@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants