#scripting #language

koto

A simple, expressive, embeddable programming language

14 releases (breaking)

0.12.0 Oct 18, 2023
0.11.0 Jul 14, 2022
0.10.0 Dec 2, 2021
0.9.1 Nov 1, 2021
0.5.0 Dec 17, 2020

#550 in Programming languages

Download history 29/week @ 2023-10-30 20/week @ 2023-11-06 6/week @ 2023-11-13 29/week @ 2023-11-20 52/week @ 2023-11-27 16/week @ 2023-12-04 5/week @ 2023-12-11 47/week @ 2023-12-18 33/week @ 2023-12-25 73/week @ 2024-01-01 9/week @ 2024-01-08 10/week @ 2024-01-15 21/week @ 2024-01-22 32/week @ 2024-01-29 11/week @ 2024-02-05 68/week @ 2024-02-12

133 downloads per month
Used in koto_cli

MIT license

1MB
19K SLoC

Koto


Docs Crates.io CI Discord

Koto is an embeddable scripting language developed in Rust. Prioritizing speed and ease of use, its goal is to be an ideal option for adding scripting support to Rust applications.

During its early development there was a focus on interactive systems, such as rapid iteration during game development, but Koto is versatile enough to be useful in a wide range of applications.

Current State

...I think we're getting somewhere?

The language is close to feature complete from my perspective, but it hasn't been used in enough real-world projects for me to suggest that anyone else should use it for anything serious. In particular, you can expect breaking changes to the language, although these are now becoming less frequent.

That said, your feedback is invaluable to Koto's development. If you decide to try it out, please let me know how you get on.

Getting Started

See the Getting Started section of the language guide.

Reference documentation for Koto's core library can be found here.

You're also welcome to reach out in Discussions, or on Discord.

A Quick Tour

## Strings
name = 'Koto'
print 'Hello, $name!'
# -> Hello, Koto!

## Functions
square = |n| n * n
print '8 squared is ${square 8}'
# -> 8 squared is 64

add_squares = |a, b| (square a) + (square b)
assert_eq (add_squares 2, 4), 20

## Iterators, Ranges, and Lists
fizz_buzz = 
  (1..100)
    .keep |n| (10..=15).contains n
    .each |n|
      match n % 3, n % 5
        0, 0 then 'Fizz Buzz'
        0, _ then 'Fizz'
        _, 0 then 'Buzz'
        else n
    .to_list()
print fizz_buzz
# -> ['Buzz', 11, 'Fizz', 13, 14, 'Fizz Buzz']

## Maps and tuples

### Maps can be defined with curly braces
fruits = {peaches: 42, pears: 99}

### Maps can also be defined using indented `key: value` pairs
more_fruits = 
  apples: 123
  plums: 99

fruits.extend more_fruits
print fruits.keys().to_tuple(),
# -> ('peaches', 'pears', 'apples', 'plums')

fruit, amount = fruits.max |(_, amount)| amount
print 'The highest amount of fruit is: $amount $fruit'
# -> The highest amount of fruit is: 123 apples

Learning the Language

The language guide and core library reference give an overview of Koto's features.

Rendered versions of the docs are available on the Koto website.

There are also some code examples that are a good starting point for getting to know the language.

Installation

The most recent release of the Koto CLI can be installed with Cargo:

cargo install koto_cli

To build and install the latest version of the CLI from source:

cargo install --path core/cli

Language Goals

  • A clean, minimal syntax designed for coding in creative contexts.
  • Fast compilation.
    • The lexer, parser, and compiler are all written with speed in mind, enabling as-fast-as-possible iteration when working on an idea.
  • Fast and predictable runtime performance.
    • Memory allocations are reference counted.
    • Currently there's no tracing garbage collector (and no plan to add one) so memory leaks are possible if cyclic references are created.
  • Lightweight integration into host applications.
    • One of the primary use cases for Koto is for it to be embedded as a library in other applications, so it should be a good citizen and not introduce too much overhead.

Editor Support

MSRV

Koto is still under active development, and is tested against the latest stable release of Rust.


lib.rs:

Koto

Pulls together the compiler and runtime for the Koto programming language.

Programs can be compiled and executed with the [Koto] struct.

Example

use koto::prelude::*;

let mut koto = Koto::default();
match koto.compile("1 + 2") {
    Ok(_) => match koto.run() {
        Ok(result) => match result {
            Value::Number(n) => println!("{n}"), // 3.0
            other => panic!("Unexpected result type: {}", other.type_as_string()),
        },
        Err(runtime_error) => {
            panic!("Runtime error: {runtime_error}");
        }
    },
    Err(compiler_error) => {
        panic!("Compiler error: {compiler_error}");
    }
}

Dependencies

~1.8–4MB
~73K SLoC