standalone: Update demos
- Add `newline` parameter to all output_* functions - Add `output_str` for printing a string - Add demo_test.py to test interop
This commit is contained in:
parent
f1664e7158
commit
e8b7d19b47
@ -20,20 +20,41 @@
|
||||
#define usize uint16_t
|
||||
#endif
|
||||
|
||||
void output_int32(const int32_t x) {
|
||||
printf("%d\n", x);
|
||||
struct cslice {
|
||||
const void *data;
|
||||
usize len;
|
||||
};
|
||||
|
||||
void output_int32(const int32_t x, bool newline) {
|
||||
printf("%d", x);
|
||||
|
||||
if (newline) {
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
}
|
||||
|
||||
void output_int64(const int64_t x) {
|
||||
printf("%ld\n", x);
|
||||
printf("%ld", x);
|
||||
|
||||
if (newline) {
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
}
|
||||
|
||||
void output_uint32(const uint32_t x) {
|
||||
printf("%d\n", x);
|
||||
printf("%d", x);
|
||||
|
||||
if (newline) {
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
}
|
||||
|
||||
void output_uint64(const uint64_t x) {
|
||||
printf("%ld\n", x);
|
||||
printf("%ld", x);
|
||||
|
||||
if (newline) {
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
}
|
||||
|
||||
void output_asciiart(const int32_t x) {
|
||||
@ -43,20 +64,29 @@ void output_asciiart(const int32_t x) {
|
||||
} else {
|
||||
fputc(chars[x], stdout);
|
||||
}
|
||||
|
||||
if (newline) {
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
}
|
||||
|
||||
struct cslice_int32 {
|
||||
const int32_t* data;
|
||||
usize len;
|
||||
};
|
||||
void output_str(struct cslice *slice, bool newline) {
|
||||
for (usize i = 0; i < slice->len; ++i) {
|
||||
fputc(((const char *) slice->data)[i], stdout);
|
||||
}
|
||||
|
||||
void output_int32_list(struct cslice_int32* slice) {
|
||||
if (newline) {
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
}
|
||||
|
||||
void output_int32_list(struct cslice *slice) {
|
||||
fputc('[', stdout);
|
||||
for (usize i = 0; i < slice->len; ++i) {
|
||||
if (i == slice->len - 1) {
|
||||
printf("%d", slice->data[i]);
|
||||
printf("%d", ((const int32_t *) slice->data)[i]);
|
||||
} else {
|
||||
printf("%d, ", slice->data[i]);
|
||||
printf("%d, ", ((const int32_t *) slice->data)[i]);
|
||||
}
|
||||
}
|
||||
puts("]");
|
||||
|
@ -1,3 +1,7 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::process::exit;
|
||||
|
||||
mod cslice {
|
||||
// copied from https://github.com/dherman/cslice
|
||||
use std::marker::PhantomData;
|
||||
@ -19,30 +23,60 @@ mod cslice {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_int32(x: i32) {
|
||||
println!("{}", x);
|
||||
pub extern "C" fn output_int32(x: i32, newline: bool) {
|
||||
let str = format!("{x}");
|
||||
|
||||
if newline {
|
||||
println!("{str}");
|
||||
} else {
|
||||
print!("{str}");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_int64(x: i64) {
|
||||
println!("{}", x);
|
||||
pub extern "C" fn output_int64(x: i64, newline: bool) {
|
||||
let str = format!("{x}");
|
||||
|
||||
if newline {
|
||||
println!("{str}");
|
||||
} else {
|
||||
print!("{str}");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_uint32(x: u32) {
|
||||
println!("{}", x);
|
||||
pub extern "C" fn output_uint32(x: u32, newline: bool) {
|
||||
let str = format!("{x}");
|
||||
|
||||
if newline {
|
||||
println!("{str}");
|
||||
} else {
|
||||
print!("{str}");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_uint64(x: u64) {
|
||||
println!("{}", x);
|
||||
pub extern "C" fn output_uint64(x: u64, newline: bool) {
|
||||
let str = format!("{x}");
|
||||
|
||||
if newline {
|
||||
println!("{str}");
|
||||
} else {
|
||||
print!("{str}");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_float64(x: f64) {
|
||||
pub extern "C" fn output_float64(x: f64, newline: bool) {
|
||||
// debug output to preserve the digits after the decimal points
|
||||
// to match python `print` function
|
||||
println!("{:?}", x);
|
||||
let str = format!("{:?}", x);
|
||||
|
||||
if newline {
|
||||
println!("{str}");
|
||||
} else {
|
||||
print!("{str}");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@ -56,7 +90,18 @@ pub extern "C" fn output_asciiart(x: i32) {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_int32_list(x: &cslice::CSlice<i32>) {
|
||||
pub extern "C" fn output_str(x: &cslice::CSlice<u8>, newline: bool) {
|
||||
for e in x.as_ref().iter() {
|
||||
print!("{}", char::from(*e));
|
||||
}
|
||||
|
||||
if newline {
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn output_int32_list(x: &cslice::CSlice<i32>, newline: bool) {
|
||||
print!("[");
|
||||
let mut it = x.as_ref().iter().peekable();
|
||||
while let Some(e) = it.next() {
|
||||
@ -66,7 +111,11 @@ pub extern "C" fn output_int32_list(x: &cslice::CSlice<i32>) {
|
||||
print!("{}, ", e);
|
||||
}
|
||||
}
|
||||
println!("]");
|
||||
print!("]");
|
||||
|
||||
if newline {
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@ -75,10 +124,19 @@ pub extern "C" fn __nac3_personality(_state: u32, _exception_object: u32, _conte
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn __nac3_raise(_state: u32, _exception_object: u32, _context: u32) -> u32 {
|
||||
unimplemented!();
|
||||
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);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn __nac3_end_catch() {}
|
||||
|
||||
extern "C" {
|
||||
fn run() -> i32;
|
||||
}
|
||||
|
@ -48,6 +48,12 @@ def patch(module):
|
||||
else:
|
||||
sys.stdout.write(" .,-:;i+hHM$*#@ "[x])
|
||||
|
||||
def output(x, newline: bool=True):
|
||||
if newline:
|
||||
print(x)
|
||||
else:
|
||||
print(x, end="")
|
||||
|
||||
def extern(fun):
|
||||
name = fun.__name__
|
||||
if name == "output_asciiart":
|
||||
@ -58,9 +64,10 @@ def patch(module):
|
||||
"output_int32_list",
|
||||
"output_uint32",
|
||||
"output_uint64",
|
||||
"output_float64"
|
||||
"output_float64",
|
||||
"output_str",
|
||||
}:
|
||||
return print
|
||||
return output
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_int64(x: int64):
|
||||
def output_int64(x: int64, newline: bool=True):
|
||||
...
|
||||
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
def f1(a: int32 = 4):
|
||||
|
61
nac3standalone/demo/src/demo_test.py
Normal file
61
nac3standalone/demo/src/demo_test.py
Normal file
@ -0,0 +1,61 @@
|
||||
@extern
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_int64(x: int64, newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_uint32(x: uint32, newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_uint64(x: uint64, newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_int32_list(x: list[int32], newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_asciiart(x: int32):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_str(x: str, newline: bool=True):
|
||||
...
|
||||
|
||||
def test_output_int32():
|
||||
output_int32(-128)
|
||||
|
||||
def test_output_int64():
|
||||
output_int64(int64(-256))
|
||||
|
||||
def test_output_uint32():
|
||||
output_uint32(uint32(128))
|
||||
|
||||
def test_output_uint64():
|
||||
output_uint64(uint64(256))
|
||||
|
||||
def test_output_asciiart():
|
||||
for i in range(17):
|
||||
output_asciiart(i)
|
||||
output_asciiart(0)
|
||||
|
||||
def test_output_int32_list():
|
||||
output_int32_list([0, 1, 3, 5, 10])
|
||||
|
||||
def test_output_str_family():
|
||||
output_str("hello ", newline=False)
|
||||
output_str("world")
|
||||
|
||||
def run() -> int32:
|
||||
test_output_int32()
|
||||
test_output_int64()
|
||||
test_output_uint32()
|
||||
test_output_uint64()
|
||||
test_output_asciiart()
|
||||
test_output_int32_list()
|
||||
test_output_str_family()
|
||||
return 0
|
@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
class A:
|
||||
|
@ -1,5 +1,5 @@
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
@extern
|
||||
def output_int32_list(x: list[int32]):
|
||||
def output_int32_list(x: list[int32], newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
class A:
|
||||
|
@ -1,7 +1,7 @@
|
||||
# For Loop using an increasing range() expression as its iterable
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
def run() -> int32:
|
||||
|
@ -1,7 +1,7 @@
|
||||
# For Loop using a decreasing range() expression as its iterable
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
def run() -> int32:
|
||||
|
@ -1,7 +1,7 @@
|
||||
# For Loop using a list as its iterable
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
def run() -> int32:
|
||||
|
@ -1,7 +1,7 @@
|
||||
# For Loop using an range() expression as its iterable, additionally reassigning the target on each iteration
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
def run() -> int32:
|
||||
|
@ -1,17 +1,17 @@
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_uint32(x: uint32):
|
||||
def output_uint32(x: uint32, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_int64(x: int64):
|
||||
def output_int64(x: int64, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_uint64(x: uint64):
|
||||
def output_uint64(x: uint64, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_float64(x: float):
|
||||
def output_float64(x: float, newline: bool=True):
|
||||
...
|
||||
|
||||
|
||||
|
@ -1,19 +1,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_uint32(x: uint32):
|
||||
def output_uint32(x: uint32, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_int64(x: int64):
|
||||
def output_int64(x: int64, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_uint64(x: uint64):
|
||||
def output_uint64(x: uint64, newline: bool=True):
|
||||
...
|
||||
@extern
|
||||
def output_float64(x: float):
|
||||
def output_float64(x: float, newline: bool=True):
|
||||
...
|
||||
|
||||
def run() -> int32:
|
||||
|
@ -1,5 +1,5 @@
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
class A:
|
||||
|
@ -1,5 +1,5 @@
|
||||
@extern
|
||||
def output_float64(f: float):
|
||||
def output_float64(f: float, newline: bool=True):
|
||||
...
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
@extern
|
||||
def output_int32(a: int32):
|
||||
def output_int32(a: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
class A:
|
||||
|
@ -1,9 +1,9 @@
|
||||
@extern
|
||||
def output_int32_list(x: list[int32]):
|
||||
def output_int32_list(x: list[int32], newline: bool=True):
|
||||
...
|
||||
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
class A:
|
||||
|
@ -1,5 +1,5 @@
|
||||
@extern
|
||||
def output_int32(x: int32):
|
||||
def output_int32(x: int32, newline: bool=True):
|
||||
...
|
||||
|
||||
class A:
|
||||
|
Loading…
Reference in New Issue
Block a user