• Stars
    star
    141
  • Rank 259,971 (Top 6 %)
  • Language
    Rust
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An insanely simple self-hosted functional programming language

Atto

Atto is an insanely simple functional programming language.

It features a syntax driven entirely by polish notation and no delimiters to speak of (it ignores all non-separating whitespace). What do you get for this simplicity? Well... an insanely simple language with a ~200 line self-hosted interpreter.

Despite these design limitations, it's actually possible to write quite pleasing code in Atto. That, combined with the extraordinarily extendable syntax (you can define your own operators, or overload those defined in the core library) make it ideal for solving a whole class of programming problems that are normally awkward to solve in more imperative languages.

Design

Atto's design is stupidly simple. There are two kinds of structure:

Functions: fn <name> [args] is <expr>

Expressions: <literal> [expr]

That's it. Expressions, function calls, literals and operations are all considered to be the same thing.

I leave you with a quick factorial calculation example demonstrating the compact expressiveness of Atto at work.

fn f n is
  if = n 0
    1
  * n f - n 1

Yes, that's it.

Atto Interpreter Written In Atto

In examples/self-hosted.at, I've written a fully-functioning REPL-based interpreter for Atto. It supports function declaration, function calling, and all of the evaluation operators that Atto does, including I/O. It has a minor issues, such as behaving unpredictably with invalid input. However, it should be able to successfully run any valid Atto program (provided your stack is big enough).

Which reminds me: I need to use a non-recursive interpretation algorithm in the Rust interpreter. Also, tail-call optimisation would be nice.

Core Library

Atto comes with a core library. It provides a series of non-intrinsic functions and utilities that are themselves written in Atto. In addition, it provides all of the operators common to Atto usage. The Atto interpreter implicitly inserts the core library above whatever you run, similar in nature to C's #include.

  • # x y: Ignore the first value, evaluate to only the second (useful for comments)
  • @ x y: Ignore the second value, evaluate to only the first
  • ! x: Negate a boolean
  • wrap x: Wrap a value in a list
  • empty: Produces the empty list
  • debug_enabled: Can be overriden to enable debugging utilities
  • debug i x: Display the value of x with the information tag x
  • assert i x: Assert that x is true
  • assert_eq x y: Assert that x and y are equivalent
  • is_atom x: Determine whether a value is atomic (i.e: null, bool or a number)
  • is_str x: Determine whether a value is a string
  • is_list x: Determine whether x is a list
  • is_bool x: Determine whether x is a bool
  • is_num x: Determine whether x is a number
  • is_null x: Determine whether x is null
  • len l: Determine the length of a list
  • skip n l: Skip the first n values in a list
  • nth n l: Get the nth item in a list
  • in x l: Determine whether x is in a list
  • split i l: Split a list into two separate lists at the ith index

You can check src/atto/core.at for full documentation about what core provides.

Tutorial

Basic numeric operators:

fn main is
	+ 5 7

Yields: 12

fn main is
	- * 3 3 5

Yields: 4

Printing values to the console:

fn main is
	print "Hello, world!"
fn main is
	print str 1337

Receiving inputs from the user and converting them to a value:

fn main is
    print + "Product = " str
    	* litr input "second: "
          litr input "first: "

Pairing values together into a two-component list:

fn main is
	pair 3 17

Yields [3, 17]

Fusing lists together:

fn main is
	fuse pair 3 17 pair 5 8

Yields: [3, 17, 5, 8]

Conditional expressions:

fn main is
	if true
		10
		5

Yields: 10

fn main is
	if false
		10
		5

Yields: 5

Selecting the first value in a list:

fn main is
	head pair 3 17

Yields: 3

Selecting values trailing after the head of a list:

fn main is
	tail fuse 3 fuse 17 9

Yields: [17, 9]

Converting a string into a value:

fn main is
	- 7 litr "3"

Yields: 4

fn main is
	+ 7 litr "8.5"

Yields: 15.5

fn main is
	= null litr "null"

Yields: true

Defining a function with parameters:

fn add x y is
	+ x y

fn main is
	add 5 3

Yields: 8

Recursion to find the size of a list:

fn size l is
	if = null head
		0
		+ 1 size tail l
fn main is
	size fuse 1 fuse 2 3

Yields: 3

Optimisation

Currently, Atto's Rust interpreter performs virtually no optimisations. Despite that, I'll attempt to talk below about some ideas I've had that seem promising.

Atto's design does not permit the realiasing of values within a function, nor does it permit mutation. This, and the fact that the syntax is incredibly quick to parse, makes it an extremely good potential target for a lot of optimisations. Inlining, constant propagation, CSE detection and tail call optimisations are naturally easy to implement on top of Atto's design.

The lack of realiasing also means that Atto has an affine type system by design, without ever requiring compiler support for move semantic analysis or anything like that.

The only real obstacles to some really impressive optimisation is its dynamic type system. However, in a significant number of cases it's likely that types can be inferred at compile-time with specialized machine code emitted for each function depending on the types passed to it.

I'm also working on ideas for a statically-typed version of Atto. However, I've yet to settle on a design that is sufficiently simple as to compliment the current design.

More Repositories

1

chumsky

Write expressive, high-performance parsers with ease.
Rust
3,334
star
2

flume

A safe and fast multi-producer, multi-consumer channel.
Rust
1,820
star
3

