Crystal: Can we have a builtin REPL?

Created on 22 May 2015  Â·  15Comments  Â·  Source: crystal-lang/crystal

Just like irb?

$ crystal repl
crystal > i = 5
5
crystal > i * 2
10
feature compiler

Most helpful comment

Since google sent me here, for completeness, from a comment by drosehn

See also: crystal play. Not quite a REPL, but pretty useful.
Run crystal play, and then connect to it using your local web browser.

https://github.com/crystal-lang/crystal/issues/4363#issuecomment-298335826

All 15 comments

Please read this: http://crystal-lang.org/2014/12/06/another-language.html

If you can find a solution to problem mentioned in Incremental compilation is not possible section without giving up on global type inference, then it is possible to build REPL

I have already given a though to it, to actually recompile the whole accumulated chain of commands each time and re-run them. With ability to clear them on demand in REPL. This would somewhat work, but it will be weird if you do something that have side-effects (like talking to external service, writing a file) - those will be executed each time you want to execute next command you type.

The other approach will be to force user to operate in one of 2 modes: either definition mode or execution mode. In definition mode you can define classes, modules, structs, methods, etc. In execution mode you can call them. With simple REPL command or keystroke you can switch between modes.

This makes it simpler to do recompilation. You just put all definition parts in one temporary file, and append one execution part that was just typed (ignoring all previous execution parts) and then compile & run this temp file.

More smarter solution would be to automatically detect definition and execution parts of the code, which will defeat the need to have 2 modes, but will work in effectively same way. In this case REPL might need to use parser/lexer part of compiler to be able to distinguish different AST nodes to differentiate between definition and execution parts (since you can have an execution line inside of class definition).

I'm wondering how this is made in haskell.

Just try printing return 0 in GHCi. Ambiguous variable in type constraint error!

In Haskell, everything is a function, so you Don have some of the complexities with trying to compile OOP classes. Also, you need to use let syntax to define functions in the REPL, and you can't create type classes or instances (I may be wrong about that one!).

How about LLVM MCJIT? I think it can execute Crystal code incrementally like lli.

In addition, cling is known as C family interpreter which uses JIT.

@rhysd The main issue we have, as always, is that the type of instance variables (and variables) changes as the program "evolves" (as the compiler finds more code to analyze). So the JIT will have to readjust the types it used, somehow. I think it's possible, just hard.

@asterite

Thank you for your anser. I've understood that it is hard (and it may mean almost the same as recompiling whole code previously input).

Does anybody knows how Elixir has a REPL ?

Yes, it has a VM and an interpreter

On Sun, Sep 20, 2015 at 1:32 AM, Emiliano Mancuso [email protected]
wrote:

Does anybody knows how Elixir has a REPL ?

—
Reply to this email directly or view it on GitHub
https://github.com/manastech/crystal/issues/681#issuecomment-141747343.

Ary Borenszweig Manas Technology Solutions
[ar.phone] 5258.5240 #ARY(279)
[us.phone] 312.612.1050 #ARY(279)
[email] [email protected]
[web] www.manas.com.ar

_cough_ @greyblake's icr _cough_

@kepler0 it is a good option, but still has a lot problems of consistency between iterations.

I'm closing this. We know we might want a REPL in the future, so that might eventually happen, but there's no point in keeping this issue open.

Since google sent me here, for completeness, from a comment by drosehn

See also: crystal play. Not quite a REPL, but pretty useful.
Run crystal play, and then connect to it using your local web browser.

https://github.com/crystal-lang/crystal/issues/4363#issuecomment-298335826

I don't know if it is the right place to ask, but is type inference strictly incremental? That means, the union types will never lose any inferences from previous lines, it will only be re-assigned, casted to its parent, or contain more types in the future, am I correct?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lbguilherme picture lbguilherme  Â·  3Comments

asterite picture asterite  Â·  3Comments

relonger picture relonger  Â·  3Comments

RX14 picture RX14  Â·  3Comments

costajob picture costajob  Â·  3Comments