Live data from Hacker News

How we built an interpreter for Swift

bitrig.app

11–20 of 39 posts

Re: How we built an interpreter for Swift

#11

Could you use this to support hot module replacement? Replacing a SwiftUI view in a live app without restarting the process?

Yeah you could! The only caveat is that either the whole app, or at least the part of the app showing the view you want to replace, would have to be running via the interpreter.

We're very interested in using the interpreter to improve the Swift developer experience in more ways like that.

Re: How we built an interpreter for Swift

#12
Swift can already be used in REPL mode, not sure if it's accurate to say Swift is a strictly compiled language that a interpreter was developed for (see 'swift' command line executable). Seems misleading that this is not mentioned anywhere in the article.

Re: How we built an interpreter for Swift

#13
post #12

Swift can already be used in REPL mode, not sure if it's accurate to say Swift is a strictly compiled language that a interpreter was developed for (see 'swift' command line executable). Seems misleading that this is not mentioned anywhere in the article.

++ had the same reaction as you. When I was picking up Swift, the interpreter was invaluable to check my understanding (although I nearly always needed to begin with `import Foundation` to have my code actually work).

Re: How we built an interpreter for Swift

#14
post #12

Swift can already be used in REPL mode, not sure if it's accurate to say Swift is a strictly compiled language that a interpreter was developed for (see 'swift' command line executable). Seems misleading that this is not mentioned anywhere in the article.

I suppose you could argue the repl is not a true interpreter (parsing and executing from an AST)

Re: How we built an interpreter for Swift

#15
post #12

Swift can already be used in REPL mode, not sure if it's accurate to say Swift is a strictly compiled language that a interpreter was developed for (see 'swift' command line executable). Seems misleading that this is not mentioned anywhere in the article.

That's a JIT. It uses the same compiler infrastructure but swaps out the AoT backend and replaces it with the JIT backend in LLVM. Notably, this blog post is targeting on-device usage which a custom JIT is not allowed. You can only interpret.

Re: How we built an interpreter for Swift

#18
>Bitrig dynamically generates and runs Swift apps on your phone. Normally this would require compiling and signing with Xcode, and you can’t do that on an iPhone.

>To make it possible to instantly run your app, we built a Swift interpreter. But it’s an unusual interpreter, since it interprets from Swift… to Swift.

I can't understand what they're talking about, and I don't know if it's because I know too little about programming languages or if it's because they're using unusual semantics.

"a compiled language" makes no sense to me because the language itself doesn't determine whether it's compiled or interpreted. It's just a function of what compilers/interpreters exist. I could write an interpreter for C or a compiler for Python, but the languages themselves aren't compiled or interpreted.

I also don't understand what it means to "interpret" Swift to Swift. Do they mean they compile Swift to Swift (or the more modern "transpile" which means the same thing)? But it sounds like they're doing something dynamically at runtime, so it sounds more like decompiling machine code back to Swift, but the rest of the post doesn't match that interpretation.

Re: How we built an interpreter for Swift

#19
post #17

Do you think you can make it run in Jupyter as a Kernel?

That's not really been something we'd been considering, but yeah I think we probably could. We're primarily using the interpreter to render SwiftUI views, but it supports running arbitrary Swift expressions or statements.

Re: How we built an interpreter for Swift

#20
post #18

> Bitrig dynamically generates and runs Swift apps on your phone. Normally this would require compiling and signing with Xcode, and you can’t do that on an iPhone. > To make it possible to instantly run your app, we built a Swift interpreter. But it’s an unusual interpreter, since it interprets from Swift… to Swift. I can't understand what they're talking about, and I don't know if it's because I know too little abou…

I refer to Swift as a "compiled language" because no officially provided interpreter exists for it.

Bitrig runs Swift apps which are dynamically generated by an LLM on the iPhone, despite the iPhone strict security provisions (e.g. inability to write executable pages of memory). The way we do this is by parsing the generated Swift code and mapping that to the compiled calls to the libraries that come in the OS. It's pretty weird, but we think it's worth it to get the ability to immediately render the generated code on the phone.

Post reply on HN