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

Sequencing toplevel phrases in `ocaml` #8813

Open
dbuenzli opened this issue Jul 19, 2019 · 6 comments

Comments

@dbuenzli
Copy link
Contributor

commented Jul 19, 2019

It seems that ocaml's readline does not allow to sequence toplevel phrases on the same input interaction. Basically the first ;; seems to act as an "end of line" comment.

Is there any rationale about this or does it qualify as a bug ?

More specifically I find the following discrepancy suprising:

> echo 'print_endline "bla";; print_endline "bli";;' > /tmp/bla.ml
> ocaml -noinit
        OCaml version 4.08.0

# #use "/tmp/bla.ml";;
bla
- : unit = ()
bli
- : unit = ()
# print_endline "bla";; print_endline "bli";;
bla
- : unit = ()
# 

Also it seems syntax errors are allowed after the first ;;:

# print_endline "bla";; lat x = 3;;
bla
- : unit = ()
@nojb

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2019

Indeed, the toplevel resets the input buffer after each phrase ending in ;; is read from the user, which results in the behaviour you have observed:

Lexing.flush_input lb;

You can stop blindly resetting the input buffer after each phrase is read, but that results in strange interactions like:

# 1;; ignore
- : int = 1
# 2;;
- : unit = ()

To improve on this it would be necessary to read several phrases at once and evaluate them all at once. The problem is knowing when to stop waiting for input. Basically, you need to stop reading after seeing ;; if this would cause the lexer to block waiting for input. As far as I know this is not easily done with the current Lexing API.

@gasche

This comment has been minimized.

Copy link
Member

commented Jul 19, 2019

@nojb: would it be possible to repeat the not-yet-interpreted portion of the buffer after the prompt, to avoid the "strange interaction" you mention?

@nojb

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2019

@nojb: would it be possible to repeat the not-yet-interpreted portion of the buffer after the prompt, to avoid the "strange interaction" you mention?

My feeling is that this is probably doable, but I haven't gone through the details.

@nojb nojb added the feature-wish label Jul 19, 2019

@dbuenzli

This comment has been minimized.

Copy link
Contributor Author

commented Jul 19, 2019

Just for context I noticed this while testing down's toplevel session support: you can record your phrases to store in a file to easily #use back later (details). And I noticed a particular recorded sequence of phrases didn't have the same effect when loaded back.

@Armael

This comment has been minimized.

Copy link
Member

commented Jul 19, 2019

Would it be hard to define a parser entry for "a list of toplevel phrases, where the last one is possibly terminated by EOF instead of ;;"?
Then we could use it to parse a toplevel input, and execute the resulting sequence of phrases.

@nojb

This comment has been minimized.

Copy link
Contributor

commented Jul 20, 2019

Would it be hard to define a parser entry for "a list of toplevel phrases, where the last one is possibly terminated by EOF instead of ;;"?
Then we could use it to parse a toplevel input, and execute the resulting sequence of phrases.

I'm not sure that I follow: EOF is typically only sent to finish the session, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.