|
11 | 11 |
|
12 | 12 | @(define codeblock-include (make-codeblock-include #'h)) |
13 | 13 |
|
14 | | -@(ev '(require rackunit a86)) |
15 | | -@(ev `(current-directory ,(path->string (build-path langs "iniquity")))) |
16 | | -@(void (ev '(with-output-to-string (thunk (system "make runtime.o"))))) |
17 | | -@(for-each (λ (f) (ev `(require (file ,f)))) |
18 | | - '("interp.rkt" "compile.rkt" "ast.rkt" "parse.rkt" "types.rkt")) |
| 14 | +@(ev '(require rackunit a86 iniquity)) |
19 | 15 |
|
20 | 16 | @(define (shellbox . s) |
21 | 17 | (parameterize ([current-directory (build-path langs "iniquity")]) |
@@ -72,19 +68,19 @@ We will extend the syntax by introducing a new syntactic category of |
72 | 68 | followed by an expression: |
73 | 69 |
|
74 | 70 | @racketblock[ |
75 | | -(define (_f0 _x00 ...) _e0) |
76 | | -(define (_f1 _x10 ...) _e1) |
| 71 | +(define (_f₀ _x₀₀ ...) _e₀) |
| 72 | +(define (_f₁ _x₁₀ ...) _e₁) |
77 | 73 | ... |
78 | | -e |
| 74 | +_e |
79 | 75 | ] |
80 | 76 |
|
81 | 77 | And the syntax of expressions will be extended to include function calls: |
82 | 78 |
|
83 | 79 | @racketblock[ |
84 | | -(_fi _e0 ...) |
| 80 | +(_fᵢ _e₀ ...) |
85 | 81 | ] |
86 | 82 |
|
87 | | -where @racket[_fi] is one of the function names defined in the program. |
| 83 | +where @racket[_fᵢ] is one of the function names defined in the program. |
88 | 84 |
|
89 | 85 | Note that functions can have any number of parameters and, |
90 | 86 | symmetrically, calls can have any number of arguments. A program |
@@ -652,37 +648,32 @@ single list: |
652 | 648 | Here's an example of the code this compiler emits: |
653 | 649 |
|
654 | 650 | @ex[ |
655 | | -(asm-display |
656 | | - (compile |
657 | | - (parse '(define (double x) (+ x x)) '(double 5)))) |
| 651 | +(compile (parse '(define (double x) (+ x x)) |
| 652 | + '(double 5))) |
658 | 653 | ] |
659 | 654 |
|
660 | 655 | And we can confirm running the code produces results consistent with |
661 | 656 | the interpreter: |
662 | 657 |
|
663 | 658 | @ex[ |
664 | | -(current-objs '("runtime.o")) |
665 | | -(define (run . p) |
666 | | - (bits->value (asm-interp (compile (apply parse p))))) |
667 | | - |
668 | | -(run '(define (double x) (+ x x)) |
669 | | - '(double 5)) |
670 | | - |
671 | | -(run '(define (tri x) |
672 | | - (if (zero? x) |
673 | | - 0 |
674 | | - (+ x (tri (sub1 x))))) |
675 | | - '(tri 9)) |
676 | | - |
677 | | -(run '(define (even? x) |
678 | | - (if (zero? x) |
679 | | - #t |
680 | | - (odd? (sub1 x)))) |
681 | | - '(define (odd? x) |
682 | | - (if (zero? x) |
683 | | - #f |
684 | | - (even? (sub1 x)))) |
685 | | - '(even? 101)) |
| 659 | +(exec (parse '(define (double x) (+ x x)) |
| 660 | + '(double 5))) |
| 661 | + |
| 662 | +(exec (parse '(define (tri x) |
| 663 | + (if (zero? x) |
| 664 | + 0 |
| 665 | + (+ x (tri (sub1 x))))) |
| 666 | + '(tri 9))) |
| 667 | + |
| 668 | +(exec (parse '(define (even? x) |
| 669 | + (if (zero? x) |
| 670 | + #t |
| 671 | + (odd? (sub1 x)))) |
| 672 | + '(define (odd? x) |
| 673 | + (if (zero? x) |
| 674 | + #f |
| 675 | + (even? (sub1 x)))) |
| 676 | + '(even? 101))) |
686 | 677 | ] |
687 | 678 |
|
688 | 679 | The complete compiler code: |
|
0 commit comments