forked from M-Labs/nac3
1
0
Fork 0

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.
This commit is contained in:
David Mak 2024-07-08 12:53:51 +08:00 committed by sb10q
parent 76defac462
commit 9238a5e86e
5 changed files with 20 additions and 5 deletions

View File

@ -79,6 +79,10 @@ void output_str(struct cslice *slice) {
for (usize i = 0; i < slice->len; ++i) { for (usize i = 0; i < slice->len; ++i) {
putchar(data[i]); putchar(data[i]);
} }
}
void output_strln(struct cslice *slice) {
output_str(slice);
putchar('\n'); putchar('\n');
} }

View File

@ -107,6 +107,9 @@ def patch(module):
def output_float(x): def output_float(x):
print("%f" % x) print("%f" % x)
def output_strln(x):
print(x, end='')
def dbg_stack_address(_): def dbg_stack_address(_):
return 0 return 0
@ -120,6 +123,8 @@ def patch(module):
return output_asciiart return output_asciiart
elif name == "output_float64": elif name == "output_float64":
return output_float return output_float
elif name == "output_str":
return output_strln
elif name in { elif name in {
"output_bool", "output_bool",
"output_int32", "output_int32",
@ -127,7 +132,7 @@ def patch(module):
"output_int32_list", "output_int32_list",
"output_uint32", "output_uint32",
"output_uint64", "output_uint64",
"output_str", "output_strln",
}: }:
return print return print
elif name == "dbg_stack_address": elif name == "dbg_stack_address":

View File

@ -7,7 +7,7 @@ def output_int64(x: int64):
... ...
@extern @extern
def output_str(x: str): def output_strln(x: str):
... ...
@ -33,7 +33,7 @@ class A:
class Initless: class Initless:
def foo(self): def foo(self):
output_str("hello") output_strln("hello")
def run() -> int32: def run() -> int32:
a = A(10) a = A(10)

View File

@ -34,6 +34,10 @@ def output_asciiart(x: int32):
def output_str(x: str): def output_str(x: str):
... ...
@extern
def output_strln(x: str):
...
def test_output_bool(): def test_output_bool():
output_bool(True) output_bool(True)
output_bool(False) output_bool(False)
@ -68,7 +72,8 @@ def test_output_int32_list():
output_int32_list([0, 1, 3, 5, 10]) output_int32_list([0, 1, 3, 5, 10])
def test_output_str_family(): def test_output_str_family():
output_str("hello world") output_str("hello")
output_strln(" world")
def run() -> int32: def run() -> int32:
test_output_bool() test_output_bool()

View File

@ -23,11 +23,12 @@ def run() -> int32:
output_int32(x) output_int32(x)
output_str(" * ") output_str(" * ")
output_float64(n / x) output_float64(n / x)
output_str("\n")
except: # Assume this is intended to catch x == 0 except: # Assume this is intended to catch x == 0
break break
else: else:
# loop fell through without finding a factor # loop fell through without finding a factor
output_int32(n) output_int32(n)
output_str(" is a prime number") output_str(" is a prime number\n")
return 0 return 0