Jump to content

Making your own code


Recommended Posts

2 hours ago, Cheif Operations Director said:

Out of this thread I still do not know where to even begin writing a compiler or interpreter. Where do I find this? How does it work?

With respect to anybody who has this knowledge and skill to do this, for the average person capable of doing it, usually requires a 4 year computer science degree.    Two of my college roommates were a Comp Sci Major and a Comp Engineering major.   They teamed up to do their senior projects.  The CompE guy built the computer from scratch.  By scratch I mean he took a box of  8088 chips and other electrical components and designed and built a computer.  My CompSci buddy then wrote the OS to run the computer, including a new language (based on Lisp I believe) and a compiler for it so that they could write new software if they wanted. 

Of course there are people out there who haven't gone the four year route and have obtained this level of skill.  But they didn't do this overnight.  It took a lot of work and study. 

An arduino is the best choice for you here, IMO.   Go grab the Arduino for Dummies book.  That's not an insult to you, Any new skill I want to learn, I usually grab a dummies book first.  They usually make the learning process fun and easy.  Pair the book with an arduino starter kit, play with it and the book's examples for a hour a day, and by the end of two weeks, you will know 90% of what your rocket's hardware is going to be, how you'll do it, and how to get it to talk to the rest of your devices. 

The language used by an Arduino is C++, but it's so simplified and easy to read, that it is a breeze to learn.   The majority of simple arduino programs is analog, turning stuff on or off.  And then you will want to add some servos for your fins, and that too is easy to code. 

I love the simplicity of the arduino.  My desk lamp is an arduino.  I built a motion activated, scheduled thermostat for the barn using an arduino; it replaced the 'normal' one mounted on the wall. Dozens more projects I have done.  

But I am not a coder.   I am a hack at best.   You just have to learn the absolute basics to be able to produce a competent arduino sketch (program).  The rabbit hole can go very very deep with it, but the barrier to entry is almost non existent for an arduino. 

Link to comment
Share on other sites

It sounds to me rather like someone saying, "I don't want to learn anatomy, pharmacology, pathology, or any of that. I just want to be a doctor. Surely I can do that in a week or two, as long as I only concentrate on treating 35 year old men." Just because you limit the application of a skill to a small area does not necessarily make the skill itself trivial to learn.

Link to comment
Share on other sites

One of my friends recently graduated from university, majoring in CS. His senior project? A compiler.

You have three choices to achieve your goal of controlling a rocket electronically:
1. Use an off-the-shelf language to write a control program
2. Use an off-the-shelf language to write an interpreter or compiler for your custom language, and then write the control program in your custom language
3. Write your control program in assembly code

The second involves the most effort and yields results either about the same as the first but less efficiently (if interpreted), or about the same as the third (if compiled). If you choose to compile, you'll essentially have to do all the work of 1 (that fancy language has to interface with the rocket somehow), 3 (you need to make it run on the hardware somehow), and learn how to write a compiler. If you choose to interpret, you'll need to do all the work of 1 and learn how to write an interpreter. In the end, a well-structured implementation of option 1 is probably your best bet at minimal effort.

However, if you're dead-set on making your own language, your first step is to construct a language specification. This means describing your language in enough detail that a five-year-old banging on a calculator could execute it. This is necessary so that you can remember it when you write an interpreter or compiler for it, when you write the control program itself, and when you show it to your mates. After that, you need to make some way of turning a text file into a collection of commands for the processor. This might include writing a program (with code!) to do so, which is known as either an interpreter or a compiler, depending on whether it reads the input at the same time as it executes it, or it might involve many long hours translating the text into assembly code based on your reference.

By the end of this, you might actually have a nice way of representing control programs! Also, you'll have learned how to code, and maybe that means you won't hate it so much.

On the note of not hating coding so much: I find it's good practice to get some foundational knowledge in the language you've picked, and then find problems to solve. Programming just to program isn't really fun, at least for me. But programming because there's a problem you need to solve? Well, once you've finished, you have a solved problem! And, if you've done it right, your solution will work for other, similar problems. There are few things more satisfying than seeing your ungainly heap of code rear up, devour a complicated math problem, and then spit out the answer. Just pick a problem you know how to solve and a documentation site you trust, and you'll have a solution in no time. Don't get frustrated by always forgetting semicolons or other apparently-trivial details—with practice you'll remember them, at least most of the time. Remember that once you understand how to make your computer do things, you can make it do essentially whatever you want.

