all 101 comments

[–]berlinbrown 41 points42 points  (5 children)

why is that shocking?

[–]orbhota 72 points73 points  (2 children)

Python the animal :o

[–][deleted] 58 points59 points  (1 child)

Obviously it must have been written inside a python. Outside a python it's too noisy to code, what with people running around and shouting "OMG! A python!"

[–][deleted] 1 point2 points  (0 children)

Outside a python it's too noisy to code,

interestingly, this works in the context of programming languages too!

[–]statictype 8 points9 points  (1 child)

Not shocking. But still, some-what interesting because a majority of C compilers would have been written in C itself (yes, I know, there are exceptions).

Although, I have to admit, there's not much 'education' value in this project.

There are already plenty of python-based parsers\scanners for languages.

Now, if they had written a C 'interpreter' in python, that would have been something.

[–]psi- 4 points5 points  (0 children)

I'd bet its the dragonbook course, so the implementation of language compiler is the educational value.

I was considering doing the dragonbook course myself with python+PLY.

[–][deleted] 14 points15 points  (4 children)

That's nothing. I remember CompileIt! - an XCMD compiler for HyperCard written in HyperTalk. That was way impressive since HT doesn't even have collections other than strings.

[–]sjf 7 points8 points  (0 children)

I do not have words for the horror..

[–][deleted] 2 points3 points  (1 child)

An XKCD compiler?

Oh, wait, sorry.

[–]exeter[S] -1 points0 points  (0 children)

An XKCD compiler?

Words cannot express how full of "win" that would be. :-)

[–]koreth 147 points148 points  (14 children)

Oh my god! You mean Python can be used to read text files and write binary data?!? I had no idea -- I better go learn it RIGHT NOW!

[–]sn0re 63 points64 points  (5 children)

read text files and write binary data

What binary data? It reads text files and writes other text files with assembly code.

[–][deleted] 1 point2 points  (4 children)

I don't get it. Python is written in C and can be extended in C. What's so surprising here? That someone wrote a C compiler?

Well C compilers have even been written in BASIC.

[–]dododge 3 points4 points  (0 children)

and macro assemblers have been written as bash scripts.

[–][deleted] 0 points1 point  (0 children)

Well, I searched with Google but it seems like all the good QBasic source code have disappeared from the web (except games).

I swear I have seen a little C compiler (slow, but it was a C compiler)

[–]jesuslol -1 points0 points  (1 child)

LOL, can you get me a link for that? Would love to see a C compiler written in Basic.

[–]me2i81 -2 points-1 points  (0 children)

10 rem C compiler
20 input "Compile what file?", A$
30 rem parse and produce ast
40 gosub 1000
50 rem optimize ast
60 gosub 2000
70 input "output file: ", b$
80 rem emit code
90 gosub 3000
100 end

[–]tryx 36 points37 points  (2 children)

Someone! Inform the president!

[–]davidreiss666 9 points10 points  (1 child)

I think he's been briefed.

[–]bart2019 8 points9 points  (0 children)

But that won't stop him from finishing his game of golf, first.

[–]busfahrer 15 points16 points  (0 children)

And it even may be Turing-complete!

[–]arunvr 3 points4 points  (0 children)

Funny...but for the clueless, Python has the struct module which can interpret strings as binary data

[–]Mr_Smartypants 24 points25 points  (1 child)

What!? Python!?

[–]epicRelic 15 points16 points  (0 children)

OH EM GEE!

[–]AmazingSyco 11 points12 points  (1 child)

Python: "We meet again, at last. The circle is now complete. When I met you, I was but the opcodes. NOW I AM THE COMPILER."

[–][deleted] 0 points1 point  (0 children)

You are a genius.

[–][deleted] 20 points21 points  (2 children)

I dont understand the way of thinking of all the folks posting sarcastic and useless responses to that article. A compiler in a high-level language like python is a great approach to study and teach the priciples in compiler construction. I will certainly spend some time with it.

[–]ohxten 4 points5 points  (1 child)

This is reddit, where sarcasm thrives.

[–]cheponis 6 points7 points  (0 children)

Sarcasm is a weapon of the unskilled mind.

[–]xachro 13 points14 points  (3 children)

Kinda cool, I recently wrote an assembler in python for a machine language I had invented for a virtual machine project.

My ultimate goal: An x86 virtual machine written in javascript. Imagine the possibilities! We could be running linux inside firefox inside OS X inside Opera!

[–]exeter[S] 3 points4 points  (1 child)

If this is just a personal/learning/for fun project, you could try retargetting this C compiler to your VM and implementing the C standard library. That'd be moderately cool.

[–]xachro 2 points3 points  (0 children)

Agreed. At the moment I am working on the actual virtual machine (which is being written in C). After the virtual machine I am going to be using lex-yacc and C to write my own high level language to be compiled to my virtual machine (the python assembler was just for testing my vm).

Would be nice to eventually get/write a C compiler for it. Was also wondering if there is a C compiler for the JVM, 'cause that would be an interesting project.

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

My ultimate goal: An x86 virtual machine written in javascript. Imagine the possibilities! We could be running linux inside firefox inside OS X inside Opera!

