Skip to content

Commit

Permalink
Implement embedded input function
Browse files Browse the repository at this point in the history
  • Loading branch information
knogu committed Aug 23, 2023
1 parent 4700251 commit 4a7e7ba
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions stdlib/internal/builtin.codon
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def print(*args, sep: str = " ", end: str = "\n", file=_stdout, flush: bool = Fa
if flush:
_C.fflush(fp)

def input(prompt: str = '') -> str:
if (prompt != ''):
print(prompt, end='')
buf = Ptr[byte]()
sz = 0
rd = _C.getline(Ptr[Ptr[byte]](__ptr__(buf)), Ptr[int](__ptr__(sz)), _C.seq_stdin())
if rd == -1:
return ''
return str(buf, rd - 1)

@extend
class __internal__:
def print(*args):
Expand Down
3 changes: 3 additions & 0 deletions test/app/input.codon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print(input("input: "), end=',')
print(input(), end=',')
print(input())
3 changes: 3 additions & 0 deletions test/app/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aa bb

cc
3 changes: 3 additions & 0 deletions test/app/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ gcc "$testdir/test.c" -L"$arg" -Wl,-rpath,"$arg" -lcodon_export_test -o "$arg/te

# exit code test
$codon run "$testdir/exit.codon" || if [[ $? -ne 42 ]]; then exit 4; fi

# input test
[ "$($codon run "$testdir/input.codon" < "$testdir/input.txt")" == "input: aa bb,,cc" ] || exit 5

0 comments on commit 4a7e7ba

Please sign in to comment.