about summary refs log tree commit diff stats
path: root/archive/2.transect/compiler10
blob: ce0e487a67618ecd08137a1d9d548ed06dc72b9b (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
=== Goal

A memory-safe language with a simple translator to x86 that can be feasibly written without itself needing a translator/compiler.

Memory-safe: it should be impossible to:
  a) create a pointer out of arbitrary data, or
  b) to access heap memory after it's been freed.

Simple: do all the work in a 2-pass translator:
  Pass 1: check each statement's types in isolation.
  Pass 2: emit code for each statement in isolation.

=== Language summary

Program organization is going to be fairly conventional and in the spirit of C: programs will consist of a series of type, global and function declarations. More details below. Functions will consist of a list of statements, each containing a single operation. Since we try to map directly to x86 instructions, combinations of operations and operands will not be orthogonal. You won't be able to operate at once on two memory locations, for example, since no single x86 instruction can do that.

Statement operands will be tagged with where they lie. This mostly follows C: local variables are on the stack, and variables not on the stack are in the global segment. The one addition is that you can lay out (only word-size) variables on registers. This is kinda like C's `register` keyword, but not quite: if you don't place a variable on a register, you are *guaranteed* it won't be allocated a register. Programmers do register allocation in this language.

The other memorable feature of the language is two kinds of pointers: a 'ref' is a fat pointer manually allocated on the heap, and an 'address' is a far more ephemeral thing described below.

--- Ref

Refs are used to manage heap allocations. They are fat pointers that augment the address of a payload with an allocation id. On x86 a ref requires 8 bytes: 4 for the address, and 4 for the alloc id. Refs can only ever point to the start of a heap allocation. Never within a heap allocation, and *certainly* never to the stack or global segment.

How alloc ids work: Every heap allocation allocates an additional word of space for an alloc id in the payload, and stores a unique alloc id in the payload as well as the pointer returned to the caller. Reclaiming an allocation resets the payload's alloc id. As long as alloc ids are always unique, and as long as refs can never point to within a heap allocation, we can be guaranteed that a stale pointer whose payload has been reclaimed will end up with a mismatch between pointer alloc id and payload alloc id.

  x <- alloc   # x's alloc id and *x's alloc id will be the same, say A
  y <- copy x  # y also has alloc id A
  free x       # x's alloc id is now 0, as is *x's alloc id
  ..*y..       # y's alloc id is A, but *y's alloc id is 0, so we can signal an error
  z <- alloc   # say z reuses the same address, but now with a new alloc id A'
  ..*y..       # y's alloc id is A, but *y's alloc id is A', so we can signal an error

--- Address

Since our statements are really simple, many operations may take multiple statements. To stitch a more complex computation like `A[i].f = 34` across multiple statements, we need addresses.

Addresses can be used to manage any memory address. They can point inside objects, on the stack, heap or global segment. Since they are so powerful we greatly restrict their use. Addresses can only be stored in a register, never in memory on the stack or global segment. Since user-defined types will usually not fit on a register, we forbid addresses in any user-defined types. Since an address may point inside a heap allocation that can be freed, and since `free` will be a function call, addresses will not persist across function calls. Analyzing control flow to find intervening function calls can be complex, so addresses will not persist across basic block boundaries.

The key open question with this language: can we find *clear* rules of address use that *don't complicate* programs, and that keep the type system *sound*?

=== Language syntax

The type system basically follows Hindley-Milner with product and (tagged) sum types. In addition we have address and ref types. Type declarations have the following syntax:

  # product type
  type foo [
    x : int
    y : (ref int)
    z : bar
  ]

  # sum type
  choice bar [
    x : int
    y : point
  ]

Functions have a header and a series of statements in the body:

  fn f a : int b : int -> b : int [
    ...
  ]

Statements have the following format:

  io1, io2, ... <- operation i1, i2, ...

i1, i2 operands on the right hand side are immutable. io1, io2 are in-out operands. They're written to, and may also be read.

