Live data from Hacker News

Swift as a C Language REPL

medium.com

1–10 of 24 posts

Re: Swift as a C Language REPL

#3
post #2

GDB can be used as C language REPL since at least late 90's, and lldb as well. And if their pseudo-REPL features aren't enough, there are a couple of C interpreters to choose from. http://www.softintegration.com/ https://gitlab.com/zsaleeba/picoc https://root.cern.ch/cling

CPython is my personal favorite C language REPL.

Re: Swift as a C Language REPL

#4
There is the germ of an idea here, for sure. It is very easy to map C libraries to Swift. Binding LibPQ, for example, requires just a five line "module map" file:

https://github.com/solidsnack/CLibPQ/blob/master/module.modu...

Once you've mapped in the library, trying it out interactively in Swift is easy and fun because of Swift's high-level features.

Re: Swift as a C Language REPL

#5
Would not be my first choice. A good REPL allows you to write code quickly and off the top of your head. As article notes, Swift’s C pointer/buffer semantics are absolutely horrendous, liable to melt the top of your head instead. If you understand C it just gets in your way. If you don’t understand C it gets in your way even worse. Working with ObjC classes is cleaner, but even that has bridging issues.

Incidentally, the Swift REPL itself has another nasty misfeature: it always outputs the fully introspected debug representation of complex values, rather than calling `description`/`debugDescription` where available. Not really an issue when working with pointers, but for native structs and classes with complex internal state it can print screens and screens of unreadable noise, totally defeating the purpose. Conversely, the GUI-based “Swift Playgrounds” REPL prints line output right but lacks the CLI’s persistency so re-evaluates all lines every time a new line is entered, making it useless for stateful work.

TL;DR: Swift’s a hairy mess. [paraphrased jwz aphorism goes here]

Re: Swift as a C Language REPL

#7
post #2

GDB can be used as C language REPL since at least late 90's, and lldb as well. And if their pseudo-REPL features aren't enough, there are a couple of C interpreters to choose from. http://www.softintegration.com/ https://gitlab.com/zsaleeba/picoc https://root.cern.ch/cling

What would I have to do to use gdb as a C repl? I'm not very familiar with gdb other than using the backtrace to debug segmentation faults.

Re: Swift as a C Language REPL

#8
So `let` is being used to define a var that is lexically scoped to... the global environment?! I've noticed you can do this in Javascript as well. As a Lisp programmer, its very jarring and seems like a bad practice. Is there a reason you would do this?

Re: Swift as a C Language REPL

#9
post #7
post #2

GDB can be used as C language REPL since at least late 90's, and lldb as well. And if their pseudo-REPL features aren't enough, there are a couple of C interpreters to choose from. http://www.softintegration.com/ https://gitlab.com/zsaleeba/picoc https://root.cern.ch/cling

What would I have to do to use gdb as a C repl? I'm not very familiar with gdb other than using the backtrace to debug segmentation faults.

Compile your executable with debug info, and then make use of gdb expressions.

https://sourceware.org/gdb/current/onlinedocs/gdb/Expression...

It is not fully featured REPL, don't expect to use it to create stuff on the fly, it is more to query and call existing code.

For a proper REPL, check one of the listed interpreters.

Post reply on HN