Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add io_seek, operate on bytes instead of strings #386

Merged
merged 12 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: dsherret/rust-toolchain-file@v1
- name: compare perf
run: |
git fetch origin main
Expand Down
48 changes: 41 additions & 7 deletions examples/demo_io/main.bend
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# run io on tests
test-io = 1

type IO_T:
Expand All @@ -18,12 +17,47 @@ def IO_T/bind(a, b):
def call_io(func, argm):
return IO_T/Call(IO_T/MAGIC, func, argm, lambda x: IO_T/Done(IO_T/MAGIC, x))

def main:
def read_input():
with IO_T:
* <- call_io("WRITE", (1, "Hi! What's your name?\n"))
name <- call_io("READ_LINE", 0)
* <- call_io("WRITE", (1, "Your name is '"))
* <- call_io("WRITE", (1, name))
* <- call_io("WRITE", (1, "'!\n"))
* <- call_io("WRITE", (1, "What is your name?\n"))
return call_io("READ", (0, 10))

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 main():
with IO_T:
* <- write_to_file()
* <- read_from_file()
* <- call_io(write, one_newline_pair)

return 42
221 changes: 158 additions & 63 deletions examples/demo_io/main.hvm
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,126 @@
@call_io__C0 = a
& @IO_T/Done ~ (@IO_T/MAGIC a)

@main = a
& @IO_T/bind ~ (@main__C12 (@main__C11 a))
@main = g
& @IO_T/bind ~ (@write_to_file ((((* e) f) f) g))
& @IO_T/bind ~ (@read_from_file ((((* c) d) d) e))
& @IO_T/bind ~ (a ((((* 42) b) b) c))
& @call_io ~ (@write (@one_newline_pair a))

@main__C0 = (((* 42) a) a)
@newline = a
& @String/Cons ~ (10 (@String/Nil a))

@main__C1 = i
& @call_io ~ (e ((1 h) i))
@one = 1

@one_newline_pair = (@one @newline)

@read_from_file = a
& @IO_T/bind ~ (@read_from_file__C11 (@read_from_file__C10 a))

@read_from_file__C0 = (f (* g))
& @call_io ~ (e (f g))
& @String/Cons ~ (67 (d e))
& @String/Cons ~ (76 (c d))
& @String/Cons ~ (79 (b c))
& @String/Cons ~ (83 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@read_from_file__C1 = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))
& @String/Cons ~ (39 (g h))
& @String/Cons ~ (33 (f g))
& @String/Cons ~ (10 (@String/Nil f))

@main__C10 = (* a)
& @IO_T/bind ~ (@main__C9 (@main__C8 a))
@read_from_file__C10 = ((@read_from_file__C9 a) a)

@read_from_file__C11 = q
& @call_io ~ (d ((o p) q))
& @String/Cons ~ (79 (c d))
& @String/Cons ~ (80 (b c))
& @String/Cons ~ (69 (a b))
& @String/Cons ~ (78 (@String/Nil a))
& @String/Cons ~ (116 (n o))
& @String/Cons ~ (101 (m n))
& @String/Cons ~ (115 (l m))
& @String/Cons ~ (116 (k l))
& @String/Cons ~ (105 (j k))
& @String/Cons ~ (110 (i j))
& @String/Cons ~ (103 (h i))
& @String/Cons ~ (46 (g h))
& @String/Cons ~ (116 (f g))
& @String/Cons ~ (120 (e f))
& @String/Cons ~ (116 (@String/Nil e))
& @String/Cons ~ (114 (@String/Nil p))

@read_from_file__C2 = (c (a e))
& @IO_T/bind ~ (b (((@read_from_file__C0 (c d)) d) e))
& @call_io ~ (@read_from_file__C1 ((1 a) b))

@read_from_file__C3 = d
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@read_from_file__C4 = ({a c} (* e))
& @IO_T/bind ~ (b (((@read_from_file__C2 (c d)) d) e))
& @call_io ~ (@read_from_file__C3 ((a 5) b))

