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 open, close, read, write #363

Merged
merged 12 commits into from
May 31, 2024
Merged

add open, close, read, write #363

merged 12 commits into from
May 31, 2024

Conversation

enricozb
Copy link
Contributor

@enricozb enricozb commented May 30, 2024

Adds OPEN_FILE, CLOSE_FILE, READ_LINE, READ_CHAR, and WRITE IO functions. Since the read and write operations take in a file descriptor, I've also reserved numbers 0 1 and 2 to have their usual FD meaning, so users can print to stderr, read from stdin, etc.

Here's an example:

type IO_T:
  Done { magic, expr }
  Call { magic, func, argm, cont }

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)
    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"))
    name <- call_io("READ_LINE", 0)
    * <- call_io("WRITE", (1, "Hello, '"))
    * <- call_io("WRITE", (1, name))
    * <- call_io("WRITE", (1, "'!\n"))

    file <- call_io("OPEN_FILE", ("testing.txt", "w"))
    * <- call_io("WRITE", (file, "Your name is: "))
    * <- call_io("WRITE", (file, name))
    * <- call_io("WRITE", (file, "!"))
    * <- call_io("CLOSE_FILE", file)

    return 42

src/hvm.c Outdated
@@ -1240,7 +1235,7 @@ Port expand(Net* net, Book* book, Port port) {

// Reads back a λ-Encoded constructor from device to host.
// Encoding: λt ((((t TAG) arg0) arg1) ...)
Ctr read_ctr(Net* net, Book* book, Port port) {
Ctr port_to_ctr(Net* net, Book* book, Port port) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep the readback functions prefixed by read_ as a convention

Copy link
Contributor Author

@enricozb enricozb May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about functions that are inverses of readback functions? I named it port_to_ctr b/c I also created a ctr_to_port. What do you think we should call the functions that convert Ctr to Port?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer readback_x and inject_x or create_x for the inverses, because it is consistent with how it is in other codes of the past.

@enricozb enricozb marked this pull request as ready for review May 31, 2024 09:12
@enricozb enricozb merged commit 7e547e0 into main May 31, 2024
3 checks passed
@VictorTaelin
Copy link
Member

VictorTaelin commented Jun 3, 2024

Is that example right? Isn't it with instead of do? Also IO_T should be renamed to IO, and it should be a builtin on Bend.

@edusporto
Copy link
Contributor

@VictorTaelin That example was fixed in #369, it should be correct now

@VictorTaelin
Copy link
Member

oh sure, my bad

@developedby developedby mentioned this pull request Aug 20, 2024
2 tasks
@kings177 kings177 added this to the HVM IO lib v0 milestone Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants