Skip to content

About

my own programming language

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Sprout

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.

Run it

npm install
npm start -- examples/fizzbuzz.sprout
printf 'bloom(2 + 3);\n' | npm start

Packages

Sprout 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 -- garden
import { 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.

Language Basics

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.

Objects and Mutable Collections

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);

Loops and Control Flow

for (let i = 0; i < 10; i++) {
  if (i == 5) { continue; }
  print(i);
}

for (let item in [10, 20, 30]) {
  print(item);
}

Lambdas and Closures

fn multiplier(x) {
  return fn(y) { return x * y; };
}

let double = multiplier(2);
print(double(10));

String Interpolation

Expressions inside ${...} are evaluated in the current lexical scope:

let name = "Tejas";
let age = 23;
print("Hello ${name}, you are ${age}");

Standard Library

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) or reduce(list, fn, initial) combines items.
  • range(end), range(start, end), and range(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), and trim(value) transform strings.
  • split(value, separator) creates a list.
  • contains(value, part), startsWith(value, part), and endsWith(value, part) return booleans.

Math functions:

  • sqrt, floor, ceil, round, abs, pow, and random.
  • min(...) and max(...) accept one or more numbers.

Modules

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.

About

my own programming language

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages