From 2995a322bf50feb4d96182abd6927bc6e9eeae7c Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 8 Jul 2024 12:53:51 +0800 Subject: [PATCH] standalone: Rename output_str to output_strln and add output_str output_str is for outputting strings without newline, and the newly introduced output_strln now has the old behavior of ending with a newline. --- nac3standalone/demo/demo.c | 4 ++++ nac3standalone/demo/interpret_demo.py | 7 ++++++- nac3standalone/demo/src/classes.py | 4 ++-- nac3standalone/demo/src/demo_test.py | 7 ++++++- nac3standalone/demo/src/loop_try_break.py | 3 ++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/nac3standalone/demo/demo.c b/nac3standalone/demo/demo.c index e674a5f5..9fc14893 100644 --- a/nac3standalone/demo/demo.c +++ b/nac3standalone/demo/demo.c @@ -79,6 +79,10 @@ void output_str(struct cslice *slice) { for (usize i = 0; i < slice->len; ++i) { putchar(data[i]); } +} + +void output_strln(struct cslice *slice) { + output_str(slice); putchar('\n'); } diff --git a/nac3standalone/demo/interpret_demo.py b/nac3standalone/demo/interpret_demo.py index 1b68bea6..5167ae8e 100755 --- a/nac3standalone/demo/interpret_demo.py +++ b/nac3standalone/demo/interpret_demo.py @@ -107,6 +107,9 @@ def patch(module): def output_float(x): print("%f" % x) + def output_strln(x): + print(x, end='') + def dbg_stack_address(_): return 0 @@ -120,6 +123,8 @@ def patch(module): return output_asciiart elif name == "output_float64": return output_float + elif name == "output_str": + return output_strln elif name in { "output_bool", "output_int32", @@ -127,7 +132,7 @@ def patch(module): "output_int32_list", "output_uint32", "output_uint64", - "output_str", + "output_strln", }: return print elif name == "dbg_stack_address": diff --git a/nac3standalone/demo/src/classes.py b/nac3standalone/demo/src/classes.py index b00fa776..ff66064e 100644 --- a/nac3standalone/demo/src/classes.py +++ b/nac3standalone/demo/src/classes.py @@ -7,7 +7,7 @@ def output_int64(x: int64): ... @extern -def output_str(x: str): +def output_strln(x: str): ... @@ -33,7 +33,7 @@ class A: class Initless: def foo(self): - output_str("hello") + output_strln("hello") def run() -> int32: a = A(10) diff --git a/nac3standalone/demo/src/demo_test.py b/nac3standalone/demo/src/demo_test.py index bc20c879..d31aab0b 100644 --- a/nac3standalone/demo/src/demo_test.py +++ b/nac3standalone/demo/src/demo_test.py @@ -34,6 +34,10 @@ def output_asciiart(x: int32): def output_str(x: str): ... +@extern +def output_strln(x: str): + ... + def test_output_bool(): output_bool(True) output_bool(False) @@ -68,7 +72,8 @@ def test_output_int32_list(): output_int32_list([0, 1, 3, 5, 10]) def test_output_str_family(): - output_str("hello world") + output_str("hello") + output_strln(" world") def run() -> int32: test_output_bool() diff --git a/nac3standalone/demo/src/loop_try_break.py b/nac3standalone/demo/src/loop_try_break.py index 2592bacd..8b19da18 100644 --- a/nac3standalone/demo/src/loop_try_break.py +++ b/nac3standalone/demo/src/loop_try_break.py @@ -23,11 +23,12 @@ def run() -> int32: output_int32(x) output_str(" * ") output_float64(n / x) + output_str("\n") except: # Assume this is intended to catch x == 0 break else: # loop fell through without finding a factor output_int32(n) - output_str(" is a prime number") + output_str(" is a prime number\n") return 0 \ No newline at end of file