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

Documentation and help system improvements #271

Merged
merged 5 commits into from
Jan 2, 2024
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
305 changes: 177 additions & 128 deletions crates/cli/src/help.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ fn is_octal_digit(c: char) -> bool {
}

fn is_hex_digit(c: char) -> bool {
matches!(c, '0'..='9' | 'a'..='f' | 'A'..='F')
c.is_ascii_hexdigit()
}

fn is_whitespace(c: char) -> bool {
Expand Down
5 changes: 1 addition & 4 deletions crates/runtime/src/core_lib/iterator/adaptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,7 @@ impl Iterator for TakeWhile {
return None;
}

let Some(iter_output) = self.iter.next() else {
return None;
};

let iter_output = self.iter.next()?;
let predicate = self.predicate.clone();
let predicate_result = match &iter_output {
Output::Value(value) => self
Expand Down
4 changes: 1 addition & 3 deletions docs/language/basics.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Language Basics

## Koto Programs

Koto programs contain a series of expressions that are evaluated by Koto's runtime.

For example, this program asks for the user's name and then offers them a
Expand All @@ -14,7 +12,7 @@ print "Hi there, $name!"
```

Try placing the above example in a file named `hello.koto`, and then running
`koto hello.koto`.
`koto hello.koto`, or entering the expressions one at a time in the REPL.

## Comments

Expand Down
2 changes: 2 additions & 0 deletions docs/language/conditional_expressions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Conditional Expressions

Koto includes several ways of producing values that depend on _conditions_.

## if

`if` expressions come in two flavours; single-line:
Expand Down
39 changes: 22 additions & 17 deletions docs/language/functions_advanced.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Advanced Functions

Functions in Koto stored by the runtime as values and can hold internal captured state.

If a value is accessed in a function that wasn't assigned locally,
then the value is copied into the function (or _captured_) when it's created.

```koto
x = 1

# x is assigned outside the function,
# so it gets captured when it's created.
f = |n| n + x

# Reassigning x here doesn't modify the value
# of x that was captured when f was created.
x = 100

print! f 2
check! 3
```

It's worth noting that this behavior is different to many other scripting languages, where captures are often taken by _reference_ rather than by _value_.

## Optional Arguments

When calling a function, any missing arguments will be replaced by `null`.
Expand Down Expand Up @@ -126,20 +148,3 @@ check! (('foo_a', 1), ('foo_b', 3))

## Captured Values

If a value is accessed in a function that wasn't assigned locally,
then the value is copied into the function (or _captured_) when it's created.

```koto
x = 1

# x is assigned outside the function,
# so it gets captured when it's created.
f = |n| n + x

# Reassigning x here doesn't modify the value
# of x that was captured when f was created.
x = 100

print! f 2
check! 3
```
2 changes: 2 additions & 0 deletions docs/language/loops.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Loops

Koto includes several ways of evaluating expressions repeatedly in a loop.

## for

`for` loops can be used to iterate over any iterable value.
Expand Down
2 changes: 2 additions & 0 deletions docs/language/modules.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Modules

Koto includes a module system that helps you to organize and re-use your code when your program grows too large for a single file.

## `import`

Module items can be brought into the current scope using `import`.
Expand Down
45 changes: 44 additions & 1 deletion docs/language/ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,25 @@ print! r.contains 200
check! true
```

Ranges are iterable, so can be used in for loops, and with the `iterator` module.
If a value is missing from either side of the range operator then an _unbounded_
range is created.

```koto
r = 10..
print! r.start()
check! 10
print! r.end()
check! null
r = ..=100
print! r.start()
check! null
print! r.end()
check! 100
```

_Bounded_ ranges are iterable, so can be used in for loops, and with the
`iterator` module.

```koto
for x in 1..=3
Expand All @@ -38,3 +56,28 @@ print! (0..5).to_list()
check! [0, 1, 2, 3, 4]
```

## Slices

Ranges can be used to create a _slice_ of a container's data.

```koto
x = (10, 20, 30, 40, 50)
print! x[1..=3]
check! (20, 30, 40)
```

For tuples and strings, slices share the original container's data, which
avoids making copies of the elements in the slice. For lists (which contain
mutable data), copies of the slices elements are made.

If a range doesn't have a defined start, then the slice starts from the
beginning of the container's data. Similarly, if a range doesn't have a defined
end, then the slice includes elements up to the end of the container's data.

```koto
z = 'Hëllø'
print! z[..2]
check! Hë
print! z[2..]
check! llø
```
4 changes: 2 additions & 2 deletions docs/language/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ print! '2 plus 3 is ${2 + 3}.'
check! 2 plus 3 is 5.
```

## String Escape codes
## String Escape Codes

Strings can contain the following escape codes to define special characters,
all of which start with a `\`.
Expand All @@ -79,7 +79,7 @@ print! 'Hi \u{1F44B}'
check! Hi 👋
```

## Single or double quotes
## Single or Double Quotes

Whether you use `'` or `"` for your strings doesn't make a difference, except that you can use the other quote character freely in the string without having to escape it with `\`.

Expand Down
8 changes: 7 additions & 1 deletion docs/language/testing.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Testing

Koto includes some features that help you to automatically check that your code is behaving as you expect.

## Assertions

A collection of [assertion functions](../../core/test) are available.
A collection of assertion functions are available in the [`test` core library module](../../core/test),
which are included by default in the [prelude](./prelude).

```koto
try
Expand All @@ -22,6 +25,9 @@ check! An assertion failed

Tests can be organized in a Map by defining `@test` functions.

The runtime can be configured to automatically run tests when importing modules,
e.g. the CLI will run module tests when the `--import_tests` option is used.

The tests can then be run with [`test.run_tests`](../../core/test#run-tests).

```koto
Expand Down
4 changes: 4 additions & 0 deletions docs/language/tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ print! b
check! (1, 2, 3, 4, 5, 6)
```

## Creating Empty or Single-Value Tuples

To create an empty Tuple, or a Tuple with a single entry, use a trailing `,` inside the parentheses.

```koto
Expand All @@ -51,6 +53,8 @@ print! (1,)
check! (1)
```

## Tuple Mutability

Although Tuples have a fixed structure, mutable values in a Tuple (e.g. Lists and Maps) can still be modified.

```koto
Expand Down
Loading