Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mdbook plugin for testing mun code in book #263

Merged
merged 1 commit into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions book/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ title = "The Mun Programming Language"
additional-css = ["theme/mun.css"]
additional-js = ["theme/trailing_slash_hack.js"]
git-repository-url = "https://github.com/mun-lang/mun/tree/master/book"

[output.mun_mdbook_test_plugin]
command = "mun_mdbook_test_plugin"
4 changes: 4 additions & 0 deletions book/listings/ch01-getting-started/listing01.mun
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# pub fn main() {
# fibonacci_n();
# }

pub fn fibonacci_n() -> i64 {
let n = arg();
fibonacci(n)
Expand Down
4 changes: 4 additions & 0 deletions book/listings/ch01-getting-started/listing02.mun
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# pub fn main() {
# fibonacci(arg());
# }

pub fn arg() -> i64 {
5
}
Expand Down
3 changes: 1 addition & 2 deletions book/src/ch01-02-hello-fibonacci.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ Open up the new source file and enter the code in Listing 1-1.

Filename: hello_fibonacci.mun

<!-- HACK: Add an extension to support hiding of Mun code -->
```mun
```mun
{{#include ../listings/ch01-getting-started/listing01.mun}}
```

Expand Down
2 changes: 1 addition & 1 deletion book/src/ch01-03-hello-hot-reloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ both `args` and `fibonacci`.
Filename: hello_fibonacci.mun

<!-- HACK: Add an extension to support hiding of Mun code -->
```mun
```mun
{{#include ../listings/ch01-getting-started/listing02.mun}}
```

Expand Down
33 changes: 27 additions & 6 deletions book/src/ch02-01-values-and-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ forced to explicitly annotate variables in a few locations to ensure a contract
between interdependent code.

```mun
# pub fn main() {
# bar(1);
# }
fn bar(a: i32) -> i32 {
let foo = 3 + a;
foo
Expand Down Expand Up @@ -70,7 +73,7 @@ single-precision float of 32 bits, and the `f64` type has double precision -
requiring 64 bits.

```mun
fn main() {
pub fn main() {
let f = 3.0; // f64
}
```
Expand All @@ -82,7 +85,7 @@ The `bool` (or *boolean*) type has two values, `true` and `false`, that are
used to evaluate conditions. It takes up one 1 byte (or 8 bits).

```mun
fn main() {
pub fn main() {
let t = true;

let f: bool = false; // with explicit type annotation
Expand All @@ -101,10 +104,12 @@ written as a decimal, hexadecimal, octal or binary value. These are all
examples of valid literals:

```mun
# pub fn main() {
let a = 367;
let b = 0xbeaf;
let c = 0o76532;
let d = 0b0101011;
# }
```

A floating-point literal comes in two forms:
Expand All @@ -116,9 +121,11 @@ A floating-point literal comes in two forms:
Examples of valid floating-point literals are:

```mun
# pub fn main() {
let a: f64 = 3.1415;
let b: f64 = 3.;
let c: f64 = 314.1592654e-2;
# }
```

#### Separators
Expand All @@ -128,8 +135,10 @@ visually separate numbers from one another. They do not have any semantic
significance but can be useful to the eye.

```mun
# pub fn main() {
let a: i64 = 1_000_000;
let b: f64 = 1_000.12;
# }
```

#### Type suffix
Expand All @@ -148,16 +157,20 @@ Note that integer literals can have floating-point suffixes. This is not the
case the other way around.

```mun
# pub fn main() {
let a: u8 = 128_u8;
let b: i128 = 99999999999999999_i128;
let c: f32 = 10_f32; // integer literal with float suffix
# }
```

When providing a literal, the compiler will always check if a literal value will
fit the type. If not, an error will be emitted:

```mun
```mun,compile_fail
# pub fn main() {
let a: u8 = 1123123124124_u8; // literal out of range for `u8`
# }
```

### Numeric operations
Expand All @@ -166,7 +179,7 @@ Mun supports all basic mathematical operations for number types: addition,
subtraction, division, multiplication, and remainder.

```mun
fn main() {
pub fn main() {
// addition
let a = 10 + 5;

Expand All @@ -191,7 +204,7 @@ same type.
Unary operators are also supported:

```mun
fn main() {
pub fn main() {
let a = 4;
// negate
let b = -a;
Expand All @@ -209,21 +222,26 @@ shadow any previous declaration in the same block. This is often useful if you
want to change the type of a variable.

```mun
# pub fn main() {
let a: i32 = 3;
let a: f64 = 5.0;
# }
```

### Use before initialization

All variables in Mun must be initialized before usage. Uninitialized variables
can be declared but they must be assigned a value before they can be read.

```mun
```mun,compile_fail
# pub fn main() {
# let some_conditional = false;
let a: i32;
if some_conditional {
a = 4;
}
let b = a; // invalid: a is potentially uninitialized
# }
```

Note that declaring a variable without a value is often a bad code smell since
Expand All @@ -232,10 +250,13 @@ the above could have better been written by *returning* a value from the
uninitialized value.

```mun
# pub fn main() {
# let some_conditional = true;
let a: i32 = if some_conditional {
4
} else {
5
}
let b = a;
# }
```
21 changes: 15 additions & 6 deletions book/src/ch02-02-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Mun uses *snake case* as the conventional style for function and variable names.
In snake case all letters are lowercase and words are separated by underscores.

```mun
fn main() {
pub fn main() {
another_function();
}

Expand All @@ -31,6 +31,9 @@ Marking a function with the `pub` keyword allows you to use it from outside of
the module it is defined in.

```mun
# pub fn main() {
# bar();
# }
// This function is not accessible outside of this code
fn foo() {
// ...
Expand Down Expand Up @@ -59,7 +62,7 @@ The following is a rewritten version of `another_function` that shows what an
argument looks like:

```mun
fn main() {
pub fn main() {
another_function(3);
}

Expand All @@ -72,7 +75,7 @@ type. When you want a function to use multiple arguments, separate them with
commas:

```mun
fn main() {
pub fn main() {
another_function(3, 4);
}

Expand All @@ -90,7 +93,7 @@ Creating a variable and assigning a value to it with the `let` keyword is a
statement. In the following example, `let y = 6;` is a statement.

```mun
fn main() {
pub fn main() {
let y = 6;
}
```
Expand All @@ -108,6 +111,9 @@ evaluate to the last expression in them. Blocks can therefore also be used on
the right hand side of a `let` statement.

```mun
# pub fn main() {
# foo();
# }
fn foo() -> i32 {
let bar = {
let b = 3;
Expand All @@ -131,7 +137,7 @@ fn five() -> i32 {
5
}

fn main() {
pub fn main() {
let x = five();
}
```
Expand All @@ -144,7 +150,10 @@ is specified too, as `-> i32`.
Whereas the last expression in a block implicitly becomes that blocks return
value, explicit `return` statements always return from the entire function:

```mun
```mun,ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```mun,ignore
```mun

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should compile if you convert return bar + 3; into return 3;, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be a separate pr?

pub fn main() {
let _ = foo();
}
fn foo() -> i32 {
let bar = {
let b = 3;
Expand Down
18 changes: 12 additions & 6 deletions book/src/ch02-03-control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ constructs that allow developers to control the flow of execution. Mun provides
An `if` expression allows you to branch your code depending on conditions.

```mun
fn main() {
pub fn main() {
let number = 3;

if number < 5 {
Expand All @@ -30,7 +30,7 @@ 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() {
pub fn main() {
let number = 6;
if number > 10 {
// The number if larger than 10
Expand All @@ -51,13 +51,14 @@ The `if` expression can be used on the right side of a `let` statement
just like a block:

```mun
fn main() {
pub 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
Expand All @@ -72,7 +73,7 @@ A `loop` expression can be used to create an infinite loop. Breaking out of the
loop is done using the `break` statement.

```mun
fn main() {
pub fn main() {
let i = 0;
loop {
if i > 5 {
Expand All @@ -88,6 +89,9 @@ Similar to `if`/`else` expressions, `loop` blocks can have a return value that
can be returned through the use of a `break` statement.

```mun
# pub fn main() {
# count(4, 4);
# }
fn count(i: i32, n: i32) -> i32 {
let loop_count = 0;
loop {
Expand All @@ -102,11 +106,13 @@ fn count(i: i32, n: i32) -> i32 {

All `break` statements in a `loop` must have the same return type.

```mun
```mun,compile_fail
# pub fn main() {
let a = loop {
break 3;
break; // expected `{integer}`, found `nothing`
};
# }
```


Expand All @@ -118,7 +124,7 @@ block of code to execute upon each iteration. Just like with the `if`
expression, no parentheses are required around the condition expression.

```mun
fn main() {
pub fn main() {
let i = 0;
while i <= 5 {
i += 1;
Expand Down
2 changes: 1 addition & 1 deletion book/src/ch02-04-extern-fn.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ definitions have to be provided to the runtime when loading a Mun library.
Failure to do so will result in a runtime link error, and loading the library
will fail. Take this code for example:

```mun
```mun,no_run
{{#include ../listings/ch02-basic-concepts/listing01.mun}}
```

Expand Down
Loading