Two example programs:

  i) Factorial:

    fn factorial n : int -> result/EAX : int [
      result/EAX <- copy 1
      {
        compare n, 1
        break-if <=
        var tmp/EBX : int
        tmp/EBX <- copy n
        tmp/EBX <- subtract 1
        var tmp2/EAX : int
        tmp2/EAX <- call factorial, tmp/EBX
        result/EAX <- multiply tmp2/EAX, n
      }
      return result/EAX
    ]

  ii) Writing to a global variable:

    var x : char

    fn main [
      call read, 0/stdin, x, 1/size
      result/EAX <- call write, 1/stdout, x, 1/size
      call exit, result/EAX
    ]

One thing to note: variables refer to addresses (not to be confused with the `address` type) just like in Assembly. We'll uniformly use '*' to indicate getting at the value in an address. This will also provide a consistent hint of the addressing mode.

=== Compilation strategy

--- User-defined statements

User-defined functions will be called with the same syntax as primitives. They'll translate to a sequence of push instructions (one per operand, both in and in-out), a call instruction, and a sequence of pop instructions, either to a black hole (in operands) or a location (in-out operands). This follows the standard Unix calling convention:

  push EBP
  copy ESP to EBP
  push arg 1
  push arg 2
  ...
  call
  pop arg n
  ...
  pop arg 1
  copy EBP to ESP
  pop ESP

Implication: each function argument needs to be something push/pop can accept. It can't be an address, so arrays and structs will either have to be passed by value, necessitating copies, or allocated on the heap. We may end up allocating members of structs in separate heap allocations just so we can pass them piecemeal to helper functions. (Mu has explored this trade-off in the past.)

--- Primitive statements

Operands may be:
  in code (literals)
  in registers
  on the stack
  on the global segment

Operands are always scalar. Variables on the stack or global segment are immutable references.

  - Variables on the stack are stored at addresses like *(EBP+n)
  - Global variables are stored at addresses like *disp32, where disp32 is a statically known constant

  #define local(n)  1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8
  #define disp32(N) 0/mod 5/rm32/include-disp32 N/disp32

Since the language will not be orthogonal, compilation proceeds by pattern matching over a statement along with knowledge about the types of its operands, as well as where they're stored (register/stack/global). We now enumerate mappings for various categories of statements, based on the type and location of their operands.

Many statements will end up encoding to the exact same x86 instructions. But the types differ, and they get type-checked differently along the way.

A. x : int <- add y

  Requires y to be scalar (32 bits). Result will always be an int. No pointer arithmetic.

  reg <- add literal    => 81 0/subop 3/mod                                                                                           ...(0)
  reg <- add reg        => 01 3/mod                                                                                                   ...(1)
  reg <- add stack      => 03 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 reg/r32                                        ...(2)
  reg <- add global     => 03 0/mod 5/rm32/include-disp32 global/disp32 reg/r32                                                       ...(3)
  stack <- add literal  => 81 0/subop 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 literal/imm32                          ...(4)
  stack <- add reg      => 01 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 reg/r32                                        ...(5)
  stack <- add stack    => disallowed
  stack <- add global   => disallowed
  global <- add literal => 81 0/subop 0/mod 5/rm32/include-disp32 global/disp32 literal/imm32                                         ...(6)
  global <- add reg     => 01 0/mod 5/rm32/include-disp32 global/disp32 reg/r32                                                       ...(7)
  global <- add stack   => disallowed
  global <- add global  => disallowed

Similarly for sub, and, or, xor and even copy. Replace the opcodes above with corresponding ones from this table:

                            add             sub           and           or            xor         copy/mov
  reg <- op literal         81 0/subop      81 5/subop    81 4/subop    81 1/subop    81 6/subop  c7
  reg <- op reg             01 or 03        29 or 2b      21 or 23      09 or 0b      31 or 33    89 or 8b
  reg <- op stack           03              2b            23            0b            33          8b
  reg <- op global          03              2b            23            0b            33          8b
  stack <- op literal       81 0/subop      81 5/subop    81 4/subop    81 1/subop    81 6/subop  c7
  stack <- op reg           01              29            21            09            31          89
  global <- op literal      81 0/subop      81 5/subop    81 4/subop    81 1/subop    81 6/subop  c7
  global <- op reg          01              29            21            09            31          89

