summaryrefslogtreecommitdiff
path: root/source/main.elna
blob: dae045b5565e1e2d5c3144407fdd7fcd03fff2d1 (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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
(* This Source Code Form is subject to the terms of the Mozilla Public License,
  v. 2.0. If a copy of the MPL was not distributed with this file, You can
  obtain one at https://mozilla.org/MPL/2.0/. *)
program;

import cstdio, cctype, common, command_line_interface, lexer;

type
  SourceFile* = record
    buffer: [1024]Char;
    handle: ^FILE;
    size: Word;
    index: Word
  end;
  StringBuffer* = record
    data: Pointer;
    size: Word;
    capacity: Word
  end;
  SourceCode = record
    position: TextLocation;

    input: Pointer;
    empty: proc(Pointer) -> Bool;
    advance: proc(Pointer);
    head: proc(Pointer) -> Char
  end;
  Token* = record
    kind: LexerKind;
    value: union
      int_value: Int;
      string: String;
      boolean_value: Bool;
      char_value: Char
    end
  end;
  Tokenizer* = record
    length: Word;
    data: ^Token
  end;

(*
  Standard procedures.
*)
proc reallocarray(ptr: Pointer, n: Word, size: Word) -> Pointer;
  return realloc(ptr, n * size)
end;

proc substring(string: String, start: Word, count: Word) -> String;
  return String(string.ptr + start, count)
end;

proc open_substring(string: String, start: Word) -> String;
  return substring(string, start, string.length - start)
end;

proc string_dup(origin: String) -> String;
var
  copy: ^Char;
begin
  copy := cast(malloc(origin.length): ^Char);
  strncpy(copy, origin.ptr, origin.length);

  return String(copy, origin.length)
end;

proc string_buffer_new() -> StringBuffer;
var
  result: StringBuffer;
begin
  result.capacity := 64u;
  result.data := malloc(result.capacity);
  result.size := 0u;

  return result
end;

proc string_buffer_push(buffer: ^StringBuffer, char: Char);
begin
  if buffer^.size >= buffer^.capacity then
    buffer^.capacity := buffer^.capacity + 1024u;
    buffer^.data := realloc(buffer^.data, buffer^.capacity)
  end;
  cast(buffer^.data + buffer^.size: ^Char)^ := cast(char: Char);
  buffer^.size := buffer^.size + 1u
end;

proc string_buffer_pop(buffer: ^StringBuffer, count: Word);
begin
  buffer^.size := buffer^.size - count
end;

proc string_buffer_clear(buffer: ^StringBuffer) -> String;
var
  result: String;
begin
  result := String(cast(buffer^.data: ^Char), buffer^.size);
  buffer^.size := 0u;
  return result
end;

(*
  Source code stream procedures.
*)

proc read_source(filename: ^Char) -> ^SourceFile;
var
  result: ^SourceFile;
  file_handle: ^FILE;
begin
  file_handle := fopen(filename, "rb\0".ptr);

  if file_handle <> nil then
    result := cast(malloc(#size(SourceFile)): ^SourceFile);
    result^.handle := file_handle;
    result^.size := 0u;
    result^.index := 1u
  end;
  return result
end;

proc source_file_empty(source_input: Pointer) -> Bool;
var
  source_file: ^SourceFile;
begin
  source_file := cast(source_input: ^SourceFile);

  if source_file^.index > source_file^.size then
    source_file^.size := fread(cast(@source_file^.buffer: Pointer), 1u, 1024u, source_file^.handle);
    source_file^.index := 1u
  end;

  return source_file^.size = 0u
end;

proc source_file_head(source_input: Pointer) -> Char;
var
  source_file: ^SourceFile;
begin
  source_file := cast(source_input: ^SourceFile);

  return source_file^.buffer[source_file^.index]
end;

proc source_file_advance(source_input: Pointer);
var
  source_file: ^SourceFile;
begin
  source_file := cast(source_input: ^SourceFile);

  source_file^.index := source_file^.index + 1u
end;

proc source_code_empty(source_code: ^SourceCode) -> Bool;
  return source_code^.empty(source_code^.input)
end;

proc source_code_head(source_code: SourceCode) -> Char;
  return source_code.head(source_code.input)
end;

proc source_code_advance(source_code: ^SourceCode);
begin
  source_code^.advance(source_code^.input);
  source_code^.position.column := source_code^.position.column
end;

proc source_code_break(source_code: ^SourceCode);
begin
  source_code^.position.line := source_code^.position.line + 1u;
  source_code^.position.column := 0u
end;

proc source_code_expect(source_code: ^SourceCode, expected: Char) -> Bool;
  return ~source_code_empty(source_code) & source_code_head(source_code^) = expected
end;

(*
  Token procedures.
*)

proc lexer_escape(escape: Char, result: ^Char) -> Bool;
var
  successful: Bool;
begin
  case escape of
    'n':
      result^ := '\n';
      successful := true
    | 'a':
      result^ := '\a';
      successful := true
    | 'b':
      result^ := '\b';
      successful := true
    | 't':
      result^ := '\t';
      successful := true
    | 'f':
      result^ := '\f';
      successful := true
    | 'r':
      result^ := '\r';
      successful := true
    | 'v':
      result^ := '\v';
      successful := true
    | '\\':
      result^ := '\\';
      successful := true
    | '\'':
      result^ := '\'';
      successful := true
    | '"':
      result^ := '"';
      successful := true
    | '?':
      result^ := '\?';
      successful := true
    | '0':
      result^ := '\0';
      successful := true
    else
      successful := false
  end;
  return successful
end;

(* Skip spaces. *)
proc lexer_spaces(source_code: ^SourceCode);
var
  current: Char;
begin
  while ~source_code_empty(source_code) & isspace(cast(source_code_head(source_code^): Int)) <> 0 do
    current := source_code_head(source_code^);

    if current = '\n' then
      source_code_break(source_code)
    end;
    source_code_advance(source_code)
  end
end;

(* Checker whether the character is allowed in an identificator. *)
proc lexer_is_ident(char: Char) -> Bool;
  return isalnum(cast(char: Int)) <> 0 or char = '_'
end;

proc lexer_identifier(source_code: ^SourceCode, token_content: ^StringBuffer);
var
  content_length: Word;
begin
  while ~source_code_empty(source_code) & lexer_is_ident(source_code_head(source_code^)) do
    string_buffer_push(token_content, source_code_head(source_code^));
    source_code_advance(source_code)
  end
end;

proc lexer_comment(source_code: ^SourceCode, token_content: ^StringBuffer) -> Bool;
var
  trailing: Word;
begin
  trailing := 0u;

  while ~source_code_empty(source_code) & trailing < 2u do
    if source_code_head(source_code^) = '*' then
      string_buffer_push(token_content, '*');
      trailing := 1u
    elsif source_code_head(source_code^) = ')' & trailing = 1u then
      string_buffer_pop(token_content, 1u);
      trailing := 2u
    else
      string_buffer_push(token_content, source_code_head(source_code^));
      trailing := 0u
    end;
    source_code_advance(source_code)
  end;

  return trailing = 2u
end;

proc lexer_character(source_code: ^SourceCode, token_content: ^Char) -> Bool;
var
  successful: Bool;
begin
  successful := ~source_code_empty(source_code);

  if successful then
    if source_code_head(source_code^) = '\\' then
      source_code_advance(source_code);

      successful := ~source_code_empty(source_code) & lexer_escape(source_code_head(source_code^), token_content)
    else
      token_content^ := source_code_head(source_code^);
      successful := true
    end
  end;
  if successful then
    source_code_advance(source_code)
  end;
  return successful
end;

proc lexer_string(source_code: ^SourceCode, token_content: ^StringBuffer) -> Bool;
var
  token_end, constructed_string: ^Char;
  token_length: Word;
  is_valid: Bool := true;
  next_char: Char;
begin
  while is_valid & ~source_code_empty(source_code) & source_code_head(source_code^) <> '"' do
    is_valid := lexer_character(source_code, @next_char);

    if is_valid then
      string_buffer_push(token_content, next_char)
    end
  end;

  if is_valid & source_code_expect(source_code, '"') then
    source_code_advance(source_code)
  else
    is_valid := false
  end;
  return is_valid
end;

proc lexer_number(source_code: ^SourceCode, token_content: ^Int);
begin
  token_content^ := 0;

  while ~source_code_empty(source_code) & isdigit(cast(source_code_head(source_code^): Int)) <> 0 do
    token_content^ := token_content^ * 10 + (cast(source_code_head(source_code^): Int) - cast('0': Int));

    source_code_advance(source_code)
  end
end;

(* Categorize an identifier. *)
proc lexer_categorize(token_content: String) -> Token;
var
  current_token: Token;
begin
  if token_content = "if" then
    current_token.kind := LexerKind._if
  elsif token_content = "then" then
    current_token.kind := LexerKind._then
  elsif token_content = "else" then
    current_token.kind := LexerKind._else
  elsif token_content = "elsif" then
    current_token.kind := LexerKind._elsif
  elsif token_content = "while" then
    current_token.kind := LexerKind._while
  elsif token_content = "do" then
    current_token.kind := LexerKind._do
  elsif token_content = "proc" then
    current_token.kind := LexerKind._proc
  elsif token_content = "begin" then
    current_token.kind := LexerKind._begin
  elsif token_content = "end" then
    current_token.kind := LexerKind._end
  elsif token_content = "extern" then
    current_token.kind := LexerKind._extern
  elsif token_content = "const" then
    current_token.kind := LexerKind._const
  elsif token_content = "var" then
    current_token.kind := LexerKind._var
  elsif token_content = "case" then
    current_token.kind := LexerKind._case
  elsif token_content = "of" then
    current_token.kind := LexerKind._of
  elsif token_content = "type" then
    current_token.kind := LexerKind._type
  elsif token_content = "record" then
    current_token.kind := LexerKind._record
  elsif token_content = "union" then
    current_token.kind := LexerKind._union
  elsif token_content = "true" then
    current_token.kind := LexerKind.boolean;
    current_token.value.boolean_value := true
  elsif token_content = "false" then
    current_token.kind := LexerKind.boolean;
    current_token.value.boolean_value := false
  elsif token_content = "nil" then
    current_token.kind := LexerKind.null
  elsif token_content = "or" then
    current_token.kind := LexerKind._or
  elsif token_content = "return" then
    current_token.kind := LexerKind._return
  elsif token_content = "cast" then
    current_token.kind := LexerKind._cast
  elsif token_content = "defer" then
    current_token.kind := LexerKind._defer
  elsif token_content = "program" then
    current_token.kind := LexerKind._program
  elsif token_content = "module" then
    current_token.kind := LexerKind._module
  elsif token_content = "import" then
    current_token.kind := LexerKind._import
  else
    current_token.kind := LexerKind.identifier;
    current_token.value.string := string_dup(token_content)
  end;

  return current_token
end;

proc lexer_add_token(lexer: ^Tokenizer, token: Token);
var
  new_length: Word;
begin
  new_length := lexer^.length + 1u;
  lexer^.data := cast(reallocarray(cast(lexer^.data: Pointer), new_length, #size(Token)): ^Token);
  (lexer^.data + lexer^.length)^ := token;
  lexer^.length := new_length
end;

(* Read the next token from the input. *)
proc lexer_next(source_code: SourceCode, token_buffer: ^StringBuffer) -> Token;
var
  current_token: Token;
  first_char: Char;
begin
  current_token.kind := LexerKind.unknown;

  first_char := source_code_head(source_code);

  if isalpha(cast(first_char: Int)) <> 0 or first_char = '_' then
    lexer_identifier(@source_code, token_buffer);
    current_token := lexer_categorize(string_buffer_clear(token_buffer))
  elsif first_char = '#' then
    source_code_advance(@source_code);
    lexer_identifier(@source_code, token_buffer);

    current_token.kind := LexerKind.trait;
    current_token.value.string := string_dup(string_buffer_clear(token_buffer))
  elsif isdigit(cast(first_char: Int)) <> 0 then
    lexer_number(@source_code, @current_token.value.int_value);

    if source_code_expect(@source_code, 'u') then
      current_token.kind := LexerKind.word;
        source_code_advance(@source_code)
    else
      current_token.kind := LexerKind.integer
    end
  elsif first_char = '(' then
    source_code_advance(@source_code);

    if source_code_empty(@source_code) then
      current_token.kind := LexerKind.left_paren
    elsif source_code_head(source_code) = '*' then
      source_code_advance(@source_code);

      if lexer_comment(@source_code, token_buffer) then
        current_token.value.string := string_dup(string_buffer_clear(token_buffer));
        current_token.kind := LexerKind.comment
      else
        current_token.kind := LexerKind.unknown
      end
    else
      current_token.kind := LexerKind.left_paren
    end
  elsif first_char = ')' then
    current_token.kind := LexerKind.right_paren;
    source_code_advance(@source_code)
  elsif first_char = '\'' then
    source_code_advance(@source_code);

    if lexer_character(@source_code, @current_token.value.char_value) & source_code_expect(@source_code, '\'') then
      current_token.kind := LexerKind.character;
      source_code_advance(@source_code)
    else
      current_token.kind := LexerKind.unknown
    end
  elsif first_char = '"' then
    source_code_advance(@source_code);

    if lexer_string(@source_code, token_buffer) then
      current_token.kind := LexerKind.string;
      current_token.value.string := string_dup(string_buffer_clear(token_buffer))
    else
      current_token.kind := LexerKind.unknown
    end
  elsif first_char = '[' then
    current_token.kind := LexerKind.left_square;
    source_code_advance(@source_code)
  elsif first_char = ']' then
    current_token.kind := LexerKind.right_square;
    source_code_advance(@source_code)
  elsif first_char = '>' then
    source_code_advance(@source_code);

    if source_code_empty(@source_code) then
      current_token.kind := LexerKind.greater_than
    elsif source_code_head(source_code) = '=' then
      current_token.kind := LexerKind.greater_equal;
      source_code_advance(@source_code)
    elsif source_code_head(source_code) = '>' then
      current_token.kind := LexerKind.shift_right;
      source_code_advance(@source_code)
    else
      current_token.kind := LexerKind.greater_than
    end
  elsif first_char = '<' then
    source_code_advance(@source_code);

    if source_code_empty(@source_code) then
      current_token.kind := LexerKind.less_than
    elsif source_code_head(source_code) = '=' then
      current_token.kind := LexerKind.less_equal;
      source_code_advance(@source_code)
    elsif source_code_head(source_code) = '<' then
      current_token.kind := LexerKind.shift_left;
      source_code_advance(@source_code)
    elsif source_code_head(source_code) = '>' then
      current_token.kind := LexerKind.not_equal;
      source_code_advance(@source_code)
    else
      current_token.kind := LexerKind.less_than
    end
  elsif first_char = '=' then
    current_token.kind := LexerKind.equal;
    source_code_advance(@source_code)
  elsif first_char = ';' then
    current_token.kind := LexerKind.semicolon;
    source_code_advance(@source_code)
  elsif first_char = '.' then
    current_token.kind := LexerKind.dot;
    source_code_advance(@source_code)
  elsif first_char = ',' then
    current_token.kind := LexerKind.comma;
    source_code_advance(@source_code)
  elsif first_char = '+' then
    current_token.kind := LexerKind.plus;
    source_code_advance(@source_code)
  elsif first_char = '-' then
    source_code_advance(@source_code);

    if source_code_empty(@source_code) then
      current_token.kind := LexerKind.minus
    elsif source_code_head(source_code) = '>' then
      current_token.kind := LexerKind.arrow;
      source_code_advance(@source_code)
    else
      current_token.kind := LexerKind.minus
    end
  elsif first_char = '*' then
    current_token.kind := LexerKind.multiplication;
    source_code_advance(@source_code)
  elsif first_char = '/' then
    current_token.kind := LexerKind.division;
    source_code_advance(@source_code)
  elsif first_char = '%' then
    current_token.kind := LexerKind.remainder;
    source_code_advance(@source_code)
  elsif first_char = ':' then
    source_code_advance(@source_code);

    if source_code_empty(@source_code) then
      current_token.kind := LexerKind.colon
    elsif source_code_head(source_code) = '=' then
      current_token.kind := LexerKind.assignment;
      source_code_advance(@source_code)
    else
      current_token.kind := LexerKind.colon
    end
  elsif first_char = '^' then
    current_token.kind := LexerKind.hat;
    source_code_advance(@source_code)
  elsif first_char = '@' then
    current_token.kind := LexerKind.at;
    source_code_advance(@source_code)
  elsif first_char = '!' then
    current_token.kind := LexerKind.exclamation;
    source_code_advance(@source_code)
  elsif first_char = '&' then
    current_token.kind := LexerKind.and;
    source_code_advance(@source_code)
  elsif first_char = '~' then
    current_token.kind := LexerKind.not;
    source_code_advance(@source_code)
  elsif first_char = '|' then
    current_token.kind := LexerKind.pipe;
    source_code_advance(@source_code)
  else
    current_token.kind := LexerKind.unknown;
    source_code_advance(@source_code)
  end;

  return current_token
end;

(* Split the source text into tokens. *)
proc lexer_text(source_code: SourceCode) -> Tokenizer;
var
  current_token: Token;
  token_buffer: StringBuffer;
  lexer: Tokenizer;
begin
  lexer := Tokenizer(0u, nil);
  token_buffer := string_buffer_new();

  lexer_spaces(@source_code);

  while ~source_code_empty(@source_code) do
    current_token := lexer_next(source_code, @token_buffer);

    if current_token.kind <> LexerKind.unknown then
      lexer_add_token(@lexer, current_token);
        lexer_spaces(@source_code)
    else
      write_s("Lexical analysis error on \"");
      write_c(source_code_head(source_code));
      write_s("\".\n")
    end
  end;

  return lexer
end;

(*
  Parser.
*)

proc parse(tokens: ^Token, tokens_size: Word);
var
  current_token: ^Token;
  i: Word := 0u;
begin
  while i < tokens_size do
    current_token := tokens + i;

    case current_token^.kind of
      LexerKind._if:
        write_s("IF")
      | LexerKind._then:
        write_s("THEN")
      | LexerKind._else:
        write_s("ELSE")
      | LexerKind._elsif:
        write_s("ELSIF")
      | LexerKind._while:
        write_s("WHILE")
      | LexerKind._do:
        write_s("DO")
      | LexerKind._proc:
        write_s("PROC")
      | LexerKind._begin:
        write_s("BEGIN")
      | LexerKind._end:
        write_s("END")
      | LexerKind._extern:
        write_s("EXTERN")
      | LexerKind._const:
        write_s("CONST")
      | LexerKind._var:
        write_s("VAR")
     | LexerKind._case:
        write_s("CASE")
      | LexerKind._of:
        write_s("OF")
      | LexerKind._type:
        write_s("TYPE")
      | LexerKind._record:
        write_s("RECORD")
      | LexerKind._union:
        write_s("UNION")
      | LexerKind.pipe:
        write_s("|")
      | LexerKind.to:
        write_s("TO")
      | LexerKind.boolean:
        write_s("BOOLEAN<");
        write_b(current_token^.value.boolean_value);
        write_c('>')
      | LexerKind.null:
        write_s("NIL")
      | LexerKind.and:
        write_s("&")
      | LexerKind._or:
        write_s("OR")
      | LexerKind.not:
        write_s("~")
      | LexerKind._return:
        write_s("RETURN")
      | LexerKind._cast:
        write_s("CAST")
      | LexerKind.shift_left:
        write_s("<<")
      | LexerKind.shift_right:
        write_s(">>")
      | LexerKind.identifier:
        write_c('<');
        write_s(current_token^.value.string);
        write_c('>')
      | LexerKind.trait:
        write_c('#');
        write_s(current_token^.value.string)
      | LexerKind.left_paren:
        write_s("(")
      | LexerKind.right_paren:
        write_s(")")
      | LexerKind.left_square:
        write_s("[")
      | LexerKind.right_square:
        write_s("]")
      | LexerKind.greater_equal:
        write_s(">=")
      | LexerKind.less_equal:
        write_s("<=")
      | LexerKind.greater_than:
        write_s(">")
      | LexerKind.less_than:
        write_s("<")
      | LexerKind.equal:
        write_s("=")
      | LexerKind.not_equal:
        write_s("<>")
      | LexerKind.semicolon:
        write_c(';')
      | LexerKind.dot:
        write_c('.')
      | LexerKind.comma:
        write_c(',')
      | LexerKind.plus:
        write_c('+')
      | LexerKind.minus:
        write_c('-')
      | LexerKind.multiplication:
        write_c('*')
      | LexerKind.division:
        write_c('/')
      | LexerKind.remainder:
        write_c('%')
      | LexerKind.assignment:
        write_s(":=")
      | LexerKind.colon:
        write_c(':')
      | LexerKind.hat:
        write_c('^')
      | LexerKind.at:
        write_c('@')
      | LexerKind.comment:
        write_s("(* COMMENT *)")
      | LexerKind.integer:
        write_c('<');
        write_i(current_token^.value.int_value);
        write_c('>')
      | LexerKind.word:
        write_c('<');
        write_i(current_token^.value.int_value);
        write_s("u>")
      | LexerKind.character:
        write_c('<');
        write_i(cast(current_token^.value.char_value: Int));
        write_s("c>")
      | LexerKind.string:
        write_s("\"...\"")
      | LexerKind._defer:
        write_s("DEFER")
      | LexerKind.exclamation:
        write_c('!')
      | LexerKind.arrow:
        write_s("->")
      | LexerKind._program:
        write_s("PROGRAM")
      | LexerKind._module:
      write_s("MODULE")
      | LexerKind._import:
      write_s("IMPORT")
      else
        write_s("UNKNOWN<");
        write_i(cast(current_token^.kind: Int));
        write_c('>')
    end;
    write_c(' ');

    i := i + 1u
  end;
  write_c('\n')
end;

(*
  Compilation entry.
*)

proc compile_in_stages(command_line: ^CommandLine, source_code: SourceCode) -> Int;
var
  return_code: Int := 0;
  lexer: Tokenizer;
begin
  if command_line^.lex or command_line^.parse then
    lexer := lexer_text(source_code)
  end;
  if command_line^.parse then
    parse(lexer.data, lexer.length)
  end;

  return return_code
end;

proc process(argc: Int, argv: ^^Char) -> Int;
var
  tokens: ^Token;
  tokens_size: Word;
  source_code: SourceCode;
  command_line: ^CommandLine;
  return_code: Int := 0;
  source_file: ^SourceFile;
begin
  command_line := parse_command_line(argc, argv);
  if command_line = nil then
    return_code := 2
  end;

  if return_code = 0 then
    source_file := read_source(command_line^.input);

    if source_file = nil then
      perror(command_line^.input);
      return_code := 3
    end
  end;

  if return_code = 0 then
    defer
      fclose(source_file^.handle)
    end;

    source_code.position := TextLocation(1u, 1u);
    source_code.input := cast(source_file: Pointer);
    source_code.empty := source_file_empty;
    source_code.head := source_file_head;
    source_code.advance := source_file_advance;

    return_code := compile_in_stages(command_line, source_code)
  end;
  return return_code
end;

  return process(count, parameters)
end.