Skip to content

Commit 2f5e12b

Browse files
committed
Initial Neerdowell notes and code cleanup.
1 parent 33d32f0 commit 2f5e12b

6 files changed

Lines changed: 446 additions & 28 deletions

File tree

langs/neerdowell/ast.rkt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
;; type Defn = (Defn Id (Listof Id) Expr)
88
(struct Defn (f xs e) #:prefab)
99

10-
;; type StructDefn = (Struct Id (Listof Id))
11-
(struct Struct (name flds) #:prefab)
12-
1310
;; type Expr = (Eof)
1411
;; | (Quote Datum)
1512
;; | (Prim Op (Listof Expr))

langs/neerdowell/compile-ops.rkt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,17 @@
324324
(Add rax r8)
325325
(Mov rax (Offset rax 0)))]))
326326

327+
;; Nat -> Asm
328+
;; Emit instructions for creating a structure of length n
329+
;; using values on top of stack
327330
(define (compile-make-struct n)
328331
(seq (compile-make-struct/a n 1)
329332
(Mov rax rbx)
330333
(Or rax type-struct)
331334
(Add rbx (* 8 n))))
332335

336+
;; Nat Nat -> Asm
337+
;; Pop elements off stack, writing them to heap
333338
(define (compile-make-struct/a n i)
334339
(if (= n i)
335340
(seq (Mov (Offset rbx (* 8 (- n i))) rax))

langs/neerdowell/interp-prims.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
(require "ast.rkt")
33
(provide interp-prim)
44

5+
;; type Struct = (StructVal Symbol (Vectorof Value))
56
(struct StructVal (name vals))
67

8+
;; Op [Listof Value] -> Answer
79
(define (interp-prim p vs)
810
(match (cons p vs)
911
;; Op0
@@ -58,7 +60,7 @@
5860
(if (<= 0 v2 (sub1 (string-length v1)))
5961
(string-ref v1 v2)
6062
'err)]
61-
[(list 'struct? (? symbol? s) v)
63+
[(list 'struct? s v)
6264
(match v
6365
[(StructVal n _) (eq? s n)]
6466
[_ #f])]
@@ -67,7 +69,7 @@
6769
(if (<= 0 v2 (sub1 (vector-length v1)))
6870
(vector-set! v1 v2 v3)
6971
'err)]
70-
[(list 'struct-ref (? symbol? s) (? integer? i) (StructVal n vs))
72+
[(list 'struct-ref s i (StructVal n vs))
7173
(if (and (eq? s n) (<= 0 i (sub1 (vector-length vs))))
7274
(vector-ref vs i)
7375
'err)]

langs/neerdowell/parse.rkt

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#lang racket
2-
(provide parse parse-define parse-e)
2+
(provide parse parse-define parse-e parse-struct)
33
(require "ast.rkt")
44

55
;; [Listof S-Expr] -> Prog
66
(define (parse s)
77
(match s
88
[(cons (and (cons 'struct _) d) s)
99
(match (parse s)
10-
[(Prog ds e)
11-
(Prog (append (make-struct-defns (parse-struct d)) ds) e)])]
10+
[(Prog ds e)
11+
(Prog (append (parse-struct d) ds) e)])]
1212
[(cons (and (cons 'define _) d) s)
1313
(match (parse s)
1414
[(Prog ds e)
1515
(Prog (cons (parse-define d) ds) e)])]
1616
[(cons e '()) (Prog '() (parse-e e))]
1717
[_ (error "program parse error")]))
1818

19-
;; Struct -> [Listof Defn]
20-
(define (make-struct-defns s)
19+
;; S-Expr -> [Listof Defn]
20+
(define (parse-struct s)
2121
(match s
22-
[(Struct n flds)
23-
(list* (make-struct-defn-construct n flds)
24-
(make-struct-defn-predicate n)
25-
(make-struct-defn-accessors n (reverse flds)))]))
22+
[(list 'struct (? symbol? n) flds)
23+
(if (andmap symbol? flds)
24+
(list* (make-struct-defn-construct n flds)
25+
(make-struct-defn-predicate n)
26+
(make-struct-defn-accessors n (reverse flds)))
27+
(error "parse struct definition error"))]
28+
[_ (error "parse struct definition error")]))
2629

2730
;; Id [Listof Id] -> [Listof Defn]
2831
(define (make-struct-defn-construct n flds)
@@ -33,28 +36,23 @@
3336
(define (make-struct-defn-predicate n)
3437
(Defn (symbol-append n '?) (list 'x)
3538
(Prim 'struct? (list (Quote n) (Var 'x)))))
36-
39+
3740
;; Id [Listof Id] -> [Listof Defn]
3841
(define (make-struct-defn-accessors n flds)
3942
(match flds
4043
['() '()]
4144
[(cons f flds)
4245
(cons (Defn (symbol-append n '- f) (list 'x)
43-
(Prim 'struct-ref (list (Quote n) (Quote (length flds)) (Var 'x))))
46+
(Prim 'struct-ref
47+
(list (Quote n)
48+
(Quote (length flds))
49+
(Var 'x))))
4450
(make-struct-defn-accessors n flds))]))
4551

4652
;; Symbol ... -> Symbol
4753
(define (symbol-append . ss)
48-
(string->symbol (apply string-append (map symbol->string ss))))
49-
50-
;; S-Expr -> StructDefn
51-
(define (parse-struct s)
52-
(match s
53-
[(list 'struct (? symbol? n) flds)
54-
(if (andmap symbol? flds)
55-
(Struct n flds)
56-
(error "parse struct definition error"))]
57-
[_ (error "parse struct definition error")]))
54+
(string->symbol
55+
(apply string-append (map symbol->string ss))))
5856

5957
;; S-Expr -> Defn
6058
(define (parse-define s)
@@ -76,7 +74,7 @@
7674
[(list (? (op? op1) p1) e) (Prim p1 (list (parse-e e)))]
7775
[(list (? (op? op2) p2) e1 e2) (Prim p2 (list (parse-e e1) (parse-e e2)))]
7876
[(list (? (op? op3) p3) e1 e2 e3)
79-
(Prim p3 (list (parse-e e1) (parse-e e2) (parse-e e3)))]
77+
(Prim p3 (list (parse-e e1) (parse-e e2) (parse-e e3)))]
8078
[(list 'begin e1 e2)
8179
(Begin (parse-e e1) (parse-e e2))]
8280
[(list 'if e1 e2 e3)
@@ -91,7 +89,7 @@
9189
(Lam (gensym 'lambda) xs (parse-e e))
9290
(error "parse lambda error"))]
9391
[(cons e es)
94-
(App (parse-e e) (map parse-e es))]
92+
(App (parse-e e) (map parse-e es))]
9593
[_ (error "Parse error" s)]))
9694

9795
(define (parse-match e ms)

www/notes.scrbl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ suggestions for improving the material, @bold{please},
3232
@include-section{notes/loot.scrbl}
3333
@include-section{notes/mug.scrbl}
3434
@include-section{notes/mountebank.scrbl}
35+
@include-section{notes/neerdowell.scrbl}
3536
@;include-section{notes/shakedown.scrbl}

0 commit comments

Comments
 (0)