You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spec/9.md
+113-1Lines changed: 113 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
## Quality of life
2
2
3
-
Adding `!` and `else` would *really* improve quality of life, but let's do that later.
3
+
Adding `~` (for logical negation) and `else` would *really* improve quality of life, but let's do that later.
4
4
5
5
## For loops
6
6
@@ -28,3 +28,115 @@ Although, **this** does seem to work:
28
28
}
29
29
}
30
30
```
31
+
32
+
## Logical negation
33
+
34
+
Simply do the following:
35
+
36
+
```asm
37
+
cmp rax, 0
38
+
sete al
39
+
movzx eax, al
40
+
```
41
+
42
+
Note that I've made an unconventional choice and used the symbol `~` instead of `!` for logical negation.
43
+
44
+
## Else
45
+
46
+
```av
47
+
if (cond) {
48
+
<body1>
49
+
} else {
50
+
<body2>
51
+
}
52
+
```
53
+
54
+
Currently, plain `if` works like this:
55
+
56
+
```asm
57
+
mov rax, <cond>
58
+
cmp rax, 0
59
+
jz .after
60
+
<body1>
61
+
.after:
62
+
```
63
+
64
+
To add `else` to this, we can do this:
65
+
66
+
```asm
67
+
mov rax, <cond>
68
+
cmp rax, 0
69
+
jz .body
70
+
<body1>
71
+
jmp .after
72
+
.body:
73
+
<body2>
74
+
.after:
75
+
```
76
+
77
+
As for `else if`, let's say we had:
78
+
79
+
```av
80
+
if (cond1) {
81
+
<body1>
82
+
} else if (cond2) {
83
+
<body2>
84
+
} else if (cond3) {
85
+
<body3>
86
+
} else {
87
+
<body4>
88
+
}
89
+
```
90
+
91
+
We'd write this as:
92
+
93
+
```asm
94
+
mov rax, <cond1>
95
+
cmp rax, 0
96
+
jz .cond2
97
+
<body1>
98
+
jmp .after
99
+
.cond2:
100
+
mov rax, <cond2>
101
+
cmp rax, 0
102
+
jz .cond3
103
+
<body2>
104
+
jmp .after
105
+
.cond3:
106
+
mov rax, <cond3>
107
+
cmp rax, 0
108
+
jz .cond4
109
+
<body3>
110
+
jmp .after
111
+
.cond4:
112
+
<body4>
113
+
.after:
114
+
```
115
+
116
+
So the best way to do this would probably be to let the desugaring layer group together `if`, `else if`, ..., `else` under some node called IfElseBlock, and then just spit out the above codegen.
117
+
118
+
## Correction regarding for loops
119
+
120
+
I was very close last time, but the actual correct lowering should be:
121
+
122
+
```av
123
+
{
124
+
init;
125
+
while (cond) {
126
+
{
127
+
...
128
+
}
129
+
incr;
130
+
}
131
+
}
132
+
```
133
+
134
+
Since, otherwise, loops like this would be problematic:
0 commit comments