Quick Start
This page walks through the language basics in the order you are likely to need them. Every example runs in the playground as well as on the command line.
Hello, world
Section titled “Hello, world”Create a file named hello.pyl:
print("Hello, world.")Run it with the interpreter:
pyle hello.pylprint writes its arguments separated by spaces and adds a newline. printf and format use {} placeholders:
let name = "world"printf("Hello, {}!", name)let message = format("{} + {} = {}", 1, 2, 3)print(message)Variables
Section titled “Variables”Variables are declared with let. Reassignment does not repeat let.
let count = 0count = count + 1count += 1
let name = "Pyle"let pi = 3.14159let ready = truelet missing = nonePyle is dynamically typed. A variable can hold any value, and type hints are documentation only.
let value: int = 42value = "now a string" # legal; hints are not enforcedFunctions
Section titled “Functions”Functions are declared with fn. The last expression can be returned with =>.
fn add(a, b) { return a + b}
fn double(x) => x * 2
printf("{}", add(2, 3))printf("{}", double(21))Functions are values, so they can be stored in arrays, passed as arguments, and returned from other functions.
fn make_counter() { let count = 0 return fn() { count += 1 return count }}
let next = make_counter()print(next()) # 1print(next()) # 2Control flow
Section titled “Control flow”let score = 87
if score >= 90 { print("A")} elif score >= 80 { print("B")} else { print("C")}
for i in 1..5 { printf("{}", i) # 1 2 3 4}
let total = 0for value in [10, 20, 30] { total += value}print(total)1..5 is a range with an exclusive end. Maps are not directly iterable; use .keys() or .values().
Arrays and maps
Section titled “Arrays and maps”let numbers = [1, 2, 3]numbers.append(4)numbers[0] = 10print(numbers) # [10, 2, 3, 4]print(numbers.slice(1, 3)) # [2, 3]
let person = {name: "Ada", age: 36}print(person.name) # Adaperson["age"] = 37print(person.keys()) # ["name", "age"]Array and map indexing is checked at runtime. A missing map key through brackets raises IndexError; dot access returns none instead.
Structs
Section titled “Structs”Structs have a fixed shape declared in the header. Methods receive self automatically.
struct Rect(w, h) { fn area() => self.w * self.h
static fn square(size) { return Rect(size, size) }}
let r = Rect(4, 6)printf("area = {}", r.area())
let s = Rect.square(5)printf("square area = {}", s.area())Imports
Section titled “Imports”Native modules and other scripts are loaded with import.
let math = import("math")let os = import("os")
printf("sqrt(144) = {}", math.sqrt(144))print(os.platform())A script can import another script by path. The .pyl extension is optional.
let helpers = import("lib/helpers")helpers.greet("Pyle")Error handling
Section titled “Error handling”Errors are values. Use pcall to catch a failing call.
fn divide(a, b) { assert(b != 0, "cannot divide by zero") return a / b}
let result = pcall(divide, 10, 0)
if result.ok { print(result.value)} else { printf("{}: {}", result.error.type, result.error.message)}Command-line arguments
Section titled “Command-line arguments”os.args contains the arguments after the script path. Flags before the script belong to the interpreter.
let os = import("os")print(os.args)pyle script.pyl one two --flagInside the script, os.args is ["one", "two", "--flag"].
Where to go next
Section titled “Where to go next”- Language guide for the full syntax.
- Standard library for modules and built-in types.
- Embedding to call Pyle from C++.