Crates.io | cxc |
lib.rs | cxc |
version | 0.3.1 |
source | src |
created_at | 2023-06-24 23:33:48.965306 |
updated_at | 2023-11-01 05:32:07.625609 |
description | A high performance scripting language |
homepage | http://amjoshuamichael.com/cxc/ |
repository | https://github.com/amjoshuamichael/cxc/ |
max_upload_size | |
id | 899197 |
size | 598,505 |
cxc is a high-performance scripting language designed for speed: both of writing code, and of running code. It is built to be used as a Rust-hosted scripting language and as such, it has full interoperability with Rust. cxc is about playing with software at a low level–getting your hands dirty without gloves, just because it's fun. Like Rust, cxc compiles down to an MIR, which can then be compiled or interpreted by a number of backends, including LLVM and Cranelift. Because of this, cxc has near-Rust performance, but it can be compiled and modified at runtime.
Put the following in your Cargo.toml
:
[dependencies]
cxc = "0.3"
The default features of the crate use cranelift as a compiler backend. Alternatively, you can activate the "backend-llvm" feature, which uses LLVM, but it does require that you have LLVM installed, and the subdirectories llvm/includes
and llvm/bin
both in your path. Both backends have full feature parity. The Cranelift backend has faster compile times, and is more portable, but the emitted code is slower. The LLVM backend is less portable because it requires that users have LLVM installed, but the emitted code is faster.
You can access the LLVM backend by putting this in your Cargo.toml
:
[dependencies]
cxc = {
version = "0.3",
default-features = false,
features = ["backend-llvm", "ffi-assertions"]
}
prime.cxc
# function that takes a 32 bit integer and returns a boolean
is_prime(num: i32); bool {
divider := 2 # declare divider (type is inferred as i32)
@ divider < num { # while divider is less than num
? num % divider == 0 { # if num is divisible by divider
; false # number is not prime, so return false
}
divider = divider + 1 # increment divider
}
; true # num is not divisible by any numbers, so return true
}
main.rs
fn main() {
let mut unit = cxc::Unit::new();
let prime_code: String =
include_str!("../README.md").lines().skip(27).take(14).collect::<Vec<_>>().join("\n");
unit.push_script(&*prime_code).unwrap();
let is_prime = unit.get_fn("is_prime").unwrap().downcast::<(i32,), bool>();
assert_eq!(unsafe { is_prime(29) }, true);
}
If any of these are deal breakers for you, you can try a different Rust scripting solution.