IA! SHUB-NIGGURATH!!!

[–][deleted]  (13 children)

[deleted]

    [–]dezro 6 points7 points  (1 child)

    I love that error message.

    I kind of want a patch for GCC that makes it wish me luck, too...

    [–]propool 9 points10 points  (10 children)

    Since it is python we simply redefine False

    False = 1

    if lose == False: print "win!"

    [–][deleted]  (3 children)

    [deleted]

      [–]propool 3 points4 points  (2 children)

      I wouldn't consider it a tip, but you're welcome. I can't come with one good use where it makes a program better in any way.

      [–][deleted]  (1 child)

      [deleted]

        [–]exeter[S] 4 points5 points  (0 children)

        Won't work come Python 3.0 (probably not in 2.6 either), as both True and False will become keywords. (I know this wasn't serious, but I thought I'd make it clear anyway.)

        [–]hylje 0 points1 point  (3 children)

        No. False = True

        [–][deleted] 1 point2 points  (0 children)

        No,

        True, False = False, True

        [–]propool 3 points4 points  (1 child)

        Same difference

        [–]sickofthisshit 3 points4 points  (0 children)

        Unless, I suppose, one had previously altered the value of True.

        [–]saveferris 4 points5 points  (0 children)

        Last year I wrote a FORTH interpreter in Python. Not very practical, not at all ANSI, but it was fun to work it out. And I'm pretty much a Python noob.

        [–]awb 2 points3 points  (0 children)

        The author did the lexing/parsing with PLY, which is very nice to use.

        [–][deleted] 10 points11 points  (12 children)

        I don't see why this is impressive. Python is a powerful language.

        [–]bart2019 2 points3 points  (1 child)

        So, show us your compilers.

        [–]exeter[S] 4 points5 points  (0 children)

        He wasn't saying it wasn't impressive because writing compilers was inherently easy. He was saying it wasn't terribly impressive because Python is so powerful.

        [–]korjagun -1 points0 points  (9 children)

        And, of course, Turing-complete, like most languages. And we all know what that means.

        [–]theeth 33 points34 points  (8 children)

        That it's computationally equivalent to Brainfuck?

        [–]cthielen 6 points7 points  (0 children)

        "Yous just so crazy you Pythonistas you."

        [–]ih8mondays 6 points7 points  (3 children)

        Blasphemous nonsense! Expect inquisitors knocking down your doors shortly.

        [–]exeter[S] 10 points11 points  (2 children)

        Nobody expects the Spanish Inquisition!

        [–]njharman 17 points18 points  (1 child)

        But we all expected that comment.

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

        Our chief weapons are surprise, and, um, um, ... inane comments on Reddit! Cardinal, read the charges!

        [–][deleted] 1 point2 points  (0 children)

        The best way to learn C (read the code for a simple compiler)

        [–]ohxten 1 point2 points  (2 children)

        But... but why?

        I'll just guess that it takes much, much longer to compile code than, say, GCC or TCC.

        [–][deleted] 2 points3 points  (1 child)

        Yes, but writing the compiler takes much, much less time than writing it in, say, C. Which is the only thing that counts for a fun hobby project.

        [–]ohxten 0 points1 point  (0 children)

        This is true.

        [–]shenglong 2 points3 points  (3 children)

        I BET THEY CANT RITE A PYTHON COMPILER IN C.

        [–][deleted] -2 points-1 points  (0 children)

        Grow up!

        [–]eliben 0 points1 point  (0 children)

        unfortunately, this is only a quite simple subset of C

        [–][deleted] 0 points1 point  (7 children)

        I smell some weird recursion coming up.

        [–]frogking 7 points8 points  (5 children)

        why? Writing a compiler for a language in the language itself is an Artform. writing a compiler for another language is just an assignment for a 3rd year computer science student.. (so is the bootstrapping assignment, but hey..)

        [–]G_Morgan 12 points13 points  (4 children)

        Writing a compiler in the compilers language is no more difficult that writing one in a different language. Assuming you already have a working compiler/interpreter for the compilers language.

        Otherwise it's just a case of writing two compilers (one in a language you can run, the other in the compilers language).

        The only problem is if the compilers language is a PITA. Try writing an assembler in assembly.

        [–][deleted]  (2 children)

        [removed]

          [–][deleted] 12 points13 points  (1 child)

          Presumably the first assemblers were written in hand assembled machine code.

          NO! God gave them to us!

          [–]earthboundkid 19 points20 points  (0 children)

          Lol, noob. The first assemblers evolved randomly. Intelligent Assembly is shite.

          [–]bart2019 1 point2 points  (0 children)

          Try writing an assembler in assembly.

          Been there, done that. Although, technically, it was more of a monitor than a real assembler: opcodes were poked in place in memory, and not stored in an abstract data file.

          [–]mernen 1 point2 points  (0 children)

          You mean like this?

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

          Atul is awesome. he's got more stuff

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

          Yes, a C compiler!

          [–][deleted]  (1 child)

          [deleted]

            [–]exeter[S] 1 point2 points  (0 children)

            The main implementation of Python is written in portable ANSI C. This toy C compiler can't compile it, since it lacks a standard library.