For the purpose of providing problems, I recommend Project Euler. It's essentially a giant pile of math problems, which range from incredibly simple to confoundingly complex, with a few "okay, I know how to solve this, but how do I do it programmatically?" in between.

Edited by 0111narwhalz
Link to comment
Share on other sites

8 hours ago, wumpus said:

The C++ spec is 1605 pages long.  While being able to program in the "pigeon english" equivalent of C++ might take little more than C, being able to parse code using all the features may be as hard as doing the same with a human language.  It certainly is an outlier, and its popularity has turned it into the proverbial kitchen sink.

Hi. True. But i was talking about C as well, not K&R because it is history but C11, prepares better for a future step to C++.

1600 pages, i assume much if not most of it is STL, aren't really much compared to all the stuff of a life natural language. That easily fills a few decimeters on a shelf for grammar, vocabulary, dictionarie, etc. pp. It does for me with my Spanish after having moved.

 

Be it as it may, without some coding knowledge there is no way of accomplishing what OP wants to. I know it looks all so nice and quick on youtube etc. but real life is hard and requires a few months to years of concentration :-)

Edited by Green Baron
Link to comment
Share on other sites

On 8/8/2018 at 11:22 PM, Gargamel said:

But that's just re-inventing a wheel, and attaching it as a training wheel to the wheel you designed it on. 


// Messy tangle of ad hoc command interpretation logic here

By the time they get that part figured out, it would have been much much easier to just use C in the arduino. 

 

As an exercise in learning how to make your own language, I'd say go for it.  There's no reason not to try!   But since you want a practical use from it, off the shelf stuff might be the better idea. 

this stuff is pretty easy to do in c. you just use a big jump table so you can feed it a command byte and get the pointer to the function that handles it. you can also use the command byte to determine how many other bytes to load. so the command byte is loaded, in that value is encoded the length of the arguments field from 0-4 bytes. its set up so a simple bitwise mask returns the argument length field, and the value itself is just an index into an array of pointers. a very small amount of overhead for each instruction. 

Link to comment
Share on other sites

5 hours ago, Gargamel said:

With respect to anybody who has this knowledge and skill to do this, for the average person capable of doing it, usually requires a 4 year computer science degree.    Two of my college roommates were a Comp Sci Major and a Comp Engineering major.   They teamed up to do their senior projects.  The CompE guy built the computer from scratch.  By scratch I mean he took a box of  8088 chips and other electrical components and designed and built a computer.  My CompSci buddy then wrote the OS to run the computer, including a new language (based on Lisp I believe) and a compiler for it so that they could write new software if they wanted. 

Of course there are people out there who haven't gone the four year route and have obtained this level of skill.  But they didn't do this overnight.  It took a lot of work and study. 

An arduino is the best choice for you here, IMO.   Go grab the Arduino for Dummies book.  That's not an insult to you, Any new skill I want to learn, I usually grab a dummies book first.  They usually make the learning process fun and easy.  Pair the book with an arduino starter kit, play with it and the book's examples for a hour a day, and by the end of two weeks, you will know 90% of what your rocket's hardware is going to be, how you'll do it, and how to get it to talk to the rest of your devices. 

The language used by an Arduino is C++, but it's so simplified and easy to read, that it is a breeze to learn.   The majority of simple arduino programs is analog, turning stuff on or off.  And then you will want to add some servos for your fins, and that too is easy to code. 

I love the simplicity of the arduino.  My desk lamp is an arduino.  I built a motion activated, scheduled thermostat for the barn using an arduino; it replaced the 'normal' one mounted on the wall. Dozens more projects I have done.  

But I am not a coder.   I am a hack at best.   You just have to learn the absolute basics to be able to produce a competent arduino sketch (program).  The rabbit hole can go very very deep with it, but the barrier to entry is almost non existent for an arduino. 

i usually use arduinos because you can get clones for $3 a pop off ebay. i use the 3.3v atmega32u4 based arduino clones mostly for doing usb hacking and custom devices. i usually want something a little better though, something fast, 32-bit with enough memory to do everything. ive actually been playing with the esp32 (formerly 8266) boards because they are also very cheap (~$5) and come with wireless networking. esp32 also has 520k sram, usually 4megs of flash (some as high as 16 or as low as 2) and bluetooth. adcs are 12 bit, and a couple 8-bit dacs as well. you can just use the arduino ide to program on them. 

