Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: A Perl interpreter written in Perl?

by harangzsolt33 (Deacon)
on Apr 16, 2025 at 13:51 UTC ( [id://11164738]=note: print w/replies, xml ) Need Help??


in reply to Re: A Perl interpreter written in Perl?
in thread A Perl interpreter written in Perl?

I'm not sure, but it would be an interesting exercise. I know that other languages have produced their interpreters or compilers in their language. For example, there is a Python interpreter written in Python. There are Basic interpreters written in Basic. There's a Rust compiler written in Rust. And of course, there are C compilers written in C. But I guess, the whole point is to prove that it CAN BE done, and it would be an interesting exercise. And I think, if we look at languages, the more popular a language is, the more likely that there will be many such seemingly "pointless" exercises written in that language, because people want to spend time coding in that language. So, they'll write all sorts of things, even things that don't make any sense. So, this would be one of those projects.
  • Comment on Re^2: A Perl interpreter written in Perl?

Replies are listed 'Best First'.
Re^3: A Perl interpreter written in Perl?
by bliako (Abbot) on Apr 16, 2025 at 15:17 UTC

    A compiler can be written in a "slow" language and still be producing fast/efficient binaries, slowly. Whereas, with an interpreter things are a bit different because the speed of the running script is determined by both the script's efficiency and the interpreter's efficiency. You can get rid of the compiler once your program is compiled but you can never get rid of the interpreter, you need it every time and you suffer its inefficiencies every time you run your script. I am not sure whether Perl is strictly interpreting the script or it compiles (parts of?) it somehow. For this reason, it is logical to write your compiler in as high-level language there is available/you are familiar with. And write your interpreter in as "fast" a language there is. C IMO was the sweet spot for speed/efficiency/high-level. Perhaps not any more. But it works well for the Perl interpreter, it is fast.

    I believe a C compiler starts as an assembly program or as a program in another language preceding C for which there is a compiler available. Then, once you have the C compiler binary, you write the compiler in C and compile that. And then you don't depend on other languages in order to maintain/evolve your C compiler, assuming you already have a C compiler binary. If all the C compiler binaries of the world disappear one day (here is a grand SciFi scenario!) perhaps because antivirii gone mad, then we need to start from assembly again. Or modula-2 or whatever language for which there is a compiler available. The higher level the easier it would be.

    A compiler also depends on a parser to parse the input program. And that can get really complex if in assembly (in fact that's how Yacc was developed). But once you have a basic C compiler then you can write the parser (aka compiler-compiler) in C and compile that. The benefit being the higher level language will be helpful in adding more features/complexity to your parser. In fact a parser acts as a rudimentary compiler (omitting the efficiency heuristics a fullfledge modern compiler has) therefore it can be said that "a compiler is written in its "grammar"", which the parser parses, as opposed to "a compiler is written in C", for example.

    I do not know what other reasons there are for writing a C compiler in C. But do not discount these: it would be a good test for the language and the compiler program. Plus, the whole exercise has a turing-complete kind of sound to it. Oh, and because Computer Science curriculum was really nerdy at some point in time and the students loved it immensely, despite the famous book cover being a red Godzilla (dinosaur?).

    Of course all the above do not apply for Python. For Python there must be one and only one reason: the perversity of a sect of Conformists.

      I'm not aware of any real interpreter languages anymore. That's a distinction from the 80s or 90s, reality evolved faster than terminology.¹

      Perl has a compiler creating optimized op-codes, you can even precompile .pl to .plc code.

      The engine running this code is roughly kind of like the JVM, but not as flexible because of many idiosyncracies.

      The real difference nowadays is not interpreted vs compiled, but dynamic typing vs static typing.

      Compare JavaScript vs TypeScript.²

      In this light, it's hard to answer the OP because I don't know what he thinks an interpreter does.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      ¹) Microsoft BASIC was interpreted, the source for almost all BASIC dialects back then. But even here the source code was normalized internally to 2 letter mnemonics to optimize space and speed.

      ²) That's a fair comparison, since JS was heavily influenced by Perl.

        I'm not aware of any real interpreter languages anymore.

        Pretty much all of the shells (bash, ash, zsh, etc.) are still real interpreters, AFAIAA.


        🦛

Re^3: A Perl interpreter written in Perl?
by Fletch (Bishop) on Apr 16, 2025 at 15:00 UTC

    Using a digital computer? Bah, piker.

    The Oracle hands you a large roll of paper tape and a hole punch engraved with the name "A. Turing".

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^3: A Perl interpreter written in Perl?
by karlgoethebier (Abbot) on Apr 18, 2025 at 11:47 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11164738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2025-07-19 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.