Cosmos: How do I make a way for users to create and execute programs?

Created on 18 Jul 2020  路  10Comments  路  Source: CosmosOS/Cosmos

I think it's going to be difficult, but it's essential to every OS, or else the OS will be limited.

My OS is CLI-Based, and I already made a simple text editor, how do I make a code editor for programs and a way to execute these programs?

Most helpful comment

You could read the lines of a program file to an array and then evaluate each line as a command. So for example:
string[] lines = System.IO.File.ReadAllLines("program.exe");
int i = 0;
while(i < lines.Length) {

if (lines[i] == "sayhello") {

Console.WriteLIne("Hello!");

} else if (lines[i] == "beep") {

Console.Beep();

}

}
This example gives you 2 commands:
sayhello to print "Hello!"
and beep to make the system beep.
You would need a filesystem implementation for this to work as it reads a file.

All 10 comments

You can execute a flat binary that can interact with your OS with the POSIX interface or custom syscalls (like here: https://github.com/aura-systems/Aura-Operating-System/pull/38) but it's really not secure, you let the program take the total control of your machine, Cosmos doesn't have usermode, paging..

You can also write an ELF parser or use another one (https://github.com/Myvar/CosmosELF), but same problem not secure at all.

Another way to execute a program is within a VM, you can see this project: https://github.com/kozit/KsIL

https://github.com/kozit/KsIL not longer supports cosmos but https://github.com/strobeLANG should still work (not has not had an update since 2017)

o but looking at it only some part of system.Linq and Dictionary need to be pluged (or replaced) and in system.cs Parallel.ForEach(CPUs, cpu => cpu.Tick()); just needs to be made a for loop then it would work in cosmos

You could read the lines of a program file to an array and then evaluate each line as a command. So for example:
string[] lines = System.IO.File.ReadAllLines("program.exe");
int i = 0;
while(i < lines.Length) {

if (lines[i] == "sayhello") {

Console.WriteLIne("Hello!");

} else if (lines[i] == "beep") {

Console.Beep();

}

}
This example gives you 2 commands:
sayhello to print "Hello!"
and beep to make the system beep.
You would need a filesystem implementation for this to work as it reads a file.

You could read the lines of a program file to an array and then evaluate each line as a command. So for example:
string[] lines = System.IO.File.ReadAllLines("program.exe");
int i = 0;
while(i < lines.Length) {

if (lines[i] == "sayhello") {

Console.WriteLIne("Hello!");

} else if (lines[i] == "beep") {

Console.Beep();

}

}
This example gives you 2 commands:
sayhello to print "Hello!"
and beep to make the system beep.
You would need a filesystem implementation for this to work as it reads a file.

It's a good idea, but that'll take a long time to make, what about the if/switch statements or the for and foreach loops, how do I do all of this?

Oh! I forgot after checking what the command is and running it (after the big if/else checker) you need to put i++. To increment the counter so it will go to the next line. As for the if/switch stuff basically you keep adding else if blocks until you have all of your commands. The loops I messed up you need to increment your i variable so it moves to evaluate the next line. And as for time? I've used this method in the past and it took quite a while but afterwards I had a very usable scripting language. There is probably a faster way but this is what I came up with.

Well it depends upon what executable system you are using. Is it MS-DOS exe, Linux ELF, or have you rolled you own? Then you need to get the tool-chain together. The tool chains for exes and ELFs already exist as you would expect.

The tool chains may need work to get working on your OS, but it is not too difficult to do - but it can be time consuming.

This is incredibly similar to Batchfile or Bash. But if you put methods like StartsWith, or Split to use you can get something similar to Java or C++.

You can write a simple stack based vm

@AAli107

It's a good idea, but that'll take a long time to make, what about the if/switch statements or the for and foreach loops, how do I do all of this?

You'd need to write a source code parser.
Essentially, you could do the following:

  • Create a text editor (you already have done this!)
  • Create a compiler
  • Create a runtime environment + application loader

A user would create a text file, in which he stores his code (you can define your own programming language at this point).
You parse your programming language based on your own grammar.
Take a look at http://timjones.io/blog/archive/2014/04/13/writing-a-minic-to-msil-compiler-in-fsharp-part-0-introduction . This is a fantastic resource for compiling C-code using F#. However, the same can be implemented in pure C#.
Store the generated MSIL instructions into a binary file.

Now you need to create an application loader. This is a "VM" like the JVM or .NET VM. It manages threads, resources, the heap and stack purely inside the userspace. The VM takes the compiled instructions and runs them inside a "safe" environment (thus not compromising the system when executing invalid instructions).

This kind of architecture is incredibly complex, but it is more or less your only path.
An alternative would be to write some kind of interpreter (e.g. like Python). This is essentially a big if-statement running iteratively over all lines of your input source code.

Additional resources

If you need some inspiration, I wrote an AutoIt-Interpreter in C# and F#: https://github.com/Unknown6656/AutoIt-Interpreter (AutoIt is a BASIC-style language).
You can see the AutoIt expression parser here: https://github.com/Unknown6656/AutoIt-Interpreter/blob/daddy/new/AutoItParser/ExpressionParser.fs

Take a look at https://en.wikipedia.org/wiki/LR_parser for the basics of LR expression parsing.
I have a fork of the Piglet parser library here, if you need it: https://github.com/Unknown6656/Piglet

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lammaurice034 picture lammaurice034  路  5Comments

geomtech picture geomtech  路  9Comments

365Tito picture 365Tito  路  10Comments

fanoI picture fanoI  路  8Comments

seba4316 picture seba4316  路  8Comments