Skip to content

Commit

Permalink
docs: added some basic language constructs to the docs
Browse files Browse the repository at this point in the history
* refactor: integrated highlight.js with mun support
* docs: added some basic language constructs to the docs

Co-Authored-By: Wodann <[email protected]>
  • Loading branch information
baszalmstra and Wodann authored Nov 8, 2019
1 parent 4fa385b commit 876f6a0
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[Introduction](ch00-00-introduction.md)
[Case studies](ch00-01-case-studio-abbey-games.md)

## Types

- [Types](ch01-00-types.md)
- [Basic concepts](ch01-00-basic-concepts.md)
- [Values and types](ch01-01-values-and-types.md)
- [Control flow](ch01-02-control-flow.md)

5 changes: 5 additions & 0 deletions src/ch01-00-basic-concepts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Basic Concepts

This section describes the basic concepts of the Mun programming language.


37 changes: 0 additions & 37 deletions src/ch01-00-types.md

This file was deleted.

80 changes: 80 additions & 0 deletions src/ch01-01-values-and-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Values and types

Mun is a statically typed language, which helps to detect type-related errors at
compile-time. A type error is an invalid operation on a given type, such as an
integer divided by a string, trying to access a field that doesn't exist, or
calling a function with the wrong number of arguments.

Some languages require a programmer to explicitly annotate syntactic constructs
with type information:

```c++
int foo = 3 + 4;
```

However, often variable types can be inferred by their usage. Mun uses type
inferencing to determine variable types at compile time. However, you are still
forced to explicitly annotate variables in a few locations to ensure a contract
between interdependent code.

```mun
fn bar(a:int):int {
let foo = 3 + a
foo
}
```

Here, the parameter `a` and the return type must be annotated because it solidifies
the signature of the function. The type of `foo` can be inferred through its
usage.

## Basic types

Mun knows two basic numeric types: `float` and `int`. A `float` is a
double-precision IEEE 64-bit floating point number and an `int` represents a
64-bit integral number.

In Mun an `int` can be explicitly cast to `float`, but the reverse is not true.
Assigning a `float` to an `int` might cause loss of precision and can therefore
not be cast implicitly.

A `bool` has two possible values: `true` and `false`.

## Implicit & explicit returns

A block is a grouping of statements and expressions surrounded by curly braces.
Function bodies are an example of blocks. In Mun, blocks evaluate to the last
expression in them. Blocks can therefore also be used on the right hand side of a
`let` statement.

```mun
fn foo():int {
let bar = {
let b = 3;
b+3
}
// `bar` has a value 6
bar+3
}
```

Besides implicit returns, explicit returns can also be used with `return`
expressions. However, explicit `return` statements always return from the
function, not from the block:

```mun
fn foo():int {
let bar = {
let b = 3;
return b+3
}
// This code will never be executed
return bar+3;
}
```

## Shadowing

> WIP
66 changes: 66 additions & 0 deletions src/ch01-02-control-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Control flow

Executing or repeating a block of code only under specific conditions are common
constructs that allow developers to control the flow of execution. Mun provides
`if...else` expressions and loops.

## `if` expressions

An `if` expression allows you to branch your code depending on conditions.

```mun
fn main() {
let number = 3;
if number < 5 {
number = 4;
} else {
number = 6;
}
}
```

All `if` expressions start with the keyword `if`, followed by a condition.
As opposed to many C-like languages, Mun omits parentheses around the
condition. Only when the condition is true - in the example, whether the `number`
variable is less than 5 - the consecutive code block (or *arm*) is executed.

Optionally, an `else` expression can be added that will be executed when the
condition evaluates to false. You can also have multiple conditions by combining
`if` and `else` in an `else if` expression. For example:

```mun
fn main() {
let number = 6;
if number > 10 {
// The number if larger than 10
} else if number > 8 {
// The number is larger than 8 but smaller or equal to 10
} else if number > 2 {
// The number is larger than 2 but smaller or equal to 8
} else {
// The number is smaller than- or equal to 2.
}
}
```


### Using `if` in a `let` statement

The `if` expression can be used on the right side of a `let` statement
just like a block:

```mun
fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
```

Depending on the condition, the `number` variable will be bound to the value
of the `if` block or the `else` block. This means that both the `if` and `else`
arms need to evaluate to the same type.
If the types are mismatched the compiler will report an error.
2 changes: 1 addition & 1 deletion vendor/highlight.js

0 comments on commit 876f6a0

Please sign in to comment.