;; -*- mode:lisp; package:com.informatimago.languages.linc.c -*-

(include <math.h>)
(include <stdlib.h>)

(define-function test-priorities () void
  (let* ((i int 42)
         (j int 33)
         (k int 21)
         (a unsigned 101)
         (b unsigned 202)
         (c unsigned 303)
         (d unsigned 404))

    (= i (*= j 4))
    (*= (*= i j) 4)

    (+= i (*= j (? (== k 0) 1 0)))
    (+= (*= i j) (? (== k 0) 1 0))

    (*= i (\|\| (&& a b) (&& a c)))
    (*= i (&& (\|\| a b) (\|\| a c)))

    (+= i (\| (& a b) (& (~ a) c)))
    (+= i (& (\| a b) (~ (\| a c))))
    (+= i (~ (\| (& a b) (& (~ a) c))))
    (+= i (& (~ (\| a (~ b))) (~ (\| a c))))

    (+= j (\| a (^ b (& c (~ d)))))
    (+= j (~ (& a (^ b (\| c d)))))

    (+= k (+ (<< a (+ i j))
             (<< b (+ j k))
             (<< c (+ k i))))
    (+= k (+ (>> (+ a i) j)
             (>> (+ b j) k)
             (>> (+ c k) i)))
    (+= k (+ (- (<< a i) j)
             (- (<< b j) k)
             (- (<< c k) i)))))

;; Unary:
(define-function main () (int)
  (block

      (define-variable cptr      (pointer char)            "foo bar")
    (define-variable cptrptr   (pointer (pointer char))  (& cptr))
    (define-variable char1     char                      (* cptr))

    (define-variable pos       int                       (+ 1))
    (define-variable neg       int                       (- 1))

    ;; Binary:
    (define-variable sum2      int                       (+ 1 2))
    (define-variable sum4      int                       (+ 1 2 3 4))
    (define-variable dif2      int                       (- 1 2))
    (define-variable dif4      int                       (- 1 2 3 4))
    (define-variable mul2      int                       (* 2 3))
    (define-variable mul4      int                       (* 2 3 4 5))
    (define-variable div2      int                       (/ 2 3))
    (define-variable div4      int                       (/ 2 3 4 5))
    (define-variable mod2      int                       (% 2 3))
    (define-variable mod4      int                       (% 2 3 4 5))
    (define-variable boolior   bool                      (\|\| (== pos 1) (== neg 1)))
    (define-variable booland   bool                      (&& (== pos 1) (== neg 1)))
    (define-variable bit-ior   int                       (\| sum2 mul2))
    (define-variable bit-and   int                       (& sum2 mul2))
    (define-variable bit-eor   int                       (^ sum2 mul2))
    (define-variable beq       bool                      (== sum2 mul2))
    (define-variable bne       bool                      (!= sum2 mul2))
    (define-variable blt       bool                      (<  sum2 mul2))
    (define-variable bgt       bool                      (>  sum2 mul2))
    (define-variable ble       bool                      (<= sum2 mul2))
    (define-variable bge       bool                      (>= sum2 mul2))
    (define-variable lshift    int                       (<< sum2 pos))
    (define-variable rshift    int                       (>> sum2 pos))
    ;; memptr deref     .*
    ;; ptrmemptr-deref ->*
    (define-variable ucast     (unsigned int)              (cast mul4 unsigned int))
    (define-variable size1     (unsigned int)              (sizeof char))
    (define-variable size2     (unsigned int)              (sizeof cptr))

    ;; Unary:
    (define-variable mul4-p1   int                       (++ mul4))
    (define-variable mul4-m1   int                       (-- mul4))
    (define-variable mul4-p1p  int                       (post++ mul4))
    (define-variable mul4-m1p  int                       (post-- mul4))
    (define-variable boolnot   bool                      (! (== pos 1)))
    (define-variable bit-not   int                       (~ mul4))


    ;; Ternary
    (define-variable if3       int                       (? (== mul2 6) 1 0))

    ;; Miscellaneous:

    (declare-structure point (x int) (y int))
    ;; (define-variable pt1 point {1 2})
    (declare-variable pt1 (struct point))
    (= (. pt1 x) 1)
    (= (. pt1 y) 2)
    (define-variable pt1-x int (. pt1 x))
    (define-variable ppt (pointer (struct point)) (& pt1))
    (define-variable ppt1-y int (-> ppt y))
    (define-variable cho    char (aref cptr 1))

    (declare-type link-t (struct link
                                 (next (pointer (struct link)))
                                 (item (struct
                                        (link (pointer (struct link)))
                                        (also (struct
                                               (link (pointer (struct link)))))))))
    (define-variable ll0 (pointer link-t) NULL)
    (= ll0  (-> ll0 next next next))
    (= ll0  (-> (-> (-> ll0 next) next) next))
    (= ll0  (-> ll0 (-> next (-> next next))))

    (= ll0  (-> ll0 (. (-> next item) link)))
    (= ll0  (-> ll0 (. (-> next (. item also)) link)))
    (= ll0  (-> ll0 (-> next (. item also link))))
    (= ll0  (-> ll0 (-> next (. item link))))
    (= ll0  (-> ll0 (. (-> next item) link)))

    (= ll0  (-> ll0 (-> next (. item (-> link next)))))
    (= ll0  (-> ll0 (-> next (-> (. item link) next))))
    (= ll0  (-> ll0 (. (-> next item) link) next))


    (define-variable sin-pi-2 double (sin (/ M-PI 2)))

    (define-variable seq  int (\, (++ size1) (++ size2) 3))

    (cast cptr void)
    (cast cptrptr void)
    (cast char1 void)
    (cast pos void)
    (cast neg void)
    (cast sum2 void)
    (cast sum4 void)
    (cast dif2 void)
    (cast dif4 void)
    (cast mul2 void)
    (cast mul4 void)
    (cast div2 void)
    (cast div4 void)
    (cast mod2 void)
    (cast mod4 void)
    (cast boolior void)
    (cast booland void)
    (cast bit-ior void)
    (cast bit-and void)
    (cast bit-eor void)
    (cast beq void)
    (cast bne void)
    (cast blt void)
    (cast bgt void)
    (cast ble void)
    (cast bge void)
    (cast lshift void)
    (cast rshift void)
    (cast ucast void)
    (cast size1 void)
    (cast size2 void)
    (cast mul4-p1 void)
    (cast mul4-m1 void)
    (cast mul4-p1p void)
    (cast mul4-m1p void)
    (cast boolnot void)
    (cast bit-not void)
    (cast if3 void)
    (cast pt1 void)
    (cast pt1-x void)
    (cast ppt void)
    (cast ppt1-y void)
    (cast cho void)
    (cast sin-pi-2 void)
    (cast seq void)

    (return 0)))
ViewGit