-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
repl for zig #596
Comments
Zig behaves very much like an interpreted language with comptime code execution. For example, Additionally the One major missing feature is the ability to declare functions in the same syntactic context as imperative code. Top level scope in Zig is declarative and declaration order never matters. This means that if there were a zig repl, it would not be top level syntax. It might make more sense that what you type goes into an implicit If there were a zig repl, the syntax would need to be special repl syntax that doesn't work anywhere in a zig source file. It might be neat for rapid development, but it wouldn't make as much sense for Zig as it does for top-level-imperative languages like Python. |
The "implicit main" idea is pretty nice. It's good to think about it as a main function that grows down as you type commands in repl. I think it will be possible without closures. If you copypaste a function in repl, it will be declared as a new function, near the "implicit main". I even have an idea: once you called a function that doesn't exist, zig will offer you to create it, and you can autoexpand it just like you autoexpanded main before. |
I believe a repl-like environment would be nice for interactive testing. However there are many issues that have to solved first. Here are some that come into my mind. Nature of ZigZig is a relatively low level language at least at the moment. While we have some high level features, Zig is still quite close to C but it has compile time features for replacing build systems and the preprocessor. Memory managementA repl is fundamentally a big global environment which you manipulate by adding and deleting variables and functions and you evaluate expressions in this environment. How do you securely manage memory manually in this kind of environment? Python, many lisp implementation and smalltalk are all garbage collected languages. They all implement some form of automatic memory management that Zig doesn't have. Foreing function interfaceFor the repl to have any value, other than it mimicking the syntax of Zig, it would have to have to able to actually run real Zig code and that real Zig code probably wants to interface with the operating system and other libraries that are already compiled. Loading and unloading dynamic libraries at runtime should be possible. Syntax issuesWould the repl have special syntax for behavior that doesn't exists in normal Zig or would the features of the repl be limited by Zig syntax? |
I want to point out that you can load up a binary in gdb, and it's pretty darn close to a zig repl. You can call functions, allocate memory, do pretty much anything. The other thing I want to point out is that we could have a zig repl that behaves like everything is in a giant ...or maybe that's the difference between zig repl and normal comptime blocks, we hack up some stuff to let these operations work in the comptime environment, and it works well enough to be useful. |
I don't know how comptime and build system in zig works, so I can't comment on that. I want there to not be any language difference between programming in repl zig and programming in zig.
garbage collection is absolutely not necessary in repl. Repl is just a bunch of text on input and a bunch of executable binary code on output. Practically, two arrays. I had something like repl in assembly, I changed code and executed it immediately.
Pretty easy, just need to provide correct headers.
This. Except using gdb is a torture. Need to make a proper ide as soon as possible. |
About "Optional: need to also minimize the need for declaring variable type ahead of time", disregard this, it's impossible. To take some variable outside of the Variable creation and variable deletion must be on the same level, in the same scope. Overwise it would require a really complex logic to figure out if a variable still exists after some |
I'm not sure whether DRY should apply to zig's design for both a repl and a whole-program-optimized-with-compiler-flags module. Some copy semantics/other complex bugs caught at 'comptime' could be triggered by a line entered later. What do you do, retype all of the lines since then and time-travel-debug (maybe gdb does that, I dunno)? Maybe that's an issue for most repls in general... And @andrewrk pointed out that
If that's inconvenient (assuming the repl/command line is line-based and can't 'scroll up'), the repl task might be better served by a real IDE/editor in a (compile-fast) edit-save-run loop.
Since that's been rejected in the past, what if a special |
I cooked up something simple: https://github.com/Hejsil/hacky-zig-repl |
Swift implements a REPL on top of LLDB. https://swift.org/lldb/ Perhaps that technique might work for Zig too? Although it looks like they manage this by maintaining a snowflake version of lldb: https://github.com/apple/swift-lldb |
This is essentially implemented with the new |
following up on this. is there a guide on how to use If I just wanted to evaluate some copy and paste zig code from the net. what's the best way to evaluate it? |
please reopen |
offtopic, but https://zig.godbolt.org/ |
Having read eval print loop for zig would've been cool. I'd be able to make zig programs in the same way I made python programs: by copypasting code into console and getting feedback immediately, but I would still be able compile and distribute it as a binary program.
hot code swapping #68 is somewhat related, but it's more about editing existing functions. It's much simplier to implement.
The best way to archive this is to copy python and lisp (and maybe smalltalk), they are the prime examples of languages that work like that. They can be programmed by editing your program without stopping to recompile anything. Maybe there's something to learn from shells too, I'm not good at them. I like how you can make labels in bash and use goto.
One thing about python is that using functions in there is completely optional. You can just type code directly, and create variables directly, they will end up as global variables. In an enviroment like that, being able to delete variables is important, because they will never be deleted overwise. See issue #594, deleting a variable mid function.
Getting rid of defer is also important because of that. It only fires at the end of function, which is never in this scenario. I'd get rid of it anyway, because I prefer error handling through goto, I sometimes need for defer to execute somewhere mid function.
Need to make some support of object versioning (or structure versioning, whatever) to be able to modify structures at runtime. The way I see it, is that if you need to change something in
some_structure
, you createsome_structure_2
, convert everysome_structure
intosome_structure_2
, and then rename structure declarations. If there are not too many instances ofsome_structure
, maybe it could be done automatically.Optional: one thing that annoyed me about python, is that I can't save result from function call easily. I type
do_something()
, look at the result, see if it's what I need, go back to the beginning of the line, addres =
, execute it again. I'd like for my code to be executed as I type. So that I typedo
, tooltip appears, I press tab, now I havedo-something
, it gets executed instantly, I see if everything went good, I add-- res
at the end if I want to. It would be much more comfortable and fast. It's pretty much polish notation, I think it's a really good thing for programming languages to have, don't need to memorise operator precedence. Polish notation is more natural for computers,+ - and or
came from humans.Optional: need to also minimize the need for declaring variable type ahead of time, make it behave like
auto
in c++ and everything python. The only place types should appear is in function declarations. This is what:=
in golang does. It's most likely impossible to archive completely, but at least to some degree, maybe. Creating scopes (code blocks) stands in a way of thisauto
, because then you have to create variables ahead of time. The only alternative to scopes is deleting every no longer needed variable manually, withdel
statement or something like that. Something like//
after the variable. Once an ide is done, it will be possible to highlight the scope of a variable, all it's lifetime. Just creating code blocks may be more simple though, but I think deleting variables is more natural.This is my dream language, it completely blurs the border between interpreted and compiled languages. I'm describing a completely different language from zig, ain't I? I'll probably have to do it myself. If you want to read some of my ramblings, here https://board.flatassembler.net/topic.php?t=20106
The text was updated successfully, but these errors were encountered: