-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshort_circuit_test.fun
More file actions
executable file
·57 lines (46 loc) · 1.14 KB
/
short_circuit_test.fun
File metadata and controls
executable file
·57 lines (46 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
// Short-circuit demo for || and &&
print("=== short-circuit demo ===")
fun mark(name, v)
// Show when this branch is evaluated
print(name)
return v
// OR short-circuits: RHS not evaluated when LHS is true
if (true || mark("OR-RHS", 0))
print(1) // expect: prints 1; no "OR-RHS"
else
print(0)
// AND short-circuits: RHS not evaluated when LHS is false
if (false && mark("AND-RHS", 1))
print(1)
else
print(0) // expect: prints 0; no "AND-RHS"
// OR evaluates RHS when needed (LHS false)
if (false || mark("OR-NEEDED", 1))
print(1) // expect: prints "OR-NEEDED" then 1
else
print(0)
// AND evaluates RHS when needed (LHS true)
if (true && mark("AND-NEEDED", 1))
print(1) // expect: prints "AND-NEEDED" then 1
else
print(0)
print("=== end ===")
/* Expected output:
=== short-circuit demo ===
1
0
OR-NEEDED
1
AND-NEEDED
1
=== end ===
*/