Sprout is a tiny programming language implemented in TypeScript. It is an interpreter, not a source-to-source translator: Node.js supplies the host runtime while Sprout owns its syntax, parser, scopes, and function calls.
npm install
npm start -- examples/fizzbuzz.sprout
printf 'bloom(2 + 3);\n' | npm startSprout includes the Garden package manager for local packages. A package is a
directory with a garden.json file and an entry .sprout file:
{
"name": "greeter",
"version": "1.0.0",
"main": "index.sprout",
"grafts": {}
}Create an application, install a package, and use it by its package name:
npm start -- cultivate my-app
npm start -- graft ../greeter
npm start -- gardenimport { greet } from "greeter";
bloom(greet("Sprout"));
Grafted packages live in garden_modules. sprout graft restores the local
file: grafts listed in garden.json, and sprout uproot removes one. Exact
versions and sources are recorded in garden-lock.json. Network nurseries are
not implemented yet.
grow factorial(n) {
branch (n <= 1) { harvest 1; }
harvest n * factorial(n - 1);
}
plant numbers = [1, 2, 3];
plant i = 0;
tend (i < len(numbers)) {
bloom(factorial(numbers[i]));
i = i + 1;
}
Sprout uses garden vocabulary throughout: plant for mutable bindings, seed
for constants, grow for functions, harvest for returns,
branch/otherwise for decisions, tend and each for loops,
prune/skip for loop control, and bloom(...) for output. Conventional
spellings such as let, const, fn, return, if, while, for, and
print(...) remain supported for compatibility. The language also supports
assignment,
compound assignment (+=, -=, *=, /=, %=), named and anonymous
functions, return, if/else, while, C-style for, for-in, break,
continue, lists, objects, indexing, property access, strings, numbers,
booleans, nil, comments beginning with //, and operators + - * / % == != < <= > >= ! and or.
let user = {
name: "Tejas",
profile: { age: 23 }
};
user.profile.age += 1;
let values = [1, 2, 3];
values[0] = 100;
print(user.name);
print(user["name"]);
print(values);
for (let i = 0; i < 10; i++) {
if (i == 5) { continue; }
print(i);
}
for (let item in [10, 20, 30]) {
print(item);
}
fn multiplier(x) {
return fn(y) { return x * y; };
}
let double = multiplier(2);
print(double(10));
Expressions inside ${...} are evaluated in the current lexical scope:
let name = "Tejas";
let age = 23;
print("Hello ${name}, you are ${age}");
Output and collections:
print(...)prints values separated by spaces.len(value)returns the length of a string or list.push(list, value)appends and returns the list.pop(list)removes and returns the last value.map(list, fn)transforms every item.filter(list, fn)keeps items whose callback is truthy.reduce(list, fn)orreduce(list, fn, initial)combines items.range(end),range(start, end), andrange(start, end, step)create lists of numbers.slice(value, start, end)slices a list or string.join(list, separator)joins list values into a string.
String functions:
upper(value),lower(value), andtrim(value)transform strings.split(value, separator)creates a list.contains(value, part),startsWith(value, part), andendsWith(value, part)return booleans.
Math functions:
sqrt,floor,ceil,round,abs,pow, andrandom.min(...)andmax(...)accept one or more numbers.
Modules use relative file paths. Export declarations must be top-level. The
.sprout extension is optional in imports:
// math.sprout
export fn add(a, b) { return a + b; }
export let answer = 42;
Import named exports from another .sprout file:
// app.sprout
import { add, answer } from "./math.sprout";
print(add(answer, 1));
Imported modules are evaluated once and cached. The importing file must be
known, so imports work from the CLI or when calling run(source, output, filename).
Run the test suite with npm test and type-check it with npm run typecheck.