-
Notifications
You must be signed in to change notification settings - Fork 82
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
base: master
Are you sure you want to change the base?
Conversation
@@ -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(), |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope. does it matter?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Other than these two nitpicks, LGTM. |
Co-authored-by: Maksym Hazevych <dpadar@protonmail.com>
There was a problem hiding this 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?
ExprType::VariableGet(_) => translate_computation(meta, ArithOp::Sub, Some(to), Some("1".to_string())), | ||
_ => unreachable!("range expression must be either Number or VariableGet"), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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
ExprType::VariableGet(_) => translate_computation(meta, ArithOp::Sub, Some(to), Some("1".to_string())), | ||
_ => unreachable!("range expression must be either Number or VariableGet"), |
There was a problem hiding this comment.
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.
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)