Edited by Nuke
Link to comment
Share on other sites

9 hours ago, Green Baron said:

1600 pages, i assume much if not most of it is STL, aren't really much compared to all the stuff of a life natural language.

I've never looked at specs as an actual book, or more than a few sections at a time. But I'd be surprised if 1600 pages is more than just core language specs. Standard libraries are going to bloat that by factor of 10 at least. I'll see if I can find a digital version somewhere.

Edit: I was completely wrong. The full standard is not freely available, but there's a 1440 page draft that should be pretty close. Core language is about 400 pages of that, rest being compatibility and libraries. STL (containers) is only about 100 pages, though.

There's a lot more standard C++ incoming, though. File systems, networking, co-routines, modules, concepts. C++20 standard is going to be a lot larger than C++17.

Also, there's difference between understanding the standard, which you need to know if you're writing a compiler, and in some cases, when you're doing optimization/debugging, and understanding nonsensical edge cases like this, which you need to survive in C++ dev environment.

std::string text{"Hello_World"};
for (auto i = 0; i[&text[0]]&='_'; i=-~i);
std::cout << text << std::endl;

Edited by K^2
Link to comment
Share on other sites

In order not to chase newbies off ;-), the usual C++ books of which i have 3 here (Stroustroup (11) and two German ones, Breymann (14) and a more modern one (on 17) which i found super) are in that area 1200 -1500 pages (*), with 600-800 500-600 of that the being STL (including all the containers, iterators, etc.). If, like me, you're basically familiar with classic C and know the fundamental concepts of oop, you can do reasonable things like a naive renderer or an interpreter, what OP is planning after a few (not many) months, 2-3 hours practicing every day and additional research/reading on internet or from books. One doesn't need Lambda magic, though it's fun and actually not complicated.

Usual classes of a natural language take longer, much longer ;-) And it takes 2-3 years (for me) until one can speak somewhat freely. It definitely takes less time learning any formal language, including C++. Which isn't that hard.

My intention is actually not to chase the aspiring guys and girls away, but to get them to do something meaningful that will make them feel like having accomplished something. Starting with something like C++ is probably for most of us too steep a hill, anyway for most APIs you need classic C, much IO is still classic C style and will remain so for the overseeable future. You know i am not a programmer, i just code for fun. We should give others the opportunity to find that fun, too. Especially when they ask.

That's why ;-)

(*) Edit: which includes some padding with examples and exercises.

Edited by Green Baron
Corrected page numbers to avoid nitpicking ;-)
Link to comment
Share on other sites

I don't disagree. I could probably write a mostly-compliant C compiler in a couple of weeks. Not an optimized one, mind. Just something that takes C sources and spits out an executable that will do what the specs say it should. I wouldn't touch a C++ compiler with a very long stick. I'd be ok with working on some components as part of a compiler team, but you have to be either extremely naive or extremely stubborn to take on a project of writing a C++ compiler from scratch solo. It'd be months, if not years of work for very questionable gain.

In contrast, I have written simple interpreters, and even compilers, in an afternoon. Brainf**k compiler, as an extreme example, took me just a few hours to write, debug, and do some fun projects with.

Link to comment
Share on other sites

Hehe. How do you build a skyscraper ? Or cook a 4 course menu ?

I'd say, get a computer and install Linux. If it is debian based, install the package "build-essential" and get going. Linux costs nothing, has everything built in including all documentation you want. What's not built in is available from repositories. Choose a tutorial or better get a good book. If C, be sure it covers C11 standard, not some computer folklore :-) Sooner or later you'll have one or two books on the shelf because often times you'll need to look for a specific thing or procedure. Join a programming forum.

I assume you will have to start with the basics, command line, editor, environment, invoke the compiler, etc. That'll accompany you all the way, even when you use an IDE.

You'll have a lot of fun in this early phase, but it'll take a few days or maybe even weeks until you can oversee the complexity and decide if you like to go on. Otoh, the time is not wasted. Compared to staring at a brick wall ;-) There are 10 sorts of people in the world ...

Edited by Green Baron
Link to comment
Share on other sites

On 8/10/2018 at 2:57 AM, Cheif Operations Director said:

I do not want to learn other programming languages, I already told you I would rather stare at a brick wall for an hour I will gladly learn about computers and programming but no specific language unless I must.

