Jump to content

Making your own code


Recommended Posts

This is a strange question/subject but here it goes. If I'm interested in making my own code form such as my own version of c++ what is the requirement. How can I make a computer recognise and process the code for example if in my own coding language I said

>hi<

This would mean do not process anything inside the symbols how would I go about that

Another example if I said this in my own coding language

%5seconds%

This would mean

count/initiate a timer for 5 second

How can I make it scan folders and process the code and do two seperate process make connections to other forms of code. 

Link to comment
Share on other sites

The process of breaking source code down to tokens is known as lexing. Once that is done, a parser assigns meaning to the tokens. Here's a reasonable high-level overview of how one might go about defining a new programming language and creating the tools to work with it: https://medium.freecodecamp.org/the-programming-language-pipeline-91d3f449c919

Link to comment
Share on other sites

5 minutes ago, stibbons said:

The process of breaking source code down to tokens is known as lexing. Once that is done, a parser assigns meaning to the tokens. Here's a reasonable high-level overview of how one might go about defining a new programming language and creating the tools to work with it: https://medium.freecodecamp.org/the-programming-language-pipeline-91d3f449c919

Oh boy...

lol that's going to take a minute to wrap my head around fully.

Thanks

 

8 minutes ago, stibbons said:

The process of breaking source code down to tokens is known as lexing. Once that is done, a parser assigns meaning to the tokens. Here's a reasonable high-level overview of how one might go about defining a new programming language and creating the tools to work with it: https://medium.freecodecamp.org/the-programming-language-pipeline-91d3f449c919

The language I'm working on (even though it does nothing yet) is designed for aerospace and is based around operations  Identify folders to run seperate operations. I'm not doing that many variables... Maby I should

Link to comment
Share on other sites

Ohhh boy...

Compiled or interpreted language? Both are going to require a tokenizer and a parser, but the semantics are typically different. From what you've said it sounds like you're writing some kind of interpreter, since you're talking about scanning directories (which wouldn't make much sense for a compiled language). If you are in fact trying to make something useful for aerospace, I recommend you re-think this choice. Interpreted languages are typically far slower than compiled languages, which isn't good for the kinds of high-performance computing needed by aerospace. Additionally, you've already gone and assumed that you're going to be running on some system that has a directory tree. This isn't guaranteed - plenty of microcontrollers don't (and can't) run an operating system in the first place. Even if you're running on something with a directory tree, though, you'll have to deal with cross-platform compatibility.

You say your language is based around "operations". What, exactly, do you mean by this? Because depending on how you define these operations, you could easily end up with a language that isn't Turing-complete - i.e. a language that is essentially useless for computation due to its limitations (this is a generalization, but for our purposes is close enough). It sounds to me like what you're shooting for is some sort of grammar for defining the operation of a finite state machine, which wouldn't be Turing-complete.

Link to comment
Share on other sites

7 minutes ago, IncongruousGoat said:

Ohhh boy...

Compiled or interpreted language? Both are going to require a tokenizer and a parser, but the semantics are typically different. From what you've said it sounds like you're writing some kind of interpreter, since you're talking about scanning directories (which wouldn't make much sense for a compiled language). If you are in fact trying to make something useful for aerospace, I recommend you re-think this choice. Interpreted languages are typically far slower than compiled languages, which isn't good for the kinds of high-performance computing needed by aerospace. Additionally, you've already gone and assumed that you're going to be running on some system that has a directory tree. This isn't guaranteed - plenty of microcontrollers don't (and can't) run an operating system in the first place. Even if you're running on something with a directory tree, though, you'll have to deal with cross-platform compatibility.

You say your language is based around "operations". What, exactly, do you mean by this? Because depending on how you define these operations, you could easily end up with a language that isn't Turing-complete - i.e. a language that is essentially useless for computation due to its limitations (this is a generalization, but for our purposes is close enough). It sounds to me like what you're shooting for is some sort of grammar for defining the operation of a finite state machine, which wouldn't be Turing-complete.

I'm very new to the realm of computers. I figured I should give it a shot. I hate coding but I need it for my model rockets. Instead of using the traditional methods of coding I figured I would use my own code because why not. When I say operations what I mean is that if I wanted a rocket to pitch I would say

<

%+5seconds% trigger folder "engine1"

%+2seconds% trigger folder "engine2" 

%+5 seconds% trigger folder "engine3"

>

translation:

%_+seconds% is an operation that locks out the processing of the program until the time inserted has been completed off example %+5seconds% will mean the program will wait 5 seconds before doing anything else in the program

