v0.1A scripting language for the web

Syntax

A tour of every piece of the May language: literals, control flow, functions, and the pipe operator.

Comments

There are no special comment delimiters. Use whitespace and naming to keep code clear.

Literals

42         // int
3.14       // float
"hello"    // string
[1, 2, 3]  // array
{ k: 1 }   // object

Variables

Declare a variable with a type keyword followed by a name and an initializer:

int score = 100;
string name = "May";
float ratio = 0.5;
array nums = [1, 2, 3];
object user = { name: "Ada", age: 30 };
var anything = 42;

Use var when you do not want to commit to a type. The other keywords double as documentation for what the variable holds.

Operators

Arithmetic

a + b   // add (also concatenates strings)
a - b   // subtract
a * b   // multiply
a / b   // divide
a++     // increment in place

Comparison

a == b
a != b
a < b
a > b
a <= b
a >= b

The pipe operator

The pipe ( | ) feeds the value on its left into the function on its right. It chains transformations without nesting.

"hello world" | upper | print;
// is equivalent to
print(upper("hello world"));

Pipes work with any single-argument function, including ones you write.

Control flow

If / else

if (score >= 90) {
    print("A");
} else {
    print("Not yet");
}

While

int i = 0;
while (i < 5) {
    print(toString(i));
    i++;
}

Use break to exit a loop and continue to skip to the next iteration.

Functions

Functions begin with a return type, a name, parameters with their types, and a body. Use return to send a value back.

int add(int a, int b) {
    return a + b;
}

add(3, 4) | print;

Functions that do not need to return a value can return void:

string greet(string name) {
    return "Hello, " + name;
}

Objects and field access

Objects are key-value collections written with curly braces. Access fields with the arrow operator ->:

object user = { name: "Ada", age: 30 };
user->name | print;
user->age | print;

Field access is also how you reach into the request object returned by read_request:

object req = read_request(client);
print(req->method + " " + req->path);

Arrays

Arrays use square brackets. Use length() to get the size and index with brackets:

array nums = [10, 20, 30];
nums[0] | print;
nums | length | print;

Imports and exports

Pull in standard library modules by name:

import "io";
import "http";
import "html";

Pull in your own files by relative path. Mark anything you want to expose with the export keyword:

// utils.may
export string greet(string name) {
    return "Hello, " + name;
}

// main.may
import "utils.may";
greet("world") | print;

That is the whole language

If you have written C, JavaScript, or Python before, you already know May. The unique parts are the pipe operator and the HTML builder, both of which you pick up the first time you see them.