Skip to content

Add initial implementation of input() keyword support - #669

Draft
kc611 wants to merge 1 commit into
spylang:mainfrom
kc611:kc/input_impl
Draft

kc611 wants to merge 1 commit into
spylang:mainfrom
kc611:kc/input_impl

Conversation

@kc611

@kc611 kc611 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

As titled,

This PR aims to add support for the input() keyword allowing user input from terminal.

@antocuni

antocuni commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

@kc611 I'm not sure this is the right approach.

What you are doing is essentially to implement most of the logic in C and expose a single function input which does the equivalent of sys.stdin.readline(). This is not the "spy approach".
The "spy approach" is to write as little C as possible, and to write as much as possible in spy itself.

I propose the following plan:

  1. implement/expose sys.stdin, sys.stdout and sys.stderr
  2. implement input() in spy (e.g. inside stdlib/_builtins.spy) on top of sys.stdin.readline.

The good news is that we already have all the machinery needed to implement (1). Look at stdlib/_file.spy:

spy/stdlib/_file.spy

Lines 1 to 17 in 8ef0a1b

from posix import (
_FILE, _FILE_NULL, _fopen, _fread, _freadall, _freadline,
_fwrite, _ftell, _fseek, _fflush, _fileno, _isatty, _fclose, SEEK_SET,
)
from unsafe import gc_ptr, gc_alloc
@struct
class _FileData:
fp: _FILE
filename: str
mode: str
@struct
class file:
__ll__: gc_ptr[_FileData]

You can see that a file is a ptr to _FileData, which is in turn a small wrapper around libc FILE* type (exposed by the posix module).
posix is implemented at interp-level in spy/vm/modules/posix.

The easiest way is probably to implement helper functions such as posix._get_stdin which return a
W__FILE (which corresponds to _FILE at app-level), and then wrap it inside a proper applevel stdlib._file.file object.

We don't have a sys module yet, so as a first approximation I think it's fine to implement _file.get_stdin() as a normal function. We will understand how to write that into sys later.

If you don't fully follow what I'm saying it's perfectly normal because it cross-cuts a lot of spy layers, so it's normal to be a bit confused about it initially. Happy to talk more about it.


NOTE

Normally the standard approach to implement input() and similar is to link against libreadline (or libedit on mac) which gives you line editing, history, etc.
But for that we need a bit more machinery on the spy side, because we need to understand how we want to distribute/install/discover compile-time dependencies.

@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Documentation Preview
Preview documentation for PR #669.
Last Updated: 2026-09-06 09:17 UTC

This branch has not been deployed

No deployments
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.

2 participants