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

FIXED BOPS #47

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions balnea/tests/format_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ comparison_test!(
"096",
Config::default()
);
comparison_test!(
bops_with_different_precedence,
"097",
short_line_plus_indent()
);

// Tidyverse styleguide examples
comparison_test!(tidyverse_commas, "tidyverse_style_guide_001");
Expand Down Expand Up @@ -434,3 +439,4 @@ comparison_test!(
Config::default()
);
comparison_test!(rle_tmc, "real_life_004", Config::default());
comparison_test!(rle_somehow_exceeds_120, "real_life_005", Config::default());
1 change: 1 addition & 0 deletions balnea/tests/test_cases/097.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
74102938751029387501283715098237 + 7014287340192783401927304123 + 74109237018273401723^710923741082374192734 + 74091827304197230497 * 0172837410293874109273 |> 309173048172039847121974
6 changes: 6 additions & 0 deletions balnea/tests/test_cases/097.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
74102938751029387501283715098237 +
7014287340192783401927304123 +
74109237018273401723^710923741082374192734 +
74091827304197230497 *
0172837410293874109273 |>
309173048172039847121974
4 changes: 1 addition & 3 deletions balnea/tests/test_cases/real_life_003.expected
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ example_module <- function(label = "example teal module", datanames = "all", tra
})

