Jump to content

Making your own code


Recommended Posts

I don't see what's outdated there. It depends on what and how you program. Sure, some things can be much faster executed in C++ than C, especially when using STL routines, template programming and allowing to compiler to inline what can be inlined, nobody said otherwise, but for an interpreter C is more than enough. And for a young aspiring guy it is much easier to learn and leads to earlier success than digging through C++. It is a good idea.

Edit: what do you guys think (or know :-)) is the reason for C's comeback in the last few years ?

Edited by Green Baron
Link to comment
Share on other sites

39 minutes ago, K^2 said:

This is a somewhat dated view. Compilers got a lot better, and standard enforces some good rules that lead to better optimization now. There isn't a reason to chose C over C++ for performance anymore. Of course, you have to know how to write in C++ without relying on standard libraries if you're going to writer drivers or OS code, and when writing for performance, know which standard library calls do expensive error checking.

theres no reason c++ cant be as fast as c. its just easy to bog yourself down in oop overhead. its not a lot of overhead (especially with so much code running on vms as opposed to being compiled). of course the thing i like about c++ is that it doesn't force you to use classes for everything. you can run your oop code and use functional code when you really need to push it. i actually went and looked at my interpreter code. the entire vm is pretty much wrapped up into a single c++ class, its actually very lightweight since i wrote it to run on an 8-bit mcu. its just full of a lot of low level stuff that wouldn't look out of place in c.

c is still a good choice for those tasks though. even if not for performance reasons (performance in this case has more to do with the programmer than the language they are using). for one its good to approach a low level problem with a low level language, and c is about as low as you can go before you have to start using asm. 

Edited by Nuke
Link to comment
Share on other sites

To sum up this thread. 

If you are thinking about writing your own programming language, have read this thread, and understood at least 80% of what each and every post was talking about, then you are probably read to tackle your own language.   If you didn't, then you are not.    :D

 

Link to comment
Share on other sites

3 hours ago, Green Baron said:

[...] for an interpreter C is more than enough. And for a young aspiring guy it is much easier to learn and leads to earlier success than digging through C++.

If you mean, C is easier to learn and use efficiently for somebody who's new to programming in general, with a goal of writing a simple command interpreter, then yes, I agree. What I was saying is that reason that existing interpreters, ones that were written by people you can't call amateurs by any measure, were written in C are largely outdated. I guess, we were talking about different things.

3 hours ago, Nuke said:

c is still a good choice for those tasks though. even if not for performance reasons (performance in this case has more to do with the programmer than the language they are using). for one its good to approach a low level problem with a low level language, and c is about as low as you can go before you have to start using asm. 

C will result in a whole lot of repetitive code that can be minimized with templates. Consider a task of writing a 6507 VM for an Atari emulator. It has a 1 byte instructions, so short of transpiling or JITing, your best bet for good performance is to simply have a 256 instruction lookup table filled with function pointers. But a lot of the instructions in the table are going to be essentially the same code, with variations on which registers it operates on. Each instruction that operates on memory, for example, will have a version that operates on the zero page. And while you can write each operation function to include conditionals based on specific op code, that results in a lot of unnecessary thrashing on every single byte of code. In practice, what you want is to have each entry in your instruction map to point to a simple, linear code. That results in very good prediction performance. The C solution is to either write a separate function for each instruction, which takes a lot of time, hard to read, and hard to debug. Or you can write macros, which will take longer to write, be harder to read, and even harder to debug. :/ Or you just byte the bullet, write messy switch for each instruction, and hope that performance just won't matter for such a project. (Which is probably true, but it still stings.)

Your other option is to write templated code in C++. In this case, you can pull all of your register and memory code into common templated structs with getters and setters, which, being templated statics, will get inlined in each version of your operation. And now you write just one version of ADD instruction, templatized on memory/register type, that uses the same getters/setters as every other instruction. You end up with clean, readable code that is very cache and pipeline friendly, and performs as well as if you've written the interpreter in assembly.

Of course, if you're making your own scripting language, you can design its features around being friendly to simple C code. Whether or not that's a limitation in any particular case will depend. But there are definitely good reasons to bring out heavy guns for interpreter if you aren't squeamish about templates. The C++20 concepts will make this even better.

Link to comment
Share on other sites

7 hours ago, Gargamel said:

If you are thinking about writing your own programming language, have read this thread, and understood at least 80% of what each and every post was talking about, then you are probably read to tackle your own language.

If you have read this thread, and understood at least 80% of what each and every post was talking about, you've probably already realised that it's a whole lot easier and more productive to learn an existing language than it is to write a new one. :P

I can see the appeal as a learning exercise, but for practical use, why?

Link to comment
Share on other sites

I think OP wasn't aware initially. It happens that we ask questions that seem devious to the more experienced. That is natural.

People do invent old wheels every day, for fun, exercise, practice, private use, etc. No problem ... :-)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...