Lesson 16 of 16

Complete Reference

every keyword, operator & built-in function

download this lesson (.md) ↓ all lessons

16. Complete Reference

A full cheat-sheet of every keyword, operator, and built-in function in STencil.

Keywords

let mut var func return if elif else while for in loop class model new this stencil import try catch true false null and or not

Literals

  • int: 42, -7, hex 0xFF (= 255)
  • float: 3.14, 4.0
  • string: "hi", 'hi', f-string f"x={1+1}"
  • bool: true false • null: null
  • list: [1, 2, 3] • dict: { "k": v }

Operators

  • arithmetic: + - * / %
  • comparison: == != < <= > >=
  • logic: && || ! (aliases and or not)
  • ternary: cond ? a : b
  • membership: x in coll
  • range: a..b (in loop i in a..b)
  • stencil stamp: Name!(args) (single), Name!*list (map)
  • index/member: x[i], obj.field, obj.method()

Statements

let/mut/var name = expr · name = expr · if/elif/else · while · for(init;cond;inc) · for x in iter · loop i in a..b · loop {} · func f(..) {} · f(..) => expr · class C {} · model M {} · stencil S(..) {} · return · try/catch · import "..." · >> expr (= print)


Built-in functions

Core / convert

Function Description
print(...) print values space-separated + newline
input(prompt?) read a line from stdin
type(x) type name as string
len(x) length of string/list/dict
str(x) / repr(x) to string
int(x) float(x) bool(x) convert
list(...) dict() construct
range(n) / range(a,b) / range(a,b,step) list of ints
exit(code) stop the program

Math

abs round floor ceil pow(a,b) sqrt sin cos tan log log10 exp min(list-or-args) max(list-or-args) sum(list)

Random

random() / rand() (0..1) · randint(a,b) (inclusive) · choice(list) · shuffle(list)

Strings

upper lower trim split(s,sep) join(list,sep) contains(coll,x) reversed(list-or-string) sorted(list) Methods: s.upper() s.lower() s.len() · index s[i]

Lists (methods)

a.push(x) a.append(x) a.pop() a.len() · index read/write a[i] / a[i] = v

Dicts (methods)

d.keys() d.values() d.has(k) d.len() · d[k] / d[k] = v

Files

read_file(p) write_file(p,text) append_file(p,text) file_exists(p) delete_file(p) list_dir(p) make_dir(p)

OS

args() env(name) cwd() exec(cmd) exit(code)

JSON

json_parse(str) json_str(value) json_pretty(value)

Regex

regex_match(p,t) regex_find(p,t) regex_find_all(p,t) regex_replace(p,t,repl)

Date / time

now() (ms) timestamp() (s) date() date_format(fmt) ticks() (ms since start)

Networking

http_get(url) http_post(url,body) · serve(port,handler) · tcp_connect(host,port) tcp_send(id,data) tcp_recv(id) tcp_close(id)

Database (SQLite)

db_open(path) db_exec(sql) db_query(sql)

Terminal graphics

game_init() game_end() clear() draw(x,y,text) / at(x,y,text) color(text,name) sleep(ms) ticks() screen_size() key()

Pixel window graphics

window_open(w,h,title) fill(color) pixel(x,y,color) rect(x,y,w,h,color) window_update() window_is_open() window_key(name) window_mouse() window_mouse_down() window_close()

Sound

beep(freq_hz, ms)

Colors (names for color/fill/rect/pixel)

red green blue white black yellow cyan magenta gray orange — or an int like 0x101828.

Key names (key(), window_key())

up down left right space enter esc and letters w a s d q e r f (more for key()).


Constants

math.pi, math.e

CLI

stencil file.st · stencil -c "code" · stencil (REPL) · stencil build file.st -o out.exe · stencil init · stencil install · stencil --version · stencil --help