2023-09-21 15:30:03 +08:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::exit;
|
|
|
|
|
2022-02-21 18:27:46 +08:00
|
|
|
mod cslice {
|
|
|
|
// copied from https://github.com/dherman/cslice
|
2022-01-23 14:28:08 +08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::slice;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct CSlice<'a, T> {
|
|
|
|
base: *const T,
|
|
|
|
len: usize,
|
2022-02-21 18:27:46 +08:00
|
|
|
marker: PhantomData<&'a ()>,
|
2022-01-23 14:28:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> AsRef<[T]> for CSlice<'a, T> {
|
|
|
|
fn as_ref(&self) -> &[T] {
|
2022-02-21 18:27:46 +08:00
|
|
|
unsafe { slice::from_raw_parts(self.base, self.len) }
|
2022-01-23 14:28:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 10:51:10 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_int32(x: i32) {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_int64(x: i64) {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2022-03-08 21:54:27 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_uint32(x: u32) {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_uint64(x: u64) {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_float64(x: f64) {
|
2022-03-08 23:21:58 +08:00
|
|
|
// debug output to preserve the digits after the decimal points
|
|
|
|
// to match python `print` function
|
|
|
|
println!("{:?}", x);
|
2022-03-08 21:54:27 +08:00
|
|
|
}
|
|
|
|
|
2022-01-09 10:51:10 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_asciiart(x: i32) {
|
|
|
|
let chars = " .,-:;i+hHM$*#@ ";
|
|
|
|
if x < 0 {
|
|
|
|
println!("");
|
|
|
|
} else {
|
|
|
|
print!("{}", chars.chars().nth(x as usize).unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:30:03 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_str(x: &cslice::CSlice<u8>) {
|
|
|
|
for e in x.as_ref().iter() {
|
|
|
|
print!("{}", char::from(*e));
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:28:08 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn output_int32_list(x: &cslice::CSlice<i32>) {
|
|
|
|
print!("[");
|
|
|
|
let mut it = x.as_ref().iter().peekable();
|
2022-02-21 18:27:46 +08:00
|
|
|
while let Some(e) = it.next() {
|
2022-01-23 14:28:08 +08:00
|
|
|
if it.peek().is_none() {
|
|
|
|
print!("{}", e);
|
|
|
|
} else {
|
|
|
|
print!("{}, ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("]");
|
|
|
|
}
|
|
|
|
|
2022-02-13 11:35:00 +08:00
|
|
|
#[no_mangle]
|
2022-02-23 11:43:41 +08:00
|
|
|
pub extern "C" fn __nac3_personality(_state: u32, _exception_object: u32, _context: u32) -> u32 {
|
2022-02-13 11:35:00 +08:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2022-03-27 10:14:22 +08:00
|
|
|
#[no_mangle]
|
2023-09-21 15:30:03 +08:00
|
|
|
pub extern "C" fn __nac3_raise(state: u32, exception_object: u32, context: u32) -> u32 {
|
|
|
|
writeln!(io::stderr(),
|
|
|
|
"__nac3_raise(state: {:#010x}, exception_object: {:#010x}, context: {:#010x})",
|
|
|
|
state,
|
|
|
|
exception_object,
|
|
|
|
context
|
|
|
|
).unwrap();
|
|
|
|
exit(101);
|
2022-03-27 10:14:22 +08:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:17:29 +08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __nac3_end_catch() {}
|
|
|
|
|
2022-01-09 10:51:10 +08:00
|
|
|
extern "C" {
|
|
|
|
fn run() -> i32;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
}
|