# Engine - Yet Another Programming Language

بسم الله الرحمن الرحيم  
اللَّهُمَّ انْفَعْنِي بِمَا عَلَّمْتَنِي، وَعَلِّمْنِي مَا يَنْفَعُنِي، وَزِدْنِي عِلْمًا

Although there exist many programming languages on earth, This will not weave my determination to announce my new programming language ( `Engine - The Next Generation Of Programming Languages` ) ✌️

wanting to create something new and to deepen my knowledge, I have been thinking why not create a new language? with new features and designs?

[Engine](https://github.com/engine-lang/engine) is a programming language that is based on `Rust` programming language.

## Introduction

Engine is a new programming language which it's compiler is based on Rust programming language. This has many benefits:

* Rust is fast. with memory safty guaranties
    
* No Garbash collection model.
    
* You control over your details.
    
* It actually can be used as
    
    * Compiler
        
    * Interpreter
        
    * Byte Code Generator (That compiles your code into a byte code file)
        
    * Byte code executer (Interpreter that executes your code)
        

## Installing

First, clone the repo

```bash
>>> git clone git@github.com:engine-lang/engine.git
```

Then create an engine file

```bash
>>> touch test.en
```

and put this code

```ex
string variable_1 = "hello"
print(variable_1)
string variable_2 = ", world!!!!"
print(variable_2)
```

### Run engine as Interpreter

You can run Engine as an interpreter on the file by just running

```bash
>>> cargo run "test.en"
hello, world!!!!
```

### Run engine as a Compiler

You can run Engine as a compiler which will generate an executable.

```bash
>>> cargo run "test.en" -e
```

it will generate an executable file called `test`, and to run it

```bash
>>> ./test
hello, world!!!!
```

### Generate byte Code

You can generate a byte code like this

```bash
>>> cargo run "test.en" -b
```

it will generate a file called `test.en.byte` in this format

```ex
0:EngineByteCode:v0.1.0
1:Assign:string:"temp_stack1_variable_1":"hello"
2:Assign:string:"stack1_variable_variable_1":""
3:Convert:string:"stack1_variable_variable_1":"temp_stack1_variable_1"
4:Print:"stack1_variable_variable_1"
5:Assign:string:"temp_stack1_variable_2":", world!!!!"
6:Assign:string:"stack1_variable_variable_2":""
7:Convert:string:"stack1_variable_variable_2":"temp_stack1_variable_2"
8:Print:"stack1_variable_variable_2"
9:End:
```

you can then use it for your specific cases, or you can run it using engine vm

### Run engine as VM(Virtual Machine)

To execute the engine byte code file, you can run

```bash
>>> cargo run "test.en.byte" --vm
```

it will interpret the file

\_\_

## Comments

```ex
# This is a single line comment
/*
    This is
    multi line
    comment
*/
```

\_\_

## Defining Variables

```ex
bool variable = True
bool variable_1 = False
bool variable_2 = False || True

int variable_0 = 5
int variable_1 = 6 + 5 - 7 * 7 / 2 + (
    5 - variable_0 * ( 6 % 2 )
)

variable_1 += 5

double variable = 6.2 + 8
double variable_2 = 0.1 - 0.4

char variable = 'H'
char variable_1 = "h"

string variable = "Hello World"
string variable_1 = 'Hello world!!'
string variable_2 = "Hello" + " World" + ',' + ' Dude!!!'
```

### Define variable without writing the type

You can define variables without writing the variable type like that.

```ex
var new_variable = True
```

or directly

```ex
new_variable = True
```

the language will try to infer the type of the variable and then define a new variable based on the inferred type, so the new variable will be of type boolean.

### A Note

take special note that if you define a character like this

```ex
new_variable = 'H'
```

the language will consider it as a string.

\_\_

A new line is the line terminator for the statement (like semicolon in c and C++)

\_\_

## Precedence

This is the precedence of the operators

1. ( True, False, Int Number, Double Number, Variable, Character, String, Open Parentheses - Close Parentheses - () )
    
2. ( \*, /, % )
    
3. ( +, - )
    
4. ( &lt;, &lt;=, &gt;, &gt;= )
    
5. ( ==, != )
    
6. ( && )
    
7. ( || )
    

\_\_

## Print Statement

To print a variable you can do

```ex
bool variable = False
print(variable)
print("Hello, " + "World !!!")
```

\_\_

## Input Statement

To get an input from the console

```ex
var line = input()
```

input will be of type `String`.

to get input of another type

```ex
var line1 = input() as bool
var line2 = input() as int
var line3 = input() as double
var line4 = input() as char
var line5 = input() as string
```

this will get the input from the console and convert it to the type you specified.

you can also use it in an expression like this.

```ex
var x = 5 + input() as int + 7
print(x)
```

it takes the input and assigns the result sum value to `x` variable.

\_\_

## If Statement

You can make an if statement in this syntax

```ex
x = input() as int
if x > 5 {
    print("X is Greater than 5")
}
else if x < 5{
    print("X is less than 5")
}
else{
    print("X is equal to 5")
}
```

### Nested If

you can make nested if

```ex
x = input() as int
if x > 5{
    if x < 10{
        print("X is Greater than 5 and less than 10")
    }
    else{
        print("X is Greater than 10")
    }
}
```

\_\_

## For Loop

### Syntax

you can create a for-loop statement like this

```ex
for i in (start, end, step){
    # Statements
}
```

Or you can skip any element like this

```ex
for i in (, end, step){
    # Statements
}
```

This will generate a loop starting from `0`

Or

```ex
for i in (start, , step){
    # Statements
}
```

This will make an infinity loop as we didn't define `end`

Or

```ex
for i in (start, end, ){
    # Statements
}
```

This will loop forever as we didn't increase the step counter

### Examples

```ex
for i in (0, 10, 1){
    print(i)
    print("\n")
}
```

\_\_

### Break Statement

You can define a break statement inside for loop statement like this

```ex
start = 0
end = 10
step = 1
for i in (start, end, step){
    if x > 5{
        break
    }
}
```

### Continue Statement

You can define a continue statement inside for loop statement like this

```ex
start = 0
end = 10
step = 1
for i in (start, end, step){
    if x > 5{
        continue
    }
    print("Hello World\n")
}
```

\_\_

## Byte Code

### Introduction

This is the byte code instructions that will be generated if you want to compile the code into byte code.

---

### `EngineByteCode` Instruction

This is the first instruction in the file

`LINE:EngineByteCode:v0.1.0`

* `LINE` =&gt; is the current instruction line, it will start from `0`
    
* `EngineByteCode` =&gt; Instruction Type
    
* `v0.1.0` =&gt; Current version
    

---

### `Assign` Instruction

This instruction is used to create a new variable and then assign it with value.

`LINE:Assign:VariableType:"VariableName":VariableValue`

* `LINE` =&gt; is the current instruction line
    
* `Assign` =&gt; is the instruction Type
    
* `VariableType` =&gt; is the Variable type for ex: (bool, int, double, character, string)
    
* `"VariableName"` =&gt; Variable Name with double quotes
    
* `VariableValue` =&gt; The value for the variable for ex: (True, False, 5, 'H', "Hello")
    

#### Examples

```ex
5:Assign:bool:"variable_1":True
6:Assign:bool:"variable_2":False
7:Assign:int:"variable_3":5
8:Assign:double:"variable_4":5.2
9:Assign:char:"variable_5":'H'
10:Assign:string:"variable_6":"Hello, World!"
```

---

### `Convert` Instruction

This instruction is used to convert the value from one type to another.

`LINE:Convert:ConvertToType:"AsssignToVariableName":"FromVariableName"`

* `LINE` =&gt; is the current instruction line
    
* `Convert` =&gt; Instruction Type
    
* `ConvertToType` =&gt; This is the convert to type for ex: (bool, int, double, character, string)
    
* `"AsssignToVariableName"` =&gt; Variable name which the converted value will be put in it.
    
* `"FromVariableName"` =&gt; Variable name which will have the value which needs to be converted.
    

#### Examples

```ex
5:Convert:bool:"Variable2":"Variable1"
6:Convert:double:"Variable3":"Variable4"
7:Convert:char:"Variable5":"Variable6"
```

---

### `Operation` Instruction

This is the operations instruction

`LINE:Operation:OperationType:"AssignToVariableName":"LeftVariableName":"RightVariableName"`

* `LINE` =&gt; is the current instruction line
    
* `Operation` =&gt; Instruction Type
    
* `OperationType` =&gt; Operation Type ( Plus, Minus, Mul, Div, Mod, And, Or, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, Equal, NotEqual)
    
* `"AssignToVariableName"` =&gt; The variable name to which the final value will be assigned to it.
    
* `"LeftVariableName"` =&gt; the first variable
    
* `"RightVariableName"` =&gt; The second variable
    

#### Examples

```ex
6:Operation:Plus:"Variable3":"Variable2":"Variable1"
7:Operation:And:"Variable5":"Variable4":"Variable3"
```

---

### `Print` Instruction

This is the print instruction

`LINE:Print:"VariableName"`

* `LINE` =&gt; is the current instruction line
    
* `Print` =&gt; Instruction Type
    
* `"VariableName"` =&gt; The variable name to print
    

#### Examples

```ex
5:Print:"Variable"
```

---

### `Input` Instruction

This is the input instruction.

`LINE:Input:"VariableName"`

* `LINE` =&gt; is the current instruction line
    
* `Input` =&gt; Instruction Type
    
* `"VariableName"` =&gt; The variable name to put the input value in it.
    

#### Examples

```ex
18:Input:"temp_stack1_variable_7"
```

---

### `If` Instruction

This is the if control flow instruction.

`LINE:If:"VariableName":GoToLINE`

* `LINE` =&gt; is the current instruction line
    
* `If` =&gt; Instruction Type
    
* `"VariableName"` =&gt; Variable name to compare its value, its value will be True or False.
    
* `GoToLINE` =&gt; The Instruction line to go to if the condition for the if statement is False, (`"VariableName"` value is False)
    

#### Examples

```ex
227:If:"temp_stack2_variable_108":234
```

---

### `GoTo` Instruction

This is the go-to instruction, which is used to just go for another line.

`LINE:GoTo:GoToLine`

* `LINE` =&gt; is the current instruction line
    
* `GoTo` =&gt; Instruction Type
    
* `GoToLine` =&gt; The line number to go to it.
    

#### Examples

```ex
233:GoTo:246
```

---

### `End` Instruction

This is the last byte code instruction to be generated

`LINE:End:`

* `LINE` =&gt; is the current instruction line
    
* `End` =&gt; Instruction Type
    

#### Examples

```ex
130:End:
```

## Conclusion & Contributing

* Engine Lang is still under development, but I hope to release it in this year.
    
* So if you want to contribute, please do not hesitate to do that. you don't need to know `Rust` to contribute, there are many ways to contribute like improving syntax, improving docs, etc...
    
* [Engine language Repo](https://github.com/engine-lang/engine)
    
* You can DM Me at [`abdoaslam000@gmail.com`](mailto:abdoaslam000@gmail.com) or at [LinkedIn](https://www.linkedin.com/in/hady-eslam/)
