Make range a type so that it can be used as parameters #452

Merged
sb10q merged 3 commits from enhance/range-type into master 2024-07-09 04:44:40 +08:00
5 changed files with 20 additions and 5 deletions
Showing only changes of commit 2995a322bf - Show all commits

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