trigger folder_____ allows you to let a certain folder complete an operation without having to process the folder until it's needed. In reference to my example "engine1" would be in practice, throttle engine too 75%. "engine2" would be 50% throttle and "engine3" would be 100% throttle

< is the beginning of the program

> is the end of the program 

P.S. The program reads left to right and then down just like english.

In practice this code allows the rocket to throttle at certain rates at certain times with just 3 lines of code

P.S. I'm still working on the calculator system

It may not be perfect but using a similar system you could get to a moderate level of sophistication.

Also the primary flight code could only be Maby 10 pages of script with hundreds of folders than are only called to use once so it would most likely not be that difficult to calculate

Done ranting now

Link to comment
Share on other sites

Again, we have the XY problem here.

3 hours ago, Cheif Operations Director said:

I figured I would use my own code because why not

Because coming up with a brand new language is a lot more work than just taking a ready made language and compiler and work with stuff that's been bugfixed and developed for many years by individuals far more experienced and skilled than you.

 

3 hours ago, Cheif Operations Director said:

%_+seconds% is an operation that locks out the processing of the program until the time inserted has been completed off example %+5seconds% will mean the program will wait 5 seconds before doing anything else in the program

trigger folder_____ allows you to let a certain folder complete an operation without having to process the folder until it's needed. In reference to my example "engine1" would be in practice, throttle engine too 75%. "engine2" would be 50% throttle and "engine3" would be 100% throttle

Your "folder" sounds suspiciously like what is usually called a "function".

Do yourself a favor and pick an off the shelf microcontroller that has all the tools already made and ready for usage. Or better yet...

There are several options of entire development environments based on different microcontrollers, two of the most popular options for hobbyist are Arduino and PIC, based on Atmel's Atmega series micros and Microchip's stuff, respectively.

For example, for Arduino a command for the software to stop doing everything and wait for 5 seconds before doing something else is as simple as

delay (5000);

... but you should try to avoid using that approach, there are better ways of writing code that don't block everything else.

Link to comment
Share on other sites

39 minutes ago, Shpaget said:

Again, we have the XY problem here.

Yup.  Dead on here.  

 

Why reinvent the wheel? 

LIke Shpaget said, there a numerous applications already out there that do what you want already.

Anything you create, in a new language, has two goals to hit: 

1) It must be easier to create it from scratch than learn an already existing one.

2) It must be superior on it's intended function than anything already existing.

If you can't reach both of those goals, then don't do it.   Unless the goal is to create a new language from scratch for the sake of it, there really isn't a reason to. 

Take a look at Arduino.  It's super easy to use, pretty dang powerful with some practice, and really has no limits.    It's also cheap, really cheap.  For almost anything you'd want to do with a model rocket, an arduino or one of it's cousins would be perfect for it. 

They make beginners Arduino Kits, take a look at picking one up.  The Arduino for Dummies book is excellent, and if you don't want to buy a book, there are many many tutorials on the web. 

Link to comment
Share on other sites

i wrote a crappy virtual machine and macro-assembler of same (which i havent finished so i still have to hand assemble things). everything was broken down into 2 byte commands, often in command:payload order. it had 4 8-bit data registers and 2 8-bit pointer registers. 2 more bytes served as the status/control registers. address space is mapped out to 256 byte pages. like i had a 512 byte eeprom that was divided up into 2 pages. first one being read only and the second being writable. i also threw in a page of ram. i have it setup to handle 16 pages right now but i can quadruple it before its a problem (i have room in the scr). the page system made it really simple to add memory devices. in fact one of the first programs i wrote was a driver for an sd card reader that fit in one page. all the driver really did was turned on the spi bus and sent the right bytes to configure a device on the end, and then one more command to map a page to it. after that i could load code and data from anywhere. and it need not be limited to memory devices, like you could do a driver for an spi radio and get code remotely.  it only had 92 instructions and is actually very simple. 

unfortunately i decided i didnt like the 2 byte commands and started rethinking it with one byte commands, with variable byte payloads. i stopped working on it shortly there after. it was already kind of workable and did what i wanted (storing control mappings for input devices and rc stuffs). never finished the macro assembler. it could handle, subroutines, but that was about it, i had plans to handle control structures and jumps, i wanted to have a whole language but i lost interest. the compiler was written in lua of all things, mostly because i love its pattern matching libraries, great for parsing assembly-like mnemonics and formatted data, and convert it to 2-byte opcodes which it stores to an "executable" file that can be uploaded to the mcu if need be. routines existed to load code over serial if i wanted to copy over new code. i would have to short a pin to ground (with a button) on start up to get it into a serial console. then i could directly read and write to any attatched memory pages. this is why the eeprom became the boot sector of sorts, since i could load code to it, reboot and start with different code. 

