Cassette (a small, Lisp-like programming language)

Cassette (a small, Lisp-like programming language)

Cassette is a small, Lisp-like programming language.
It is a home-cooked
meal
. It appears like this:

import List
import Math
import Canvas
import System

let width = 800,
 top = 480,
 canvas = Canvas.new(width, top)

canvas.textual content("Lines!", {200, 2})

System.seed(System.time())

def rand-line(i) do
 let x0 = Math.flooring(i * width / 100),
 y0 = Math.rand-int(20, top / 10),
 x1 = Math.rand-int(0, width),
 y1 = Math.rand-int(20, top)
 canvas.line({x0, y0}, {x1, y1})
finish

List.map(i -> rand-line(i), List.vary(0, 100))

I made Cassette as a easy language for “playful
programming”. Playful programming is writing one thing
for the sake of writing it. It’s making a software program 3D
renderer or a GIF reader, despite the fact that higher
implementations of these exist already. It’s making
generative artwork applications and drawing them with a pen
plotter. Cassette itself is playful programming—there
are actually different scripting languages which may be
higher for private tasks like these, however this one is
mine.

Here are a few of the design objectives of Cassette:

  • Functional
  • Immutable varieties
  • Simplicity over effectivity
  • Small implementation
  • Few dependencies

In specific, I needed Cassette to really feel “important”,
the place every facet of the language displays some
basic facet of computing (from a practical
language perspective, at the very least). For instance, I contemplate
rubbish assortment, lexical scopes, and immutable varieties
important. The result’s somewhat boring, however I hope
it’s a very good platform to play with different programming
ideas.

Here’s some future work for Cassette:

  • Bignum assist
  • Generational rubbish assortment
  • Compiler & VM optimization
  • Support different backends (WebAssembly, LLVM)
  • Destructuring project (v2?)
  • Pattern-based operate dispatch (v2?)

This challenge requires a C construct toolchain and SDL2.
The supply code will be discovered right here.

  1. Clone the repo with
    git clone https://git.sr.ht/~zjm/Cassette.
  2. Run make to construct the challenge. This
    creates the executable cassette.
  3. Optionally, run make set up to put in
    the Cassette executable. You can set the set up folder
    within the Makefile.
  4. Try the instance with
    ./cassette take a look at/take a look at.ct.
  5. Write somewhat script and run it with
    ./cassette script.ct.

Cassette has two quantity varieties, integers and floats.
Integers will be written in decimal, hexadecimal, or as a
character. The regular infix arithmetic operations work
on numbers, and bitwise operations work on
integers.

-1

1 ; decimal integer
0x1F ; hex integer
$a ; => 0x61
1.0 ; float

-4 ; => -4
1 + 2 ; => 3
5 * 5 ; => 25
10 / 2 ; => 5.0
12 % 10 ; => 2

0xF0 >> 4 ; => 0x0F (shift proper)
0x55 << 1 ; => 0xAA (shift left)
0x37 & 0x0F ; => 0x07 (bitwise and)
20 ^ 1 ; => 21 (bitwise or)
~4 ; => 3 (bitwise not)

Cassette has symbols, which characterize an arbitrary
worth. true and false are
symbols. In boolean operations, all values are truthy
besides false and nil.
Comparison operators solely work on numbers, however equality
operators work on any kind. The and and
or operators are short-circuiting.

:okay
:not_found

3 > 1 ; => true
3 < 3 ; => false
5 <= 4 + 1 ; => true
5 >= 4 + 1 ; => true
:okay == :okay ; => true
:okay != :okay ; => false
3 > 1 and 4 == 5 ; => false
3 > 1 or 4 == 5 ; => true
3 >= 0 and 3 < 5 ; => true
nil and :okay ; => false
:error and :okay ; => true

Strings are UTF-8 encoded binaries. You can discover the
size of a string, concatenate two binaries, and take a look at
if one string or byte is current in one other string.
Binaries additionally characterize different arbitrary byte sequences,
such because the contents of a file.

"Hello!"
#"Hello!" ; => 6
#"" ; => 0

"Hi " <> "there" ; => "Hi there"
"foo" <> "bar" ; => "foobar"

"ob" in "foobar" ; => true
$r in "foobar" ; => true
65 in "Alpha" ; => true
1000 in "foobar" ; error: bytes should be between 0–255

Cassette has Lisp-style cons pairs, which type linked
lists. Pairs are fashioned with the |
operator, and can be utilized to simply prepend values to a
listing. nil is the empty listing. You can discover
the size of a listing, concatenate two lists, and take a look at if
a price is in a listing. Lists will be accessed with an
integer index.