teal.widgets::verbatim_popup_srv(
id = "rcode",
verbatim_content = reactive(teal.code::get_code(data())),
title = "Example Code"
id = "rcode", verbatim_content = reactive(teal.code::get_code(data())), title = "Example Code"
)
})
},
Expand Down
25 changes: 7 additions & 18 deletions balnea/tests/test_cases/real_life_004.expected
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
tm_g_lineplot <- function(label,
dataname,
parentname = ifelse(
inherits(strata, "data_extract_spec"),
teal.transform::datanames_input(strata),
"ADSL"
inherits(strata, "data_extract_spec"), teal.transform::datanames_input(strata), "ADSL"
),
strata = teal.transform::choices_selected(
teal.transform::variable_choices(parentname, c("ARM", "ARMCD", "ACTARMCD")),
"ARM"
teal.transform::variable_choices(parentname, c("ARM", "ARMCD", "ACTARMCD")), "ARM"
),
x = teal.transform::choices_selected(
teal.transform::variable_choices(dataname, "AVISIT"),
"AVISIT",
fixed = TRUE
teal.transform::variable_choices(dataname, "AVISIT"), "AVISIT", fixed = TRUE
),
y = teal.transform::choices_selected(
teal.transform::variable_choices(dataname, c("AVAL", "BASE", "CHG", "PCHG")),
"AVAL"
teal.transform::variable_choices(dataname, c("AVAL", "BASE", "CHG", "PCHG")), "AVAL"
),
y_unit = teal.transform::choices_selected(
teal.transform::variable_choices(dataname, "AVALU"),
"AVALU",
fixed = TRUE
teal.transform::variable_choices(dataname, "AVALU"), "AVALU", fixed = TRUE
),
paramcd = teal.transform::choices_selected(
teal.transform::variable_choices(dataname, "PARAMCD"),
"PARAMCD",
fixed = TRUE
teal.transform::variable_choices(dataname, "PARAMCD"), "PARAMCD", fixed = TRUE
),
param = teal.transform::choices_selected(
teal.transform::value_choices(dataname, "PARAMCD", "PARAM"),
"ALT"
teal.transform::value_choices(dataname, "PARAMCD", "PARAM"), "ALT"
),
conf_level = teal.transform::choices_selected(c(0.95, 0.9, 0.8), 0.95, keep_order = TRUE),
interval = "mean_ci",
Expand Down
9 changes: 9 additions & 0 deletions balnea/tests/test_cases/real_life_005.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
jnj_template_g_response <- function() {
substitute(
expr = {
data %>%
tidyr::complete(treatment_var, tidyr::nesting(visit_var), fill = list(n = 0, mean = 0.0, sd = 0.0, se = 0.0)) %>%
print()
}
)
}
11 changes: 11 additions & 0 deletions balnea/tests/test_cases/real_life_005.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
jnj_template_g_response <- function() {
substitute(
expr = {
data %>%
tidyr::complete(
treatment_var, tidyr::nesting(visit_var), fill = list(n = 0, mean = 0.0, sd = 0.0, se = 0.0)
) %>%
print()
}
)
}
7 changes: 6 additions & 1 deletion spongia/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub enum Expression<'a> {
Box<Expression<'a>>,
Box<Expression<'a>>,
),
MultiBop(
Box<Expression<'a>>,
Vec<(&'a CommentedToken<'a>, Box<Expression<'a>>)>,
),
Formula(&'a CommentedToken<'a>, Box<Expression<'a>>),
Newline(&'a CommentedToken<'a>),
Whitespace(&'a [&'a CommentedToken<'a>]),
Expand Down Expand Up @@ -39,6 +43,7 @@ impl std::fmt::Display for Expression<'_> {
Expression::Bop(op, left, right) => {
f.write_fmt(format_args!("{} {} {}", left, TokensBuffer(&[op]), right))
}
Expression::MultiBop(lhs, other) => f.write_fmt(format_args!("{} {:?}", lhs, other)),
Expression::Formula(tilde, term) => write!(f, "{} {}", tilde, term),
Expression::Newline(token) => f.write_fmt(format_args!("{}", TokensBuffer(&[token]))),
Expression::Whitespace(tokens) => f.write_fmt(format_args!("{}", TokensBuffer(tokens))),
Expand Down Expand Up @@ -126,7 +131,7 @@ impl std::fmt::Display for TermExpr<'_> {
.iter()
.map(|e| format!("(expr: {})", e))
.collect::<Vec<_>>()
.join(" "),
.join("\n"),
if let Some(post_delim) = self.post_delimiters {
post_delim.to_string()
} else {
Expand Down
148 changes: 27 additions & 121 deletions spongia/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ impl ExprParser {
mut lhs: Expression<'a>,
mut tokens: Input<'a, 'b>,
) -> IResult<Input<'a, 'b>, Expression<'a>> {
trace!("ExprParser::parse: {}", TokensBuffer(tokens));
let mut lookahead = &tokens[0];
while is_binary_operator(lookahead) && precedence(lookahead) >= self.0 {
let op = lookahead;
Expand All @@ -286,14 +285,13 @@ impl ExprParser {
});
let parser = ExprParser(q);
let (new_tokens, new_rhs) = parser.parse(rhs, tokens)?;
let new_rhs = bop_to_multibop(new_rhs);
rhs = new_rhs;
tokens = new_tokens;
lookahead = &tokens[0];
}
lhs = Expression::Bop(op, Box::new(lhs), Box::new(rhs));
}
trace!("ExprParser::parse: LHS {lhs}");
trace!("ExprParser::parse: tokens left: {}", TokensBuffer(tokens));
Ok((tokens, lhs))
}
}
Expand All @@ -303,7 +301,8 @@ pub(crate) fn expr<'a, 'b: 'a>(tokens: Input<'a, 'b>) -> IResult<Input<'a, 'b>,
let (tokens, term) = unary_term(tokens)?;
if !tokens.is_empty() {
let parser = ExprParser(0);
parser.parse(term, tokens)
let (tokens_left, xpr) = parser.parse(term, tokens)?;
Ok((tokens_left, bop_to_multibop(xpr)))
} else {
Ok((tokens, term))
}
Expand All @@ -312,57 +311,46 @@ pub(crate) fn expr<'a, 'b: 'a>(tokens: Input<'a, 'b>) -> IResult<Input<'a, 'b>,
pub(crate) fn expr_with_newlines<'a, 'b: 'a>(
tokens: Input<'a, 'b>,
) -> IResult<Input<'a, 'b>, Expression<'a>> {
trace!("expr: {}", TokensBuffer(tokens));
trace!("expr_with_newlines: {}", TokensBuffer(tokens));
let (mut tokens, term) = unary_term(tokens)?;
while !tokens.is_empty() && tokens[0].token == Newline {
tokens = &tokens[1..];
}
if !tokens.is_empty() {
let parser = ExprParser(0);
parser.parse(term, tokens)
let (tokens_left, xpr) = parser.parse(term, tokens)?;
Ok((tokens_left, bop_to_multibop(xpr)))
} else {
Ok((tokens, term))
}
}