This is getting like talking to a brick wall. Any programming language needs something called an interpreter, which is a computer program that converts your personal language into commands that are understood by the hardware.

How are you going to code that interpreter if you don't want to learn how to code ???

18 hours ago, Cheif Operations Director said:

How to I write an interpreter for my own code

You learn how to code one. This probably requires several years studying computer science and various languages. By that time, you will have realized that there is no point in writing your own language.

You cannot create your own computer language without learning to code first. It's as simple as that.


Which is why people here keep telling you to get an Arduino or a Raspberry Pi and learn C or Python. Why are you asking for advice if you don''t you listen to it?

Edited by Nibb31
Link to comment
Share on other sites

1 hour ago, Nibb31 said:

[...]

You learn how to code one. This probably requires several years studying computer science and various languages. By that time, you will have realized that there is no point in writing your own language.

You cannot create your own computer language without learning to code first. It's as simple as that.


Which is why people here keep telling you to get an Arduino or a Raspberry Pi and learn C or Python. Why are you asking for advice if you don''t you listen to it?

A very simple interpreter would probably take less than several years of study to make. Especially if you skipped conditional logic and loops.

Link to comment
Share on other sites

1 hour ago, Nibb31 said:

This is getting like talking to a brick wall. Any programming language needs something called an interpreter, which is a computer program that converts your personal language into commands that are understood by the hardware.

How are you going to code that interpreter if you don't want to learn how to code ???

You learn how to code one. This probably requires several years studying computer science and various languages. By that time, you will have realized that there is no point in writing your own language.

You cannot create your own computer language without learning to code first. It's as simple as that.


Which is why people here keep telling you to get an Arduino or a Raspberry Pi and learn C or Python. Why are you asking for advice if you don''t you listen to it?

Which language!

Ill look into it but just telling me "learn to code" is useless for me writing an interview interpreter! Which language do I need to learn to write the interpreter

binary? C? Python?

Something else I do not even know about?

If I open up Notepad right now where do I begin?

Im not saying I will not learn to code

If I did not want to learn to code I would not be asking this question! I'm just saying that if I'm going to have to learn to code I want to make it useful and writing my own code will make programming other things much simpiler. 

 

Link to comment
Share on other sites

There is no need to shout, we will help if you let us and accept the help. Is was said that C or Python can serve your needs (and of course many others, but these have an overwhelmingly large base). Also, a package with one of the mini computers was suggested for you to begin with. So, why not go out and get one and follow the instructions that come with it ? Or order it somewhere ?

When you open your Notebook (Windows or Linux ?):

- open Browser

- type in the search field "C programming tutorial"

- skip over all the c++ stuff for now

- choose a tutorial

- follow the steps there

 

I cannot recommend an online tutorial for C because i learn better from books. Maybe somebody else can. But i frequently use reference pages to look things up. Also, there are a lot of books on C. Just make sure that it covers the C11 standard.

Also, at some point (the earlier, the better) you will need some more info of how to gain control over your operating system. Again i mention here Linux because it is extremely well documented and help is absolutely free.

 

For Python, in principle the same applies.

 

Edited by Green Baron
Link to comment
Share on other sites

3 hours ago, Cheif Operations Director said:

Which language!

Ill look into it but just telling me "learn to code" is useless for me writing an interview interpreter! Which language do I need to learn to write the interpreter

binary? C? Python?

Something else I do not even know about?

C or C++.

Link to comment
Share on other sites

On 8/11/2018 at 10:39 AM, Cheif Operations Director said:

Which language!

Ill look into it but just telling me "learn to code" is useless for me writing an interview interpreter! Which language do I need to learn to write the interpreter

binary? C? Python?

Something else I do not even know about?

If I open up Notepad right now where do I begin?

Im not saying I will not learn to code

If I did not want to learn to code I would not be asking this question! I'm just saying that if I'm going to have to learn to code I want to make it useful and writing my own code will make programming other things much simpiler. 

I'm not sure which is best, but I personally learned Python first, from this book: https://www.amazon.com/Hello-World-Computer-Programming-Beginners/dp/1617290920\

Python is nice because it a minimum of fussy syntax to learn. But C is more powerful, more relevant to embedded systems and arduino, and not very much harder to learn.\

Luckily, whatever you choose to learn will also teach you a bunch of stuff that will transfer over to other languages.

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...