Cordy
Cordy is a dynamically typed, interpreted, semi-functional / semi-procedural language. It is designed as a quick-to-write, simple-yet-feature-full, scripting language for solving puzzles and other fun things as an alternative to Python. Below is a REPL for the Cordy compiler and runtime. Try it out!
Quick Introduction
This language is inspired by parts from Python, Rust, Haskell, Java, and JavaScript. It is also heavily inspired by the Crafting Interpreters book. A basic rundown of the syntax:
The basic structure of the language is C-style, with
{
and}
to separate code blocks, and imperative constructs such asif, else, while
, etc.-
let x
is used to declare a variable, andfn foo(x, y, z)
declares a function. Functions can either be followed be expressions (likefn add1(x) -> x + 1
) or blocks (likefn my_print(x) { print(x) }
).Functions can be used in expressions, too, as anonymous functions by omitting the name, i.e.
let x = fn() -> 3
. They can be followed by either->
or{
when used in expressions.
-
A few basic types:
nil
: The absence of a value, and the default value for all declared uninitialized variablesbool
: A boolean, which can be eithertrue
orfalse
int
: A 63-bit signed integercomplex
: A 64-bit signed integral complex number. Declared with thei
orj
suffix, i.e.1 + 3i
str
: A UTF-8 string, declared with either'
or"
. Like Python, there is no uniquechar
type, only single character strings.function
: The type of all functions
-
Along with some basic library collections:
list
: A ring buffer with O(1) index, pop/push front and back.set
: A collection with unique elements and O(1)contains
checks, along with insertion-order iteration.dict
: A mapping from keys to values with O(1) lookups, along with insertion-order iteration.heap
: A min-heap.vector
: Alist
variant which behaves element-wise with all basic operators.
And user definable named tuple types, with the
struct
keyword.-
Expressions should be familiar from most imperative programming languages, as should be operator precedence.
Operators on their own are functions, so
(+)
is a two argument function which adds values./
is floor division, and%
a modulo operator (same as Python)The
.
operator is actually a low precedence function composition operator:a . b . c
is equivalent toc(b(a))
, and it can be chained in a functional style.Short-circuiting
and
andor
use the keywords from Python.
Most functions (that aren't variadic) can be partially evaluated (like Haskell):
(+ 3)
is a function which takes one argument and adds three.The language is almost completely newline independent, and whitespace only used to delimit tokens. There are a few edge cases where whitespace (or semicolons) is required between expressions to reduce ambiguity.
For a more comprehensive documentation, see the language documentation or the standard library.
Implementation
Cordy is implemented in Rust, is open source, with targets both for a binary application, and a web assembly module (which powers the REPL above). The cordy
executable serves several purposes:
Text is passed through both a scanner, and a joint parse/semantic stage, which constructs a custom bytecode
Optionally, an optimizer pass is done over expressions, implementing several common optimizations such as dead code removal, constant folding, and also cordy-specific optimizations, such as partial function reordering and inlining.
The runtime uses a stack-based virtual machine, which interprets the bytecode, with heavy interaction between the interpreter, and the cordy standard library (which is implemented in Rust).