fn bop_to_multibop(bop: Expression) -> Expression {
match bop {
Expression::Bop(op, lhs, rhs) => {
let mut multibop = vec![(op, rhs)];
let original_precedence = precedence(op);
let mut lhs = lhs;
while let Expression::Bop(op, lhs_, rhs) = *lhs {
if original_precedence != precedence(op) {
lhs = Box::new(Expression::Bop(op, lhs_, rhs));
break;
}
multibop.push((op, rhs));
lhs = lhs_;
}
multibop.reverse();
Expression::MultiBop(lhs, multibop)
}
_ => bop,
}
}

#[cfg(test)]
mod tests {
use tokenizer::tokens::commented_tokens;

use super::*;
use tokenizer::Token::{self};

fn binary_op_tokens() -> Vec<Token<'static>> {
vec![
Help,
RAssign,
Tilde,
Or,
VectorizedOr,
And,
VectorizedAnd,
NotEqual,
GreaterThan,
GreaterEqual,
LowerThan,
LowerEqual,
Equal,
NotEqual,
Plus,
Minus,
Multiply,
Divide,
Colon,
Dollar,
Slot,
NsGet,
NsGetInt,
LAssign,
OldAssign,
Power,
Special("%>%"),
]
}

#[test]
fn symbol_exprs() {
Expand All @@ -380,88 +368,6 @@ mod tests {
assert_eq!(res, Expression::Literal(tokens[0]));
}

#[test]
fn expressions() {
for token in binary_op_tokens() {
let tokens_ = commented_tokens!(Literal("1"), token, Literal("1"), EOF);
let tokens: Vec<_> = tokens_.iter().collect();
let res = expr(&tokens).unwrap().1;
assert_eq!(
res,
Expression::Bop(
tokens[1],
Box::new(Expression::Literal(tokens[0])),
Box::new(Expression::Literal(tokens[2]))
)
);
}
}

#[test]
fn right_associative_bop() {
let tokens_ =
commented_tokens!(Literal("1"), Power, Literal("2"), Power, Literal("3"), EOF);
let tokens: Vec<_> = tokens_.iter().collect();
let res = expr(&tokens).unwrap().1;
assert_eq!(
res,
Expression::Bop(
tokens[1],
Box::new(Expression::Literal(tokens[0])),
Box::new(Expression::Bop(
tokens[3],
Box::new(Expression::Literal(tokens[2])),
Box::new(Expression::Literal(tokens[4]))
))
)
);
}

#[test]
fn left_associative_bop() {
let tokens_ = commented_tokens!(Literal("1"), Plus, Literal("2"), Plus, Literal("3"), EOF);
let tokens: Vec<_> = tokens_.iter().collect();
let res = expr(&tokens).unwrap().1;
assert_eq!(
res,
Expression::Bop(
tokens[3],
Box::new(Expression::Bop(
tokens[1],
Box::new(Expression::Literal(tokens[0])),
Box::new(Expression::Literal(tokens[2]))
)),
Box::new(Expression::Literal(tokens[4]))
)
);
}

#[test]
fn bop_precedence() {
let tokens_ = commented_tokens!(
Literal("1"),
Multiply,
Literal("2"),
Plus,
Literal("3"),
EOF
);
let tokens: Vec<_> = tokens_.iter().collect();
let res = expr(&tokens).unwrap().1;
assert_eq!(
res,
Expression::Bop(
tokens[3],
Box::new(Expression::Bop(
tokens[1],
Box::new(Expression::Literal(tokens[0])),
Box::new(Expression::Literal(tokens[2]))
)),
Box::new(Expression::Literal(tokens[4]))
)
)
}

#[test]
fn double_brace() {
let tokens_ = commented_tokens!(LBrace, LBrace, Literal("1"), RBrace, RBrace);
Expand Down
Loading
Loading