ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

test.ox (2262B)


      1 ns main
      2 
      3 use io, std
      4 
      5 pub rec Vehicle {
      6     uint8 age
      7     uint16 cc
      8     str model
      9     str name
     10     str owner_name
     11     float value() = age * cc
     12     bool sold = false
     13     Vehicle parent
     14 }
     15 
     16 inline pub int add (int a, b) => a + b
     17 async int add (int a, b) => await sendoff(a, b)
     18 
     19 const int jack = 7
     20 const uint16 jill = 12
     21 
     22 
     23 Vehicle build_vehicle (Vehicle v) inline pub {
     24     v = { age: 12, cc: 1200, model: "Roaster" }
     25     ret v
     26 }
     27 
     28 void print_vehicle(Vehicle v) {
     29     print "Vehicle {{v.name}} is {{v.age}} yrs old with {{v.cc}} CC."
     30 }
     31 
     32 void main () {
     33     Vehicle mazda = new { age: 12, cc: 1200, model: "Miata" }
     34 
     35     print("mazda value is {{mazda.value}}")
     36 
     37     print_vehicle(mazda)
     38 
     39     print(add(5, 6)
     40 
     41     for Vehicle v in cars {
     42         print_vehicle <- v
     43     }
     44 
     45     cars -> each { v, i: print_vehicle v }
     46 
     47     // shorthand argument in closures
     48     
     49     []Vehicle old_cars = cars.where(v: v.age > 10)
     50 
     51     []Vehicle big_cars = cars -> where { .cc > 2000 } -> map { .model }
     52     
     53     []Vehicle new_cars = cars
     54         -> where { Vehicle v => v.age < 5 } 
     55         -> sorted { Vehicle a, b => a.model > b.model }
     56         -> map { Vehicle v => v.model }
     57         -> each { str model => print(model) }
     58        
     59     []Vehicle new_cars = cars
     60         -> where { .age < 5 } 
     61         -> sorted { (Vehicle a, b) => a.age > b.age }
     62         -> map { .model -> to_lower }
     63         -> each { print }
     64 
     65     http_server(8080, {sel: get_certif("certs/digitech.cert")})
     66     
     67     http_server <- 8080, ssl: get_certif <- "certs/digitech.cert"
     68 
     69     HttpServer server = (HttpServer)
     70         -> init
     71         -> serve <- port: 8080, ssl: get_certif <- paths -> where { .link = "private" }
     72         -> background
     73 
     74     HttpServer server = new {}
     75         -> init()
     76         -> serve(port: 8080, ssl: get_certif(paths.where { .link == "private" }))
     77         -> background()
     78 
     79     pub rec Cert { ... }
     80     pub rec HttpServer {
     81         void init() { ... }
     82         void serve(int port, Cert ssl) { ... }
     83         void background() { ... }
     84     }
     85 
     86     extend HttpServer {
     87         void print { ... }
     88     }
     89 
     90     print <- "hello world"
     91 
     92     print <- "hello world" -> capitalised  // print("hello world".capitalised())
     93 
     94     print <- capitalise <- "hello world"   // print(capitalise("hello world"))
     95     
     96 }
     97