Skip to content

Commit fe3051e

Browse files
committed
Add some notes on what it means to define code for different environments and how they interact
1 parent dc8a2e3 commit fe3051e

File tree

1 file changed

+175
-21
lines changed

1 file changed

+175
-21
lines changed

compiler/notes.md

+175-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
## Loops
44

55
```rust
6+
// Infinite loop
7+
loop {
8+
solveTheHaltingProblem();
9+
}
10+
611
// Loop over a container
712
loop list {
813
operationOnItem(it);
@@ -17,29 +22,8 @@ loop 0..10 {
1722
loop i: 0..10 {
1823
i == it;
1924
}
20-
21-
// Custom loop condition
22-
mut i = 0;
23-
loop (i < 10) {
24-
i += 1;
25-
}
26-
27-
// Loop with initializer and condition
28-
loop (let i = 0; i < 10) {
29-
i += 1; // 0, 1, 2, 3, ...
30-
}
31-
32-
// Loop with initializer, condition, and post-loop expression
33-
loop (let i = 0; i < 10; i++) {
34-
i; // 0, 1, 2, 3, ...
35-
}
3625
```
3726

38-
### Problem
39-
40-
Harder to talk about in real life. It's easier to say "you should use a for loop" than it is to say "you should use a
41-
loop with an initializer, condition, and post-loop expression."
42-
4327
## Imports
4428

4529
Imports will all have a source and a series of selectors.
@@ -88,3 +72,173 @@ For referencing modules which are nearby in the file system.
8872
use ./nested/library/{ someFunction };
8973
use ../../other_library/{ someFunction };
9074
```
75+
76+
## Environments
77+
78+
The main idea of this language is to be able to write a single codebase and have it work as both a frontend and a server. What are the implications of this?
79+
80+
Any given chunk of code can be run in 1 of 3 states:
81+
82+
1. Frontend
83+
2. Backend
84+
3. Isomorphic/Both
85+
86+
What happens when trying to interact with code from one environment in another? The left side indicates the execution environment and the top indicates which environment the function was written for.
87+
88+
| | Frontend | Backend | Isomorphic |
89+
| -------- | -------------------- | -------------------- | -------------------- |
90+
| Frontend | Normal function call | Remote call | Normal function call |
91+
| Backend | Remote call | Normal function call | Normal function call |
92+
93+
When code is not given a specific environment, it will run in both.
94+
95+
How does calling across environments change the compiled code? Functions become colored. For now, everything that does a cross environment call will become asynchronous and do automatic awaiting.
96+
97+
```rust
98+
back {
99+
fn logBackend() {
100+
log("I run on the backend");
101+
}
102+
103+
logFrontend();
104+
logBackend();
105+
}
106+
107+
front {
108+
fn logFrontend() {
109+
log("I run on the frontend");
110+
}
111+
112+
logBackend();
113+
logFrontend();
114+
}
115+
116+
logFrontend();
117+
logBackend();
118+
```
119+
120+
should compile to something functionally equivalent to this:
121+
122+
```js
123+
/**
124+
* frontend.js
125+
*/
126+
function logFrontend() {
127+
console.log('I run on the frontend')
128+
}
129+
130+
function logBackend() {
131+
return fetch('/logBackend')
132+
}
133+
134+
server.on('message', (msg) => {
135+
if (msg.fnId === 'logFrontend') {
136+
logFrontend()
137+
}
138+
})
139+
140+
await logBackend()
141+
logFrontend()
142+
143+
// from the isomorphic section
144+
logFrontend()
145+
await logBackend()
146+
147+
/**
148+
* backend.js
149+
*/
150+
function logFrontend() {
151+
return client.send('message', { fnId: 'logFrontend' })
152+
}
153+
154+
function logBackend() {
155+
console.log('I run on the backend')
156+
}
157+
158+
app.get('/logBackend', () => {
159+
logBackend()
160+
})
161+
162+
await logFrontend()
163+
logBackend()
164+
165+
// from the isomorphic section
166+
await logFrontend()
167+
logBackend()
168+
```
169+
170+
Next case: defining a function which calls in to another environment without defining one
171+
172+
```rust
173+
fn log() { // Becomes backend by association
174+
logBackend();
175+
}
176+
log();
177+
```
178+
179+
compiles to:
180+
181+
```js
182+
/**
183+
* frontend.js
184+
*/
185+
async function log() {
186+
await logBackend()
187+
}
188+
189+
function logBackend() {
190+
return fetch('/logBackend')
191+
}
192+
193+
await log()
194+
195+
/**
196+
* backend.js
197+
*/
198+
function log() {
199+
logBackend()
200+
}
201+
202+
function logBackend() {
203+
console.log('I run on the backend')
204+
}
205+
206+
log()
207+
```
208+
209+
This sort of coloring will climb all the way back up to the root. As soon as you have one asynchronous/cross-environment call, the whole call stack needs to be asynchronous.
210+
211+
What about functions that do both?
212+
213+
```rust
214+
fn log() {
215+
logFrontend();
216+
logBackend();
217+
}
218+
219+
log();
220+
```
221+
222+
becomes
223+
224+
```js
225+
/**
226+
* frontend.js
227+
*/
228+
async function log() {
229+
logFrontend()
230+
await logBackend()
231+
}
232+
233+
await log()
234+
235+
/**
236+
* backend.js
237+
*/
238+
async function log() {
239+
await logFrontend()
240+
logBackend()
241+
}
242+
243+
await log()
244+
```

0 commit comments

Comments
 (0)