i mostly intended to use it for control mapping. one of the downsides to the mcu i was using was that you could only load code from flash. you could write a bootloader to let you page in code from other sources into flash, but its generally a bad idea to waste a write cycle every time you changed your profile (mtbf was only 10k write cycles). but i didnt need the code to be fast so i went the interpreter route. eeprom is rated for 10x more cycles than the flash for some reason so i could use that for storage of settings and code. memory would just be a page of ram. i could add pages for more storage locations or even device io as needed. this was to replace the old state machine which just stored and loaded config files to the eeprom. there was a state machine for handling all the ins/outs, a config file parser and a serial command parser as well, all this is replaced by a tiny virtual machine that took up less space than the strings for the old serial command parser. 

Link to comment
Share on other sites

12 hours ago, Cheif Operations Director said:

I'm very new to the realm of computers.

Then writing your own computer language probably isn't the easiest way to familiarize yourself with them.

Typically, the aerospace industry uses embedded computers that use C and Ada. They are designed for reliability, readability, and certification, which is expensive. This means that once the code is properly validated and certified, then they will avoid touching it unless it is absolutely necessary.

There are several layers to what you want to do:

- First you need some sort of low-level bus control software between the computer hardware and the flight controls that you want to activate (control surfaces, motors, hydraulics, engines, etc...).

- Then you need to code a layer that will convert functions in the code into those hardware output commands ("engine throttle 35%" means reconfiguring the turbopump, opening or closing valves, checking that values are valid, etc...)

- Then you need to the code the layer that takes input from the controls and converts those commands to call the functions above. The control can be a physical button or lever, a series of sensors, or a programmed flight script.

The script parser and interpreter, and designing the syntax, are probably the easiest bits to code once you have laid out the proper functions to call. Of course, if you have zero coding experience already, this is all going to be extremely difficult.

 

6 hours ago, Nuke said:

unfortunately i decided i didnt like the 2 byte commands and started rethinking it with one byte commands, with variable byte payloads.

Sounds like the "verb" + "noun" command architecture for the Apollo AGC.

 

Edited by Nibb31
Link to comment
Share on other sites

Spoiler

When I had quickly created an interpreted script language for some particular task, I reinvented Fortran 77.

It's strange because in childhood I used Fortran IV

Arduino is good and uses a C-like language.

Edited by kerbiloid
Link to comment
Share on other sites

This wouldn't be at all easy.  There are a lot of things to work out before you can start coding an interpreter or compiler.

Firstly - what computer hardware do you want to program?  Is the computer physical or virtual (or perhaps an emulation)?  How will the computer interface to things in the "real" world - or put another way, what will the computer control?  What firmware / monitor / operating system will run on the computer under your ?  What programming tools does that offer?  How will you store and retrieve the programs?

If you want a simple computer to control real-world objects, then you might try one of the Arduino family.  That is more in the "microcontroller" end of the hardware spectrum - but it has lots of support for interfacing to hobby electronics and lots of tools.  As @kerbiloid says, it even comes with a ready-made language based on C.  If you need something more like a PC, then take a look at the Raspberry Pi range.  That is a Linux based micro computer with screen / mouse / keyboard and ethernet.  There are various programming tools available, one popular one is Python.

From there, you can start to get experience in writing programs and work your way up to something that can read in a set of instructions as text, "parse" that (remember you need to handle any mistakes in the text) and then perform actions based on what you "parsed".  That is the essence of writing an interpreter.  You can go a step further - if you then save the symbolic representation "p-code" to a file, you can then load and execute the p-code without needing to re-parse each time.  The next step up is to convert the p-code into the native machine code of the computer.  Then you've written yourself a compiler.

(Yes, for the Computer Scientists among us, that's very simplified).  Something like this would make a good undergrad project, definitely not for beginners.

Probably you'd find it easier to write the programs in one of the provided languages than make up your own, but let's not stifle creativity, eh? :)  Whatever you decide to do, have fun!

Link to comment
Share on other sites

Whenever I write my own scripting language, what comes out is usually some flavor of lisp. I'm just usually too lazy to write an expression parser more involved than that. RPN makes ones life way easier. I've written assemblers as well, including for my own VM, but that's way easier. Of course, real compiler authors would say that none of it even counts until you have optimization and linking.

But yeah, learn to program in C and x86-64 Assembly. Then you can start reading books on compilers to understand how to make your own. Trying to come up with your own language before learning how existing ones work is definitely a cart before carriage situation.

Link to comment
Share on other sites

3 hours ago, K^2 said:

RPN makes ones life way easier.

(Reverse Polish Notation) - quite right; all nicely stack-oriented.  There's even a language whose entire syntax is based around RPN, called Forth.

