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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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)

@@ -59,7 +60,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 = match &self.to.value.as_ref().unwrap() {
ExprType::Number(_) => (to.parse::<i64>().unwrap() - 1_i64).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.

Is there any specific reason to use i64 over isize?

Copy link
Member Author

Choose a reason for hiding this comment

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

nope. does it matter?

Copy link
Member

@mks-h mks-h Sep 9, 2024

Choose a reason for hiding this comment

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

Yes, a little. The isize type is adaptive to any bitness of the target architecture — it will be 32-bit on a 32-bit machine, and 64-bit on a 64-bit machine. The i64 type can be made to work on a 32-bit arch, but it will have a performance overhead.

I know we aren't really targeting 32-bit machines, but it won't hurt to keep the codebase a little bit more universal. Especially if it is this simple to do.

Copy link
Member

Choose a reason for hiding this comment

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

That's precisely the reason why I use usize instead of u64 everywhere in the Amber compiler and Heraclitus

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
Co-authored-by: Maksym Hazevych <dpadar@protonmail.com>
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?

Comment on lines +65 to +66
ExprType::VariableGet(_) => translate_computation(meta, ArithOp::Sub, Some(to), Some("1".to_string())),
_ => unreachable!("range expression must be either Number or VariableGet"),
Copy link
Member

Choose a reason for hiding this comment

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

It's not that simple, we should be able to do this kind of things:

0..(var + 10)

just instead of specifing VariableGet as the only acceptable expression other than Number, allow for any other expression. Compiler checks if it has correct Num type, so we're safe here

Copy link
Member

Choose a reason for hiding this comment

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

Exactly. @b1ek look at the line 13 where a struct is defined. We have left and right arm that are expressions:

#[derive(Debug, Clone)]
pub struct Range {
    from: Box<Expr>,
    to: Box<Expr>,
    neq: bool
}

We can't narrow the arms to variable get and number literal. This should literally expect any expression just like before we did.

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

Comment on lines +65 to +66
ExprType::VariableGet(_) => translate_computation(meta, ArithOp::Sub, Some(to), Some("1".to_string())),
_ => unreachable!("range expression must be either Number or VariableGet"),
Copy link
Member

Choose a reason for hiding this comment

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

Exactly. @b1ek look at the line 13 where a struct is defined. We have left and right arm that are expressions:

#[derive(Debug, Clone)]
pub struct Range {
    from: Box<Expr>,
    to: Box<Expr>,
    neq: bool
}

We can't narrow the arms to variable get and number literal. This should literally expect any expression just like before we did.

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.

5 participants