Skip to content

Instantly share code, notes, and snippets.

@ichrvk ichrvk/Idioms.md
Created May 23, 2012

Embed
What would you like to do?
Zircon programming language syntax description

ZIRCON

String interpolation

apples = '33 tasty apples'
oranges = '6 juicy oranges'
output = "I had $apples and $oranges" # note double quotes
output = "I paid \$34 for them" # uninterpolated dollar sign
output = 'I paid $34 for them' # or, alternatively, single quotes

Requiring another file

use :base # .zr is assumed
use :base, :extensions, :extensions-optional # if no file is found, but folder is present, loads all .zr files from the folder
use-all # loads all .zr files in current folder
use "core/base.zr" # direct filename

Variable assigning

a = b
a, b = b, a # swapping
a, b, c = d, e, f # mass assignment
first, second, third = a # list decomposed into elements
a = :first, :second, :third # a is a list

Hashes

hash = field: 'a', other-field: 'b', yet-another: 'c' # :field, :other-field and :yet-another are symbols
var = :new-var
hash << ($var: 'd') # hash :new-var == 'd'
hash.$var = 'd' # alternative
hash.field # => a

New struct

struct phone-number = :area-code, :number
larrys-number = *phone-number
larrys-number.area-code = 303
larrys-number.number = 5053819

Testing

f multiply x, y
    5, 2 -> 10 # multiply 5, 2 should be equal to 10
    8, x -> (x << 3) # basic support for fuzzing
    133, 14 -> 1862
    x * y

“Or equals”

a = 5 if foo
a ?= 3 # if a is not set, 3, else, no change

Convert anything to string

a = 5
`a #=> '5'
`5 #=> '5'

struct name = :first-name, :last-name

john-smith = *name 'john' 'smith'

`john-smith #=> "first-name:john, last-name:smith" - default to_s for structs

f `name
    f, l = (first-name, last-name) @ capitalize
    "$f $l"

`john-smith #=> "John Smith"

Get the type of variable

a = 2.14
type a #=> :float
typeof a #=> :float
float? a #=> true. type? function is defined for every type and struct, including user-defined

Join an array

a = 3, 5, 9
`a #=> "3, 5, 9" - default to_s for lists
`a ' - ' #=> "3 - 5 - 9" - an argument specifies join string

Namespaces

use :animals-core #=> loads animals-core.zr, no namespace defined
use :pigs, Pig #=> define Pig namespace for functions in pigs.zr

Pig:oink #=> a function called which is defined in pigs.zr

f oink
    'Oink!'

p oink #=> local implementation
p Pig:oink #=> external implementation

Return values

f add x, y 
    x + y #=> last statement is returned by default

f divide x, y
    return NAN if y == 0 #=> preemptive return
    x / y

f (complex, numeric).add x, y
    y-real, y-imaginary = complex? y ? (y.real, y.imaginary) : (y, 0)
    *complex x.real + y-real, x.imaginary + y-imaginary #=> struct is returned

Exceptions

f check-twitter username
    stream = open "http://twitter.com/$username"
    err :no-connection unless stream
    err :twitter-troubles if err? stream
    stream

f twitter-status
     true unless err? check-twitter 'zircon'

Functions

f do-stuff arg1, arg2
    do-more-complex-stuff arg1, arg2, 5 # Basic function declaration

f inline-function = 'Hello world' # one-line version

f zircon? arg1, (arg2, 'zircon') = true if arg1 == arg2 # default parameters

# Warning: for every struct defined, struct-name? is reserved.

# Types of functions are normally inferred. Manual type declaration:

f complex.real-part x
    x.real

f (complex, numeric).real-part-equal? x, y
    x.real == y

# Full type signature

f (complex, numeric -> boolean).imaginary-part-equal? x, y
    x.imaginary == y

Switch

a = case b
    2 ? 'moo'
    3 ? 'frank'
    4 ? 'ziegenbock'
    :else ? 'something else'

# How case is defined

f case value, statements
    statements >> (f _ s, v = break-with v.1 if (v.0 == b || v.0 == :else))

f any:x ? any:y
    x, y 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.