forked from M-Labs/nac3
demo: rewrite in Rust
This commit is contained in:
parent
4760851638
commit
096193f7ab
@ -1,30 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void output_int32(int x) {
|
||||
printf("%d\n", x);
|
||||
}
|
||||
|
||||
void output_int64(long x) {
|
||||
printf("%ld\n", x);
|
||||
}
|
||||
|
||||
void output_asciiart(int x) {
|
||||
static char chars[] = " .,-:;i+hHM$*#@ ";
|
||||
if(x < 0) {
|
||||
putchar('\n');
|
||||
} else {
|
||||
if(x < strlen(chars)) {
|
||||
putchar(chars[x]);
|
||||
} else {
|
||||
printf("ERROR\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern int run();
|
||||
|
||||
int main() {
|
||||
run();
|
||||
return 0;
|
||||
}
|
29
nac3standalone/demo/demo.rs
Normal file
29
nac3standalone/demo/demo.rs
Normal file
@ -0,0 +1,29 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_int32(x: i32) {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_int64(x: i64) {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn run() -> i32;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
run();
|
||||
}
|
||||
}
|
@ -9,5 +9,5 @@ fi
|
||||
|
||||
rm -f *.o
|
||||
../../target/release/nac3standalone $1
|
||||
clang -Wall -O2 -o $1.elf demo.c module.o -lm
|
||||
rustc -o $1.elf demo.rs -Clink-arg=./module.o
|
||||
./$1.elf
|
||||
|
Loading…
Reference in New Issue
Block a user