ariadne

A fancy diagnostics & error reporting crate
Rust
1,309
star
4

tao

A statically-typed functional language with generics, typeclasses, sum types, pattern-matching, first-class functions, currying, algebraic effects, associated types, good diagnostics, etc.
Rust
923
star
5

pollster

A minimal async executor that lets you block on a future
Rust
288
star
6

broom

An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection
Rust
243
star
7

euc

A software rendering crate that lets you write shaders with Rust
Rust
241
star
8

forge

A lightweight, elegant scripting language with built-in Rust-FFI.
Rust
161
star
9

parze

A clean, efficient parser combinator
Rust
123
star
10

teloren

A command-line frontend for Veloren
Rust
90
star
11

funkicrab

Optimising Brainfuck compiler: Run your beloved Brainfuck code, but faster.
Rust
64
star
12

openmw-volumetric-clouds

A volumetric clouds mod for OpenMW
64
star
13

openmw-shaders

Photorealistic shaders for Morrowind
GLSL
59
star
14

vulcan

A minimalistic text editor designed for both ordinary use and software development
Vala
45
star
15

lagoon

A thread pool crate with an array of features
Rust
36
star
16

mutation

Unleash the power of nightly Rust to write code that's generic over mutation!
Rust
23
star
17

coord-rs

[deprecated] A simple, ergonomic vector mathematics crate for Rust
Rust
22
star
18

errant

A (mostly) drop-in replacement for Rust's Result that provides backtrace support.
Rust
22
star
19

leon

A lightweight scripting language for Rust
Rust
19
star
20

zte

Zesterer's Text Editor
Rust
18
star
21

tupai

A modular POSIX-like operating system created for educational purposes
C++
16
star
22

gui

An experimental stateful, structured, declarative GUI crate
Rust
12
star
23

the-bitwise-challenge

Challenge: Can you develop a game with only 8 bytes of state?
9
star
24

vast-outdated

As The Name Suggests: A Pretty Large Space Sim
C++
8
star
25

vm-perf

Performance comparisons between various virtual interpreter implementation strategies
Rust
8
star
26

gba-test

Software rasterisation on the GBA in Rust. Some experiments from a while ago.
Rust
7
star
27

thoth

A modular, x86_64 micro-kernel operating system
C
6
star
28

alonzo

A pure Rust functional compiler backend
Rust
6
star
29

que

An experimental lock-free queue
Rust
6
star
30

cargo-veloren

Name-squatting, for the moment
Rust
5
star
31

fula

A functional programming language with Hindley-Milner type inference
Rust
5
star
32

babble

A (horrendously hackish) clean room reimplementation of the Library of Babel, originally at https://libraryofbabel.info (seriously, check it out)
Python
5
star
33

synco

An experimental ECS crate that makes use of GATs
Rust
5
star
34

fuckvm

A highly experimental Brainfuck-targetting LLVM-like compiler backend
Rust
4
star
35

smash

Yet another blazingly fast hashmap written in Rust
Rust
4
star
36

emul8

Yet another CHIP-8 emulator written in Rust
Rust
4
star
37

bitwise-examples

Example games that persist just 8 bytes of state between frames
Rust
4
star
38

browser

Vala
4
star
39

nilts-old

A game about many things. I don't know what, since most content is randomly generated.
C
3
star
40

oms

[WIP] Orbital mechanics simulation tool/library
Rust
3
star
41

hire-me

Hire me! Here's why...
3
star
42

turk

A generic minmax algorithm that may be used as an AI player for 2-player games
Rust
3
star
43

libvolume

A voxel engine library used primarily in my other projects
C++
3
star
44

wavefront

A Wavefront OBJ parser and utility crate
Rust
3
star
45

voxeltest

A test voxel engine program
C++
3
star
46

emul8or

A CHIP-8 emulator written using Vala and SDL
Vala
3
star
47

sdf-test

An experiment in Signed Distance Field (SDF) raytracing and raymarching
C++
3
star
48

ir

An experimental language intermediate representation
Rust
2
star
49

yurt

A highly experiment portable runtime
Rust
2
star
50

parze-new

Rust
2
star
51

nilts

Work in progress which will hopefully one day be good
C++
2
star
52

picos

Raspberry Pi Card Operating System
C
2
star
53

async-priority-queue

An async-aware priority queue
Rust
2
star
54

spurv

A free, open-source CPU and instruction set specification with a minimalist design
2
star
55

jsbarretto

A personal website
HTML
2
star
56

opplyse

A clean, efficient GTK3 text editor for programmers. The big brother of Vulcan.
Vala
2
star
57

nilts-oldish

The procedurally generated game
C++
2
star
58

super-block

A platforming game written for the CHIP-8
1
star
59

forge-demo

Run Forge code online!
JavaScript
1
star
60

cragmoor

A text-based ASCII RPG procedurally generated game inspired by Dwarf Fortress
C++
1
star
61

snakes-on-a-continuous-plane

A 2D Continuous Snakes Game Created For Ludum Dare 34
CMake
1
star
62

timber

I got bored one afternoon and started writing a desktop panel
Vala
1
star
63

vast-old

Vast is a space sim written in C++ using OpenGL
C++
1
star
64

pokerom

A GameBoy Color (GBC) emulator written in C++ with SDL 2
CMake
1
star