Skip to content

Commit

Permalink
support ** operator (FuelLabs#3141)
Browse files Browse the repository at this point in the history
closes FuelLabs#3104 

adds `**` to the list of known tokens in the AST, parse and format the
Pow expression as well.
  • Loading branch information
eightfilms authored Oct 26, 2022
1 parent 203b590 commit a9b8bb0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
6 changes: 6 additions & 0 deletions sway-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ pub enum Expr {
forward_slash_token: ForwardSlashToken,
rhs: Box<Expr>,
},
Pow {
lhs: Box<Expr>,
double_star_token: DoubleStarToken,
rhs: Box<Expr>,
},
Modulo {
lhs: Box<Expr>,
percent_token: PercentToken,
Expand Down Expand Up @@ -223,6 +228,7 @@ impl Spanned for Expr {
Expr::Ref { ref_token, expr } => Span::join(ref_token.span(), expr.span()),
Expr::Deref { deref_token, expr } => Span::join(deref_token.span(), expr.span()),
Expr::Not { bang_token, expr } => Span::join(bang_token.span(), expr.span()),
Expr::Pow { lhs, rhs, .. } => Span::join(lhs.span(), rhs.span()),
Expr::Mul { lhs, rhs, .. } => Span::join(lhs.span(), rhs.span()),
Expr::Div { lhs, rhs, .. } => Span::join(lhs.span(), rhs.span()),
Expr::Modulo { lhs, rhs, .. } => Span::join(lhs.span(), rhs.span()),
Expand Down
1 change: 1 addition & 0 deletions sway-ast/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ define_token!(
[Colon]
);
define_token!(StarToken, "an asterisk (*)", [Star], [Equals]);
define_token!(DoubleStarToken, "`**`", [Star, Star], []);
define_token!(CommaToken, "a comma", [Comma], []);
define_token!(ColonToken, "a colon", [Colon], [Colon]);
define_token!(
Expand Down
9 changes: 9 additions & 0 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,15 @@ fn expr_to_expression(handler: &Handler, expr: Expr) -> Result<Expression, Error
let expr = expr_to_expression(handler, *expr)?;
op_call("not", bang_token.span(), span, &[expr])?
}
Expr::Pow {
lhs,
double_star_token,
rhs,
} => {
let lhs = expr_to_expression(handler, *lhs)?;
let rhs = expr_to_expression(handler, *rhs)?;
op_call("pow", double_star_token.span(), span, &vec![lhs, rhs])?
}
Expr::Mul {
lhs,
star_token,
Expand Down
8 changes: 7 additions & 1 deletion sway-parse/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,13 @@ fn parse_mul(parser: &mut Parser, ctx: ParseExprCtx) -> ParseResult<Expr> {
return Ok(expr);
}
loop {
expr = if let Some((star_token, rhs)) = parse_op_rhs(parser, ctx, parse_unary_op)? {
expr = if let Some((double_star_token, rhs)) = parse_op_rhs(parser, ctx, parse_unary_op)? {
Expr::Pow {
lhs: Box::new(expr),
double_star_token,
rhs,
}
} else if let Some((star_token, rhs)) = parse_op_rhs(parser, ctx, parse_unary_op)? {
Expr::Mul {
lhs: Box::new(expr),
star_token,
Expand Down
1 change: 1 addition & 0 deletions sway-parse/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ token_impls! {
ForwardSlashToken,
DoubleColonToken,
StarToken,
DoubleStarToken,
CommaToken,
ColonToken,
RightArrowToken,
Expand Down
19 changes: 19 additions & 0 deletions swayfmt/src/utils/language/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ impl Format for Expr {
write!(formatted_code, "{}", bang_token.span().as_str())?;
expr.format(formatted_code, formatter)?;
}
Self::Pow {
lhs,
double_star_token,
rhs,
} => {
lhs.format(formatted_code, formatter)?;
write!(formatted_code, " {} ", double_star_token.span().as_str())?;
rhs.format(formatted_code, formatter)?;
}
Self::Mul {
lhs,
star_token,
Expand Down Expand Up @@ -839,6 +848,16 @@ fn expr_leaf_spans(expr: &Expr) -> Vec<ByteSpan> {
collected_spans.append(&mut expr.leaf_spans());
collected_spans
}
Expr::Pow {
lhs,
double_star_token,
rhs,
} => {
let mut collected_spans = lhs.leaf_spans();
collected_spans.push(ByteSpan::from(double_star_token.span()));
collected_spans.append(&mut rhs.leaf_spans());
collected_spans
}
Expr::Mul {
lhs,
star_token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,114 @@ use std::revert::revert;
use core::num::*;

fn main() -> bool {
let max_u64 = ~u64::max();
let max_u32 = ~u32::max();
let max_u16 = ~u16::max();
let max_u8 = ~u8::max();

// u64
assert(2.pow(2) == 4);
assert(2 ** 2 == 4);

assert(2.pow(3) == 8);
assert(2 ** 3 == 8);

assert(42.pow(2) == 1764);
assert(42 ** 2 == 1764);

assert(42.pow(3) == 74088);
assert(42 ** 3 == 74088);

assert(100.pow(5) == 10000000000);
assert(100 ** 5 == 10000000000);

assert(100.pow(8) == 10000000000000000);
assert(100 ** 8 == 10000000000000000);

assert(100.pow(9) == 1000000000000000000);
assert(100 ** 9 == 1000000000000000000);

assert(2.pow(0) == 1);
assert(2 ** 0 == 1);

assert(0.pow(1) == 0);
assert(0 ** 1 == 0);

assert(0.pow(2) == 0);
assert(0 ** 2 == 0);

// u32
assert(2u32.pow(2u32) == 4u32);
assert(2u32 ** 2u32 == 4u32);

assert(2u32.pow(3u32) == 8u32);
assert(2u32 ** 3u32 == 8u32);

assert(42u32.pow(2u32) == 1764u32);
assert(42u32 ** 2u32 == 1764u32);

assert(100u32.pow(4u32) == 100000000u32);
assert(100u32 ** 4u32 == 100000000u32);

assert(2u32.pow(0u32) == 1u32);
assert(2u32 ** 0u32 == 1u32);

assert(0u32.pow(1u32) == 0u32);
assert(0u32 ** 1u32 == 0u32);

assert(0u32.pow(2u32) == 0u32);
assert(0u32 ** 2u32 == 0u32);

// u16
assert(2u16.pow(2u16) == 4u16);
assert(2u16 ** 2u16 == 4u16);

assert(2u16.pow(3u16) == 8u16);
assert(2u16 ** 3u16 == 8u16);

assert(42u16.pow(2u16) == 1764u16);
assert(42u16 ** 2u16 == 1764u16);

assert(20u16.pow(3u16) == 8000u16);
assert(20u16 ** 3u16 == 8000u16);

assert(15u16.pow(4u16) == 50625u16);
assert(15u16 ** 4u16 == 50625u16);

assert(2u16.pow(0u16) == 1u16);
assert(2u16 ** 0u16 == 1u16);

assert(0u16.pow(1u16) == 0u16);
assert(0u16 ** 1u16 == 0u16);

assert(0u16.pow(2u16) == 0u16);
assert(0u16 ** 2u16 == 0u16);

// u8
assert(2u8.pow(2u8) == 4u8);
assert(2u8 ** 2u8 == 4u8);

assert(2u8.pow(3u8) == 8u8);
assert(2u8 ** 3u8 == 8u8);

assert(4u8.pow(3u8) == 64u8);
assert(4u8 ** 3u8 == 64u8);

assert(3u8.pow(4u8) == 81u8);
assert(3u8 ** 4u8 == 81u8);

assert(10u8.pow(2u8) == 100u8);
assert(10u8 ** 2u8 == 100u8);

assert(5u8.pow(3u8) == 125u8);
assert(5u8 ** 3u8 == 125u8);

assert(3u8.pow(5u8) == 243u8);
assert(3u8 ** 5u8 == 243u8);

assert(2u8.pow(0u8) == 1u8);
assert(2u8 ** 0u8 == 1u8);

assert(0u8.pow(1u8) == 0u8);
assert(0u8 ** 1u8 == 0u8);

assert(0u8.pow(2u8) == 0u8);
assert(0u8 ** 2u8 == 0u8);

true
}

0 comments on commit a9b8bb0

Please sign in to comment.