B. x/reg : int <- mul y

  Requires y to be scalar.
  x must be in a register. Multiplies can't write to memory.

  reg <- mul literal    => 69                                                                                                         ...(8)
  reg <- mul reg        => 0f af 3/mod                                                                                                ...(9)
  reg <- mul stack      => 0f af 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 reg/r32                                     ...(10)
  reg <- mul global     => 0f af 0/mod 5/rm32/include-disp32 global/disp32 reg/r32                                                    ...(11)

C. x/EAX/quotient : int, y/EDX/remainder : int <- idiv z     # divide EAX by z; store results in EAX and EDX

  Requires source x and z to both be scalar.
  x must be in EAX and y must be in EDX. Divides can't write anywhere else.

  First clear EDX (we don't support ints larger than 32 bits):
  31/xor 3/mod 2/rm32/EDX 2/r32/EDX

  then:
  EAX, EDX <- idiv literal  => disallowed
  EAX, EDX <- idiv reg      => f7 7/subop 3/mod                                                                                       ...(12)
  EAX, EDX <- idiv stack    => f7 7/subop 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8                                    ...(13)
  EAX, EDX <- idiv global   => f7 7/subop 0/mod 5/rm32/include-disp32 global/disp32 reg/r32                                           ...(14)

D. x : int <- not (weird syntax, but we'll ignore that)

  Requires x to be an int.

  reg <- not                => f7 3/mod                                                                                               ...(15)
  stack <- not              => f7 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8                                            ...(16)
  global <- not             => f7 0/mod 5/rm32/include-disp32 global/disp32 reg/r32                                                   ...(17)

E. x : (address t) <- get o : T, %f

  (Assumes T.f has type t.)

  o can't be on a register since it's a non-primitive (likely larger than a word)
  f is a literal
  x must be in a register (by definition for an address)

  reg1 <- get reg2, literal       => 8d/lea 1/mod reg2/rm32 literal/disp8 reg1/r32                                                    ...(18)
  reg <- get stack, literal       => 8d/lea 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n+literal/disp8 reg/r32                  ...(19)
    (simplifying assumption: stack frames can't be larger than 256 bytes)
  reg <- get global, literal      => 8d/lea 0/mod 5/rm32/include-disp32 global+literal/disp32, reg/r32                                ...(20)

F. x : (offset T) <- index i : int, %size(T)

  This statement is used to translate an array index (denominated in the type of array elements) into an offset (denominated in bytes). It's just a multiply but with a new type for the result so that we can keep the type system sound.

  Since index statements translate to multiplies, 'x' must be a register.
  The %size(T) argument is statically known, so will always be a literal.

  reg1 <- index reg2, literal       => 69/mul 3/mod reg2/rm32 literal/imm32 -> reg1/r32
                                    or 68/mul 3/mod reg2/rm32 literal/imm8 -> reg1/r32                                                ...(21)
  reg1 <- index stack, literal      => 69/mul 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n/disp8 literal/imm32 -> reg1/r32      ...(22)
  reg1 <- index global, literal     => 69/mul 0/mod 5/rm32/include-disp32 global/disp32 literal/imm32 -> reg1/r32                     ...(23)

G. x : (address T) <- advance a : (array T), idx : (offset T)

  reg <- advance a/reg, idx/reg   => 8d/lea 0/mod 4/rm32/SIB a/base idx/index 0/scale reg/r32                                         ...(24)
  reg <- advance stack, literal   => 8d/lea 1/mod 4/rm32/SIB 5/base/EBP 4/index/none 0/scale n+literal/disp8 reg/r32                  ...(25)
  reg <- advance stack, reg2      => 8d/lea 1/mod 4/rm32/SIB 5/base/EBP reg2/index 0/scale n/disp8 reg/r32                            ...(26)
  reg <- advance global, literal  => 8d/lea 0/mod 5/rm32/include-disp32 global+literal/disp32, reg/r32                                ...(27)

=== Example

Putting it all together: code generation for `a[i].y = 4` where a is an array of 2-d points with x, y coordinates.

If a is allocated on the stack, say of type (array point 6):

  offset/EAX : (offset point) <- index i, 8  # (22)
  tmp/EBX : (address point) <- advance a : (array point 6), offset/EAX  # (26)
  tmp2/ECX : (address number) <- get tmp/EBX : (address point), 4/y  # (18)
  *tmp2/ECX <- copy 4  # (5 for copy/mov with 0 disp8)

=== More complex statements

A couple of statement types expand to multiple instructions:
  Function calls. We've already seen these above.
  Bounds checking against array length in 'advance'
  Dereferencing 'ref' types (see type list up top). Requires an alloc id check.

G'. Bounds checking the 'advance' statement begins with a few extra instructions. For example:

  x/EAX : (address T) <- advance a : (array T), literal

Suppose array 'a' lies on the stack starting at EBP+4. Its length will be at EBP+4, and the actual contents of the array will start from EBP+8.

 compare *(EBP+4), literal
 jump-if-greater panic          # rudimentary error handling

Now we're ready to perform the actual 'lea':

  lea EBP+8 + literal, reg      # line 25 above

H. Dereferencing a 'ref' needs to be its own statement, yielding an address. This statement has two valid forms:

  reg : (address T) <- deref stack : (ref T)
  reg : (address T) <- deref global : (ref T)

Since refs need 8 bytes they can't be in a register. And of course the output is an address so it must be in a register.

Compiling 'deref' will take a few instructions. Consider the following example where 's' is on the stack, say starting at EBP+4:

  EDX : (address T) <- deref s : (ref T)

The alloc id of 's' is at *(EBP+4) and the actual address is at *(EBP+8). The above statement will compile down to the following:

  EDX/s <- copy *(EBP+8)         # the address stored in s
  EDX/alloc-id <- copy *EDX      # alloc id of payload *s
  compare EDX, *(EBP+4)          # compare with alloc id of pointer
  jump-unless-equal panic        # rudimentary error handling
  # compute *(EBP+8) + 4
  EDX <- copy *(EBP+8)           # recompute the address in s because we can't save the value anywhere)
  EDX <- add EDX, 4              # skip alloc id this time

Subtleties:
  a) if the alloc id of the payload is 0, then the payload is reclaimed
  b) looking up the payload's alloc id *could* cause a segfault. What to do?

=== More speculative ideas

Initialize data segment with special extensible syntax for literals. All literals except numbers and strings start with %. Global variable declarations would now look like:

  var s : (array character) = "abc"  # exception to the '%' convention
  var p : point = %point(3, 4)

=== Credits

Forth
C
Rust
Lisp
qhasm
an>to the new editor # right is exclusive def new-editor s:text, screen:&:screen, left:num, right:num -> result:&:editor, screen:&:screen [ local-scope load-ingredients # no clipping of bounds right <- subtract right, 1 result <- new editor:type # initialize screen-related fields *result <- put *result, left:offset, left *result <- put *result, right:offset, right # initialize cursor coordinates *result <- put *result, cursor-row:offset, 1/top *result <- put *result, cursor-column:offset, left # initialize empty contents init:&:duplex-list:char <- push 167/§, 0/tail *result <- put *result, data:offset, init *result <- put *result, top-of-screen:offset, init *result <- put *result, before-cursor:offset, init result <- insert-text result, s # initial render to screen, just for some old tests _, _, screen, result <- render screen, result <editor-initialization> ] def insert-text editor:&:editor, text:text -> editor:&:editor [ local-scope load-ingredients # early exit if text is empty return-unless text, editor/same-as-ingredient:0 len:num <- length *text return-unless len, editor/same-as-ingredient:0 idx:num <- copy 0 # now we can start appending the rest, character by character curr:&:duplex-list:char <- get *editor, data:offset { done?:bool <- greater-or-equal idx, len break-if done? c:char <- index *text, idx insert c, curr # next iter curr <- next curr idx <- add idx, 1 loop } return editor/same-as-ingredient:0 ] scenario editor-initializes-without-data [ local-scope assume-screen 5/width, 3/height run [ e:&:editor <- new-editor 0/data, screen, 2/left, 5/right 2:editor/raw <- copy *e ] memory-should-contain [ # 2 (data) <- just the § sentinel # 3 (top of screen) <- the § sentinel 4 <- 0 # bottom-of-screen; null since text fits on screen # 5 (before cursor) <- the § sentinel 6 <- 2 # left 7 <- 4 # right (inclusive) 8 <- 1 # bottom 9 <- 1 # cursor row 10 <- 2 # cursor column ] screen-should-contain [ . . . . . . ] ] # Assumes cursor should be at coordinates (cursor-row, cursor-column) and # updates before-cursor to match. Might also move coordinates if they're # outside text. def render screen:&:screen, editor:&:editor -> last-row:num, last-column:num, screen:&:screen, editor:&:editor [ local-scope load-ingredients return-unless editor, 1/top, 0/left, screen/same-as-ingredient:0, editor/same-as-ingredient:1 left:num <- get *editor, left:offset screen-height:num <- screen-height screen right:num <- get *editor, right:offset # traversing editor curr:&:duplex-list:char <- get *editor, top-of-screen:offset prev:&:duplex-list:char <- copy curr # just in case curr becomes null and we can't compute prev curr <- next curr # traversing screen +render-loop-initialization color:num <- copy 7/white row:num <- copy 1/top column:num <- copy left cursor-row:num <- get *editor, cursor-row:offset cursor-column:num <- get *editor, cursor-column:offset before-cursor:&:duplex-list:char <- get *editor, before-cursor:offset screen <- move-cursor screen, row, column { +next-character break-unless curr off-screen?:bool <- greater-or-equal row, screen-height break-if off-screen? # update editor.before-cursor # Doing so at the start of each iteration ensures it stays one step behind # the current character. { at-cursor-row?:bool <- equal row, cursor-row break-unless at-cursor-row? at-cursor?:bool <- equal column, cursor-column break-unless at-cursor? before-cursor <- copy prev } c:char <- get *curr, value:offset <character-c-received> { # newline? move to left rather than 0 newline?:bool <- equal c, 10/newline break-unless newline? # adjust cursor if necessary { at-cursor-row?:bool <- equal row, cursor-row break-unless at-cursor-row? left-of-cursor?:bool <- lesser-than column, cursor-column break-unless left-of-cursor? cursor-column <- copy column before-cursor <- prev curr } # clear rest of line in this window clear-line-until screen, right # skip to next line row <- add row, 1 column <- copy left screen <- move-cursor screen, row, column curr <- next curr prev <- next prev loop +next-character:label } { # at right? wrap. even if there's only one more letter left; we need # room for clicking on the cursor after it. at-right?:bool <- equal column, right break-unless at-right? # print wrap icon wrap-icon:char <- copy 8617/loop-back-to-left print screen, wrap-icon, 245/grey column <- copy left row <- add row, 1 screen <- move-cursor screen, row, column # don't increment curr loop +next-character:label } print screen, c, color curr <- next curr prev <- next prev column <- add column, 1 loop } # save first character off-screen *editor <- put *editor, bottom-of-screen:offset, curr # is cursor to the right of the last line? move to end { at-cursor-row?:bool <- equal row, cursor-row cursor-outside-line?:bool <- lesser-or-equal column, cursor-column before-cursor-on-same-line?:bool <- and at-cursor-row?, cursor-outside-line? above-cursor-row?:bool <- lesser-than row, cursor-row before-cursor?:bool <- or before-cursor-on-same-line?, above-cursor-row? break-unless before-cursor? cursor-row <- copy row cursor-column <- copy column before-cursor <- copy prev } *editor <- put *editor, bottom:offset, row *editor <- put *editor, cursor-row:offset, cursor-row *editor <- put *editor, cursor-column:offset, cursor-column *editor <- put *editor, before-cursor:offset, before-cursor return row, column, screen/same-as-ingredient:0, editor/same-as-ingredient:1 ] def clear-screen-from screen:&:screen, row:num, column:num, left:num, right:num -> screen:&:screen [ local-scope load-ingredients # if it's the real screen, use the optimized primitive { break-if screen clear-display-from row, column, left, right return screen/same-as-ingredient:0 } # if not, go the slower route screen <- move-cursor screen, row, column clear-line-until screen, right clear-rest-of-screen screen, row, left, right return screen/same-as-ingredient:0 ] def clear-rest-of-screen screen:&:screen, row:num, left:num, right:num -> screen:&:screen [ local-scope load-ingredients row <- add row, 1 screen <- move-cursor screen, row, left screen-height:num <- screen-height screen { at-bottom-of-screen?:bool <- greater-or-equal row, screen-height break-if at-bottom-of-screen? screen <- move-cursor screen, row, left clear-line-until screen, right row <- add row, 1 loop } ] scenario editor-initially-prints-multiple-lines [ local-scope assume-screen 5/width, 5/height run [ s:text <- new [abc def] new-editor s, screen, 0/left, 5/right ] screen-should-contain [ . . .abc . .def . . . ] ] scenario editor-initially-handles-offsets [ local-scope assume-screen 5/width, 5/height run [ s:text <- new [abc] new-editor s, screen, 1/left, 5/right ] screen-should-contain [ . . . abc . . . ] ] scenario editor-initially-prints-multiple-lines-at-offset [ local-scope assume-screen 5/width, 5/height run [ s:text <- new [abc def] new-editor s, screen, 1/left, 5/right ] screen-should-contain [ . . . abc . . def . . . ] ] scenario editor-initially-wraps-long-lines [ local-scope assume-screen 5/width, 5/height run [ s:text <- new [abc def] new-editor s, screen, 0/left, 5/right ] screen-should-contain [ . . .abc . .def . . . ] screen-should-contain-in-color 245/grey [ . . . . . . . . ] ] scenario editor-initially-wraps-barely-long-lines [ local-scope assume-screen 5/width, 5/height run [ s:text <- new [abcde] new-editor s, screen, 0/left, 5/right ] # still wrap, even though the line would fit. We need room to click on the # end of the line screen-should-contain [ . . .abcd. .e . . . ] screen-should-contain-in-color 245/grey [ . . . . . . . . ] ] scenario editor-initializes-empty-text [ local-scope assume-screen 5/width, 5/height run [ e:&:editor <- new-editor [], screen, 0/left, 5/right 3:num/raw <- get *e, cursor-row:offset 4:num/raw <- get *e, cursor-column:offset ] screen-should-contain [ . . . . . . ] memory-should-contain [ 3 <- 1 # cursor row 4 <- 0 # cursor column ] ] # just a little color for mu code scenario render-colors-comments [ local-scope assume-screen 5/width, 5/height run [ s:text <- new [abc # de f] new-editor s, screen, 0/left, 5/right ] screen-should-contain [ . . .abc . .# de . .f . . . ] screen-should-contain-in-color 12/lightblue, [ . . . . .# de . . . . . ] screen-should-contain-in-color 7/white, [ . . .abc . . . .f . . . ] ] after <character-c-received> [ color <- get-color color, c ] # so far the previous color is all the information we need; that may change def get-color color:num, c:char -> color:num [ local-scope load-ingredients color-is-white?:bool <- equal color, 7/white # if color is white and next character is '#', switch color to blue { break-unless color-is-white? starting-comment?:bool <- equal c, 35/# break-unless starting-comment? trace 90, [app], [switch color back to blue] color <- copy 12/lightblue jump +exit:label } # if color is blue and next character is newline, switch color to white { color-is-blue?:bool <- equal color, 12/lightblue break-unless color-is-blue? ending-comment?:bool <- equal c, 10/newline break-unless ending-comment? trace 90, [app], [switch color back to white] color <- copy 7/white jump +exit:label } # if color is white (no comments) and next character is '<', switch color to red { break-unless color-is-white? starting-assignment?:bool <- equal c, 60/< break-unless starting-assignment? color <- copy 1/red jump +exit:label } # if color is red and next character is space, switch color to white { color-is-red?:bool <- equal color, 1/red break-unless color-is-red? ending-assignment?:bool <- equal c, 32/space break-unless ending-assignment? color <- copy 7/white jump +exit:label } # otherwise no change +exit return color ] scenario render-colors-assignment [ local-scope assume-screen 8/width, 5/height run [ s:text <- new [abc d <- e f] new-editor s, screen, 0/left, 8/right ] screen-should-contain [ . . .abc . .d <- e . .f . . . ] screen-should-contain-in-color 1/red, [ . . . . . <- . . . . . ] ]