Skip to content

Commit

Permalink
Added language syntax to documentation, loops, variables, if-else is …
Browse files Browse the repository at this point in the history
…completed
  • Loading branch information
VedantParanjape committed Jul 8, 2020
1 parent 86249f3 commit 89e82d3
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 6 deletions.
200 changes: 197 additions & 3 deletions docs/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* `{`,`}` - Braces
* `(`,`)` - Parenthesis
* `/`,`*`,`+`,`-` - Arithmetic operators
* `>`,`<`,`=`,`!=`,`>=`,`<=` - Comparison operators
* `>`,`<`,`==`,`!=`,`>=`,`<=` - Comparison operators
* `~`,`&`,`|` - Bitwise operators: not, and, or
* `not`,`and`,`or` - Logical operators: not, and, or
* `:=` - Assignment operator
Expand All @@ -49,7 +49,7 @@ Wrong: int yy := 5 > 6;

* Datatype of variable needs to be specified during compile time.
* Variables can be assigned values after declarations.
* If variable is not assigned a value after declaration, it is set to `0` for `integer` and to `false` for `boolean` by default.
* If variable is not assigned a value after declaration, it is set to `0` for `integer` and to `false` for `boolean` by default.
* Variables can be assigned other variables of same datatype.
* Variables can be assigned expressions whose output is of same datatype.

Expand Down Expand Up @@ -109,4 +109,198 @@ test_var := true;

`length1, length2, _City_1`

Detailed info: [https://www.includehelp.com/c/identifier-variable-naming-conventions.aspx](https://www.includehelp.com/c/identifier-variable-naming-conventions.aspx)
Detailed info: [https://www.includehelp.com/c/identifier-variable-naming-conventions.aspx](https://www.includehelp.com/c/identifier-variable-naming-conventions.aspx)

## Expressions

### Arithmetic expressions

```cpp
=> ((3+3)/2)
=> 3
```

### Boolean expressions

```cpp
=> (false & true) | ~false
=> true

=> (true and true) & (not false)
=> true

=> (3 > 4) and false
=> false

=> (4 != 3) & (false and true)
=> false

=> true
=> true

=> false
=> false
```

!!! note
Expressions are evaluated following the [operator precedence](#operators)

## If-else statement

Statements in the if-block are executed only if the if-expression evaluates to `true`. If the value of expression is `true`, statement1 and any other statements in the block are executed and the else-block, if present, is skipped. If the value of expression is `false`, then the if-block is skipped and the else-block, if present, is executed. If elif-block are present, they are evaluated, if they become `true`, the statement is executed, otherwise, it goes on to eval next set of statements

### Syntax

```python
if : boolean_expression {
statement 1
...
...
}
elif : boolean_expression {
statement 2
...
...
...
}
else {
statement 3
...
...
}
```

### Examples

```python
int a := 3;

if : a != 4 {
a := 4;
}
elif : a > 4 {
a := 10;
}
else {
a := 0;
}
```

* This will evaluate as follows, since `a = 3`, if-block (`3!=4`) will evaluate to true, and value of a will be set to 4, and program execution will stop.

## For-loop statement

For loop is a range based for loop. Range variable is a local variable with scope only inside the for loop.

### Syntax

```python
for : var in range_1:range_2 {
statement 1
....
....
}
```

* Here, for loop is a range based loop, value of integer variable `var` will vary from `range_1` to `range_2 - 1`. Value of `var` doesnot equal `range_2`. As of now range can't be reversed,i.e., `range_1 < range_2`

!!! Note
**var** is a **integer**, and **range_1, range_2** can be **arithmetic expression, integer variable or integer constant**.

### Examples

```python
int sum := 0;

for : i in 1:4 {
sum = sum + i;
}
```

```python
int mx := 32;
int nt;

for : j in 2:mx-10 {
nt := nt + j;
}
```

## While-loop statement

While loop statement repeatedly executes a target statement as long as a given condition is true.

### Syntax

```python
while : boolean_expression {
statement 1
...
...
}
```

### Examples

* Infinite loop

```python
while true {
do_something..
...
}
```

* Normal loop, will repeat 30 times, before exiting

```python
int tag := 0;

while : tag < 30 {
tag := tag + 1;
}
```

## Control statements

!!! warning
`break` and `continue` should only be used inside looping statements

### break

`break;` is used to break execution in a loop statement, either `for loop` or `while loop`. It exits the loop upon calling.

#### Syntax

`break;`

#### Examples

```python
for : i in 0:9 {
if : i == 3 {
break;
}
}
```

### continue

`continue` is used to continue execution in a loop statement, either `for loop` or `while loop`.

#### Syntax

`continue;`

#### Examples

```python
for : j in 9:19 {
if : i == 12 {
continue;
}
else {
break;
}
}
```
2 changes: 1 addition & 1 deletion examples/led_blink.sim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
while : 1 = 1 {
while : 1 == 1 {
digital_write(4, true);
delay(1000);
digital_write(4, false);
Expand Down
2 changes: 1 addition & 1 deletion examples/led_blink_counter.sim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
while : 1=1 {
while : 1==1 {
start_counter();
while : read_counter() < 200000000 {
digital_write(4, true);
Expand Down
2 changes: 1 addition & 1 deletion examples/test.sim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ for : o in 2:4 {
}
}
while : 3>4 {
if : 4=3 {
if : 4==3 {
;
}
}
Expand Down

0 comments on commit 89e82d3

Please sign in to comment.