Skip to content

Commit 65c786f

Browse files
committed
Milestone: compile.rkt goes through.
1 parent c2cc34e commit 65c786f

11 files changed

Lines changed: 956 additions & 94 deletions

File tree

langs/outlaw/ast.rkt

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#lang racket
22
(provide (all-defined-out))
33

4-
;; type Prog = (Prog (Listof Defn) Expr)
5-
(struct Prog (ds e) #:prefab)
4+
;; type Prog = (Prog (Listof Defn))
5+
(struct Prog (ds))
66

77
;; type Lib = (Lib (Listof Id) (Listof Defn))
8-
(struct Lib (ids ds) #:prefab)
8+
(struct Lib (ids ds))
99

1010
;; type Defn = (Defn Id Lambda)
11-
(struct Defn (f l) #:prefab)
11+
(struct Defn (f l))
1212

1313
;; type Expr = (Eof)
1414
;; | (Quote Datum)
@@ -63,31 +63,33 @@
6363
;; | (PSymb Symbol)
6464
;; | (PStr String)
6565
;; | (PStruct Id (Listof Pat))
66+
;; | (PPred Expr)
6667
;; type Lit = Boolean
6768
;; | Character
6869
;; | Integer
6970
;; | '()
7071

71-
(struct Eof () #:prefab)
72-
(struct Prim (p es) #:prefab)
73-
(struct If (e1 e2 e3) #:prefab)
74-
(struct Begin (e1 e2) #:prefab)
75-
(struct Let (xs es e) #:prefab)
76-
(struct Var (x) #:prefab)
77-
(struct App (e es) #:prefab)
78-
(struct Lam (f xs e) #:prefab)
79-
(struct LamRest (f xs x e) #:prefab)
80-
(struct LamCase (f cs) #:prefab)
81-
(struct Apply (e es el) #:prefab)
82-
(struct Quote (d) #:prefab)
83-
(struct Match (e ps es) #:prefab)
72+
(struct Eof ())
73+
(struct Prim (p es))
74+
(struct If (e1 e2 e3))
75+
(struct Begin (e1 e2))
76+
(struct Let (xs es e))
77+
(struct Var (x))
78+
(struct App (e es))
79+
(struct Lam (f xs e))
80+
(struct LamRest (f xs x e))
81+
(struct LamCase (f cs))
82+
(struct Apply (e es el))
83+
(struct Quote (d))
84+
(struct Match (e ps es))
8485

85-
(struct PVar (x) #:prefab)
86-
(struct PWild () #:prefab)
87-
(struct PLit (x) #:prefab)
88-
(struct PBox (p) #:prefab)
89-
(struct PCons (p1 p2) #:prefab)
90-
(struct PAnd (p1 p2) #:prefab)
91-
(struct PSymb (s) #:prefab)
92-
(struct PStr (s) #:prefab)
93-
(struct PStruct (n ps) #:prefab)
86+
(struct PVar (x))
87+
(struct PWild ())
88+
(struct PLit (x))
89+
(struct PBox (p))
90+
(struct PCons (p1 p2))
91+
(struct PAnd (p1 p2))
92+
(struct PSymb (s))
93+
(struct PStr (s))
94+
(struct PStruct (n ps))
95+
(struct PPred (e))

langs/outlaw/compile-expr.rkt

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
;; Pat Expr CEnv GEnv Symbol Bool -> Asm
233233
(define (compile-match-clause p e c g done t?)
234234
(let ((next (gensym)))
235-
(match (compile-pattern p '() next)
235+
(match (compile-pattern p c g '() next)
236236
[(list i f cm)
237237
(seq (Mov rax (Offset rsp 0)) ; restore value being matched
238238
i
@@ -242,8 +242,8 @@
242242
f
243243
(Label next))])))
244244

245-
;; Pat CEnv Symbol -> (list Asm Asm CEnv)
246-
(define (compile-pattern p cm next)
245+
;; Pat CEnv GEnv CEnv Symbol -> (list Asm Asm CEnv)
246+
(define (compile-pattern p c g cm next)
247247
(match p
248248
[(PWild)
249249
(list (seq) (seq) cm)]
@@ -287,9 +287,9 @@
287287
(Jmp next))
288288
cm))]
289289
[(PAnd p1 p2)
290-
(match (compile-pattern p1 (cons #f cm) next)
290+
(match (compile-pattern p1 c g (cons #f cm) next)
291291
[(list i1 f1 cm1)
292-
(match (compile-pattern p2 cm1 next)
292+
(match (compile-pattern p2 c g cm1 next)
293293
[(list i2 f2 cm2)
294294
(list
295295
(seq (Push rax)
@@ -299,7 +299,7 @@
299299
(seq f1 f2)
300300
cm2)])])]
301301
[(PBox p)
302-
(match (compile-pattern p cm next)
302+
(match (compile-pattern p c g cm next)
303303
[(list i1 f1 cm1)
304304
(let ((fail (gensym)))
305305
(list
@@ -316,9 +316,9 @@
316316
(Jmp next))
317317
cm1))])]
318318
[(PCons p1 p2)
319-
(match (compile-pattern p1 (cons #f cm) next)
319+
(match (compile-pattern p1 c g (cons #f cm) next)
320320
[(list i1 f1 cm1)
321-
(match (compile-pattern p2 cm1 next)
321+
(match (compile-pattern p2 c g cm1 next)
322322
[(list i2 f2 cm2)
323323
(let ((fail (gensym)))
324324
(list
@@ -340,7 +340,7 @@
340340
(Jmp next))
341341
cm2))])])]
342342
[(PStruct n ps)
343-
(match (compile-struct-patterns ps (cons #f cm) next 1)
343+
(match (compile-struct-patterns ps c g (cons #f cm) next 1)
344344
[(list i f cm)
345345
(let ((fail (gensym)))
346346
(list
@@ -359,16 +359,41 @@
359359
(Label fail)
360360
(Add rsp (*8 (length cm)))
361361
(Jmp next))
362-
cm))])]))
362+
cm))])]
363+
364+
[(PPred e)
365+
(list
366+
(let ((r (gensym 'ret)))
367+
(seq (Lea r15 r)
368+
(Push r15) ; rp
369+
(Push rax) ; arg (saved for the moment)
370+
(compile-e e (list* #f #f c) g #f)
371+
(Pop r15)
372+
(Push rax)
373+
(Push r15)
374+
375+
(assert-proc rax)
376+
(Xor rax type-proc)
377+
(Mov r15 1)
378+
(Mov rax (Offset rax 0))
379+
(Jmp rax)
380+
(Label r)
381+
(Cmp rax val-false)
382+
(Je next)))
383+
(seq)
384+
cm)]))
385+
386+
387+
363388

364389
;; [Listof Pat] CEnv Symbol Nat -> (list Asm Asm CEnv)
365-
(define (compile-struct-patterns ps cm next i)
390+
(define (compile-struct-patterns ps c g cm next i)
366391
(match ps
367392
['() (list '() '() cm)]
368393
[(cons p ps)
369-
(match (compile-pattern p cm next)
394+
(match (compile-pattern p c g cm next)
370395
[(list i1 f1 cm1)
371-
(match (compile-struct-patterns ps cm1 next (add1 i))
396+
(match (compile-struct-patterns ps c g cm1 next (add1 i))
372397
[(list is fs cmn)
373398
(list
374399
(seq (Mov rax (Offset rax (*8 i)))

langs/outlaw/compile-literals.rkt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
;; Prog -> [Listof (U Symbol String)]
5353
(define (literals* p)
5454
(match p
55-
[(Prog ds e)
56-
(append (append-map literals-d ds) (literals-e e))]))
55+
[(Prog ds)
56+
(append-map literals-d ds)]))
5757

5858
;; Defn -> [Listof (U Symbol String)]
5959
(define (literals-d d)
@@ -97,6 +97,7 @@
9797
[(PBox p) (literals-pat p)]
9898
[(PCons p1 p2) (append (literals-pat p1) (literals-pat p2))]
9999
[(PAnd p1 p2) (append (literals-pat p1) (literals-pat p2))]
100+
[(PPred e) (literals-e e)]
100101
[_ '()]))
101102

102103
;; Datum -> [Listof (U Symbol String)]

langs/outlaw/compile.rkt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
(define (compile p)
2121
(match p
22-
[(Prog ds e)
22+
[(Prog ds)
2323
(let ((gs (append stdlib-ids (define-ids ds))))
2424
(seq (externs)
2525
(map (lambda (i) (Extern (symbol->label i))) stdlib-ids)
@@ -30,7 +30,7 @@
3030
(init-lib)
3131

3232
(compile-defines ds gs)
33-
(compile-e e '() gs #t)
33+
(compile-variable (last-define-id ds) '() gs)
3434
(Ret)
3535
(compile-lambda-defines (lambdas p) gs)
3636
(Global 'raise_error_align)
@@ -45,6 +45,11 @@
4545
(Data)
4646
(compile-literals p)))]))
4747

48+
(define (last-define-id ds)
49+
(match ds
50+
[(cons (Defn x _) '()) x]
51+
[(cons d ds) (last-define-id ds)]))
52+
4853
(define (init-lib)
4954
(let ((r (gensym))) ; call init_lib
5055
(seq (Extern 'init_lib)
@@ -54,7 +59,7 @@
5459
(Label r))))
5560

5661
(define stdlib-ids
57-
'(list make-list list? foldr map length append
62+
'(list list* make-list list? foldr map filter length append
5863
memq member append-map vector->list
5964
reverse
6065
number->string gensym read read-char
@@ -63,6 +68,8 @@
6368
list->string string->list
6469
char<=?
6570
remove-duplicates remq* remove* remove
71+
andmap vector list->vector boolean?
72+
substring odd?
6673
;; Op0
6774
read-byte peek-byte void
6875
;; Op1
@@ -134,4 +141,4 @@
134141

135142
(compile-lambda-defines (lambdas-ds ds) g)
136143
(Data)
137-
(compile-literals (Prog ds (Quote #t)))))]))
144+
(compile-literals (Prog ds))))]))

langs/outlaw/error.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "values.h"
4+
#include "runtime.h"
5+
6+
void print_str(val_str_t*);
7+
8+
void error(val_t msg) {
9+
print_str(val_unwrap_str(msg));
10+
putchar('\n');
11+
exit(1);
12+
}

langs/outlaw/fv.rkt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
;; Pat Expr -> [Listof Id]
3030
(define (fv-clause* p e)
31-
(remq* (bv-pat* p) (fv* e)))
31+
(remq* (bv-pat* p) (append (fv-pat* e) (fv* e))))
3232

3333
;; Pat -> [Listof Id]
3434
(define (bv-pat* p)
@@ -39,3 +39,14 @@
3939
[(PBox p) (bv-pat* p)]
4040
[(PStruct n ps) (append-map bv-pat* ps)]
4141
[_ '()]))
42+
43+
;; Pat -> [Listof Id]
44+
(define (fv-pat* p)
45+
(match p
46+
[(PBox p) (fv-pat* p)]
47+
[(PCons p1 p2) (append (fv-pat* p1) (fv-pat* p2))]
48+
[(PAnd p1 p2) (append (fv-pat* p1) (fv-pat* p2))]
49+
[(PStruct n ps) (append-map fv-pat* ps)]
50+
[(PPred e) (fv* e)]
51+
[_ '()]))
52+

langs/outlaw/lambdas.rkt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
;; List all of the lambda expressions in p
77
(define (lambdas p)
88
(match p
9-
[(Prog ds e)
10-
(append (lambdas-ds ds) (lambdas-e e))]))
9+
[(Prog ds)
10+
(lambdas-ds ds)]))
1111

1212
;; Defns -> [Listof Lam]
1313
;; List all of the lambda expressions in ds
@@ -31,7 +31,9 @@
3131
[(LamRest f xs x e1) (cons e (lambdas-e e1))]
3232
[(LamCase f cs) (cons e (lambdas-cs cs))]
3333
[(Apply e es el) (append (lambdas-e e) (append-map lambdas-e es) (lambdas-e el))]
34-
[(Match e ps es) (append (lambdas-e e) (append-map lambdas-e es))]
34+
[(Match e ps es) (append (lambdas-e e)
35+
(append-map lambdas-pat ps)
36+
(append-map lambdas-e es))]
3537
[_ '()]))
3638

3739
;; [Listof LamCaseClause] -> [Listof Lam]
@@ -44,3 +46,13 @@
4446
[(cons (LamRest f xs x e) cs)
4547
(append (lambdas-e e)
4648
(lambdas-cs cs))]))
49+
50+
;; Pat -> [Listof Lam]
51+
(define (lambdas-pat p)
52+
(match p
53+
[(PBox p) (lambdas-pat p)]
54+
[(PCons p1 p2) (append (lambdas-pat p1) (lambdas-pat p2))]
55+
[(PAnd p1 p2) (append (lambdas-pat p1) (lambdas-pat p2))]
56+
[(PStruct n ps) (append-map lambdas-pat ps)]
57+
[(PPred e) (lambdas-e e)]
58+
[_ '()]))

0 commit comments

Comments
 (0)