Link to comment
Share on other sites

If you want to run this on an actual rocket, you might want to just implement this with functions in C or C++ and use an arduino. Arduino has delay() built in, which already does exactly what you want, and you could just define functions to do anything else. 

Of course, even in trying to write your own language you would certainly learn a lot about programming, so if you're not concerned about making your rocket work quickly, it would work.

Link to comment
Share on other sites

The easiest option is probably to fake it by embedding an extremely simple interpreter in your own C program on Arduino. Something like (beware, rusty buggy off-top-of-head 1990s-style C incoming)...

void runcommand(const char *command)
{
  // Messy tangle of ad hoc command interpretation logic here
}

int main()
{
  const char *mycommands[] = {
    ">hi<",
    "%5seconds%",
    "%+5seconds% trigger folder \"engine1\"",
    /* etc. */
  };
  for (int i = 0; i < sizeof(mycommands)/sizeof(char*); ++i) {
    runcommand(mycommands[i]);
  }
}

That way you don't have to worry about a true parser or code generation or a symbol table, etc. You could write logic to implement the specific simple commands you want (basically a translation layer on top of Arduino), and then the "program" you want as a big array of strings. For an amateur project this would be fairly workable, if likely to turn into a coding style nightmare after about 5 minutes.

(EDIT: In case it matters, I release the above code into the public domain. You can do what you like with it.)

Edited by HebaruSan
Link to comment
Share on other sites

4 hours ago, Vexillar said:

There's even a language whose entire syntax is based around RPN, called Forth.

Indeed, but it feels a bit too much like BASIC for my tastes. I like the option of introducing some priority via parentheses, hence Lisp-like rather than Forth-like outcome.

Link to comment
Share on other sites

11 hours ago, Nibb31 said:

Sounds like the "verb" + "noun" command architecture for the Apollo AGC.

thats pretty much the same thing (also zork which uses its own vm). you couldnt change the source code once the old ladies weaved it into core rope. but you could include a virtual machine in that code which the astronauts could work with through the dsky module. you could hear in some of the audio transcripts nasa working the astronauts through a programming operation over the radio. like the core ropes i cant change the contents of program memory at run time (i can write a bootloader so i can load part of the flash from elsewhere, but its strictly a boot time operation and you cant use it once you start running code). but i can include some basic rules for talking to the host processor. its mostly commands followed by static data or commands followed by pointers to the object you want to operate on, single commands that need to be padded out with an extra byte. one downside to the 2-byte fixed size command i was using. 

thats why i want to encode the expected length of the arguments in the command byte if a certain init sequence is present. but if the command needs to be followed by 3 bytes it can make sure all 3 are present before attempting to process the next command.  but for now the 2 byte commands kind of work. for 16 bit numbers it could take up to 7 cycles just to add two numbers (4 byte loads, 1 math op, 2 byte stores), and each of those commands is 2 bytes for 14 bytes. using length encoding would make that take one up to set up the registers, one op to load all the data, one op to do the math and another op to store it, where 2 of those are 1 byte commands, one is a 5 byte command (loads all four registers), or with pointers it can be a 3 byte command, and the store is a 2-byte command+pointer, best case scenario is 7 bytes. given i only had a few hundred bytes of program space for the virtual machine, the denser format would work out better. i just need to implement it. since much of the code is x=y stuff, id need fast load immediate commands that wouldnt waste an equal number of command bytes in the process. pointers are just one byte and only work on the current page of memory only. to move data from one page to the other you would need to load it into the 4 registers, change page, and read it back out again, 32 bits at a time, there are no inter-page pointers. i might standardize a program header to manage inter page, inter program, communication or maybe have a load byte from page command. or maybe do some unix-like piping from one page to another. 

Edited by Nuke
Link to comment
Share on other sites

5 hours ago, HebaruSan said:

The easiest option is probably to fake it by embedding an extremely simple interpreter in your own C program on Arduino.

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. 

Edited by Gargamel
Link to comment
Share on other sites

On 8/8/2018 at 3:33 AM, Shpaget said:
On 8/8/2018 at 12:17 AM, Cheif Operations Director said:

I'm very new to the realm of computers. Instead of using the traditional methods of coding I figured I would use my own code because why not.

Because coming up with a brand new language is a lot more work than just taking a ready made language and compiler and work with stuff that's been bugfixed and developed for many years by individuals far more experienced and skilled than you.

This.

Link to comment
Share on other sites

7 hours ago, 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. 

But "how do I build a program in C" is not the OP's question. We can advise against what he's asking all we like, but nothing is as effective as trying it and seeing for himself.

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