-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
return Result<T, IOError<E>> from IO functions (#407)
return Result<T, IOError<E>> from IO functions
- Loading branch information
Showing
25 changed files
with
1,107 additions
and
643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,35 @@ | ||
test-io = 1 | ||
|
||
type IO_T: | ||
Done { magic, expr } | ||
Call { magic, func, argm, cont } | ||
def unwrap(res): | ||
match res: | ||
case Result/Ok: | ||
return res.val | ||
case Result/Err: | ||
return res.val | ||
|
||
def IO_T/MAGIC: | ||
return (0xD0CA11, 0xFF1FF1) | ||
def open(): | ||
return call("OPEN", ("./LICENSE", "r")) | ||
|
||
def IO_T/bind(a, b): | ||
match a: | ||
case IO_T/Done: | ||
return (undefer(b))(a.expr) | ||
case IO_T/Call: | ||
return IO_T/Call(IO_T/MAGIC, a.func, a.argm, lambda x: IO_T/bind(a.cont(x), b)) | ||
def read(f): | ||
return call("READ", (f, 47)) | ||
|
||
def call_io(func, argm): | ||
return IO_T/Call(IO_T/MAGIC, func, argm, lambda x: IO_T/Done(IO_T/MAGIC, x)) | ||
def print(bytes): | ||
with IO: | ||
* <- call("WRITE", (1, bytes)) | ||
* <- call("WRITE", (1, "\n")) | ||
|
||
def read_input(): | ||
with IO_T: | ||
* <- call_io("WRITE", (1, "What is your name?\n")) | ||
return call_io("READ", (0, 10)) | ||
return wrap(*) | ||
|
||
def write_to_file(): | ||
with IO_T: | ||
fp <- call_io("OPEN", ("testing.txt", "w")) | ||
input <- read_input() | ||
* <- call_io("WRITE", (fp, input)) | ||
* <- call_io("WRITE", (fp, "\n")) | ||
|
||
return call_io("CLOSE", fp) | ||
|
||
def read_from_file(): | ||
with IO_T: | ||
fp <- call_io("OPEN", ("testing.txt", "r")) | ||
bytes <- call_io("READ", (fp, 5)) | ||
* <- call_io("WRITE", (1, bytes)) | ||
* <- call_io("SEEK", (fp, 2, 0)) | ||
bytes <- call_io("READ", (fp, 5)) | ||
* <- call_io("WRITE", (1, bytes)) | ||
|
||
return call_io("CLOSE", fp) | ||
|
||
def write: | ||
return "WRITE" | ||
|
||
def one: | ||
return 1 | ||
|
||
def newline: | ||
return "\n" | ||
|
||
def one_newline_pair: | ||
return (one, newline) | ||
def close(f): | ||
return call("CLOSE", f) | ||
|
||
def main(): | ||
with IO_T: | ||
* <- write_to_file() | ||
* <- read_from_file() | ||
* <- call_io(write, one_newline_pair) | ||
|
||
return 42 | ||
with IO: | ||
f <- open() | ||
f = unwrap(f) | ||
bytes <- read(f) | ||
bytes = unwrap(bytes) | ||
* <- print(bytes) | ||
res <- close(f) | ||
|
||
return wrap(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.