summaryrefslogtreecommitdiff
path: root/chapter24/README.md
blob: 268fbfcc81106fc3837fa2289697e5d8b44e8295 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Chapter 24: Chaining relationals and Sparse Conditional Constant Propagation

You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter23) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter22...linear-chapter23) it to the previous chapter.


## Chaining relationals

Chainged relationals offer a cleaner, more readable way to write chained
comparisons without repeating the same variable.

```java 
if (min <= x && x < max)
```

becomes

```java 
if (min <= x < max)
```

This syntax saves space and makes the condition more intuitive, similar to how
range checks are written in mathematics.  E.g.

```java 
int score = 75;
if (60 <= score < 90) {
    sys.io.p("Pass");
}
```
This checks if score is between 60 (inclusive) and 90 (exclusive), without repeating `score`.

Expressions that mix opposite directions of comparison, like using both <= and
>=, are not allowed, because they create ambiguous logic.

```java
if (a <= b >= c)
```


## Operator direction rules
Stacked comparisons are *only valid* if all the comparison operators
"point" the same way:
- `<` with `<`
- `<` with `<=`
- `>` with `>`
- `>` with `>=`

It is not allowed to mix directions in a single chain:
```java 
if (a <= b >= c) // Invalid
```

Comparisons can be forced into numeric context using booleans.
This is valid but unusual:
```java
return (0 < arg) > 1;
```
Depending on the result of (0 < arg) this will turn into either
```java
return 0 > 1;
```
OR
```java
return 1 > 1;
```
The equality operator is valid in this form, but since it has lower precedence than comparisons,
```java 
a < b == c
```
is parsed as:
```java
(a < b) == c
```
This might not be the intended chained comparison.
A more explicit alternative is: 
```java 
a < b && b == c
```

Comparisons can be chained together in any length, and different comparison
operators may be mixed freely as long as they point in the same direction.
Equality(`==`) and inequality(`!=`) operators may be combined with any other
comparisons at any point in the chain. E.g:

```
return 0 < arg < arg+1 < 4;
```

## SCCP(top-down)