Skip to main content
ThunderLang™

Compiler

Thunder to Rust compiler

Write a program in Thunder, the human programming language, and get Rust. The same program gives the same output in all six languages Thunder compiles to.

✓ Thunder understands this program.

Type or paste your own. It converts as you type, in your browser: nothing is sent anywhere.

In Rust

// Generated by the Thunder compiler from main.thunder.
// A build artifact: change main.thunder and build again, or keep this file and edit it as Rust.
// It needs only Rust and thunder_runtime.rs (written next to it). "// thunder:N" marks the Thunder line.
#![allow(unused_mut, unused_variables, unused_assignments, unreachable_code, non_snake_case, dead_code, unused_imports, unused_parens, unused_braces, non_upper_case_globals, clippy::all)]

use thunder_runtime::{self as T, Value};

const FILE: &str = "main.thunder";
pub fn shipping_cost(weight: Value, express: Value) -> Value {  // thunder:1
    T::call(FILE, 1, Some(3), move || {
        let mut cost: Value = T::NOTHING;
        let mut weight = weight;
        let mut express = express;
        T::set_line(3);  // thunder:3
        if { T::set_line(3); T::yes_no(T::compare("<=", weight.clone(), T::int("0"))) } {  // thunder:3
            T::set_line(4); T::stop_with(T::py_str(T::text("weight must be positive")));  // thunder:4
        }
        T::set_line(5); cost = T::add(T::int("5"), T::mul(weight.clone(), T::int("2")));  // thunder:5
        T::set_line(6);  // thunder:6
        if { T::set_line(6); T::yes_no(T::yes(T::yes_no(express.clone()) == true)) } {  // thunder:6
            T::set_line(7); cost = T::mul(cost.clone(), T::dec("1.5"));  // thunder:7
        }
        T::set_line(8); return cost.clone();  // thunder:8
        T::NOTHING
    })
}

pub fn grade(score: Value) -> Value {  // thunder:10
    T::call(FILE, 10, Some(12), move || {
        let mut score = score;
        T::set_line(12);  // thunder:12
        if { T::set_line(12); T::yes_no(T::compare(">=", score.clone(), T::int("90"))) } {  // thunder:12
            T::set_line(13); return T::text("A");  // thunder:13
        } else if { T::set_line(14); T::yes_no(T::compare(">=", score.clone(), T::int("80"))) } {  // thunder:14
            T::set_line(15); return T::text("B");  // thunder:15
        } else if { T::set_line(16); T::yes_no(T::compare(">=", score.clone(), T::int("70"))) } {  // thunder:16
            T::set_line(17); return T::text("C");  // thunder:17
        }
        T::set_line(18); return T::text("F");  // thunder:18
        T::NOTHING
    })
}

/// Work out this file's kept values (before anything runs).
pub fn init() {
}

pub fn thunder_tests() -> Vec<(&'static str, fn(), i64)> {
    vec![]
}

pub fn main() {
    T::frame(FILE, 0, move || {
        T::set_line(20); T::say(T::show(shipping_cost(T::int("3"), T::yes(false))));  // thunder:20
        T::set_line(21); T::say(T::py_str(grade(T::int("85"))));  // thunder:21
    });
}

What you get

  • Rust that runs and gives the same results as every other language Thunder compiles to: exact decimals, the same errors, the same output. The same test programs check every target.
  • Generated code, not hand-written style. Values are dynamic, and the code uses a small Thunder runtime file that thunder build writes next to it. It runs, and you can keep and edit it, but it won't read like Rust a person would write.
  • Each line ends with a mark showing the Thunder line it came from, so you can trace any line back.

What compiles today

  • Thunder Core programs: functions, decisions, lists, records, either/or data, saved data, tests, and more. Thunder Core guide.
  • Some features work on some targets only (for example, HTTP endpoints on Python, TypeScript, and the Thunder engine). When a program can't be written in Rust, the converter says so instead of writing something different.

The example, step by step

Written by the Thunder compiler 2.0.0 while this page was built, not by hand.

Thunder

To shipping cost:
  Receive weight and express
  When weight is at most 0:
    Stop with "weight must be positive"
  Set cost to 5 + weight * 2
  When express is true:
    Set cost to cost * 1.5
  Give cost

To grade:
  Receive score
  When score is at least 90:
    Give "A"
  Otherwise when score is at least 80:
    Give "B"
  Otherwise when score is at least 70:
    Give "C"
  Give "F"

say shipping cost with 3 and false
say grade with 85

Rust

// Generated by the Thunder compiler from main.thunder.
// A build artifact: change main.thunder and build again, or keep this file and edit it as Rust.
// It needs only Rust and thunder_runtime.rs (written next to it). "// thunder:N" marks the Thunder line.
#![allow(unused_mut, unused_variables, unused_assignments, unreachable_code, non_snake_case, dead_code, unused_imports, unused_parens, unused_braces, non_upper_case_globals, clippy::all)]

use thunder_runtime::{self as T, Value};

const FILE: &str = "main.thunder";
pub fn shipping_cost(weight: Value, express: Value) -> Value {  // thunder:1
    T::call(FILE, 1, Some(3), move || {
        let mut cost: Value = T::NOTHING;
        let mut weight = weight;
        let mut express = express;
        T::set_line(3);  // thunder:3
        if { T::set_line(3); T::yes_no(T::compare("<=", weight.clone(), T::int("0"))) } {  // thunder:3
            T::set_line(4); T::stop_with(T::py_str(T::text("weight must be positive")));  // thunder:4
        }
        T::set_line(5); cost = T::add(T::int("5"), T::mul(weight.clone(), T::int("2")));  // thunder:5
        T::set_line(6);  // thunder:6
        if { T::set_line(6); T::yes_no(T::yes(T::yes_no(express.clone()) == true)) } {  // thunder:6
            T::set_line(7); cost = T::mul(cost.clone(), T::dec("1.5"));  // thunder:7
        }
        T::set_line(8); return cost.clone();  // thunder:8
        T::NOTHING
    })
}

pub fn grade(score: Value) -> Value {  // thunder:10
    T::call(FILE, 10, Some(12), move || {
        let mut score = score;
        T::set_line(12);  // thunder:12
        if { T::set_line(12); T::yes_no(T::compare(">=", score.clone(), T::int("90"))) } {  // thunder:12
            T::set_line(13); return T::text("A");  // thunder:13
        } else if { T::set_line(14); T::yes_no(T::compare(">=", score.clone(), T::int("80"))) } {  // thunder:14
            T::set_line(15); return T::text("B");  // thunder:15
        } else if { T::set_line(16); T::yes_no(T::compare(">=", score.clone(), T::int("70"))) } {  // thunder:16
            T::set_line(17); return T::text("C");  // thunder:17
        }
        T::set_line(18); return T::text("F");  // thunder:18
        T::NOTHING
    })
}

/// Work out this file's kept values (before anything runs).
pub fn init() {
}

pub fn thunder_tests() -> Vec<(&'static str, fn(), i64)> {
    vec![]
}

pub fn main() {
    T::frame(FILE, 0, move || {
        T::set_line(20); T::say(T::show(shipping_cost(T::int("3"), T::yes(false))));  // thunder:20
        T::set_line(21); T::say(T::py_str(grade(T::int("85"))));  // thunder:21
    });
}

On your computer

The same conversion with the thunder command (Node.js 18+). Nothing is sent anywhere, and no AI is involved.

npm install -g @skillstech/thunderlang
thunder build shipping.thunder --core --target rust   # writes generated/rust/