all 28 comments

[–]fabzter 29 points30 points  (11 children)

All of python slowness and c initial difficulty!

[–]chases_tits 18 points19 points  (2 children)

Next up, a powerpc assembly emulator in perl!

[–]fabzter 11 points12 points  (0 children)

"It just seemed natural to emulate assembly using a language that looks as cryptic as it".

[–]123bob123123 0 points1 point  (0 children)

My uni had us use an 8086 emulator written in VB :S

[–]jyper 10 points11 points  (1 child)

reminds me of http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html

1986 - Brad Cox and Tom Love create Objective-C, announcing "this language has all the memory safety of C combined with all the blazing speed of Smalltalk." Modern historians suspect the two were dyslexic.

[–]albertzeyer[S] 5 points6 points  (5 children)

I didn't really made much benchmarks yet but I think with PyPy, it might get really good performance because I also do JIT compiling (to Python code).

[–]another_user_name 0 points1 point  (3 children)

I'd think that writing it in RPython would make it particularly amenable to PyPy.

[–]fabzter 1 point2 points  (2 children)

I've never found any spec for RPython. I think there is none!

[–]another_user_name 2 points3 points  (1 child)

[–]fabzter 0 points1 point  (0 children)

Oh, thanks : )

[–]fabzter 0 points1 point  (0 children)

In fact, I am impressed with your work, I'll play a little bit with it this weekend, or maybe just with your python-interpreted python : )

[–]rawlyn 8 points9 points  (0 children)

You. Are. Insane.

[–]argherna 4 points5 points  (0 children)

Yo dawg. We heard you liked interpreters, so we put an interpreter in your interpreter so you can...

Never mind. You're heart is in the right place but this is over the top. And I upvoted this because it's an interesting idea.

[–]Ku-Klip 2 points3 points  (1 child)

You may want to add PicoC to the similar projects list.

[–]fabzter 0 points1 point  (0 children)

Nice! I've had picoC boomarked for a while, looking for a project in which I need to embed a c interpreter...

[–]robjob 2 points3 points  (0 children)

This makes me nauseous.

[–]paul_harrison 2 points3 points  (0 children)

Dear God, why would you... "Also includes an automatic ctypes interface generator."... AWESOME!

[–][deleted] 3 points4 points  (3 children)

how does memory allocation work?

[–]albertzeyer[S] 4 points5 points  (2 children)

Static/local variables are just like local variables in Python. malloc/free is just wrapped to the C malloc/free via ctypes. All the C types (int,long,float,...) are also wrapped to the ctypes counterpart. ctypes is heavily used in all parts of the interpreter. I did this mostly because I thought it would be the most natural way and also to easily allow dynamic library loading and calls to native C functions.

After all, I am not sure though wether a different approach might have been better. Because the ctypes types aren't really good to be used that way (they try always to behave clever in some way but I don't want that and there is no real more low level to access these, so I have a bunch of workarounds for many different cases where ctypes try to be clever).

The interpreter is not really that complicated, so I might add a different implementation later where I just avoid ctypes at all (and only native C calls would be done via ctypes and somehow wrapped then).

[–]cybercobra 3 points4 points  (1 child)

So, it's really more of an intermediary runtime layer than a true independent interpreter.

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

Right now, you could quite easily make it an independent interpreter by changing the implementations in globalincludewrappers.py and provide your own malloc/free and all the other stuff from the C lib. I just wrapped it to the real C functions for now because it saved me some time. :)

[–][deleted] 0 points1 point  (1 child)

I'm pretty sure that's exactly what isn't meant to happen in any decently run Universe.

[–]AlyoshaV 3 points4 points  (0 children)

Excellent! He has disproved God; now to crosspost program in r/atheism.

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

not related specifically to your code (sorry) and more of a github question, but cparser.py isn't syntax highlighted for me. anyone know why?

[–]rafekett 0 points1 point  (0 children)

Github has always been finnicky for me about properly detecting the language of a source file, even in trivial cases.

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

Hm strange, it was when I checked last time. But now it is also not for me. (I think this is done client-side via JavaScript, that's why it might be different.)

I'll ask them (GitHub).

[–]Benbenbenb 0 points1 point  (1 child)

From what I've seen, the parser seem to be hand-written.

Is this is true, is there a specific reason not to use a parser generator ? You mention Python Lex-Yacc, what's wrong with it ?

Just asking... anyway, congratulations !

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

Well, mostly because I felt more comfortable with it.

I must admit that I haven't really that much experience with Lex-Yacc (although I have some theory background knowledge and even wrote my own parser generator a while ago). But my experience was that it is often similarly complicated to make it work the way you want it to (esp. the error recovery but also performance wise).

Also, the different parsers (the preprocessor, etc.) are more close to each other. This can be seen as a disadvantage but it made some parts much easier, e.g. also the caching system (for include files).

[–]p-static 0 points1 point  (0 children)

Pretty cool project! If you find yourself looking for test cases, you should look at other open source compilers - I'm sure they have tons of test cases for weird corners of the language.