[1, 2, 3] ; listing
[1, 2, 3].2 ; => 3
[1, 2, 3].8 ; error: out of bounds
[:a, x, 42]
nil == [] ; => true

1 | [2, 3, 4] ; => [1, 2, 3, 4]
3 | nil ; => [3]
1 | 2 | 3 | nil ; => [1, 2, 3]

#[1, 2, 3] ; => 3
#nil ; => 0
[1, 2] <> [3, 4] ; => [1, 2, 3, 4]
:okay in [:ok, 3] ; => true

Cassette has tuples, that are fixed-length arrays of
values. Tuples are much less versatile than lists, however use
much less reminiscence and are somewhat quicker to entry. You can
discover the size of a tuple, concatenate two tuples, and
take a look at if a price is in a tuple. Tuples will be accessed
with an integer index.

{1, 2, 3} ; tuple
{1, 2, 3}.2 ; => 3
{1, 2, 3}.8 ; error: out of bounds
#{1, 2, 3} ; => 3
#{} ; => 0
{1, 2} <> {3, 4} ; => {1, 2, 3, 4}
"x" in {"y", "z"} ; => false

Cassette has maps (a.ok.a. dictionaries), which will be
written like tuples with image keys. Map literals can
solely have image keys, however different features can get and
set keys of different varieties. You can discover the variety of
key/worth pairs in a map, merge two maps, and take a look at if a
map comprises a key.

{x: 3, y: 4} ; a map with keys `:x` and `:y`
my_map.x ; => 3
my_map.z ; => nil

#{x: 1, y: 2} ; => 2
{x: 1, z: 4} <> {x: 2, y: 3}
 ; => {x: 2, y: 3, z: 4}
:x in {x: 1, y: 2} ; => true

Variables are outlined with let. A
do block can introduce a brand new scope, and may
be used to mix a gaggle of expressions into one.
Cassette has lexical scoping. A variable should begin with
a letter or underscore, however might include any characters
afterward apart from whitespace and these reserved
characters: ;,.:()[]{}. This signifies that if
you need to write an infix expression, you could usually
embrace house across the operator to disambiguate it
from its operands.

let x = 1, y = 2, x-y = 3

print(x - y) ; prints "-1"
print(x-y) ; prints "3"
print(x -y) ; error: tries to name operate "x" with argument "-y"

do
 let y = 3, z = 4

 x ; => 1 (from the mother or father scope)
 y ; => 3 (shadows the mother or father `y`)
 z ; => 4
finish

x ; => 1
y ; => 2
z ; error: undefined variable

Cassette has if/else blocks
and cond blocks for conditionals. A
cond block will consider every predicate
till one is true, then consider that clause.

if x == 0 do
 IO.print("Uh oh!")
 :error
else
 :okay
finish

cond do
 x > 10 -> :ten
 x > 1 -> :one
 true -> :much less ; default
finish

Lambdas will be created with a backslash, argument
listing, and an arrow. The def syntax is
syntactic sugar for outlining a lambda as a variable,
with the excellence that def-declared
features have block scope, to allow them to be referred to as
recursively. Functions are referred to as with
parentheses.

let foo = x -> x + 1
foo(3) ; => 4

; equal, apart from scope:
let foo = x -> x + 1
def foo(x) x + 1

; these produce an error, since `b` is not outlined when the physique of `a` is compiled
let a = x -> (b x * 3),
 b = x -> (a x / 2)

; these are okay, since `a` and `b` are in scope from the start of the block
def a(x) b(x * 3)
def b(x) a(x / 2)

Cassette applications will be break up up into totally different
modules, one per file. A manifest file is used to listing
the information in a program, with the primary file listed being
the entry level. Modules will be imported immediately right into a
file’s scope, or aliased as a map.

; file "foo.ct"
module Foo

let pi = 3.14
def bar(x) x + 1
; file "foremost.ct"
import Foo ; imported as a map referred to as `Foo`

Foo.bar(3) ; => 4
Foo.pi ; => 3.14
; various "foremost.ct"
import Foo as F ; imported as a map referred to as `F`

def bar(x) x + 8

bar(3) ; => 11
F.bar(3) ; => 4
; various "foremost.ct"
import Foo as * ; imported immediately into present scope

bar(3) ; => 4
pi ; => 3.14

For extra details about Cassette, take a look at some
of those different paperwork. Stay tuned for future
articles.

Vinyl

by way of Lobsters https://lobste.rs/

January 3, 2024 at 04:06AM