(module main)

(fn add_local ((a i32)) -> i32
  (let one i32 1)
  (var total i32 (+ a one))
  (set total (+ total 1))
  total)

(fn keep_flag ((flag bool)) -> bool
  (let local_flag bool flag)
  local_flag)

(fn flip_flag ((flag bool)) -> bool
  (var current bool flag)
  (set current (if current
    false
    true))
  current)

(fn add_wide_local ((base i64)) -> i64
  (var count i64 base)
  (set count (+ count 2i64))
  count)

(fn add_ratio_local ((base f64)) -> f64
  (var amount f64 base)
  (set amount (+ amount 0.5))
  amount)

(test "locals work"
  (let base i32 2)
  (var value i32 (add_local base))
  (set value (+ value 1))
  (= value 5))

(test "bool let locals work"
  (let expected bool true)
  (let actual bool (keep_flag expected))
  actual)

(test "bool let locals preserve false"
  (if (keep_flag false)
    false
    true))

(test "bool var set flips true"
  (if (flip_flag true)
    false
    true))

(test "bool var set flips false"
  (flip_flag false))

(test "i64 var set works"
  (= (add_wide_local 40i64) 42i64))

(test "f64 var set works"
  (= (add_ratio_local 41.5) 42.0))

(fn main () -> i32
  (var flag bool false)
  (set flag (flip_flag flag))
  (if flag
    (add_local 2)
    1))
