stz the big syntax updated
It's been two months since the last 'syntax so far' post so it's probably time to revisit absolutely everything.
(updated 12th February 2025)
// booleans
true
false
// nothing
ø
// integers
1234567890
-1234567890
1_234_567_890
-1_234_567_890
0b0110101001
0o81828375
-0o81828375
0xF23BEEF
-0xF23BEEF
// floats
1234.56789
-1234.56789
1_234.567_89
-1_234.567_89
// raw strings
`a raw string`
delim`a raw stringdelim`
// escaping strings
'a\tstring\n'
foo'a\tstring\nfoo'
// template strings
"Hello ~{name}"
x"Hello ~[name]x"
"Hello ~xnamex"
// variables and assignment
foo := 1
foo: uint = 1
foo: uint
// immediate arrays
a, b, c
(a, b, c)
(a,)
(,)
(a
b
c)
// typed immediate arrays
(uint | 1, 2, 3)
(uint | 1)
foo: (array of: uint) = (1, 2, 3)
// copying/combining arrays
(...other-array,)
(...array-one, 4, 5, 6, ...array-two)
// immediate maps
(a: 1, b: 2, c: 3)
(a: 1,)
(a: 1
b: 2
c: 3)
// typed immediate maps
(string, int | "a": 1, "b": 2, "c": 3)
(string, int | "a": 1)
(string, int |)
foo: (map of: string to: int) = ("a": 1, "b": 2, "c": 3)
// copying/combining maps
(...other-map,)
(...map-one, a: 4, b: 5, c: 6, ...map-two)
// class definition
person (name: string, height: metre)
person (class | name: string, height: metre)
// union definition
pet (union | as-cat: cat, as-dog: dog)
// tagged-union definition
pet (tagged-union | cat, dog)
[my-pet: cat + dog | ...]
// enum definition
cardinality (north, east, south, west)
cardinality (north = 1, east, south, west)
cardinality (uint | north, east, south, west)
// method calls
person name
person name: 'jane'
1 + 2
2 * (3 + 4)
// deferred method calls
...person free
// object creation
foo := {person | name: 'jane', height: 175cm}
foo: person = {name: 'jane', height: 175cm}
foo := {&person | new, name: 'jane', height: 175cm}
foo: &person = {new, name: 'jane', height: 175cm}
...foo free
// block of code
my-block := [a: string, b: int -> c: int | c <- a length + b]
// block with inferred types
my-block := [a, b -> c | c <- a + length + b]
// block of code with implicit return
my-block := [a: string, b: int | a length + b]
// block of code with implicit parameter 'self'
my-block := [-> return: bool | return <- age ≥ 18]
// block of code with implicit parameter and return
my-block := [age ≥ 18]
// block closure
x := 1
my-block := [a: string, b: int -> c: int | c <- a length + b + x]
// block coroutine
my-coroutine := [a: string, b: int -> c: yield |
1 to: 10 do: [:i | c <- a length + b + i]]
// signature
person name
person name: a-name
person shake: body-part near: another-person
a + b
// method declaration
a add: b ~ a: uint, b: uint -> r: uint [r <- a + b]
// method declaration with inferred types
a add: b ~ -> r [r <- a + b]
// method declaration with inferred types and implicit return
a add: b ~ [a + b]
// method declaration with implicit parameter and return
person is-adult? ~ [age ≥ 18]
// block declaration
my-block := [a add: b | a: uint, b: uint, r: uint | r <- a + b]
my-block evaluate: 1, 2
1 add: 2
// reference type
p: &person
jane := {&person | allocate, name: 'jane', height: 175cm}
bob := {person | name: 'bob', height: 180cm}
database save: jane
database save: &bob
// array access
bob := my-array[0]
my-array[12] = jane
// array slicing
my-array[start:]
my-array[:stop]
my-array[start:stop]
my-array[::length]
my-array[start::length]
// packages
package: 'my package'
using: 'opengl'
import: 'opengl'
// scoping
public person (name: string,)
package person (name: string,)
private person (name: string,)
private a add: b ~ a: uint, b: uint -> r: uint [r <- a + b]
// scoping class members
person
(public name: string
package nickname: string
private superheroname: string)
// class specialisation
array
(#specialise: [| -> class | element-class = any]
#specialise: [of: element-class | any -> class |]
#specialise: [of: element-class length: length | any, uint -> class |]
#traits: (iterable of: element-class, sequencable of: element-class,)
public length: uint
private origin: (memory-address of: element-class))
generic-array: array
people-array: (array of: person)
people-buffer: (array of: person length: 10)
// default block parameter values
my-block := [a add: b | a: uint = 1, b: uint = 2 -> r: uint | r <- a + b]
my-block evaluate
my-block evaluate: 10
my-block evaluate: 10, -10
// default class member values
rgba32 (r: uint8, g: uint8, b: uint8, a: uint8 = 255)
// default keyword selector parameter values
display draw-line-from: start to: stop as: color
~ diplay: &drawable
start: (vec2 of: float)
stop: (vec2 of: float)
color: rgba32 = display current-color
-> ø [
...
]
render-buffer draw-line-from: 0, 0 to: 10, 10
red := {rgba32 | r: 255, g: 0, b: 0}
render-buffer draw-line-from: 10, 10 to: 20, 20 as: red
// maybe
jane?: person?
jane? then: [jane | stdout print: jane]
jane? else: [stdout print: 'no Jane']
jane? else: [the-reason | stdout print: the-reason]
// custom maybe
person_else_socket: person / socket
person_else_socket then: [person | ...]
person_else_socket else: [socket | ...]
// resolving tagged-unions
pet (tagged-union | cat, dog)
[cat switch | cat: cat | ...]
[dog switch | dog: dog | ...]
[pet switch | pet: pet | ...]
my-pet := io download-a-pet
my-pet switch