Skip to content

Commit

Permalink
fix io examples
Browse files Browse the repository at this point in the history
  • Loading branch information
enricozb committed Jun 3, 2024
1 parent bac4889 commit 18e648a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 63 deletions.
15 changes: 6 additions & 9 deletions examples/demo_io/main.bend
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TODO: update tests to handle IO
test-skip = 1
# run io on tests
test-io = 1

type IO_T:
Done { magic, expr }
Expand All @@ -8,24 +8,21 @@ type IO_T:
def IO_T/MAGIC:
return (0xD0CA11, 0xFF1FF1)

def IO_T/wrap(x):
return IO_T/Done(IO_T/MAGIC, x)

def IO_T/bind(a, b):
match a:
case IO_T/Done:
return b(a.expr)
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 call_io(func, argm):
return IO_T/Call(IO_T/MAGIC, func, argm, lambda x: IO_T/Done(IO_T/MAGIC, x))

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

Expand Down
135 changes: 81 additions & 54 deletions examples/demo_io/main.hvm
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
@IO_T/Call = (a (b (c (d ((1 (a (b (c (d e))))) e)))))
@IO_T/Call = (a (b (c (d ((@IO_T/Call/tag (a (b (c (d e))))) e)))))

@IO_T/Done = (a (b ((0 (a (b c))) c)))
@IO_T/Call/tag = 1

@IO_T/Done = (a (b ((@IO_T/Done/tag (a (b c))) c)))

@IO_T/Done/tag = 0

@IO_T/MAGIC = (13683217 16719857)

@IO_T/bind = ((@IO_T/bind__C2 a) a)

@IO_T/bind__C0 = (* (a ((a b) b)))
@IO_T/bind__C0 = (* (b (a c)))
& @undefer ~ (a (b c))

@IO_T/bind__C1 = (* (* (a (b ((c d) (e g))))))
& @IO_T/Call ~ (@IO_T/MAGIC (a (b ((c f) g))))
& @IO_T/bind ~ (d (e f))

@IO_T/bind__C2 = (?((@IO_T/bind__C0 @IO_T/bind__C1) a) a)

@IO_T/wrap = a
& @IO_T/Done ~ (@IO_T/MAGIC a)
@String/Cons = (a (b ((@String/Cons/tag (a (b c))) c)))

@String/Cons = (a (b ((1 (a (b c))) c)))
@String/Cons/tag = 1

@String/Nil = ((0 a) a)
@String/Nil = ((@String/Nil/tag a) a)

@String/Nil/tag = 0

@call_io = (a (b c))
& @IO_T/Call ~ (@IO_T/MAGIC (a (b (@call_io__C0 c))))
Expand All @@ -28,9 +34,11 @@
& @IO_T/Done ~ (@IO_T/MAGIC a)

@main = a
& @IO_T/bind ~ (@main__C7 (@main__C6 a))
& @IO_T/bind ~ (@main__C12 (@main__C11 a))

@main__C0 = (((* 42) a) a)

@main__C0 = i
@main__C1 = i
& @call_io ~ (e ((1 h) i))
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
Expand All @@ -41,38 +49,85 @@
& @String/Cons ~ (33 (f g))
& @String/Cons ~ (10 (@String/Nil f))

@main__C1 = e
@main__C10 = (* a)
& @IO_T/bind ~ (@main__C9 (@main__C8 a))

@main__C11 = ((@main__C10 a) a)

@main__C12 = bb
& @call_io ~ (e ((1 ab) bb))
& @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 ~ (115 (q r))
& @String/Cons ~ (32 (p q))
& @String/Cons ~ (121 (o p))
& @String/Cons ~ (111 (n o))
& @String/Cons ~ (117 (m n))
& @String/Cons ~ (114 (l m))
& @String/Cons ~ (32 (k l))
& @String/Cons ~ (110 (j k))
& @String/Cons ~ (97 (i j))
& @String/Cons ~ (109 (h i))
& @String/Cons ~ (101 (g h))
& @String/Cons ~ (63 (f g))
& @String/Cons ~ (10 (@String/Nil f))

@main__C2 = (* a)
& @IO_T/bind ~ (@main__C0 ((* 42) a))
& @IO_T/bind ~ (@main__C1 (@main__C0 a))

@main__C3 = 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)

@main__C3 = n
& @call_io ~ (e ((1 m) n))
@main__C5 = (a (* c))
& @IO_T/bind ~ (b (@main__C4 c))
& @call_io ~ (@main__C3 ((1 a) b))

@main__C6 = t
& @call_io ~ (e ((1 s) t))
& @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 (l m))
& @String/Cons ~ (101 (k l))
& @String/Cons ~ (108 (j k))
& @String/Cons ~ (108 (i j))
& @String/Cons ~ (111 (h i))
& @String/Cons ~ (44 (g h))
& @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__C4 = (a d)
& @IO_T/bind ~ (@main__C3 ((* c) d))
& @IO_T/bind ~ (b (@main__C2 c))
& @call_io ~ (@main__C1 ((1 a) b))
@main__C7 = (a c)
& @IO_T/bind ~ (@main__C6 (((@main__C5 (a b)) b) c))

@main__C8 = ((@main__C7 a) a)

@main__C5 = j
@main__C9 = j
& @call_io ~ (i (0 j))
& @String/Cons ~ (82 (h i))
& @String/Cons ~ (69 (g h))
Expand All @@ -84,36 +139,8 @@
& @String/Cons ~ (78 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@main__C6 = (* a)
& @IO_T/bind ~ (@main__C5 (@main__C4 a))

@main__C7 = 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 ~ (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))
& @String/Cons ~ (111 (n o))
& @String/Cons ~ (117 (m n))
& @String/Cons ~ (114 (l m))
& @String/Cons ~ (32 (k l))
& @String/Cons ~ (110 (j k))
& @String/Cons ~ (97 (i j))
& @String/Cons ~ (109 (h i))
& @String/Cons ~ (101 (g h))
& @String/Cons ~ (63 (f g))
& @String/Cons ~ (10 (@String/Nil f))
@test-io = 1

@test-skip = 1
@undefer = (((a a) b) b)


8 changes: 8 additions & 0 deletions tests/snapshots/run__io_file@demo_io__main.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/run.rs
expression: c_output
input_file: examples/demo_io/main.hvm
---
Hi! What's your name?
Your name is 'io from the tests'!
Result: 42

0 comments on commit 18e648a

Please sign in to comment.