@read_from_file__C5 = d
& @String/Cons ~ (83 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (69 (a b))
& @String/Cons ~ (75 (@String/Nil a))

@read_from_file__C6 = ({a c} (* e))
& @IO_T/bind ~ (b (((@read_from_file__C4 (c d)) d) e))
& @call_io ~ (@read_from_file__C5 ((a (2 0)) b))

@read_from_file__C7 = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@read_from_file__C8 = (c (a e))
& @IO_T/bind ~ (b (((@read_from_file__C6 (c d)) d) e))
& @call_io ~ (@read_from_file__C7 ((1 a) b))

@read_from_file__C9 = ({e g} i)
& @IO_T/bind ~ (f (((@read_from_file__C8 (g h)) h) i))
& @call_io ~ (d ((e 5) f))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@read_input = a
& @IO_T/bind ~ (@read_input__C2 (@read_input__C1 a))

@main__C11 = ((@main__C10 a) a)
@read_input__C0 = (* e)
& @call_io ~ (d ((0 10) e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@main__C12 = bb
& @call_io ~ (e ((1 ab) bb))
@read_input__C1 = ((@read_input__C0 a) a)

@read_input__C2 = y
& @call_io ~ (e ((1 x) y))
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))
& @String/Cons ~ (72 (z ab))
& @String/Cons ~ (105 (y z))
& @String/Cons ~ (33 (x y))
& @String/Cons ~ (32 (w x))
& @String/Cons ~ (87 (v w))
& @String/Cons ~ (104 (u v))
& @String/Cons ~ (97 (t u))
& @String/Cons ~ (116 (s t))
& @String/Cons ~ (39 (r s))
& @String/Cons ~ (87 (w x))
& @String/Cons ~ (104 (v w))
& @String/Cons ~ (97 (u v))
& @String/Cons ~ (116 (t u))
& @String/Cons ~ (32 (s t))
& @String/Cons ~ (105 (r s))
& @String/Cons ~ (115 (q r))
& @String/Cons ~ (32 (p q))
& @String/Cons ~ (121 (o p))
Expand All @@ -84,63 +167,75 @@
& @String/Cons ~ (63 (f g))
& @String/Cons ~ (10 (@String/Nil f))

@main__C2 = (* a)
& @IO_T/bind ~ (@main__C1 (@main__C0 a))
@test-io = 1

@undefer = (((a a) b) b)

@main__C3 = e
@write = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@main__C4 = ((@main__C2 a) a)
@write_to_file = a
& @IO_T/bind ~ (@write_to_file__C8 (@write_to_file__C7 a))

@write_to_file__C0 = (f (* g))
& @call_io ~ (e (f g))
& @String/Cons ~ (67 (d e))
& @String/Cons ~ (76 (c d))
& @String/Cons ~ (79 (b c))
& @String/Cons ~ (83 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@main__C5 = (a (* c))
& @IO_T/bind ~ (b (@main__C4 c))
& @call_io ~ (@main__C3 ((1 a) b))
@write_to_file__C1 = a
& @String/Cons ~ (10 (@String/Nil a))

@main__C6 = t
& @call_io ~ (e ((1 s) t))
@write_to_file__C2 = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))
& @String/Cons ~ (89 (r s))
& @String/Cons ~ (111 (q r))
& @String/Cons ~ (117 (p q))
& @String/Cons ~ (114 (o p))
& @String/Cons ~ (32 (n o))
& @String/Cons ~ (110 (m n))
& @String/Cons ~ (97 (l m))
& @String/Cons ~ (109 (k l))
& @String/Cons ~ (101 (j k))
& @String/Cons ~ (32 (i j))
& @String/Cons ~ (105 (h i))
& @String/Cons ~ (115 (g h))
& @String/Cons ~ (32 (f g))
& @String/Cons ~ (39 (@String/Nil f))

@main__C7 = (a c)
& @IO_T/bind ~ (@main__C6 (((@main__C5 (a b)) b) c))

@main__C8 = ((@main__C7 a) a)

@main__C9 = j
& @call_io ~ (i (0 j))
& @String/Cons ~ (82 (h i))
& @String/Cons ~ (69 (g h))
& @String/Cons ~ (65 (f g))
& @String/Cons ~ (68 (e f))
& @String/Cons ~ (95 (d e))
& @String/Cons ~ (76 (c d))

@write_to_file__C3 = ({a c} (* e))
& @IO_T/bind ~ (b (((@write_to_file__C0 (c d)) d) e))
& @call_io ~ (@write_to_file__C2 ((a @write_to_file__C1) b))

@write_to_file__C4 = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (78 (a b))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@test-io = 1

@undefer = (((a a) b) b)
@write_to_file__C5 = ({a d} (b f))
& @IO_T/bind ~ (c (((@write_to_file__C3 (d e)) e) f))
& @call_io ~ (@write_to_file__C4 ((a b) c))

@write_to_file__C6 = (a c)
& @IO_T/bind ~ (@read_input (((@write_to_file__C5 (a b)) b) c))

@write_to_file__C7 = ((@write_to_file__C6 a) a)

@write_to_file__C8 = q
& @call_io ~ (d ((o p) q))
& @String/Cons ~ (79 (c d))
& @String/Cons ~ (80 (b c))
& @String/Cons ~ (69 (a b))
& @String/Cons ~ (78 (@String/Nil a))
& @String/Cons ~ (116 (n o))
& @String/Cons ~ (101 (m n))
& @String/Cons ~ (115 (l m))
& @String/Cons ~ (116 (k l))
& @String/Cons ~ (105 (j k))
& @String/Cons ~ (110 (i j))
& @String/Cons ~ (103 (h i))
& @String/Cons ~ (46 (g h))
& @String/Cons ~ (116 (f g))
& @String/Cons ~ (120 (e f))
& @String/Cons ~ (116 (@String/Nil e))
& @String/Cons ~ (119 (@String/Nil p))


Loading
Loading