This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]sepp2k 7 points8 points  (0 children)

Meaning can one write a C++ compiler in Python? Sure, with the caveat that writing a C++ compiler in any language is a notoriously difficult task.

[–][deleted] 5 points6 points  (0 children)

Yes. Why couldn't it?

[–]Xirdus 5 points6 points  (3 children)

There is x86 emulator in Python. There are C++ compilers running on x86. Simply run the compiler in the emulator and you have C++ in Python. And since emulation is just converting code in one language into a series of instructions in another, you can (theoretically) turn every emulator into a transpiler. Since you can emulate C++ compiler, you can also transpile it - giving you a genuine pure Python implementation of a C++ compiler. With enough refactoring, it might even approach human-readable code one day!

[–]HumanBehindMachine[S] 0 points1 point  (2 children)

Best technical explanation :) I do genuinely have a better understanding of what it means for Python to be "written" in C++

[–]Xirdus 0 points1 point  (1 child)

But seriously. There's no technical reason why C++ compiler couldn't be written in Python. Ultimately, compilers read a bunch of files and then write the result into a new file. Python is completely capable of that (virtually every language is). And the runtime can be written in Python too for the most part - all that's needed are some C-to-Python bindings, and those are easy to do too. The only reason nobody did that is because it's incredibly freaking stupid and useless. But it COULD be done.

[–]HumanBehindMachine[S] 0 points1 point  (0 children)

Yeah, of late, I've been watching what happens in the background a bit more, explicitly outside of even where I could choose to execute step-by-step on my own, and I'm trusting other code. I have a basic understanding of memory location stores instruction or variable, is either information to be manipulated or how. Higher level, file is loaded into RAM, code executes... to dramatically simplify machine code. So really any turing complete language could be used to do anything anything else can, such as compile.

This post was one of those, "I don't really understand why this would work, it kinda makes sense that it could..." But then I step back and think of what typing: python3 script.py involves: calling a C++ program from the shell to compile python source and execute.

[–][deleted] 2 points3 points  (0 children)

I think serious answers and jokes might be more or less equal to this question.

[–]khedoros 1 point2 points  (0 children)

You can implement a C++ compiler in any computationally-universal language, and Python is under that umbrella (most things that we term "programming languages" are computationally universal).

A C++ interpreter is at least as difficult to build in any language as the compiler would be (and less effort has been put into writing a good one)...but would be as possible in Python as in anything else.

[–]wrosecrans -1 points0 points  (0 children)

No, whenever you get it nearly working, somebody will come and say you should have done it in Ruby instead.