Skip to main content

Crate cardiac

Crate cardiac 

Source
Expand description

Rust on CARDIAC.

A program uses this crate for CARDIAC’s card reader and teletype, inp() and out(), and for a few built-ins that CARDIAC does its own way. The same program builds two ways:

  • cargo run builds it for your machine. inp() reads numbers from standard input, and out() prints them.
  • cargo cardiac run builds it for CARDIAC with the rust-cardiac compiler and runs it on heartburn’s burn. inp() is the instruction INP, and out() is OUT.
use cardiac::*;

fn main() {
    let n = inp();
    for i in (0..=n).rev() {
        out(i);
    }
}

Given 5, both builds print 5 down to 0.

A CARDIAC cell holds a sign and three digits, so every value is from -999 to 999. A program whose values stay in that range prints the same numbers both ways. With overflow checks on, arithmetic that leaves the range panics on CARDIAC. With them off, a cell keeps the last three digits.

To build for CARDIAC, install the cargo subcommands with cargo install cargo-cardiac, then cargo cardiac new countdown makes a package like the one above.

The rest is at lab.strangequery.com/rust-cardiac: getting started, what Rust works on CARDIAC, the compiler’s messages, and the tools for running and debugging a program.

Each function’s documentation says what it is on CARDIAC, what it costs there and what it does with cargo run.

Structs§

Flag
A bool for a flag kept in a cell and tested more than once: the same trues and falses, and on CARDIAC one instruction less per test.

Functions§

add_times
n * c, as c added up n times. n is from 0 to 99, and the product must be from -999 to 999.
assume⚠
Tells the compiler something about a value that it can’t see for itself, such as a month always being from 1 to 12, so it can leave out checks it would otherwise need.
digit
Digit k of a value 0..=999: k = 0 is the hundreds, 1 the tens, 2 the units.
inp
Reads the next card.
load_resource
An array of N values from the file resources/NAME, beside the package’s Cargo.toml.
out
Outputs a value.
overlay
Ends a phase of a program too big for CARDIAC’s memory.
skip_cards
Reads n cards and drops them, as for _ in 0..n { inp(); } does. n is from 0 to 999.