diff --git a/nac3standalone/demo/demo.c b/nac3standalone/demo/demo.c index ccc15cc..aea249d 100644 --- a/nac3standalone/demo/demo.c +++ b/nac3standalone/demo/demo.c @@ -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("]"); diff --git a/nac3standalone/demo/demo.rs b/nac3standalone/demo/demo.rs index c7e4452..9b14a29 100644 --- a/nac3standalone/demo/demo.rs +++ b/nac3standalone/demo/demo.rs @@ -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) { +pub extern "C" fn output_str(x: &cslice::CSlice, 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, 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) { 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; } diff --git a/nac3standalone/demo/interpret_demo.py b/nac3standalone/demo/interpret_demo.py index 71bb426..3661888 100755 --- a/nac3standalone/demo/interpret_demo.py +++ b/nac3standalone/demo/interpret_demo.py @@ -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 diff --git a/nac3standalone/demo/src/classes.py b/nac3standalone/demo/src/classes.py index fd9b361..2f54ce5 100644 --- a/nac3standalone/demo/src/classes.py +++ b/nac3standalone/demo/src/classes.py @@ -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): ... diff --git a/nac3standalone/demo/src/default_param.py b/nac3standalone/demo/src/default_param.py index a55df21..4e292e2 100644 --- a/nac3standalone/demo/src/default_param.py +++ b/nac3standalone/demo/src/default_param.py @@ -1,5 +1,5 @@ @extern -def output_int32(x: int32): +def output_int32(x: int32, newline: bool=True): ... def f1(a: int32 = 4): diff --git a/nac3standalone/demo/src/demo_test.py b/nac3standalone/demo/src/demo_test.py new file mode 100644 index 0000000..02f41fd --- /dev/null +++ b/nac3standalone/demo/src/demo_test.py @@ -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 \ No newline at end of file diff --git a/nac3standalone/demo/src/inheritance.py b/nac3standalone/demo/src/inheritance.py index d280e3a..be37183 100644 --- a/nac3standalone/demo/src/inheritance.py +++ b/nac3standalone/demo/src/inheritance.py @@ -1,7 +1,7 @@ from __future__ import annotations @extern -def output_int32(x: int32): +def output_int32(x: int32, newline: bool=True): ... class A: diff --git a/nac3standalone/demo/src/len.py b/nac3standalone/demo/src/len.py index 5072420..c9b0a61 100644 --- a/nac3standalone/demo/src/len.py +++ b/nac3standalone/demo/src/len.py @@ -1,5 +1,5 @@ @extern -def output_int32(x: int32): +def output_int32(x: int32, newline: bool=True): ... diff --git a/nac3standalone/demo/src/lists.py b/nac3standalone/demo/src/lists.py index f05701b..e752fb7 100644 --- a/nac3standalone/demo/src/lists.py +++ b/nac3standalone/demo/src/lists.py @@ -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: diff --git a/nac3standalone/demo/src/loop.py b/nac3standalone/demo/src/loop.py index 8f383fe..940679f 100644 --- a/nac3standalone/demo/src/loop.py +++ b/nac3standalone/demo/src/loop.py @@ -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: diff --git a/nac3standalone/demo/src/loop_decr.py b/nac3standalone/demo/src/loop_decr.py index 59afb1b..f180e6e 100644 --- a/nac3standalone/demo/src/loop_decr.py +++ b/nac3standalone/demo/src/loop_decr.py @@ -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: diff --git a/nac3standalone/demo/src/loop_iterable.py b/nac3standalone/demo/src/loop_iterable.py index d97ab30..0218990 100644 --- a/nac3standalone/demo/src/loop_iterable.py +++ b/nac3standalone/demo/src/loop_iterable.py @@ -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: diff --git a/nac3standalone/demo/src/loop_mutate_var.py b/nac3standalone/demo/src/loop_mutate_var.py index 3ac5c2c..b884aa0 100644 --- a/nac3standalone/demo/src/loop_mutate_var.py +++ b/nac3standalone/demo/src/loop_mutate_var.py @@ -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: diff --git a/nac3standalone/demo/src/min_max_abs.py b/nac3standalone/demo/src/min_max_abs.py index 15153fd..de3f0e2 100644 --- a/nac3standalone/demo/src/min_max_abs.py +++ b/nac3standalone/demo/src/min_max_abs.py @@ -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): ... diff --git a/nac3standalone/demo/src/operators.py b/nac3standalone/demo/src/operators.py index 0470b96..3381890 100644 --- a/nac3standalone/demo/src/operators.py +++ b/nac3standalone/demo/src/operators.py @@ -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: diff --git a/nac3standalone/demo/src/option.py b/nac3standalone/demo/src/option.py index 8cc0d5a..099496e 100644 --- a/nac3standalone/demo/src/option.py +++ b/nac3standalone/demo/src/option.py @@ -1,5 +1,5 @@ @extern -def output_int32(x: int32): +def output_int32(x: int32, newline: bool=True): ... class A: diff --git a/nac3standalone/demo/src/pow.py b/nac3standalone/demo/src/pow.py index d17abd7..0dc868d 100644 --- a/nac3standalone/demo/src/pow.py +++ b/nac3standalone/demo/src/pow.py @@ -1,5 +1,5 @@ @extern -def output_float64(f: float): +def output_float64(f: float, newline: bool=True): ... diff --git a/nac3standalone/demo/src/recursive_type.py b/nac3standalone/demo/src/recursive_type.py index 1444f5a..325fdff 100644 --- a/nac3standalone/demo/src/recursive_type.py +++ b/nac3standalone/demo/src/recursive_type.py @@ -1,7 +1,7 @@ from __future__ import annotations @extern -def output_int32(a: int32): +def output_int32(a: int32, newline: bool=True): ... class A: diff --git a/nac3standalone/demo/src/tuple.py b/nac3standalone/demo/src/tuple.py index f71d3f9..f2fa68a 100644 --- a/nac3standalone/demo/src/tuple.py +++ b/nac3standalone/demo/src/tuple.py @@ -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: diff --git a/nac3standalone/demo/src/typevar.py b/nac3standalone/demo/src/typevar.py index 9b06221..c698cfb 100644 --- a/nac3standalone/demo/src/typevar.py +++ b/nac3standalone/demo/src/typevar.py @@ -1,5 +1,5 @@ @extern -def output_int32(x: int32): +def output_int32(x: int32, newline: bool=True): ... class A: