Skip to content

Commit

Permalink
Beginnings of standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 16, 2023
1 parent 16413ca commit bb9c0e3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sloth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ edition.workspace = true
[dependencies]
inkwell = { version = "0.2.0", features = ["llvm15-0"] }
itertools = "0.10.5"
rand = "0.8.5"
thiserror = "1.0.40"
28 changes: 28 additions & 0 deletions sloth/src/std.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use rand::Rng;

use std::ffi::CString;
use std::{thread, time};

// Random
#[no_mangle]
pub extern "C" fn rand(a: i64, b: i64) -> i64 {
let value = rand::thread_rng().gen_range(a..b);
return value;
}


// Print
#[no_mangle]
pub unsafe extern "C" fn println(s: *const c_char) {
let s = unsafe { CStr::from_ptr(s) }.to_str().unwrap();
Expand All @@ -19,3 +23,27 @@ pub unsafe extern "C" fn print(s: *const c_char) {
let s = unsafe { CStr::from_ptr(s) }.to_str().unwrap();
print("{s}");
}


// Terminal
#[no_mangle]
pub extern "C" fn termpos(x: i32, y: i32) {
print!("\x1b[{x};{y}H");
}

#[no_mangle]
pub extern "C" fn termclear() {
print!("\x1b[2J\x1b[H");
}


// Time
#[no_mangle]
pub extern "C" fn wait(t: i64) {
thread::sleep(time::Duration::from_millis(t));
}


// RP2040
#[no_mangle]

0 comments on commit bb9c0e3

Please sign in to comment.