Koy has a programming language that has simple and minimal specs. It only has function call and control flow (so, does not have class or equivalent struct). Toys(from web+db vol.125) implementation by Kotlin.
Original implementation is here: https://github.com/kmizu/toys
| Tool | Version |
|---|---|
| JDK | 21 or later |
| Gradle | Not required — use the included Gradle Wrapper (gradlew) |
./gradlew jarThis generates build/libs/koy.jar.
java -jar build/libs/koy.jar -f <filename>.koySample programs are available in the examples/ directory.
java -jar build/libs/koy.jar -f examples/hello.koyAdd the -d flag to enable debug logging (prints the AST).
java -jar build/libs/koy.jar -f examples/hello.koy -d./gradlew testThis project uses ktlint for Kotlin code style enforcement.
# Check for violations (exits non-zero if any are found)
./gradlew ktlintCheckExisting violations are recorded in config/ktlint/baseline.xml and are excluded from ktlintCheck.
When adding new code, please follow the ktlint rules.
To regenerate the baseline (e.g. after fixing existing violations):
./gradlew ktlintGenerateBaselineInt:0Bool:true,falseString:"text"Array:["Kotlin", "Java", "Koy"]Set:%{ "Kotlin", "Java", "Koy" }Object:{ x: 1, y: "y" }Function:|x| { x * x; }
val declaration creates immutable variable, so it can not accept reassign.
val f = |msg| {
"Hello, " + msg;
};
// This will fail to assign
f = || {
"Hello, Koy";
};
When declaring re-assignable variable, please add mutable keyword.
mutable val i = 0;
while (i < 10) {
++i;
}
+, -, *, /, %(remainder)
==, !=, <, <=, >, =>
and, or
++identifier, --identifier
- Index access:
collection->index - Push element:
collection <- element
Standard control flows is all expressions and therefore return last value of blocks.
mutable val i = 0;
while (i < 10) {
i = i + 1;
}
if (x < 5) {
"over 5";
} else {
"under 5";
}
for-in expression is syntax sugar for while expression. The counter is automatically incremented each iteration.
for (i in 0 to 10) {
println(i);
}
In the example above, identifier i (for a counter of for-in) is defined as mutable val i = 0.
Definition. Program should have main function. It also defines function by function literal.
fn factorial(x) {
if (x < 2) {
x
}
x * factorial(x - 1);
}
fn main() {
factorial(5);
square(5);
}
Simple function literal sample
val square = |n| {
n * n;
};
val result = square(5);
Closure-like sample
val Age = |v| {
_v = v;
{
v: _v,
getOld: |_| {
_v = _v + 1;
}
};
};
val now = Age(21);
println(now);
Function can be called by 2 way: standard call and labeled parameter call
fn main() {
factorial(5);
factorial[x = 5];
}
Functions stored as object properties can be called with dot notation.
val obj = {
greet: |msg| {
"Hello, " + msg;
}
};
val r = obj.greet("Koy");