Chapter 2

Conditional Evaluation

Note

For the purposes of conditional evaluation, Minilang only considers whether a value is nil or not. All other values (including false, 0, "", [], etc) are considered as not nil.

Comparison Operations

Like most languages, values in Minilang can be compared using infix comparison operators, = (a single =), !=, <, >, <= and >=. Unlike many other languages, comparison operators in Minilang do not return true or false, instead they return their 2nd argument if the comparison is satisfied, and nil is it is not.

  • 1 < 2 1 > 2
  • 3 = 3 3 != 3
  • 4 < 4 4 <= 4
  • 5 >= 4 5 >= 6
  • if-expressions

    Code can be evaluated conditionally in Minilang using if-expressions.

  • for X in [0, false, "", [], nil] do if X then print(X, " is considered not nil.\n") else print(X, " is considered nil.\n") end end
  • Each branch of an if-expression is a block, and can contain 0 or more variable declarations and expressions.

  • for X in [1, nil] do if X then let A := 1 + 2 print("A = ", A, "\n") else let B := 4 * 5 print("B = ", B, "\n") end end
  • Finally, nearly all constructs in Minilang are expressions, i.e. they result in a value. if-expressions evaluate to the last expression in their evaluated branch. The else branch of an if-expression can be omitted, in which case it is treated as nil.

  • for X in [1, nil] do print("Choosing the ", if X then "then" else "else" end, " branch.\n") end
  • for X in [1, nil] do print("Choosing the ", if X then "then" end, " branch.\n") end
  • Sometimes we want to use the value of the condition expression if it is not nil. In Minilang, the condition of an if-expression can be assigned to a variable with let or var.

  • if let X := 2 + 2 then print('{X} is not nil\n') end
  • and-expressions and or-expressions

    Code can also be evaluated conditionally in Minilang using and, or. An and-expression evaluates to its second argument if both arguments are not nil, and to nil otherwise. An or-expression evaluates to its first argument if it is not nil, otherwise it evaluates to its second argument. Both and-expressions and or-expressions only evaluate their second argument if required.

    X and Y

    Y = nil

    Y != nil

    X = nil

    nil (Y not evaluated)

    X != nil

    nil

    Y

    X or Y

    Y = nil

    Y != nil

    X = nil

    nil

    Y

    X != nil

    X (Y not evaluated)

  • fun test(V, X) do print('{V} = {X}\n') ret X end
  • test("X", 10) and test("Y", nil)
  • test("X", 10) and test("Y", 20)
  • test("X", nil) and test("Y", nil)
  • test("X", nil) and test("Y", 20)
  • test("X", 10) or test("Y", nil)
  • test("X", 10) or test("Y", 20)
  • test("X", nil) or test("Y", nil)
  • test("X", nil) or test("Y", 20)
  • not-expressions and xor-expressions

    A not-expression returns some if its argument returns nil and return nil otherwise. If exactly one argument of a xor-expression returns a non-nil value, that value is returned, otherwise it returns nil. Both not-expressions and xor-expressions always evaluate their arguments.

    not X

    X = nil

    X != nil

    some

    nil

    X xor Y

    Y = nil

    Y != nil

    X = nil

    nil

    Y

    X != nil

    X

    nil

  • fun test(V, X) do print('{V} = {X}\n') ret X end
  • not test("X", 10)
  • not test("X", nil)
  • test("X", 10) xor test("Y", nil)
  • test("X", 10) xor test("Y", 20)
  • test("X", nil) xor test("Y", nil)
  • test("X", nil) xor test("Y", 20)