Skip to content

Commit

Permalink
Add some hackery for conditionals where the functions are "block-like…
Browse files Browse the repository at this point in the history
…" side-effect-only functions for Rust (#996)

* Add some hackery for conditionals where the functions are "block-like" side-effect-only functions for Rust

* Fix test suite
  • Loading branch information
dfellis authored Dec 8, 2024
1 parent 931db74 commit bc1ff34
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
run: cargo build --verbose
- if: ${{ github.ref_name == 'main' }}
name: Run tests
run: cargo test --verbose -- --include-ignored
run: cargo test --release --verbose -- --include-ignored
- if: ${{ github.ref_name != 'main' }}
name: Run tests
run: cargo test --verbose
run: cargo test --release --verbose
- name: Run native tests
run: cargo run --release -- test alan/test.ln
- name: Run js tests
Expand All @@ -37,10 +37,10 @@ jobs:
run: cargo build --verbose
- if: ${{ github.ref_name == 'main' }}
name: Run tests
run: cargo test --verbose -- --include-ignored
run: cargo test --release --verbose -- --include-ignored
- if: ${{ github.ref_name != 'main' }}
name: Run tests
run: cargo test --verbose
run: cargo test --release --verbose
- name: Run native tests
run: cargo run --release -- test alan/test.ln
- name: Run js tests
Expand All @@ -58,10 +58,10 @@ jobs:
run: cargo build --verbose
- if: ${{ github.ref_name == 'main' }}
name: Run tests
run: cargo test --verbose -- --include-ignored
run: cargo test --release --verbose -- --include-ignored
- if: ${{ github.ref_name != 'main' }}
name: Run tests
run: cargo test --verbose
run: cargo test --release --verbose
- if: always()
name: Stop web server
run: yarn stop-server
Expand All @@ -78,10 +78,10 @@ jobs:
run: cargo build --verbose
- if: ${{ github.ref_name == 'main' }}
name: Run tests
run: RUST_MIN_STACK=8388608 cargo test --verbose -- --include-ignored
run: RUST_MIN_STACK=8388608 cargo test --release --verbose -- --include-ignored
- if: ${{ github.ref_name != 'main' }}
name: Run tests
run: RUST_MIN_STACK=8388608 cargo test --verbose
run: RUST_MIN_STACK=8388608 cargo test --release --verbose
- name: Run native tests
run: cargo run --release -- test alan/test.ln
- name: Run js tests
Expand All @@ -99,10 +99,10 @@ jobs:
run: cargo build --verbose
- if: ${{ github.ref_name == 'main' }}
name: Run tests
run: RUST_MIN_STACK=8388608 cargo test --verbose -- --include-ignored
run: RUST_MIN_STACK=8388608 cargo test --release --verbose -- --include-ignored
- if: ${{ github.ref_name != 'main' }}
name: Run tests
run: RUST_MIN_STACK=8388608 cargo test --verbose
run: RUST_MIN_STACK=8388608 cargo test --release --verbose
- if: always()
name: Stop web server
run: yarn stop-server
Expand Down
16 changes: 8 additions & 8 deletions alan/src/compile/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,13 +1101,13 @@ test!(maybe => r#"
export fn main {
const maybe5 = fiver(5.5);
if(maybe5.exists,
fn = maybe5.getOr(0).print,
fn = 'what?'.print);
fn { maybe5.getOr(0).print; },
fn { 'what?'.print; });
const maybeNot5 = fiver(4.4);
if(!maybeNot5.exists,
fn = 'Correctly received nothing!'.print,
fn = 'uhhh'.print);
fn { 'Correctly received nothing!'.print; },
fn { 'uhhh'.print; });
maybe5.print;
maybeNot5.print;
Expand All @@ -1129,13 +1129,13 @@ test!(fallible => r#"
export fn main {
const oneFifth = reciprocal(5.0);
if(oneFifth.f64.exists,
fn = print(oneFifth.getOr(0.0)),
fn = print('what?'));
fn { print(oneFifth.getOr(0.0)); },
fn { print('what?'); });
const oneZeroth = reciprocal(0.0);
if(oneZeroth.Error.exists,
fn = print(oneZeroth.Error.getOr(Error('No error'))),
fn = print('uhhh'));
fn { print(oneZeroth.Error.getOr(Error('No error'))); },
fn { print('uhhh'); });
oneFifth.print;
oneZeroth.print;
Expand Down
37 changes: 37 additions & 0 deletions alan/src/lntors/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,43 @@ pub fn from_microstatement(
))
}
Microstatement::FnCall { function, args } => {
// Hackery to inline `if` calls *if* it's safe to do so.
if let FnKind::Bind(fname) = &function.kind {
if fname == "ifstatementhack" {
let res = from_microstatement(&args[0], parent_fn, scope, out, deps)?;
let conditional = res.0;
out = res.1;
deps = res.2;
let res = from_microstatement(&args[1], parent_fn, scope, out, deps)?;
let successblock = res.0.replace("|| ", "");
out = res.1;
deps = res.2;
return Ok((
format!("if {} {}", conditional, successblock).to_string(),
out,
deps,
));
} else if fname == "ifelsestatementhack" {
let res = from_microstatement(&args[0], parent_fn, scope, out, deps)?;
let conditional = res.0;
out = res.1;
deps = res.2;
let res = from_microstatement(&args[1], parent_fn, scope, out, deps)?;
let successblock = res.0.replace("|| ", "");
out = res.1;
deps = res.2;
let res = from_microstatement(&args[2], parent_fn, scope, out, deps)?;
let failblock = res.0.replace("|| ", "");
out = res.1;
deps = res.2;
return Ok((
format!("if {} {} else {}", conditional, successblock, failblock)
.to_string(),
out,
deps,
));
}
}
let mut arg_types = Vec::new();
let mut arg_type_strs = Vec::new();
for arg in args {
Expand Down
9 changes: 7 additions & 2 deletions alan/src/std/root.ln
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ fn if{T}(c: bool, t: T, f: T) = if(c, fn () = t, fn () = f);
fn if{T}(c: bool, t: () -> T) = if(c, fn () = Maybe{T}(t()), fn () = Maybe{T}());
fn{Rs} if{T} "alan_std::ifbool" <- RootBacking :: (bool, () -> T, () -> T) -> T;
fn{Js} if{T} "alan_std.ifbool" <- RootBacking :: (bool, () -> T, () -> T) -> T;
fn{Js} if(c: bool, t: () -> ()) {
if(c, t, fn { 0; });
}
fn{Rs} if "ifstatementhack" :: (bool, () -> ()) -> ();
fn{Rs} if "ifelsestatementhack" :: (bool, () -> (), () -> ()) -> ();

/// Float-related functions and function bindings
fn{Rs} add Infix{"+"} :: (f32, f32) -> f32;
Expand Down Expand Up @@ -5124,8 +5129,8 @@ fn{Test} assert{T}(groupname: string[], comparator: (T, T) -> bool, actual: T, e
"\u{1b}[0m".stdout;
let res = comparator(actual, expected);
if(res,
fn = "\u{0d}\u{1b}[32;1m✅".stdout,
fn = "\u{0d}\u{1b}[31;1m❎".stdout);
fn { "\u{0d}\u{1b}[32;1m✅".stdout; },
fn { "\u{0d}\u{1b}[31;1m❎".stdout; });
line.stdout;
"\u{1b}[0m".print;
if(!res, fn {
Expand Down

0 comments on commit bc1ff34

Please sign in to comment.